/** * Products + product_images. Source of truth: `db/migrations/0001_init.sql`. */ import { pgTable, uuid, text, integer, boolean, timestamp, index, } from "drizzle-orm/pg-core"; import { tenants } from "./tenants"; export const products = pgTable( "products", { id: uuid("id").primaryKey().defaultRandom(), tenantId: uuid("tenant_id") .notNull() .references(() => tenants.id, { onDelete: "cascade" }), name: text("name").notNull(), description: text("description"), priceCents: integer("price_cents").notNull(), inventory: integer("inventory").notNull().default(0), unit: text("unit"), active: boolean("active").notNull().default(true), createdAt: timestamp("created_at", { withTimezone: true }) .notNull() .defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }) .notNull() .defaultNow(), }, (t) => ({ tenantIdx: index("products_tenant_idx").on(t.tenantId), activeIdx: index("products_active_idx").on(t.tenantId, t.active), }), ); export const productImages = pgTable( "product_images", { id: uuid("id").primaryKey().defaultRandom(), productId: uuid("product_id") .notNull() .references(() => products.id, { onDelete: "cascade" }), storageKey: text("storage_key").notNull(), position: integer("position").notNull().default(0), altText: text("alt_text"), createdAt: timestamp("created_at", { withTimezone: true }) .notNull() .defaultNow(), }, (t) => ({ productIdx: index("product_images_product_idx").on(t.productId, t.position), }), ); export type Product = typeof products.$inferSelect; export type NewProduct = typeof products.$inferInsert; export type ProductImage = typeof productImages.$inferSelect; export type NewProductImage = typeof productImages.$inferInsert;