Create insert method

This commit is contained in:
Zoe Roux 2025-03-02 17:08:46 +01:00
parent fc9afa5ede
commit e8c9dfce56
No known key found for this signature in database
3 changed files with 75 additions and 16 deletions

View File

@ -1,18 +1,69 @@
import { sql } from "drizzle-orm";
import { db } from "~/db";
import { showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept } from "~/db/utils";
import type { SeedCollection } from "~/models/collections";
import { insertShow } from "./shows";
import type { SeedMovie } from "~/models/movie";
import type { SeedSerie } from "~/models/serie";
import { processOptImage } from "../images";
export const insertCollection = async (collection?: SeedCollection) => {
type ShowTrans = typeof showTranslations.$inferInsert;
export const insertCollection = async (
collection: SeedCollection | undefined,
show: (({ kind: "movie" } & SeedMovie) | ({ kind: "serie" } & SeedSerie)) & {
nextRefresh: string;
},
) => {
if (!collection) return null;
const { translations: colTrans, ...col } = collection;
// TODO: need to compute start/end year & if missing tags & genres
const { updated, status, ...ret } = await insertShow(
{
kind: "collection",
status: "unknown",
nextRefresh,
...col,
},
colTrans,
);
return ret;
const { translations, ...col } = collection;
return await db.transaction(async (tx) => {
const [ret] = await tx
.insert(shows)
.values({
kind: "collection",
status: "unknown",
startAir: show.kind === "movie" ? show.airDate : show.startAir,
endAir: show.kind === "movie" ? show.airDate : show.endAir,
nextRefresh: show.nextRefresh,
...col,
})
.onConflictDoUpdate({
target: shows.slug,
set: {
...conflictUpdateAllExcept(shows, [
"pk",
"id",
"slug",
"createdAt",
"startAir",
"endAir",
]),
startAir: sql`least(${shows.startAir}, excluded.start_air)`,
endAir: sql`greatest(${shows.endAir}, excluded.end_air)`,
},
})
.returning({ pk: shows.pk, id: shows.id, slug: shows.slug });
const trans: ShowTrans[] = Object.entries(translations).map(
([lang, tr]) => ({
pk: ret.pk,
language: lang,
...tr,
poster: processOptImage(tr.poster),
thumbnail: processOptImage(tr.thumbnail),
logo: processOptImage(tr.logo),
banner: processOptImage(tr.banner),
}),
);
await tx
.insert(showTranslations)
.values(trans)
.onConflictDoUpdate({
target: [showTranslations.pk, showTranslations.language],
set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]),
});
return ret;
});
};

View File

@ -41,7 +41,11 @@ export const seedMovie = async (
const { translations, videos, collection, ...bMovie } = seed;
const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date());
const col = await insertCollection(collection);
const col = await insertCollection(collection, {
kind: "movie",
nextRefresh,
...seed,
});
const show = await insertShow(
{

View File

@ -68,7 +68,11 @@ export const seedSerie = async (
const { translations, seasons, entries, extras, collection, ...serie } = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
const col = await insertCollection(collection);
const col = await insertCollection(collection, {
kind: "serie",
nextRefresh,
...seed,
});
const show = await insertShow(
{