feat(schema): Move and enhance schema definitions

This commit relocates the schema definitions by deleting the existing `drizzle/schema.ts` and creating a new `schema.ts` file. It also enhances these definitions by adding `createInsertSchema` and `createSelectSchema` for each table.
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 15:06:18 +02:00
parent ed2fc3dbf9
commit 2aa793c91d
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 177 additions and 172 deletions

View File

@ -1,172 +0,0 @@
import { pgTable } from "drizzle-orm/pg-core";
import * as p from "drizzle-orm/pg-core";
export const UsersTable = pgTable("users", {
// Unique identifier on a technical aspect.
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
email: p
.varchar("email", {
length: 64
})
.unique()
.notNull(),
emailCode: p
.varchar("email_code", {
length: 6
})
.$defaultFn(()=>{
return "032745"
}),
firstName: p
.varchar("first_name", {
length: 24,
})
.notNull(),
lastName: p
.varchar("last_name", {
length: 24,
})
.notNull(),
//hash (Argonid2 hash)
hash: p
.varchar("hash", {
length: 97,
})
.notNull(),
//METADATA
iat: p
.timestamp("issued_at", {
withTimezone: true,
})
.defaultNow()
.notNull(),
gdprAcceptance: p
.timestamp("ack_gdpr", {
withTimezone: true,
})
.defaultNow()
.notNull(),
isAdmin: p
.boolean("is_admin")
.default(false)
.notNull()
});
export const ProductsTable = pgTable("products", {
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
//slugName
slugName: p
.varchar("slug_name", {
length: 32,
})
.notNull(),
//displayName
displayName: p
.varchar("display_name", {
length: 64,
})
.notNull(),
//price
price: p
.decimal("price", {
precision: 10,
scale: 2,
})
.default("0.0")
.notNull(),
//imagePath
imagePath: p
.varchar("image_path", {
length: 128,
})
.notNull(),
//playload of the description element
})
export const StocksTable = pgTable("stocks", {
productId: p.uuid("product_id")
.references(()=> ProductsTable.uuid)
.notNull(),
quantity: p
.integer("quantity")
.default(0)
.notNull(),
})
export const CommentsTable = pgTable("comments", {
productId: p.uuid("product_id")
.references(()=> ProductsTable.uuid)
.notNull(),
authorId: p.uuid("author_id")
.references(()=> UsersTable.uuid)
.notNull(),
//notation
notation: p
.integer("notation")
.default(0)
.notNull(),
//json payload with the message
message: p
.jsonb("message")
.notNull(),
//iat
iat: p
.timestamp("issued_at", {
withTimezone: true,
})
.defaultNow()
.notNull(),
})
export const PurchaseHistory = pgTable("purchases", {
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
productId: p.uuid("product_id")
.references(()=> ProductsTable.uuid)
.notNull(),
unitPrice: p
.decimal("unit_price", {
precision: 10,
scale: 2,
})
.default("0.0")
.notNull(),
quantity: p
.integer("quantity")
.default(0)
.notNull(),
clientId: p.uuid("client_id")
.references(()=> UsersTable.uuid)
.notNull(),
timestamp: p
.timestamp("timestamp", {
withTimezone: true,
})
.defaultNow()
.notNull(),
})

177
src/schema.ts Normal file
View File

@ -0,0 +1,177 @@
import { pgTable } from "drizzle-orm/pg-core";
import * as p from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
export const UsersTable = pgTable("users", {
// Unique identifier on a technical aspect.
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
email: p
.varchar("email", {
length: 64,
})
.unique()
.notNull(),
emailCode: p
.varchar("email_code", {
length: 6,
})
.$defaultFn(() => {
return "032745";
}),
firstName: p
.varchar("first_name", {
length: 24,
})
.notNull(),
lastName: p
.varchar("last_name", {
length: 24,
})
.notNull(),
//hash (Argonid2 hash)
hash: p
.varchar("hash", {
length: 97,
})
.notNull(),
//METADATA
iat: p
.timestamp("issued_at", {
withTimezone: true,
})
.defaultNow()
.notNull(),
gdprAcceptance: p
.timestamp("ack_gdpr", {
withTimezone: true,
})
.defaultNow()
.notNull(),
isAdmin: p.boolean("is_admin").default(false).notNull(),
});
export const ProductsTable = pgTable("products", {
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
//slugName
slugName: p
.varchar("slug_name", {
length: 32,
})
.notNull(),
//displayName
displayName: p
.varchar("display_name", {
length: 64,
})
.notNull(),
//price
price: p
.decimal("price", {
precision: 10,
scale: 2,
})
.default("0.0")
.notNull(),
//imagePath
imagePath: p
.varchar("image_path", {
length: 128,
})
.notNull(),
//playload of the description element
});
export const StocksTable = pgTable("stocks", {
productId: p
.uuid("product_id")
.references(() => ProductsTable.uuid)
.notNull(),
quantity: p.integer("quantity").default(0).notNull(),
});
export const CommentsTable = pgTable("comments", {
productId: p
.uuid("product_id")
.references(() => ProductsTable.uuid)
.notNull(),
authorId: p
.uuid("author_id")
.references(() => UsersTable.uuid)
.notNull(),
//notation
notation: p.integer("notation").default(0).notNull(),
//json payload with the message
message: p.jsonb("message").notNull(),
//iat
iat: p
.timestamp("issued_at", {
withTimezone: true,
})
.defaultNow()
.notNull(),
});
export const PurchaseHistory = pgTable("purchases", {
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
productId: p
.uuid("product_id")
.references(() => ProductsTable.uuid)
.notNull(),
unitPrice: p
.decimal("unit_price", {
precision: 10,
scale: 2,
})
.default("0.0")
.notNull(),
quantity: p.integer("quantity").default(0).notNull(),
clientId: p
.uuid("client_id")
.references(() => UsersTable.uuid)
.notNull(),
timestamp: p
.timestamp("timestamp", {
withTimezone: true,
})
.defaultNow()
.notNull(),
});
export const UsersTableSelectSchema = createSelectSchema(UsersTable)
export const UsersTableInsertSchema = createInsertSchema(UsersTable)
export const ProductsTableSelectSchema = createSelectSchema(ProductsTable);
export const ProductsTableInsertSchema = createInsertSchema(ProductsTable);
export const StocksTableSelectSchema = createSelectSchema(StocksTable);
export const StocksTableInsertSchema = createInsertSchema(StocksTable);
export const CommentsTableSelectSchema = createSelectSchema(CommentsTable);
export const CommentsTableInsertSchema = createInsertSchema(CommentsTable);
export const PurchaseHistorySelectSchema = createSelectSchema(PurchaseHistory);
export const PurchaseHistoryInsertSchema = createInsertSchema(PurchaseHistory);