Add studio insert method

This commit is contained in:
Zoe Roux 2025-03-02 23:00:27 +01:00
parent 1cc26c5985
commit 710675180c
No known key found for this signature in database
4 changed files with 86 additions and 3 deletions

View File

@ -0,0 +1,55 @@
import { db } from "~/db";
import { showStudioJoin, studioTranslations, studios } from "~/db/schema";
import { conflictUpdateAllExcept } from "~/db/utils";
import type { SeedStudio } from "~/models/studio";
import { processOptImage } from "../images";
type StudioI = typeof studios.$inferInsert;
type StudioTransI = typeof studioTranslations.$inferInsert;
export const insertStudios = async (seed: SeedStudio[], showPk: number) => {
if (!seed.length) return [];
return await db.transaction(async (tx) => {
const vals: StudioI[] = seed.map((x) => {
const { translations, ...item } = x;
return item;
});
const ret = await tx
.insert(studios)
.values(vals)
.onConflictDoUpdate({
target: studios.slug,
set: conflictUpdateAllExcept(studios, [
"pk",
"id",
"slug",
"createdAt",
]),
})
.returning({ pk: studios.pk, id: studios.id, slug: studios.slug });
const trans: StudioTransI[] = seed.flatMap((x, i) =>
Object.entries(x.translations).map(([lang, tr]) => ({
pk: ret[i].pk,
language: lang,
name: tr.name,
logo: processOptImage(tr.logo),
})),
);
await tx
.insert(studioTranslations)
.values(trans)
.onConflictDoUpdate({
target: [studioTranslations.pk, studioTranslations.language],
set: conflictUpdateAllExcept(studioTranslations, ["pk", "language"]),
});
await tx
.insert(showStudioJoin)
.values(ret.map((studio) => ({ show: showPk, studio: studio.pk })))
.onConflictDoNothing();
return ret;
});
};

View File

@ -5,6 +5,7 @@ import { insertCollection } from "./insert/collection";
import { insertEntries } from "./insert/entries";
import { insertShow } from "./insert/shows";
import { guessNextRefresh } from "./refresh";
import { insertStudios } from "./insert/studios";
export const SeedMovieResponse = t.Object({
id: t.String({ format: "uuid" }),
@ -18,6 +19,12 @@ export const SeedMovieResponse = t.Object({
slug: t.String({ format: "slug", examples: ["sawano-collection"] }),
}),
),
studios: t.Array(
t.Object({
id: t.String({ format: "uuid" }),
slug: t.String({ format: "slug", examples: ["disney"] }),
}),
),
});
export type SeedMovieResponse = typeof SeedMovieResponse.static;
@ -38,7 +45,7 @@ export const seedMovie = async (
seed.slug = `random-${getYear(seed.airDate)}`;
}
const { translations, videos, collection, ...bMovie } = seed;
const { translations, videos, collection, studios, ...bMovie } = seed;
const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date());
const col = await insertCollection(collection, {
@ -74,11 +81,14 @@ export const seedMovie = async (
},
]);
const retStudios = await insertStudios(studios, show.pk);
return {
updated: show.updated,
id: show.id,
slug: show.slug,
videos: entry.videos,
collection: col,
studios: retStudios,
};
};

View File

@ -5,6 +5,7 @@ import { insertCollection } from "./insert/collection";
import { insertEntries } from "./insert/entries";
import { insertSeasons } from "./insert/seasons";
import { insertShow } from "./insert/shows";
import { insertStudios } from "./insert/studios";
import { guessNextRefresh } from "./refresh";
export const SeedSerieResponse = t.Object({
@ -45,6 +46,12 @@ export const SeedSerieResponse = t.Object({
}),
}),
),
studios: t.Array(
t.Object({
id: t.String({ format: "uuid" }),
slug: t.String({ format: "slug", examples: ["mappa"] }),
}),
),
});
export type SeedSerieResponse = typeof SeedSerieResponse.static;
@ -65,7 +72,15 @@ export const seedSerie = async (
seed.slug = `random-${getYear(seed.startAir)}`;
}
const { translations, seasons, entries, extras, collection, ...serie } = seed;
const {
translations,
seasons,
entries,
extras,
collection,
studios,
...serie
} = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
const col = await insertCollection(collection, {
@ -92,6 +107,8 @@ export const seedSerie = async (
(extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })),
);
const retStudios = await insertStudios(studios, show.pk);
return {
updated: show.updated,
id: show.id,
@ -100,5 +117,6 @@ export const seedSerie = async (
entries: retEntries,
extras: retExtras,
collection: col,
studios: retStudios,
};
};

View File

@ -25,7 +25,7 @@ export const studios = schema.table("studios", {
.$onUpdate(() => sql`now()`),
});
export const studio_translations = schema.table(
export const studioTranslations = schema.table(
"studio_translations",
{
pk: integer()