diff --git a/db.sqlite b/db.sqlite index 89f4c0d..f111237 100644 Binary files a/db.sqlite and b/db.sqlite differ diff --git a/src/db/data/index.ts b/src/db/data/index.ts index 3074486..ed9dc32 100644 --- a/src/db/data/index.ts +++ b/src/db/data/index.ts @@ -1,4 +1,4 @@ -import publishers from './publishers' -import sources from './sources' +import publishersData from './publishers' +import sourcesData from './sources' -export { publishers, sources } +export { publishersData, sourcesData } diff --git a/src/db/data/publishers.ts b/src/db/data/publishers.ts index bca0eb0..6a02bf3 100644 --- a/src/db/data/publishers.ts +++ b/src/db/data/publishers.ts @@ -2,11 +2,13 @@ import { publisherTable } from '../schema'; type Publisher = typeof publisherTable.$inferInsert; -const publishers: Array = [ - { - name: 'Limithron', - url: 'https://www.limithron.com/', - } -] +function data(): Array { + return [ + { + name: 'Limithron', + url: 'https://www.limithron.com/', + } + ] as Array +} -export default publishers; +export default data; diff --git a/src/db/data/sources.ts b/src/db/data/sources.ts index 819f2f9..aae0a76 100644 --- a/src/db/data/sources.ts +++ b/src/db/data/sources.ts @@ -1,12 +1,21 @@ -import { sourceTable } from '../schema'; +import { sourceTable, publisherTable } from '../schema'; type Source = typeof sourceTable.$inferInsert; +type Publisher = typeof publisherTable.$inferSelect; -const sources: Array = [ - { - name: 'Pirate Borg', - url: 'https://www.limithron.com/pirateborg', - } -] +function findByName({ name: publisherName }: Publisher, name: String) { return publisherName === name } +function findIdByName(publishers: Publisher[], name: String): number | undefined { + return publishers.find(publisher => findByName(publisher, name))?.id +} -export default sources; +function data(publishers: Publisher[]): Array { + return [ + { + name: 'Pirate Borg', + url: 'https://www.limithron.com/pirateborg', + publisherId: findIdByName(publishers, 'Limithron') + } + ] as Array +} + +export default data; diff --git a/src/db/index.ts b/src/db/index.ts index c229c76..a15c6e4 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,7 +1,7 @@ import 'dotenv/config'; import { drizzle } from 'drizzle-orm/bun-sqlite'; import { publisherTable, sourceTable } from './schema'; -import { publishers, sources } from './data' +import { publishersData, sourcesData } from './data' const db = drizzle({ connection: { source: process.env.DB_FILE_NAME! } }); @@ -14,11 +14,11 @@ async function deleteData() { } async function seedData() { - await db.insert(publisherTable).values(publishers) + const publishers = await db.insert(publisherTable).values(publishersData()).returning() console.log('- Publishers Created') - await db.insert(sourceTable).values(sources) - console.log('- Sources Created') + const sources = await db.insert(sourceTable).values(sourcesData(publishers)).returning() + console.log('- Sources Created', sources) } (async () => { diff --git a/src/db/schema.ts b/src/db/schema.ts index e08bcb0..f4426b0 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -1,14 +1,15 @@ -import { int, sqliteTable, text } from "drizzle-orm/sqlite-core"; +import { int, sqliteTable as table, text } from "drizzle-orm/sqlite-core"; -export const publisherTable = sqliteTable("publisher", { +export const publisherTable = table("publisher", { id: int().primaryKey({ autoIncrement: true }), name: text().notNull().unique(), url: text().notNull().unique(), }); -export const sourceTable = sqliteTable("source", { +export const sourceTable = table("source", { id: int().primaryKey({ autoIncrement: true }), name: text().notNull().unique(), url: text().notNull().unique(), + publisherId: int("publisher_id").references(() => publisherTable.id), });