From deb860bc71b6109ae8200b62efc4e7e4fa9c760f Mon Sep 17 00:00:00 2001 From: Ava Gaiety W Date: Wed, 13 Nov 2024 19:18:53 -0700 Subject: [PATCH] Can tie sources to publishers --- db.sqlite | Bin 32768 -> 32768 bytes src/db/data/index.ts | 6 +++--- src/db/data/publishers.ts | 16 +++++++++------- src/db/data/sources.ts | 25 +++++++++++++++++-------- src/db/index.ts | 8 ++++---- src/db/schema.ts | 7 ++++--- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/db.sqlite b/db.sqlite index 89f4c0d87d893c114f8bee6fd4fac24c3e3f7289..f111237b6f050560e47cb8ccc0766cd8435c4391 100644 GIT binary patch delta 181 zcmZo@U}|V!njkG0&%nUI0mLxCK2gV*Kb}Fatb><7mw}zfh=IpwW8M*Nu0{<;c5z8b z#wP8_<@_CzItmE|rAaxN#TltZ@tG+J3YmE&sp+Xjnw#(POkiRG8aH`zA^%jClMMVP zHw!B4=Vvz+V`WedWt@CbUY}87lfqMe0Wn@CW_d=qF%p}<>T?M&ifwZE&krO6Ztyco QY`z(ZXIsE4bk^wh> = [ - { - 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), });