Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define names for DB schema #7

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions src/db/schema/batches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,46 @@ import {
date,
doublePrecision,
pgTable,
primaryKey,
text,
timestamp,
uuid,
} from "drizzle-orm/pg-core";

import { sellingUnits } from "./sellingUnits";

export const batches = pgTable("batches_bt", {
id: uuid("id").primaryKey().defaultRandom(),
number: text("number").notNull(),
expiresOn: date("expires_on")
.notNull()
.default(sql`CURRENT_TIMESTAMP + INTERVAL '1 year'`),
note: text("note"),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
export const batches = pgTable(
"batches_bt",
{
id: uuid("id"),
number: text("number").notNull(),
expiresOn: date("expires_on")
.notNull()
.default(sql`CURRENT_TIMESTAMP + INTERVAL '1 year'`),
note: text("note"),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
},
(table) => ({
pk: primaryKey({
name: "bat_bt_pk",
columns: [table.id],
}),
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});
);

export const batchesRelations = relations(batches, ({ one, many }) => ({
sellingUnits: many(sellingUnits),
Expand Down
45 changes: 27 additions & 18 deletions src/db/schema/bundles.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { relations } from "drizzle-orm";
import { pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
import { pgTable, primaryKey, timestamp, uuid } from "drizzle-orm/pg-core";

import { nestables } from "./nestables";
import { sellingUnits } from "./sellingUnits";

export const bundles = pgTable("bundles_bt", {
id: uuid("id").primaryKey().defaultRandom(),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
export const bundles = pgTable(
"bundles_bt",
{
id: uuid("id"),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
},
(table) => ({
pk: primaryKey({
name: "bun_bt_pk",
columns: [table.id],
}),
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});
);

export const bundlesRelations = relations(bundles, ({ one, many }) => ({
sellingUnit: one(sellingUnits),
Expand Down
53 changes: 34 additions & 19 deletions src/db/schema/configurations.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { integer, pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
import {
integer,
pgTable,
primaryKey,
timestamp,
uuid,
} from "drizzle-orm/pg-core";

export const configurations = pgTable("configurations", {
id: uuid("id").primaryKey().defaultRandom(),
version: integer("version").notNull(),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
export const configurations = pgTable(
"configurations",
{
id: uuid("id"),
version: integer("version").notNull(),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
},
(table) => ({
pk: primaryKey({
name: "con_pk",
columns: [table.id],
}),
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});
);

export type Configuration = typeof configurations.$inferSelect;
export type NewConfiguration = typeof configurations.$inferInsert;
45 changes: 27 additions & 18 deletions src/db/schema/constituents.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { relations } from "drizzle-orm";
import { pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
import { pgTable, primaryKey, timestamp, uuid } from "drizzle-orm/pg-core";

import { goods } from "./goods";
import { ingredients } from "./ingredients";
import { recipeHasConstituents } from "./recipeHasConstituents";

export const constituents = pgTable("constituents_bt", {
id: uuid("id").primaryKey().defaultRandom(),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
export const constituents = pgTable(
"constituents_bt",
{
id: uuid("id"),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
},
(table) => ({
pk: primaryKey({
name: "con_bt_pk",
columns: [table.id],
}),
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});
);

export const constituentsRelations = relations(
constituents,
Expand Down
62 changes: 42 additions & 20 deletions src/db/schema/doughs.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
import { relations } from "drizzle-orm";
import { AnyPgColumn, pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
import {
AnyPgColumn,
foreignKey,
pgTable,
primaryKey,
timestamp,
uuid,
} from "drizzle-orm/pg-core";

import { goods } from "./goods";
import { recipes } from "./recipes";

export const doughs = pgTable("doughs", {
id: uuid("id")
.primaryKey()
.references(() => goods.id, { onDelete: "cascade", onUpdate: "cascade" }),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
export const doughs = pgTable(
"doughs",
{
id: uuid("id"),
insertedAt: timestamp("inserted_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
},
(table) => ({
pk: primaryKey({
name: "dou_pk",
columns: [table.id],
}),
fkGooBt: foreignKey({
name: "fk_dou_goo_bt",
columns: [table.id],
foreignColumns: [goods.id],
})
.onDelete("cascade")
.onUpdate("cascade"),
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
mode: "date",
precision: 3,
withTimezone: false,
})
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});
);

export const doughsRelations = relations(doughs, ({ one }) => ({
good: one(goods, {
fields: [doughs.id],
references: [goods.id],
relationName: "dou_fk_goo_bt",
}),
}));

Expand Down
24 changes: 16 additions & 8 deletions src/db/schema/goods.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { relations } from "drizzle-orm";
import {
AnyPgColumn,
foreignKey,
pgTable,
primaryKey,
text,
timestamp,
unique,
Expand All @@ -14,12 +16,7 @@ import { recipes } from "./recipes";
export const goods = pgTable(
"goods_bt",
{
id: uuid("id")
.primaryKey()
.references(() => constituents.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
id: uuid("id"),
currentRecipeId: uuid("current_recipe_id").references(
(): AnyPgColumn => recipes.id
),
Expand All @@ -40,19 +37,30 @@ export const goods = pgTable(
.defaultNow()
.$onUpdate(() => new Date()),
},
(t) => ({
unq: unique().on(t.number),
(table) => ({
pk: primaryKey({
name: "goo_bt_pk",
columns: [table.id],
}),
fkConBt: foreignKey({
name: "fk_goo_bt_con_bt",
columns: [table.id],
foreignColumns: [constituents.id],
}),
unq: unique("goo_bt_num_uq").on(table.number),
})
);

export const goodsRelations = relations(goods, ({ one, many }) => ({
constituent: one(constituents, {
fields: [goods.id],
references: [constituents.id],
relationName: "goo_bt_fk_con_bt",
}),
currentRecipe: one(recipes, {
fields: [goods.currentRecipeId],
references: [recipes.id],
relationName: "goo_bt_fk_rec",
}),
recipes: many(recipes),
}));
Expand Down
Loading
Loading