1
0
Fork 0

Can tie sources to publishers

This commit is contained in:
Ava Gaiety W 2024-11-13 19:18:53 -07:00
parent 12ae2f5979
commit deb860bc71
6 changed files with 37 additions and 25 deletions

BIN
db.sqlite

Binary file not shown.

View file

@ -1,4 +1,4 @@
import publishers from './publishers' import publishersData from './publishers'
import sources from './sources' import sourcesData from './sources'
export { publishers, sources } export { publishersData, sourcesData }

View file

@ -2,11 +2,13 @@ import { publisherTable } from '../schema';
type Publisher = typeof publisherTable.$inferInsert; type Publisher = typeof publisherTable.$inferInsert;
const publishers: Array<Publisher> = [ function data(): Array<Publisher> {
return [
{ {
name: 'Limithron', name: 'Limithron',
url: 'https://www.limithron.com/', url: 'https://www.limithron.com/',
} }
] ] as Array<Publisher>
}
export default publishers; export default data;

View file

@ -1,12 +1,21 @@
import { sourceTable } from '../schema'; import { sourceTable, publisherTable } from '../schema';
type Source = typeof sourceTable.$inferInsert; type Source = typeof sourceTable.$inferInsert;
type Publisher = typeof publisherTable.$inferSelect;
const sources: Array<Source> = [ 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
}
function data(publishers: Publisher[]): Array<Source> {
return [
{ {
name: 'Pirate Borg', name: 'Pirate Borg',
url: 'https://www.limithron.com/pirateborg', url: 'https://www.limithron.com/pirateborg',
publisherId: findIdByName(publishers, 'Limithron')
}
] as Array<Source>
} }
]
export default sources; export default data;

View file

@ -1,7 +1,7 @@
import 'dotenv/config'; import 'dotenv/config';
import { drizzle } from 'drizzle-orm/bun-sqlite'; import { drizzle } from 'drizzle-orm/bun-sqlite';
import { publisherTable, sourceTable } from './schema'; 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! } }); const db = drizzle({ connection: { source: process.env.DB_FILE_NAME! } });
@ -14,11 +14,11 @@ async function deleteData() {
} }
async function seedData() { async function seedData() {
await db.insert(publisherTable).values(publishers) const publishers = await db.insert(publisherTable).values(publishersData()).returning()
console.log('- Publishers Created') console.log('- Publishers Created')
await db.insert(sourceTable).values(sources) const sources = await db.insert(sourceTable).values(sourcesData(publishers)).returning()
console.log('- Sources Created') console.log('- Sources Created', sources)
} }
(async () => { (async () => {

View file

@ -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 }), id: int().primaryKey({ autoIncrement: true }),
name: text().notNull().unique(), name: text().notNull().unique(),
url: text().notNull().unique(), url: text().notNull().unique(),
}); });
export const sourceTable = sqliteTable("source", { export const sourceTable = table("source", {
id: int().primaryKey({ autoIncrement: true }), id: int().primaryKey({ autoIncrement: true }),
name: text().notNull().unique(), name: text().notNull().unique(),
url: text().notNull().unique(), url: text().notNull().unique(),
publisherId: int("publisher_id").references(() => publisherTable.id),
}); });