Doppler not working with Prisma ORM

Hey everyone, I have an issue with using Primsa ORM + Doppler. To initialize my Postgres db via Prisma, I have to connect to the db via a schema.prisma file. You can either provide a raw URL string or an env variable with the env() command. Here’s how my datasource looks:

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

This works if DATABASE_URL is defined in a .env file in my local repo. However, I’m using Doppler secret management to insert env variables in at runtime. If I remove the .env file then Prisma throws an Environment variable not found error because it can’t fetch the env variable from Doppler. All my other Doppler env variables work in my other files by calling process.env.ENV_VAR_NAME, so I’m not sure how to load doppler env variables in before schema.prisma runs.

Here’s my current package.json script, which runs doppler before prisma: doppler run -- prisma generate --schema=./infrastructure/database/schema.prisma && prisma migrate dev --name=updateModel --schema=./infrastructure/database/schema.prisma

Solved it.

The error was in a script I wrote called updatePrisma that handled generating the clientand migrating db changes.

Original script:
doppler run -- prisma generate --schema=./infrastructure/database/schema.prisma && prisma migrate dev --name=updateModel --schema=./infrastructure/database/schema.prisma

New, working script:
prisma generate --schema=./infrastructure/database/schema.prisma && doppler run -- prisma migrate dev --name=updateModel --schema=./infrastructure/database/schema.prisma

1 Like

Hey! Glad you were able to identify the issue there! This kind of mistake can be hard to see sometimes and can be super frustrating! Happy it didn’t block you for too long here!

Have a great weekend!