mrtom
02/03/2022, 9:28 PMMONGO_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.
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?Joël
.env
file since there you can combine env vars using the format specified by dotenv-expand like
DATABASE_URL="mongodb://${MONGO_APP_USER}:${MONGO_APP_PASSWORD}@${MONGO_HOST_CONNECTION}/tools?${MONGO_OPTIONS}"
and have your schema like
// schema.prisma
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
See https://www.prisma.io/docs/guides/development-environment/environment-variables#expanding-variablesJoël
mrtom
02/07/2022, 8:02 PMprisma
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?mrtom
02/07/2022, 8:29 PMJoël