Better relationships in schema
This commit is contained in:
parent
eec99d44fc
commit
51353121a3
1 changed files with 40 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { sql } from "drizzle-orm";
|
import { sql, relations } 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", {
|
||||||
|
@ -7,30 +7,65 @@ export const publisherTable = table("publisher", {
|
||||||
url: text().notNull().unique(),
|
url: text().notNull().unique(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const publisherRelations = relations(publisherTable, ({ many }) => ({
|
||||||
|
sources: many(sourceTable)
|
||||||
|
}))
|
||||||
|
|
||||||
export const sourceTable = table("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),
|
publisherId: int("publisher_id"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const sourceRelations = relations(sourceTable, ({ one, many }) => ({
|
||||||
|
publisher: one(publisherTable, {
|
||||||
|
fields: [sourceTable.publisherId],
|
||||||
|
references: [publisherTable.id],
|
||||||
|
}),
|
||||||
|
chunks: many(chunkTable)
|
||||||
|
}))
|
||||||
|
|
||||||
export const chunkTable = table("chunk", {
|
export const chunkTable = table("chunk", {
|
||||||
id: int().primaryKey({ autoIncrement: true }),
|
id: int().primaryKey({ autoIncrement: true }),
|
||||||
name: text().notNull(),
|
name: text().notNull(),
|
||||||
pages: text("pages", { mode: "json" }).notNull().$type<string[]>().default(sql`'[]'`),
|
pages: text("pages", { mode: "json" }).notNull().$type<string[]>().default(sql`'[]'`),
|
||||||
sourceId: int("source_id").references(() => sourceTable.id),
|
sourceId: int("source_id"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const chunkRelations = relations(chunkTable, ({ one, many }) => ({
|
||||||
|
source: one(sourceTable, {
|
||||||
|
fields: [chunkTable.sourceId],
|
||||||
|
references: [sourceTable.id],
|
||||||
|
}),
|
||||||
|
tables: many(tableTable)
|
||||||
|
}))
|
||||||
|
|
||||||
export const tableTable = table("table", {
|
export const tableTable = table("table", {
|
||||||
id: int().primaryKey({ autoIncrement: true }),
|
id: int().primaryKey({ autoIncrement: true }),
|
||||||
name: text(),
|
name: text(),
|
||||||
order: int().notNull().default(sql`0`),
|
order: int().notNull().default(sql`0`),
|
||||||
chunkId: int("chunk_id").notNull().references(() => chunkTable.id),
|
chunkId: int("chunk_id"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const tableRelations = relations(tableTable, ({ one, many }) => ({
|
||||||
|
chunk: one(chunkTable, {
|
||||||
|
fields: [tableTable.chunkId],
|
||||||
|
references: [chunkTable.id],
|
||||||
|
}),
|
||||||
|
outcomes: many(outcomeTable)
|
||||||
|
}))
|
||||||
|
|
||||||
export const outcomeTable = table("outcome", {
|
export const outcomeTable = table("outcome", {
|
||||||
id: int().primaryKey({ autoIncrement: true }),
|
id: int().primaryKey({ autoIncrement: true }),
|
||||||
name: text().notNull(),
|
name: text().notNull(),
|
||||||
roll: int().notNull(),
|
roll: int().notNull(),
|
||||||
tableId: int("table_id").notNull().references(() => tableTable.id),
|
tableId: int("table_id"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const outcomeRelations = relations(outcomeTable, ({ one }) => ({
|
||||||
|
table: one(tableTable, {
|
||||||
|
fields: [outcomeTable.tableId],
|
||||||
|
references: [tableTable.id],
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
|
Loading…
Add table
Reference in a new issue