From 573d4eac9c6b300f3ea275b210ecb23aeeba5d91 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 9 Jul 2024 13:44:50 +0200 Subject: [PATCH] feat(schema): add user, product, stock, comment and purchase tables This commit adds several new tables to the schema for the drizzle ORM, including tables for users, products, stock, comments, and purchase history. Each table contains defined fields with appropriate data types and constraints. This forms the basis for storage and retrieval of relevant data within the application. --- src/drizzle/schema.ts | 172 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/drizzle/schema.ts diff --git a/src/drizzle/schema.ts b/src/drizzle/schema.ts new file mode 100644 index 0000000..813948b --- /dev/null +++ b/src/drizzle/schema.ts @@ -0,0 +1,172 @@ +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(), + +}) \ No newline at end of file