bagsy/apps/api/db/schema.ts

111 lines
3.7 KiB
TypeScript

import {
pgTable,
text,
integer,
timestamp,
boolean,
uuid,
varchar,
json,
} from 'drizzle-orm/pg-core';
/**
* Stores table - grocery stores with locations
*/
export const stores = pgTable('stores', {
id: uuid('id').primaryKey().defaultRandom(),
householdId: uuid('household_id').notNull(), // references better-auth organization
name: varchar('name', { length: 255 }).notNull(),
location: text('location'),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
/**
* Aisles table - sections within a store
*/
export const aisles = pgTable('aisles', {
id: uuid('id').primaryKey().defaultRandom(),
storeId: uuid('store_id')
.notNull()
.references(() => stores.id, { onDelete: 'cascade' }),
name: varchar('name', { length: 255 }).notNull(),
order: integer('order').notNull(),
colour: varchar('colour', { length: 7 }).notNull(), // hex color
createdAt: timestamp('created_at').defaultNow(),
});
/**
* Items table - grocery items
*/
export const items = pgTable('items', {
id: uuid('id').primaryKey().defaultRandom(),
householdId: uuid('household_id').notNull(),
name: varchar('name', { length: 255 }).notNull(),
barcode: varchar('barcode', { length: 255 }),
aisleId: uuid('aisle_id').references(() => aisles.id, { onDelete: 'set null' }),
storeId: uuid('store_id').references(() => stores.id, { onDelete: 'set null' }),
alternatives: json('alternatives'), // JSON array of alternative item names
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
/**
* Shopping lists table
*/
export const shoppingLists = pgTable('shopping_lists', {
id: uuid('id').primaryKey().defaultRandom(),
householdId: uuid('household_id').notNull(),
storeId: uuid('store_id')
.notNull()
.references(() => stores.id),
name: varchar('name', { length: 255 }).notNull(),
isPreset: boolean('is_preset').notNull().default(false),
autoReset: boolean('auto_reset').notNull().default(false),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
/**
* List items table - items in a shopping list
*/
export const listItems = pgTable('list_items', {
id: uuid('id').primaryKey().defaultRandom(),
shoppingListId: uuid('shopping_list_id')
.notNull()
.references(() => shoppingLists.id, { onDelete: 'cascade' }),
itemId: uuid('item_id')
.notNull()
.references(() => items.id),
quantity: integer('quantity').notNull().default(1),
checked: boolean('checked').notNull().default(false),
addedBy: uuid('added_by').notNull(), // userId from better-auth
excludedFromPreset: boolean('excluded_from_preset').notNull().default(false),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
/**
* Scan history table - for tracking forgotten items
*/
export const scanHistory = pgTable('scan_history', {
id: uuid('id').primaryKey().defaultRandom(),
householdId: uuid('household_id').notNull(),
barcode: varchar('barcode', { length: 255 }).notNull(),
itemId: uuid('item_id')
.notNull()
.references(() => items.id),
scannedAt: timestamp('scanned_at', { withTimezone: true }).notNull().defaultNow(),
});
/**
* Household invite codes table - for join-by-code feature
*/
export const householdInviteCodes = pgTable('household_invite_codes', {
id: uuid('id').primaryKey().defaultRandom(),
householdId: uuid('household_id').notNull(),
code: varchar('code', { length: 8 }).notNull().unique(),
createdBy: uuid('created_by').notNull(),
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
createdAt: timestamp('created_at').defaultNow(),
});