I’m also running into a problem with the environme...
# mongodb
m
I’m also running into a problem with the environment variable setup. Rather than providing a single string for the connection URL we prefer to have 4 strings that later get concatenated together, i.e.:
Copy code
MONGO_OPTIONS=ssl=true&replicaSet=shard-1&authSource=admin&retryWrites=true
MONGO_APP_USER=user
MONGO_APP_PASSWORD=password
MONGO_HOST_CONNECTION=<http://shard-1.mongodb.net:27017|shard-1.mongodb.net:27017>
Whilst not essential, we have multiple different productionlike tiers (testing, staging, production etc) and we use this pattern everywhere. And we source the username and password from a secrets manager, and the other two from a shared template. It would be annoying to have to change this. I understand from this GitHub Issue that I can’t concatenate multiple variables in the
schema.prisma
file (which itself is a pretty frustrating limitation), but instead I can pass in an optional param to the Prisma Client, i.e.
Copy code
const DB_CONNECTION_URL = `mongodb://${process.env.MONGO_APP_USER}:${process.env.MONGO_APP_PASSWORD}@${process.env.MONGO_HOST_CONNECTION}/tools?${process.env.MONGO_OPTIONS}`;
const prisma = new PrismaClient({
  datasources: { db: { url: DB_CONNECTION_URL } },
});
However, this is not working. Prisma is still using the connection URL provided in the
schema.prisma
file. I assume this is a bug?
1
j
For this you could create a
.env
file since there you can combine env vars using the format specified by dotenv-expand like
Copy code
DATABASE_URL="mongodb://${MONGO_APP_USER}:${MONGO_APP_PASSWORD}@${MONGO_HOST_CONNECTION}/tools?${MONGO_OPTIONS}"
and have your schema like
Copy code
// schema.prisma
datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}
See https://www.prisma.io/docs/guides/development-environment/environment-variables#expanding-variables
Curious for your feedback here 🙂
m
Thanks @Joël. That should work for the app running within node, but wouldn’t that break running
prisma
from the CLI, i.e. if I wanted to pull a new schema with
npx prisma db pull
presumably that would need the
DATABASE_URL
to be set as a static string?
OK, I think I misunderstood how the CLI works and using dotenv-expand seems to work fine for me. Apologies for missing that in the documentation!
👍 1
j
No problem! Happy it helped 🙂