Database Management
Relevant source files
The following files were used as context for generating this wiki page:The Analog application utilizes a PostgreSQL database, managed through the Drizzle ORM. This system is designed for local development using Docker and is orchestrated via Bun scripts. The database layer is encapsulated within the @repo/db workspace package, which provides the database client instance, schema definitions, and management scripts.
This document outlines the architecture, setup process, and tooling for managing the database within the Analog project. The configuration ensures a consistent development environment and provides a clear workflow for schema modifications and database operations.
Architecture
The database architecture is composed of several key technologies that work together to provide a robust data layer for the application.
- Database: PostgreSQL is the chosen relational database system.
- ORM: Drizzle ORM is used as the TypeScript ORM for type-safe database queries and schema management.
- Client: The
postgres(postgres.js) library is used as the high-performance PostgreSQL client for Node.js.
The following diagram illustrates the technology stack for the database layer.
Sources: packages/db/package.json, packages/db/src/index.ts, README.md:80
Connection Management
The database connection is established and managed in packages/db/src/index.ts. A single connection instance is created using the DATABASE_URL environment variable.
Key features of the connection logic include:
- Connection Caching: In development (
NODE_ENV !== "production"), the database connection (conn) is cached on theglobalThisobject. This prevents the creation of new connections on every Hot Module Replacement (HMR) update. - Prefetch Disabled: The
prepare: falseoption is passed to thepostgresclient. This is explicitly done to support Supabase's "Transaction" pool mode. - Casing Convention: The Drizzle ORM is configured with
casing: "snake_case". This automatically maps camelCase field names in the application code to snake_case column names in the database schema.
Sources: packages/db/src/index.ts:3-28
// packages/db/src/index.ts:14-28
/**
* Disable prefetch as it is not supported for Supabase "Transaction" pool mode.
* @see https://supabase.com/docs/guides/database/connecting-to-postgres#supavisor-transaction-mode
*/
const conn = globalForDb.conn ?? postgres(env.DATABASE_URL, { prepare: false });
if (env.NODE_ENV !== "production") globalForDb.conn = conn;
/**
* Automatically map camelCase to snake_case in the database.
* @see https://orm.drizzle.team/docs/sql-schema-declaration#camel-and-snake-casing
*/
export const db = drizzle(conn, { schema, casing: "snake_case" });
The following sequence diagram shows the application startup and database connection process.
Sources: packages/db/src/index.ts:10-28
Setup and Configuration
To run the database locally, you need Docker and correctly configured environment variables.
Environment Variables
The primary environment variable required for the database connection is DATABASE_URL. This variable must be defined in a .env file at the root of the repository.
| Variable | Description | Example Value | Source File |
|---|---|---|---|
DATABASE_URL | The connection string for the PostgreSQL database. | postgresql://postgres:postgres@localhost:5432/postgres | README.md:40, turbo.json:85 |
This variable is copied from .env.example during initial setup.
Sources: README.md:36-40, CONTRIBUTING.md:27-31
Docker Setup
The project uses Docker to run the PostgreSQL database in a container. The setup is managed by docker-compose.yml.
-
Start the database container:
bun run docker:upThis command, defined in the root
package.json, executesdocker compose up -d. -
Initialize the database:
Once the container is running, the database schema must be pushed.bun run db:pushThis command uses Drizzle Kit to synchronize the schema defined in the code with the database.
Sources: README.md:80-90, package.json:8,13
Tooling and Scripts
Database management tasks are handled by drizzle-kit, a CLI tool for Drizzle ORM. These tasks are exposed as npm scripts in both the root package.json and the @repo/db package.
The following diagram shows the typical development workflow for schema changes.
Scripts
The following scripts are available for database management. They are defined in packages/db/package.json and can be run from the root directory via turbo.
| Script | drizzle-kit Command | Description |
|---|---|---|
db:generate | drizzle-kit generate | Generates SQL migration files based on schema changes. |
db:migrate | drizzle-kit migrate | Applies pending migrations to the database. |
db:push | drizzle-kit push | Pushes schema changes directly to the database without creating migration files. Ideal for development. |
db:studio | drizzle-kit studio | Launches a local web GUI to browse and query the database. |
Sources: packages/db/package.json:9-12, package.json:11-14, turbo.json:79-98
The turbo.json configuration ensures that these scripts have access to the DATABASE_URL environment variable.
Sources: turbo.json:85,90,95
Dependencies
The @repo/db package contains all the necessary dependencies for database operations. The @repo/api package lists @repo/db as a dependency, indicating it consumes the database client.
@repo/db Dependencies
| Package | Version | Description |
|---|---|---|
drizzle-orm | ^0.44.5 (peer) | The core Drizzle ORM library. |
postgres | ^3.4.7 | The PostgreSQL client library. |
drizzle-kit | ^0.31.4 (dev) | CLI tool for migrations and schema management. |
@repo/env | workspace:* | Provides access to environment variables. |
Sources: packages/db/package.json:14-29
@repo/api Dependency
The API layer depends on the database package to interact with the data store.
| Package | Version | Description |
|---|---|---|
@repo/db | workspace:* | Provides the configured db client and schema types. |
Sources: packages/api/package.json:35