1
0
Fork 0

all DB tables with seed data

This commit is contained in:
Ava Gaiety W 2024-11-14 12:40:14 -07:00
parent deb860bc71
commit eec99d44fc
7 changed files with 107 additions and 6 deletions

BIN
db.sqlite

Binary file not shown.

21
src/db/data/chunks.ts Normal file
View file

@ -0,0 +1,21 @@
import { chunkTable, sourceTable } from '../schema';
type Chunk = typeof chunkTable.$inferInsert;
type Source = typeof sourceTable.$inferSelect;
function findByName({ name: sourceName }: Source, name: String) { return sourceName === name }
function findIdByName(sources: Source[], name: String): number | undefined {
return sources.find(source => findByName(source, name))?.id
}
function data(sources: Source[]): Array<Chunk> {
return [
{
name: 'Distinctive Flaws',
pages: ['56'],
sourceId: findIdByName(sources, 'Pirate Borg')
}
] as Array<Chunk>
}
export default data;

View file

@ -1,4 +1,7 @@
import publishersData from './publishers' import publishersData from './publishers'
import sourcesData from './sources' import sourcesData from './sources'
import chunksData from './chunks'
import tableData from './tables'
import outcomeData from './outcomes'
export { publishersData, sourcesData } export { publishersData, sourcesData, chunksData, tableData, outcomeData }

26
src/db/data/outcomes.ts Normal file
View file

@ -0,0 +1,26 @@
import { outcomeTable, tableTable } from '../schema';
type Outcome = typeof outcomeTable.$inferInsert;
type PartialOutcome = Omit<Outcome, "tableId"> | Omit<Outcome, "tableId" | "roll">
type Table = typeof tableTable.$inferSelect;
function outcomesForTableId(tableId: number, partialOutcomes: Array<PartialOutcome>): Array<Outcome> {
return partialOutcomes.map((outcome, index) => {
return {
roll: index + 1,
...outcome,
tableId,
}
});
}
function data(tables: Table[]): Array<Outcome> {
return [
...outcomesForTableId(tables[0].id, [
{ name: 'Drunken lush' },
{ name: 'Stubborn' },
])
] as Array<Outcome>
}
export default data;

12
src/db/data/tables.ts Normal file
View file

@ -0,0 +1,12 @@
import { tableTable, chunkTable } from '../schema';
type Table = typeof tableTable.$inferInsert;
type Chunk = typeof chunkTable.$inferSelect;
function data(chunks: Chunk[]): Array<Table> {
return [
{ chunkId: chunks[0].id }
] as Array<Table>
}
export default data;

View file

@ -1,16 +1,25 @@
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, chunkTable, tableTable, outcomeTable } from './schema';
import { publishersData, sourcesData } from './data' import { publishersData, sourcesData, chunksData, tableData, outcomeData } from './data'
const db = drizzle({ connection: { source: process.env.DB_FILE_NAME! } }); const db = drizzle({ connection: { source: process.env.DB_FILE_NAME! } });
async function deleteData() { async function deleteData() {
await db.delete(publisherTable) await db.delete(outcomeTable)
console.log('- Publishers Deleted') console.log('- Outcomes Deleted')
await db.delete(tableTable)
console.log('- (roll) Tables Deleted')
await db.delete(chunkTable)
console.log('- Chunks Deleted')
await db.delete(sourceTable) await db.delete(sourceTable)
console.log('- Sources Deleted') console.log('- Sources Deleted')
await db.delete(publisherTable)
console.log('- Publishers Deleted')
} }
async function seedData() { async function seedData() {
@ -18,7 +27,16 @@ async function seedData() {
console.log('- Publishers Created') console.log('- Publishers Created')
const sources = await db.insert(sourceTable).values(sourcesData(publishers)).returning() const sources = await db.insert(sourceTable).values(sourcesData(publishers)).returning()
console.log('- Sources Created', sources) console.log('- Sources Created')
const chunks = await db.insert(chunkTable).values(chunksData(sources)).returning()
console.log('- Chunks Created')
const tables = await db.insert(tableTable).values(tableData(chunks)).returning()
console.log('- (roll) Tables Created')
await db.insert(outcomeTable).values(outcomeData(tables)).returning()
console.log('- Outcomes Created')
} }
(async () => { (async () => {

View file

@ -1,3 +1,4 @@
import { sql } from "drizzle-orm";
import { int, sqliteTable as table, text } from "drizzle-orm/sqlite-core"; import { int, sqliteTable as table, text } from "drizzle-orm/sqlite-core";
export const publisherTable = table("publisher", { export const publisherTable = table("publisher", {
@ -13,3 +14,23 @@ export const sourceTable = table("source", {
publisherId: int("publisher_id").references(() => publisherTable.id), publisherId: int("publisher_id").references(() => publisherTable.id),
}); });
export const chunkTable = table("chunk", {
id: int().primaryKey({ autoIncrement: true }),
name: text().notNull(),
pages: text("pages", { mode: "json" }).notNull().$type<string[]>().default(sql`'[]'`),
sourceId: int("source_id").references(() => sourceTable.id),
});
export const tableTable = table("table", {
id: int().primaryKey({ autoIncrement: true }),
name: text(),
order: int().notNull().default(sql`0`),
chunkId: int("chunk_id").notNull().references(() => chunkTable.id),
});
export const outcomeTable = table("outcome", {
id: int().primaryKey({ autoIncrement: true }),
name: text().notNull(),
roll: int().notNull(),
tableId: int("table_id").notNull().references(() => tableTable.id),
});