Can tie sources to publishers
This commit is contained in:
parent
12ae2f5979
commit
deb860bc71
6 changed files with 37 additions and 25 deletions
BIN
db.sqlite
BIN
db.sqlite
Binary file not shown.
|
@ -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 }
|
||||
|
|
|
@ -2,11 +2,13 @@ import { publisherTable } from '../schema';
|
|||
|
||||
type Publisher = typeof publisherTable.$inferInsert;
|
||||
|
||||
const publishers: Array<Publisher> = [
|
||||
{
|
||||
name: 'Limithron',
|
||||
url: 'https://www.limithron.com/',
|
||||
}
|
||||
]
|
||||
function data(): Array<Publisher> {
|
||||
return [
|
||||
{
|
||||
name: 'Limithron',
|
||||
url: 'https://www.limithron.com/',
|
||||
}
|
||||
] as Array<Publisher>
|
||||
}
|
||||
|
||||
export default publishers;
|
||||
export default data;
|
||||
|
|
|
@ -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<Source> = [
|
||||
{
|
||||
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<Source> {
|
||||
return [
|
||||
{
|
||||
name: 'Pirate Borg',
|
||||
url: 'https://www.limithron.com/pirateborg',
|
||||
publisherId: findIdByName(publishers, 'Limithron')
|
||||
}
|
||||
] as Array<Source>
|
||||
}
|
||||
|
||||
export default data;
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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),
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue