comicbox/prisma/seed.ts

88 lines
2.5 KiB
TypeScript

import { prisma } from '../src/db';
import { ComicCharacter, ComicPage, Prisma } from '@prisma/client';
import rain from './seed-data/comics/rain.json';
async function seedComicRain() {
const comicData = {
slug: 'rain',
url: 'https://rain.thecomicseries.com/',
title: rain.title,
};
const comic = await prisma.comic.upsert({
where: {
slug: 'rain',
},
update: comicData,
create: comicData,
});
{
// Chapters not directly related to the story
const SKIP_CHAPTERS = [
0, // Cover page
30, // SRS Hiatus
37, // Summer 2018 Hiatus
42, // Quarantine Hiatus 2020
48, // Rain delays
49, // Specials
];
const ops: Prisma.Prisma__ComicPageClient<ComicPage>[] = [];
for (const page of rain.pages) {
if (SKIP_CHAPTERS.includes(page.chapterId)) {
continue;
}
const pageData = {
id: page.id,
title: page.name,
imageUrl: page.imageUrl,
url: page.url,
comicId: comic.id,
width: page.width,
height: page.height,
};
const op = prisma.comicPage.upsert({
where: {
comicId_id: {
comicId: comic.id,
id: page.id,
},
},
create: pageData,
update: pageData,
});
ops.push(op);
}
await prisma.$transaction(ops);
}
{
const ops: Prisma.Prisma__ComicCharacterClient<ComicCharacter>[] = [];
for (const char of rain.characters) {
const character =
typeof char === 'string'
? { longName: char, shortName: char }
: char;
const characterData = {
comicId: comic.id,
...character,
};
const op = prisma.comicCharacter.upsert({
where: {
longName: character.longName,
},
create: characterData,
update: characterData,
});
ops.push(op);
}
await prisma.$transaction(ops);
}
}
(async function () {
await seedComicRain();
})().catch((e) => {
console.error('failed to seed', e);
process.exitCode = 1;
});