bagsy/apps/api/db/schema.ts
bananabas 4fe4a98b1c feat: Migrate from self-hosted to Vercel + Neon + Clerk
- Move database from self-hosted Postgres to Neon (free tier)
- Replace better-auth with Clerk authentication (email/password + OAuth)
- Integrate Ably for real-time sync (replaces custom WebSocket)
- Remove passkey authentication (pending Apple Developer account)
- Add Clerk Organizations for household management
- Implement custom household join-code flow with Clerk
- Add secure Ably key fetching via backend API
- Update iOS app for Clerk auth and Ably real-time sync
- Configure Vercel deployment settings
- Update environment configuration for new infrastructure

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-06-27 18:59:00 +01:00

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 Clerk 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 Clerk
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(),
});