last updated April 18th, 2026
This document explains how the backend connects to PostgreSQL using Prisma, where the connection is created, and what environment variables are required.
- The server starts and creates a
BackendController. BackendControllergets a singletonDatabaseServiceinstance.DatabaseService.initialize()callsprisma.$connect().- Backend methods call
DatabaseServicemethods, which run SQL through Prisma. - On shutdown (
SIGINT/SIGTERM), backend cleanup callsDatabaseService.close()and disconnects Prisma.
The db package and server package should use Prisma connection URLs.
# Runtime queries (pooled, Supabase Transaction mode)
DATABASE_URL=postgresql://<user>:<password>@<host>:6543/postgres?pgbouncer=true
# Direct connection (for prisma migrate / db push / db pull)
DIRECT_URL=postgresql://<user>:<password>@<host>:5432/postgresNotes:
- Use
DATABASE_URLfor app runtime. - Use
DIRECT_URLfor schema operations and migrations.
From packages/db:
yarn db:upThe local database container is defined in packages/db/docker-compose.yml and uses postgres:17.
Set values in:
packages/db/.envfor Prisma CLI operations.packages/server/.envfor backend runtime.
At minimum, set DATABASE_URL. For migration/pull/push commands, also set DIRECT_URL.
From packages/db:
yarn db:pull
yarn db:generateOr push schema changes:
yarn db:pushFrom the repository root:
yarn dev:serverFile: packages/server/src/controllers/BackendController/BackendController.ts
- The backend imports
DatabaseServicefrom the db package. - In the constructor, it sets
this.databaseService = DatabaseService.getInstance(). - It then calls
this.initializeDatabase(). initializeDatabase()awaitsthis.databaseService.initialize()and logs success or failure.
File: packages/db/src/services/DatabaseService.ts
DatabaseServiceis a singleton (private static instance).initialize()checksisConnectedbefore callingprisma.$connect().- Methods run SQL via Prisma raw-query methods.
close()callsprisma.$disconnect()and flipsisConnectedtofalse.
File: packages/db/src/data-source.ts
- Loads env files from the db package (
.env,.db.env). - Ensures
DATABASE_URLexists (or builds one from host/port/username/password). - Exports a shared
prismaclient instance.
- Server logs should include
Database connection established successfully! - To inspect local DB container logs:
cd packages/db && yarn db:logs- To open a SQL shell against local container:
cd packages/db && docker-compose exec db psql -U postgres -d postgresUse this checklist any time telemetry fields are added, renamed, or removed.
- Update field names/types in
packages/shared/src/types.ts. - If needed, update fake packet generators in
packages/shared/src/functions.tsand client fake data files.
- In
packages/db/src/services/DatabaseService.ts, ensureflattenTelemetryData()emits keys that match the shared type names exactly. - For nested objects, add or remove explicit mapped keys (for example, motor detail field mappings).
- Add/remove matching columns in
packages/db/prisma/schema.prismaundermodel telemetry_packet. - Keep column names consistent with flattened packet keys (including exact casing, such as
12Vvs12v).
From repo root:
yarn workspace db db:push
yarn workspace db db:generateyarn workspace db build
yarn workspace server build- Run server and insert telemetry (real or fake packet).
- Confirm inserts succeed and no missing-column errors occur.
- Remove it from shared types and any packet generators.
- Remove it from
flattenTelemetryData()mapping. - Remove it from Prisma schema.
- Run schema sync and builds again.
- If production data must be preserved, create and review a migration instead of direct push.
Make sure packages/server/.env includes DATABASE_URL.
Make sure DIRECT_URL is set and reachable. Supabase schema commands should use the direct 5432 URL.
If using local DB, ensure the container is running and port 5432 is available.
Confirm credentials and host in the URL are correct and URL-encoded if needed.