← Back to blog

Why I use Prisma for all my projects

by Sukhpal Saini

Having the DB schema in the project as a file is a damn blessing - this is why I love having Prisma as my ORM. Prisma lets you define your tables as a file in your DB which means my AI agent always knows what columns etc. are avialable. This helps massively in 3 ways:

  1. It can create new API routes + UI views automatically + test them as well

  2. When I ask for product related reports, it knows how all the data is linked together to give me a full picture of a query like - "How many people signed up yesterday and how much cost did we incur for the carousels in total."

  3. Migrations are pain free. I never have to touch the tables directly myself. Prisma will see the change I made in the schema and reflect that onto the DB by creating a SQL file itself.

How to safely use Prisma

Prisma is basically an ORM like Supabase but they also have a hosting service which we use. You save all your table details in a file called schema.prisma , you make changes there and the idea is that Prisma will take care of whatever migrations that need to happen.

How to work in Dev

You always want to have your DB in .env pointed to Localhost. When you need Prod data, you can keep a sync script that imports all the data from the Prod DB to the local one. This is what mine looks like:

#!/usr/bin/env bash
set -euo pipefail

REMOTE='PROD_URL'
LOCAL='postgresql://postgres:postgres@localhost:5432/my-database'
JOBS="${JOBS:-4}"
DUMP_DIR="${TMPDIR:-/tmp}/my-database-dump"

dropdb --if-exists my-database -p 5432
createdb my-database -p 5432

rm -rf "$DUMP_DIR"
pg_dump \
  --format=directory \
  --jobs="$JOBS" \
  --no-owner \
  --no-acl \
  --no-comments \
  --schema=public \
  --file="$DUMP_DIR" \
  "$REMOTE"

PGOPTIONS="-c maintenance_work_mem=1GB -c synchronous_commit=off" \
  pg_restore \
    --jobs="$JOBS" \
    --no-owner \
    --no-acl \
    --dbname="$LOCAL" \
    "$DUMP_DIR" || true

rm -rf "$DUMP_DIR"

Make the script executable on Mac with: chmod +x sync-db.sh and run it with: ./sync-db.sh

Making a schema change

Say you want to add another table or make a column change, update the prisma.schema , make sure you are on Local DB and run:

npx prisma migrate dev

If the change is easy, it will create a migration.sql automatically. Sometimes it says that it has to reset the DB with:

npx prisma migrate reset

this basically means wiping the DB. It’s safe to say yes in Local. Once done, it will make the migration.sql.

This change is auto applied to the Local DB so you can write code now referencing the new column names etc.

This change still has not been applied to Prod.

Updating Prod DB

Once you’re ready to deploy your code, you need to first update the pending migrations (it already knows which ones have been applied and which haven’t). You do that by running:

npx prisma migrate deploy

This will never say that it will reset the DB. If there’s a problem it will decline to apply.

Once successful, you can deploy the code.