Shared Database
The shared-db package provides database connectivity and ORM functionality for the Substrate application using Prisma ORM with PostgreSQL.
Features
- Prisma ORM - Type-safe database access with auto-generated client
- PostgreSQL Support - Uses the
@prisma/adapter-pgfor connections - Profile Management - Built-in Profile entity for user data
- Type Integration - ORM types conform to shared-types interfaces
Type Integration
The package demonstrates how to connect shared-types TypeScript interfaces with Prisma ORM models through compile-time verification that ensures Prisma-generated types are compatible with shared-types interfaces.
Environment Variables
Configure the database connection using environment variables:
Connection String
The DATABASE_URL environment variable is required and determines which database adapter to use:
# PostgreSQL connection (production)
DATABASE_URL=postgres://user:pass@host:5432/database
# SQLite connection (local development and testing)
DATABASE_URL=file:./dev.db
# or with absolute path
DATABASE_URL=file:/app/test.db
Database Selection
The package automatically selects the appropriate database adapter based on the DATABASE_URL protocol:
file:protocol → SQLite adapter (@prisma/adapter-better-sqlite3)postgres://orpostgresql://protocol → PostgreSQL adapter (@prisma/adapter-pg)
Individual Parameters (PostgreSQL Only)
When running in Docker or other environments with PostgreSQL, you can also configure using individual parameters:
| Variable | Description | Default |
|---|---|---|
DATABASE_URL | Full database connection string | Required |
POSTGRES_HOST | PostgreSQL host | localhost |
POSTGRES_PORT | PostgreSQL port | 5432 |
POSTGRES_DB | Database name | substrate |
POSTGRES_USER | Database user | postgres |
POSTGRES_PASSWORD | Database password | - |
POSTGRES_SSL | Enable SSL connection | false |
Running Migrations
Containers should be able to run yarn prisma and its subcommands.
SQLite
To create and initialize an SQLite database, run the following from inside the container:
$ yarn prisma db push --schema node_modules/shared-db/prisma/schema.sqlite.prisma --config node_modules/shared-db/prisma.config.ts
API Reference
See the API Reference for detailed documentation on all exports.