-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple Connections / Databases / Datasources #2443
Comments
I think it would be nice to put each database in a separate file and have one file to which they will be connected. It also seems to me that it would be more convenient to have one client for all databases, something like this: |
@WellFail I'm not completely sure of what you mean by putting each database in a separate file. Your
|
I'm not sure if this would work, but have considered creating a model and a client for each datasource and then creating the contexts for them? |
@FredericLatour I meant that the data set may differ depending on the database, and it is not very correct to store them all in one file, this complicates the understanding. I understand that in your case this is not necessary. But a similar approach will be more flexible. |
@WellFail Having entirely different models for different databases/connection is a specific use case. In a multi tenant application, models will be shared by databases/connection. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@FredericLatour Different databases for different environments currently work depending on environment variables and do it perfectly, what you offer is unnecessary. If we are talking about the possibility of several databases within the same application, then I understand what it is about, but the best approach would be to create several |
@WellFail The approach I'm proposing has the advantage to be fully compatible with the existing approach. When defining a single datasource (like this is the case now) in your schema, then :
Please, provide some example of what is wrong in your mind and how you see things should work. |
@FredericLatour Currently, I use environment variables to use different databases in different environments. During the deployment, migrations are performed and a client is generated, depending on the variables. As for multiple databases within a single application.
In result we have one client like this
At the moment I am not considering a multi-tenant option, because how it is implemented now is more than enough. In this case, work with the prisma remains the same as it is now, and different data sets relating to different databases are clearly delimited by files, which simplifies the work, but we have a limitation, we cannot have models with the same names in different databases. In your solution I need to manually handle many prisma clients in my app for using different databases. In any case, in your approach it is worth spreading different data schemes into different files for better understanding and convenience. We do not consider multi-tenant, but I do not see other examples in which we need the same data set in different databases |
@WellFail Honestly I can't understand what's the point of your approach:
My approach has basically 2 added optional "keywords":
Having 1, 2 or 10 files for the schema definition is a completely different matter. It does not change anything to the proposed solution. |
@FredericLatour Oh, sorry, I’ve made a mistake, the database provider really can’t be a variable, but nothing prevents changing it by other means, unless of course you deploy app manually. Regarding the problem, my solution has only the problem of the impossibility of creating models with the same names in different databases (it seems to me that having the same models in different databases is not a good idea). |
Thanks for raising the issue and it's great to see such a lively discussion! What is already possible todayIt seems that important information is missing here. datasource db {
provider = "postgres"
url = env("PG_URL")
} import { PrismaClient } from '@prisma/client'
const client1 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db1' }} })
const client2 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db2' }} }) That means you can override the connection string in the What is possible today, but not niceAnother use-case mentioned in this issue is having different datamodels active in one application. In order to do that, you would need multiple
datasource db {
provider = "postgres"
url = env("DB1_URL")
}
generator client {
provider = "prisma-client-js"
output = "./generated/client1"
}
model Model1 {
id Int @id @default(autoincrement())
model1Name String
}
datasource db {
provider = "postgres"
url = env("DB2_URL")
}
generator client {
provider = "prisma-client-js"
output = "./generated/client2"
}
model Model2 {
id Int @id @default(autoincrement())
model2Name String
} In the codeimport { PrismaClient as PrismaClient1 } from '../prisma/generated/client1'
import { PrismaClient as PrismaClient2 } from '../prisma/generated/client2'
const client1 = new PrismaClient1()
const client2 = new PrismaClient2() In the CLIprisma generate --schema prisma/schema1.prisma
prisma generate --schema prisma/schema2.prisma We're aware that this solution is not nice to use and are looking into solutions to improve this experience. But from what I could see in this issue, the mentioned use-cases are already possible today. |
@timsuchanek thanks for the update. I had initially opened a discussion asking if it was possible to have multiple connections pointing to different db/schemas and was told that it was not currently supported by Prisma. So, yes, indeed, it seems that the mentioned use cases are already possible even though it's a bit hacky. Now, if you want to stick with one file=one datasource (am I correct assuming that is the case?), another alternative would be to allow for being able to dispatch the schema over multiple files either at the CLI level or by having an |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Maybe I am missing something here, but is the below "workaround" the only way for me to have a development database and production database for my Next.js project? This is a standard practice for my Firebase projects so I can interact and test my database without touching the production records. I'd be shocked coming from Firebase if this is the best work around using Prisma.
|
Nope! Prisma definitely supports your use case without having to resort to this. This issue is for setting up multiple databases in the same process, not configuring which db to use in a given process. For managing different environments, you can check out this: You can just configure your prod |
Amazing, thank you @JoeRoddy, this is exactly what I was looking for! |
I hope I am correct in bringing this question up here - otherwise please let know and I put in a separate discussion... We are currently evaluating prisma and are discussing possible architectures for a solution. The current approach is based von Next.JS, Prisma and MSSQL Server and involves the need that users can connect to different databases by configuration at runtime. So a user might have access to multiple databases by permission provided from a different backend and can choose at runtime to which db she wants to connect to. For now I'd assume that schema is structure-wise identical for all. So we wouldn't need to generate and maintain multiple prisma schemas - at least not for this. Potential user base is some 100s up to 1,000 of users, for concurrent users I wouldn't assume more than 50 (rather less). So we are wondering if there is a way to implement this using prisma. Is there any experience towards any of the approaches here? Obvious alternatives would be:
Any advice, recommendations are appreciated! Thank you! |
@sbuss1980 You might want to create a new discussion for this, from your description it does not sound like you want to be connected to multiple different databases (with different schemas, or even different database system) at the same time. What you describe sounds more like a multi tenancy setup. And that can already work independent of "multi database" supprot in Prisma, which this issue is about. |
|
Just posted a possible schema solution for this feature |
I would like to make a contribution on the subject. For the use case where 'user1 connects to db1, and user2 to db2' (same tables and schemas). LucidORM (by AdonisJS) does this simply, and can serve as inspiration for PrismJS: // List of available connections
connections: {
db1: {
client: 'pg',
connection: {
host: Env.get('PG1_HOST'),
port: Env.get('PG1_PORT'),
user: Env.get('PG1_USER'),
password: Env.get('PG1_PASSWORD', ''),
database: Env.get('PG1_DB_NAME'),
},
db2: {
client: 'pg',
connection: {
host: Env.get('PG2_HOST'),
port: Env.get('PG2_PORT'),
user: Env.get('PG2_USER'),
password: Env.get('PG2_PASSWORD', ''),
database: Env.get('PG2_DB_NAME'),
},
}
... //Query
import Database from '@ioc:Adonis/Lucid/Database'
Database
.connection('db1') // only change
.from('posts')
.select('*') I don't know technical details about how LUCID changes or manages these connections. (I'm new to programming) translated by Google |
This is the kind of functionality that we need. Thanks for the recommendation @souzacavalheiro |
It's been over 3 years, has Prisma solved this already? |
Is this related to the talk about Prisma at Prisma Day 2019: https://youtu.be/RAoXdyI_PH4?si=Zbi_zKwLskyX4tKk&t=1410 |
Hello all I also have the same problem, I have an app in which every customer has it's own db with the same schema. Do you know how I can solve my problem? The database is a mysql managed on Google Cloud SQL. Maybe prism data proxy could solve it? Thank you! |
We would also need this, please, if possible We currently have two services with NestJS
both services share the same database but the auth needs another account to |
Thanks! @notflip i prefer this approach because of clean structure which separate each database migration folders into different places. It works like a charm. |
I also wondered how to make dev and production in 1 scheme. So that I can test, for example, dev sqlite and production mysql. Since the provider environment cannot be loaded to create 2 env |
when I implement multiple database connection, I encounter error during build on Windows only
as I mentioned here |
Hello! This error returns to me when i try create more than one How to reproduce:
Returs this error: Error validating datasource |
Read the comments above |
I've read now. I did not saw these comments yesterday, sorry sir. |
Did you encounter a conflict in doing |
@AlecBlance did you succes to build this? I failed when I do pnpm build on windows and success when I build it on linux or Mac |
@vatoer some recent dependency update has broken it, I've been using postgres and sqlite side by side for a year successfully and after doing a bunch of updates in my repo it is now broken (next app failing to import with pnpm) |
Problem
An application may need to access different connections/databases.
One use case could be having the exact same set of tables into different databases (multi-tenant approach).
Another use case could be having to access punctually some specific data in a separate database/server.
In any cases "a single connection" for the entire application is a really strong limitation.
Any SQL driver or ORM is capable of connecting to multiple databases either by direct support (TypeOrm) or by creating 2 instances. Unless I missed something this is not possible with Prisma client.
Suggested solution
I believe that it can be easily achieved with the following approach that does not change much both on the schema and client side of things.
You would then need to make it possible to use one connection/db or another:
Additional context
Please note that it has nothing to do with having a dynamic url for being able to point to a different database when in development and/or staging. This feature is also needed but it's a different matter.
Note also that this is not exclusively to solve "multi-tenant" scenarios. I may need to access a database on another server whatever the reason.
Update 01: different database per environment use case
Thinking a bit more about it, this feature also provides a solution to the problem of having a different database (sqlite vs other database in prod for instance) depending on the environment (prod, staging, dev).
Adding on my previous example, you could have :
And then depending on the environment:
The text was updated successfully, but these errors were encountered: