|
| 1 | +import { pgTable, serial, text, integer, timestamp, boolean, jsonb, decimal, uuid } from 'drizzle-orm/pg-core'; |
| 2 | +import { relations } from 'drizzle-orm'; |
| 3 | + |
| 4 | +// 1. Tenants (SaaS context) |
| 5 | +export const tenants = pgTable('tenants', { |
| 6 | + id: serial('id').primaryKey(), |
| 7 | + name: text('name').notNull(), |
| 8 | + slug: text('slug').unique().notNull(), |
| 9 | + plan: text('plan').default('free'), // free, pro, enterprise |
| 10 | + createdAt: timestamp('created_at').defaultNow(), |
| 11 | +}); |
| 12 | + |
| 13 | +// 2. Users (Belong to tenants) |
| 14 | +export const users = pgTable('users', { |
| 15 | + id: serial('id').primaryKey(), |
| 16 | + tenantId: integer('tenant_id').references(() => tenants.id).notNull(), |
| 17 | + email: text('email').notNull(), |
| 18 | + role: text('role').default('member'), // admin, member, viewer |
| 19 | + isActive: boolean('is_active').default(true), |
| 20 | + createdAt: timestamp('created_at').defaultNow(), |
| 21 | +}); |
| 22 | + |
| 23 | +// 3. Products (Catalog) |
| 24 | +export const products = pgTable('products', { |
| 25 | + id: serial('id').primaryKey(), |
| 26 | + tenantId: integer('tenant_id').references(() => tenants.id).notNull(), |
| 27 | + name: text('name').notNull(), |
| 28 | + description: text('description'), |
| 29 | + price: decimal('price', { precision: 10, scale: 2 }).notNull(), |
| 30 | + category: text('category').notNull(), |
| 31 | + tags: jsonb('tags').$type<string[]>(), // Array of strings |
| 32 | + metadata: jsonb('metadata'), // Flexible JSON |
| 33 | + isArchived: boolean('is_archived').default(false), |
| 34 | +}); |
| 35 | + |
| 36 | +// 4. Orders (Transactional) |
| 37 | +export const orders = pgTable('orders', { |
| 38 | + id: serial('id').primaryKey(), |
| 39 | + tenantId: integer('tenant_id').references(() => tenants.id).notNull(), |
| 40 | + userId: integer('user_id').references(() => users.id).notNull(), |
| 41 | + status: text('status').default('pending'), // pending, paid, shipped, cancelled |
| 42 | + total: decimal('total', { precision: 10, scale: 2 }).default('0'), |
| 43 | + createdAt: timestamp('created_at').defaultNow(), |
| 44 | + deletedAt: timestamp('deleted_at'), // Soft delete |
| 45 | +}); |
| 46 | + |
| 47 | +// 5. Order Items (M:N relation details) |
| 48 | +export const orderItems = pgTable('order_items', { |
| 49 | + id: serial('id').primaryKey(), |
| 50 | + orderId: integer('order_id').references(() => orders.id).notNull(), |
| 51 | + productId: integer('product_id').references(() => products.id).notNull(), |
| 52 | + quantity: integer('quantity').default(1), |
| 53 | + unitPrice: decimal('unit_price', { precision: 10, scale: 2 }).notNull(), |
| 54 | +}); |
| 55 | + |
| 56 | +// --- Relations --- |
| 57 | + |
| 58 | +export const tenantRelations = relations(tenants, ({ many }) => ({ |
| 59 | + users: many(users), |
| 60 | + products: many(products), |
| 61 | + orders: many(orders), |
| 62 | +})); |
| 63 | + |
| 64 | +export const userRelations = relations(users, ({ one, many }) => ({ |
| 65 | + tenant: one(tenants, { |
| 66 | + fields: [users.tenantId], |
| 67 | + references: [tenants.id], |
| 68 | + }), |
| 69 | + orders: many(orders), |
| 70 | +})); |
| 71 | + |
| 72 | +export const productRelations = relations(products, ({ one }) => ({ |
| 73 | + tenant: one(tenants, { |
| 74 | + fields: [products.tenantId], |
| 75 | + references: [tenants.id], |
| 76 | + }), |
| 77 | +})); |
| 78 | + |
| 79 | +export const orderRelations = relations(orders, ({ one, many }) => ({ |
| 80 | + tenant: one(tenants, { |
| 81 | + fields: [orders.tenantId], |
| 82 | + references: [tenants.id], |
| 83 | + }), |
| 84 | + user: one(users, { |
| 85 | + fields: [orders.userId], |
| 86 | + references: [users.id], |
| 87 | + }), |
| 88 | + items: many(orderItems), |
| 89 | +})); |
| 90 | + |
| 91 | +export const orderItemRelations = relations(orderItems, ({ one }) => ({ |
| 92 | + order: one(orders, { |
| 93 | + fields: [orderItems.orderId], |
| 94 | + references: [orders.id], |
| 95 | + }), |
| 96 | + product: one(products, { |
| 97 | + fields: [orderItems.productId], |
| 98 | + references: [products.id], |
| 99 | + }), |
| 100 | +})); |
0 commit comments