mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Create insert method
This commit is contained in:
parent
fc9afa5ede
commit
e8c9dfce56
@ -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 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;
|
if (!collection) return null;
|
||||||
const { translations: colTrans, ...col } = collection;
|
const { translations, ...col } = collection;
|
||||||
// TODO: need to compute start/end year & if missing tags & genres
|
|
||||||
const { updated, status, ...ret } = await insertShow(
|
return await db.transaction(async (tx) => {
|
||||||
{
|
const [ret] = await tx
|
||||||
kind: "collection",
|
.insert(shows)
|
||||||
status: "unknown",
|
.values({
|
||||||
nextRefresh,
|
kind: "collection",
|
||||||
...col,
|
status: "unknown",
|
||||||
},
|
startAir: show.kind === "movie" ? show.airDate : show.startAir,
|
||||||
colTrans,
|
endAir: show.kind === "movie" ? show.airDate : show.endAir,
|
||||||
);
|
nextRefresh: show.nextRefresh,
|
||||||
return ret;
|
...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;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
@ -41,7 +41,11 @@ export const seedMovie = async (
|
|||||||
const { translations, videos, collection, ...bMovie } = seed;
|
const { translations, videos, collection, ...bMovie } = seed;
|
||||||
const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date());
|
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(
|
const show = await insertShow(
|
||||||
{
|
{
|
||||||
|
@ -68,7 +68,11 @@ export const seedSerie = async (
|
|||||||
const { translations, seasons, entries, extras, collection, ...serie } = seed;
|
const { translations, seasons, entries, extras, collection, ...serie } = seed;
|
||||||
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
|
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(
|
const show = await insertShow(
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user