diff --git a/api/src/controllers/seed/insert/entries.ts b/api/src/controllers/seed/insert/entries.ts index ad346e81..3cc0c84c 100644 --- a/api/src/controllers/seed/insert/entries.ts +++ b/api/src/controllers/seed/insert/entries.ts @@ -6,7 +6,7 @@ import { entryVideoJoint as entryVideoJoin, videos, } from "~/db/schema"; -import { conflictUpdateAllExcept } from "~/db/utils"; +import { conflictUpdateAllExcept, values } from "~/db/utils"; import type { SeedEntry } from "~/models/entry"; import { processOptImage } from "../images"; import { guessNextRefresh } from "../refresh"; @@ -83,15 +83,15 @@ export const insertEntries = async ( const hasRenderingQ = db .select() .from(entryVideoJoin) - .where(eq(entryVideoJoin.entry, sql`vids.entryPk`)); + .where(eq(entryVideoJoin.entry, sql`vids.entryPk::integer`)); const retVideos = await db .insert(entryVideoJoin) .select( db .select({ - entry: sql`vids.entryPk`.as("entry"), - video: videos.pk, + entry: sql`vids.entryPk::integer`.as("entry"), + video: sql`${videos.pk}`.as("video"), slug: sql` concat( ${show.slug}::text, @@ -101,11 +101,14 @@ export const insertEntries = async ( ) `.as("slug"), }) - .from(sql`values(${vids}) as vids(videoId, entryPk)`) - .innerJoin(videos, eq(videos.id, sql`vids.videoId`)), + .from(values(vids).as("vids")) + .innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)), ) .onConflictDoNothing() - .returning({ slug: entryVideoJoin.slug, entryPk: sql`vids.entryPk` }); + .returning({ + slug: entryVideoJoin.slug, + entryPk: entryVideoJoin.entry, + }); return retEntries.map((entry) => ({ id: entry.id, diff --git a/api/src/db/utils.ts b/api/src/db/utils.ts index 79fbdb48..d71296e1 100644 --- a/api/src/db/utils.ts +++ b/api/src/db/utils.ts @@ -70,3 +70,25 @@ export function conflictUpdateAllExcept< export function sqlarr(array: unknown[]) { return `{${array.map((item) => `"${item}"`).join(",")}}`; } + +// TODO: upstream this +// TODO: type values (everything is a `text` for now) +export function values(items: Record[]) { + const [firstProp, ...props] = Object.keys(items[0]); + const values = items + .map((x) => { + let ret = sql`(${x[firstProp]}`; + for (const val of props) { + ret = sql`${ret}, ${x[val]}`; + } + return sql`${ret})`; + }) + .reduce((acc, x) => sql`${acc}, ${x}`); + const valueNames = [firstProp, ...props].join(", "); + + return { + as: (name: string) => { + return sql`(values ${values}) as ${sql.raw(name)}(${sql.raw(valueNames)})`; + }, + }; +}