Skip to main content

Prisma

Migrations

Proxying

To run your migrations through the proxy setup your schema.prisma to read the database URL from an environment variable:

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

You can then use dotenv-cli to setup multiple .env files. For example, create a .env.proxy with a database URL to connect to the proxy:

DATABASE_URL=postgresql://postgres:${PG_PROXY_PASSWORD}@localhost:${PG_PROXY_PORT}/mydb

And then run commands using this env file:

dotenv -e .env.proxy -- npx prisma migrate dev

Which you can wrap up into a package script, as per the Prisma docs for Running migrations on different environments:

  "scripts": {
"migrate:postgres": "dotenv -e .env.proxy -- npx prisma migrate dev",
},

Applying DDLX statements

Customize a migration to include an unsupported feature.

First, use the --create-only flag to generate a new migration without applying it:

npx prisma migrate dev --create-only

Open the generated migration.sql file and add the electrify call:

ALTER TABLE items ENABLE ELECTRIC;

Apply the migration:

dotenv -e .env.proxy -- npx prisma migrate dev

Generator

Electric uses a customised version of the zod-prisma-types Prisma client generator to generate the Electric Client.

Event sourcing

Prisma provides Pulse, a type-safe API for subscribing to database change events.