Lucid - Database change database when running query

#1

Hi,

I am looking to do a multi-tenant website with one database for each tenant. (SaaS - Me and my team have gone through all the pros and cons for all our requirements and we decided on it).

With Lucid I thought about extend the Provider but I don’t think that will work.

I tried a bit with Objection JS and it works great. Why?

Because I can just specify the correct connection when doing queries like:

MyModel.query(myNewConnection).findOne( ... )

I basically have a middleware that creates that said connection by looking at the subdomain of the request and that corresponds to part of the database from the tenant.

With Lucid I can’t find a way to do that. Is there a way by extending the service provider that I’m not seeing? So that when I use a Model for a query it uses a new connection?

Thanks

0 Likes

#2

I have not tried this but can you not:

MyModel.connection('someOtherDb').first()

?

0 Likes

#3

Models do not take a connection right now. But I believe we can add support for same

Can you please create an issue for same with adonis-lucid repo?

0 Likes

#4

@virk I created an issue here:

2 Likes

#5

Hope this feature is implemented :star_struck:

0 Likes