Skip to main content

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-pg for 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:

Individual Parameters (PostgreSQL Only)

When running in Docker or other environments with PostgreSQL, you can also configure using individual parameters:

VariableDescriptionDefault
DATABASE_URLFull database connection stringRequired
POSTGRES_HOSTPostgreSQL hostlocalhost
POSTGRES_PORTPostgreSQL port5432
POSTGRES_DBDatabase namesubstrate
POSTGRES_USERDatabase userpostgres
POSTGRES_PASSWORDDatabase password-
POSTGRES_SSLEnable SSL connectionfalse

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.