Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  32] [ 6]  / answers: 1 / hits: 5676  / 3 Years ago, tue, june 29, 2021, 12:00:00

I have .env file like


DATABASE_URL="sqlserver://srv:50119;initial catalog=mydb;user=aaa;password=bbb;"

and then schema.prisma like


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

generator client {
provider = "prisma-client-js"
previewFeatures = ["microsoftSqlServer"]
}

I generate a client using:


npx prisma generate

and then Prisma works great in my express app using:


const prisma = new PrismaClient();

Say I wanted to use a different db for user for multi-tenancy, how can I achieve this? Ideally I'd want to switch the db connection at runtime but it seems that DATABASE_URL is only read during prisma generate and not at runtime so the generated client ends up with a hardcoded db url.


More From » prisma

 Answers
8

You can use the datasource property to create a new PrismaClient instance and pass a dynamic URL.



datasources


Programmatically overrides properties of the datasource block in the schema.prisma file - for example, as part of
an integration test. See also: Data sources



[#1166] Tuesday, June 22, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
Fri, Aug 21, 20, 00:00, 4 Years ago
Tue, Aug 11, 20, 00:00, 4 Years ago
Tue, Apr 14, 20, 00:00, 4 Years ago
;