bd5df81f37
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { inArray, sql } from "drizzle-orm"
|
|
import { db } from "../../shared/db/client.ts"
|
|
import { igdbMetadata } from "../../shared/db/schema/igdb.ts"
|
|
|
|
export interface IgdbMetadata {
|
|
summary: string | null
|
|
coverImageId: string | null
|
|
screenshots: string[]
|
|
videoIds: string[]
|
|
genres: string[]
|
|
aggregatedRating: number | null
|
|
releaseDate: string | null
|
|
developers: string[]
|
|
}
|
|
|
|
export async function getMetadataBatch(canonicalIds: string[]): Promise<Map<string, IgdbMetadata>> {
|
|
if (canonicalIds.length === 0) return new Map()
|
|
const rows = await db
|
|
.select()
|
|
.from(igdbMetadata)
|
|
.where(inArray(igdbMetadata.canonicalId, canonicalIds))
|
|
const result = new Map<string, IgdbMetadata>()
|
|
for (const row of rows) {
|
|
result.set(row.canonicalId, {
|
|
summary: row.summary,
|
|
coverImageId: row.coverImageId,
|
|
screenshots: row.screenshots ?? [],
|
|
videoIds: row.videoIds ?? [],
|
|
genres: row.genres ?? [],
|
|
aggregatedRating: row.aggregatedRating,
|
|
releaseDate: row.releaseDate,
|
|
developers: row.developers ?? [],
|
|
})
|
|
}
|
|
return result
|
|
}
|
|
|
|
export async function setMetadataBatch(entries: Map<string, IgdbMetadata>) {
|
|
if (entries.size === 0) return
|
|
const values = Array.from(entries, ([canonicalId, meta]) => ({
|
|
canonicalId,
|
|
summary: meta.summary,
|
|
coverImageId: meta.coverImageId,
|
|
screenshots: meta.screenshots,
|
|
videoIds: meta.videoIds,
|
|
genres: meta.genres,
|
|
aggregatedRating: meta.aggregatedRating,
|
|
releaseDate: meta.releaseDate,
|
|
developers: meta.developers,
|
|
}))
|
|
await db
|
|
.insert(igdbMetadata)
|
|
.values(values)
|
|
.onConflictDoUpdate({
|
|
target: igdbMetadata.canonicalId,
|
|
set: {
|
|
summary: sql`excluded.summary`,
|
|
coverImageId: sql`excluded.cover_image_id`,
|
|
screenshots: sql`excluded.screenshots`,
|
|
videoIds: sql`excluded.video_ids`,
|
|
genres: sql`excluded.genres`,
|
|
aggregatedRating: sql`excluded.aggregated_rating`,
|
|
releaseDate: sql`excluded.release_date`,
|
|
developers: sql`excluded.developers`,
|
|
fetchedAt: sql`now()`,
|
|
},
|
|
})
|
|
}
|