3.8 KiB
@repo/db
Shared database package for the monorepo using Drizzle ORM and Supabase (Postgres) as the database. This package provides a centralized way to manage database connections, models, migrations, and seeders across all applications in the monorepo.
WIP - Current implementation has no authorization and application logic yet. It is a basic setup to get started with Drizzle ORM and Supabase.
TODO:
- Configure to work with
Clerkfor authentication and authorization - Implement application logic for the database models
What's inside?
This repo uses Drizzle ORM for database interactions and includes the following:
Apps and Packages
drizzle-orm: ORM for TypeScript and JavaScript@drizzle-orm/postgres: Postgres driver for Drizzle ORMdrizzle-kit: Tool for generating migrations and managing the database schema@faker-js/faker: Library for generating fake data for seeding the database
Structure
The database package is structured as follows:
src/
├── queries/ # Database queries
│ ├── products.ts # Example product query
│ └── users.ts # Example user query
├── schema/ # Database models
│ ├── column.helpers.ts # Helper functions for column definitions
│ ├── products.ts # Example product models
│ └── user.ts # Example user model
├── seeders/ # Database migration files
│ ├── products.ts # Example product seeder
│ ├── seed.ts # Main seeder file
│ └── user.ts # Example user seeder
├── database.ts # Database connection and initialization
└── index.ts # Database schema exports
supabase/ # Supabase configuration and migrations
├── migrations/ # Database migration files
│ └── *.sql # Example migration file
├── seed.sql # SQL file for seeding the database
└── config.toml # Supabase configuration file
Local Supabase Instance
To run a local Supabase instance, you need to have the Supabase CLI installed
https://supabase.com/docs/guides/local-development/cli/getting-started
Some commands to manage the local Supabase instance:
npx supabase init: Initialize Supabase in the monoreponpx supabase login: Log in to your Supabase accountnpx supabase link: Link the Supabase project to the monoreponpx supbase start: Start the Supabase local development server using Docker. Docker must be installed and running.npx supabase stop: Stop the Supabase local development server
Development Commands
turbo run db:check: Check the database schema against the modelsturbo run db:generate: Generate database schema and typesturbo run db:migrate: Run database migrationsturbo run db:push: Push the current schema to the databaseturbo run db:pull: Pull the current schema from the databaseturbo run db:reset: Reset the online database. Connection to the online Supabase DB with Supabase CLI is required; Database password is required in.envfile;turbo run db:generatemust be run before this command to ensure the database schema is up-to-date, as the CLI will look for the migrations sql in the supabase directory.turbo run db:seed: Seed the database with initial data
Usage
WIP
To use the database package in your applications, you can import the necessary modules and functions from the @repo/db package. For example:
Known Issues
Please install latest version of "drizzle-orm"
- For
npmusers, runnpm install --force drizzle-kit drizzle-ormin root of monorepo - remove the entries in
package.jsonfile and runnpm installagain - https://github.com/drizzle-team/drizzle-orm/issues/2699#issuecomment-2660850530