This workspace defines the database schema, manages migrations, and provides the database client for the application using Drizzle ORM with a PostgreSQL database, hosted and managed via Supabase.
To provide a single source of truth for the database structure and a typed client for interacting with the database from other services and applications within the monorepo.
schema/: Contains the Drizzle schema definitions, defining tables, columns, relations, and types.publicTables.ts: Exports tables intended for direct migration generation bydrizzle-kit.internalTables.ts: Exports tables potentially used internally but not directly included in auto-generated migrations.index.ts: Exports all schema components (from bothpublicTablesandinternalTables) for use by the Drizzle client (./client.ts).tables/: Contains the actual SQL table definitions (e.g.,profiles.sql).
migrations/: Stores the SQL migration files generated bydrizzle-kit(excluding index definitions).customMigrations/: Stores custom SQL files, primarily containingCREATE INDEXstatements extracted from the main migrations by theextractIndexes.tsscript. These are applied separately bymigrate.ts.client.ts: Exports the configured Drizzle database client instance, using schemas fromschema/index.ts.tables.ts: Exports a helper objecttablescontaining only thePgTableinstances from the combined schema, useful for direct table access.migrate.ts: Script executed to apply database migrations. It first runs standard migrations frommigrations/using Drizzle's migrator, then executes SQL statements fromcustomMigrations/individually (outside transactions). It targets the local development database by default and includes checks to prevent accidental production runs.seed.ts: Script to populate the database with initial or test data.scripts/extractIndexes.ts: A custom script that reads generated migration files inmigrations/, moves anyCREATE INDEXstatements tocustomMigrations/0000_CUSTOM_indexes.sql, and removes them from the original migration files.drizzle.config.ts: Configuration file fordrizzle-kit, specifyingschema/publicTables.tsas the input for migration generation.
Key exports (client, schema, tables) are defined in package.json.
- Drizzle ORM: TypeScript ORM for interacting with the database.
- PostgreSQL: The underlying relational database (hosted on Supabase).
- postgres: Node.js PostgreSQL client library used by Drizzle.
- Supabase: Used for database hosting, storage (buckets created in
migrate.ts), and provides CLI tools (supabase) for local development workflows (start, stop, reset, status). - drizzle-kit: CLI tool for managing Drizzle schemas and generating migrations based on
schema/publicTables.ts. - tsx: For running TypeScript files directly (used in migration and seeding scripts).
- dotenv: For loading environment variables (database connection strings from
.env.local).
Depends On:
-
@op/core: For configuration or shared types, specificallyadminEmailsused inseed.ts. -
@op/typescript-config(Dev): Used for TypeScript configuration during development.
Depended On By:
@op/trpc: Uses the DB client and schema for API procedures.
- Local Database: Use
pnpm start(runssupabase start) to spin up the local Supabase development environment. - Schema Changes: Modify files in
schema/tables/*.sqland update exports inschema/publicTables.tsorschema/internalTables.ts. - Generate Migrations: Run
pnpm generateafter schema changes. This runsdrizzle-kit generate(based onpublicTables.ts) and the customextractIndexes.tsscript (which moves index statements tocustomMigrations/). - Apply Migrations: Run
pnpm migrateto apply pending migrations frommigrations/andcustomMigrations/to the configured database (local development database by default). - View Schema: Use
pnpm studio(runsdrizzle-kit studio) to browse the schema visually. - Seeding: Run
pnpm seedto populate the database. - Reset Local DB: Use
pnpm reset(runssupabase db reset) to completely reset the local Supabase database. - Lint: Run
pnpm lint.