Multiple Database Connection - Vapor 3 and Fluent

Hello!

I would like to be able to configure multiple connections to databases. I got a possible solution here: Vapor 3: Using multiple databases - Stack Overflow.

However, I am currently using Fluent and cannot find a solution. It should be noted that my models are not migrated since the database is predefined, so I only have to register the default database. I leave you a image of the configuration file.

Captura de Pantalla 2020-07-14 a la(s) 23.50.26

I have tried in various ways, but I always run into the problem that the database connection to the models is defined in the config file when starting the server, the idea would be to modify them at runtime.

I hope someone can help me out :frowning:

(Sorry for my English)

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi!

So if you want to use multiple databases it’s fairly simple. First you need to create a new database identifier (.mysql is a default one). You can then set the default database for whichever model should live in which database, e.g.

RegionModel.defaultDatabase = .mysql
CustomerModel.defaultDatabase = .otherMysql

Then in your DatabasesConfig you just need to register that database:

databases.add(database: database, as: .mysql)
databases.add(database: otherDatabase, as: .otherMysql)

Hope that helps!

Thanks for answering.

However, I already tried it and it works perfectly, but it really doesn’t do what I need. :cry:

Currently I have 3 databases and each one belongs to a different customer, my logic in the backend is the same for everyone, so if I change something in one, I must replicate it in another. My idea is to have a backend and, through a header, tell my backend if it is going to communicate with customer database 1, 2 or 3.

From what I understand, using Fluent, models must necessarily be initialized with their database when starting the server and cannot be modified at runtime. Apparently this can be done but without using Fluent, as this person tries: Vapor 3: Using multiple databases - Stack Overflow. Basically using direct SQL against the Database connection.

Ahhh I see what you mean - yeah that isn’t possible in Vapor 3 at all. I think you should be able to do it in Fluent 4 though

1 Like

:+1: I’ll try Fluent 4. Thank you very much. :smile:

Excuse me @0xtim, I have the same necessity that Richard and I need to connect more that one database on Vapor 4. I’m trying various ways but with failed results. I don’t know how to set the defaultDatabase on a model in Vapor 4 because defaultDatabase is missing, and in the next part on DatabasesConfig, I see that you use a DatabaseIdentifier but on Vapor 4 does not exist too (different from Vapor 3 where it can be possible define as extension, but DatabaseKit used for that, now does not part from Vapor 4) and DatabaseID is not the same kind… and im very confused how can be possible to set various databases connections with Vapor and Fluent 4. Can you help me please? Thanks!

Hi! (Apologies I’ve been on holiday)

So in Vapor 4 things are a little different. Basically whenever you perform a query, you pass in the database to perform the query on. This just uses a default database in most cases (MyModel.query(on: req.db)) but you can specify a different database instead of req.db if you need to connect to different databases

Hello @danurigom I hope you are well. I hadn’t checked the “Ray” forum for a long time and hadn’t seen your question. In my case, I managed to solve the problem of several database connections in both Vapor 3 and Vapor 4. In Vapor 3 I had to overwrite several classes of Vapor, however it is no longer necessary to go into details because we currently have Vapor 4. I’m currently finishing the migration to Vapor 4 ( :neutral_face: this update broke a lot of code). Vapor 4 is basically a bit more flexible when it comes to allowing it to make connections to different databases due to invocations using Fluent 4 does not use the “Request” object but needs a “Database” object which makes the implementation a bit easier. However, Vapor 4 (in my opinion) still has some problems regarding the BD’s configuration, however I managed to solve in a simple way:

  1. The connections to the databases must be declared within the project since Vapor needs to load them when deploying the backend, for example:

Captura de Pantalla 2021-04-07 a la(s) 17.19.59

  1. Generate a function to intercept the requests made to the backend and using a header I determine the database that I am going to use:

Captura de Pantalla 2021-04-07 a la(s) 17.20.07

Basically I get the request header and depending on that this function decides which DB connection to use.

  1. Finally, a request would be as follows:

Captura de Pantalla 2021-04-07 a la(s) 17.20.13

I always get the connection to the DB and then I pass it on to the query I am going to make.

In short, this would be my solution. If you still have problems, I hope you can get an idea.

NOTE: I’m sorry for my English :sweat_smile:.

1 Like