[Lucid] [v6] Switching Connection with the Model #4433
Replies: 2 comments
-
Up, V5 uses Database.connection for change, in version v6 it removed everything and didn't document it |
Beta Was this translation helpful? Give feedback.
-
There's a specific note on the This property is more for if you wanted a specific model (or set of models) to use their own database. All of the base model methods take an additional optional What you can do is pass to this a connection retrieved from the Here I have two databases: The first database ( |
Beta Was this translation helpful? Give feedback.
-
I am conducting some tests with AdonisJS and would like to inquire about the multi-tenancy handling capabilities of Lucid/AdonisJS. We are considering using the framework for the largest group of universities in Latin America, and the framework has significantly captured my attention. However, there is not much content available regarding its operation with multi-tenancy.
Currently, I am attempting to create a Proof of Concept (PoC) that adheres to the following premises:
In this context, I have successfully tested some use cases, for example, using the domain to determine the tenant. I implemented a middleware that identifies which tenant the connection refers to and then calls the
addTenantConnection
function. This middleware is used to populate the tenant information in the HttpContext, which I also use to successfully add a connection in theconfig/database.ts
.I also succeeded in implementing a migration routine for tenants using the above-described feature, also utilizing the MigrationRunner available within the Lucid package.
Although the code may not be elegant, it is functional, and I plan to refine it later.
Now, moving on to my question: how can I use this capability to utilize the models available in
App/models
with the active record pattern? I searched the documentation, but it only references how to switch the connection for the queryClient, which is a distinct class from BaseModel, therefore not allowing me to reuse the documentation. I noticed that theconnection
property exists, but it only changes a property, and the connection still persists on 'master':Whenever I execute this route, I notice that:
Another point I noticed is that the master database has the same email. I tested this, and even with such information, nothing returns.
Does this functionality exist? Or in my case, will I be forced to directly make the queries?
Beta Was this translation helpful? Give feedback.
All reactions