# Database ## Setup PostgreSQL 16. Two databases: | Database | Port | Usage | |----------|------|-------| | `lunarfront` | 5432 | Development | | `lunarfront_api_test` | 5432 | API integration tests (auto-created by test runner) | ## Migrations Migrations are managed by Drizzle Kit and live in `packages/backend/src/db/migrations/`. ```bash cd packages/backend # Generate a migration from schema changes bunx drizzle-kit generate # Apply pending migrations bunx drizzle-kit migrate ``` Schema files: `packages/backend/src/db/schema/` ## Multi-Tenancy All domain tables include `company_id` (uuid FK to `company`). Every query filters by the authenticated user's company. Location-scoped tables additionally include `location_id`. ## Schema Overview ### Core | Table | Description | |-------|-------------| | `company` | Tenant (tenant business) | | `location` | Physical store location | | `user` | Staff/admin user account | ### Accounts & Members | Table | Description | |-------|-------------| | `account` | Billing entity (family, individual, business) | | `member` | Individual person on an account | | `member_identifier` | ID documents (DL, passport, school ID) | | `payment_method` | Stored payment methods | | `processor_link` | Payment processor integrations | | `tax_exemption` | Tax exemption records | ### Inventory | Table | Description | |-------|-------------| | `product` | Product catalog entry | | `inventory_unit` | Individual serialized/non-serialized unit | | `stock_receipt` | Incoming stock records | | `category` | Product categories | | `supplier` | Product suppliers | | `inventory_unit_status` | Lookup: unit statuses | | `item_condition` | Lookup: item conditions | ### RBAC | Table | Description | |-------|-------------| | `permission` | System permissions (global, seeded) | | `role` | Company-scoped roles (system + custom) | | `role_permission` | Role-to-permission mapping | | `user_role_assignment` | User-to-role mapping | ### Files | Table | Description | |-------|-------------| | `file` | File metadata (path, type, size, entity reference) | ## Key Conventions - UUIDs for all primary keys (`defaultRandom()`) - `created_at` and `updated_at` timestamps with timezone on all tables - Soft deletes via `is_active` boolean where applicable - Auto-generated sequential numbers: `account_number` (6-digit), `member_number` - Lookup tables support both system (immutable) and custom (company-scoped) values