moleculerjs / moleculer-db

:battery: Database access service mixins for Moleculer
https://moleculer.services/
MIT License
152 stars 121 forks source link

How to use multiple databases (multitenancy) #147

Open thailan opened 4 years ago

thailan commented 4 years ago

Hello , how can I use multiple databases for my project? For my current php application , I created a middleware where it gets subdomain and connects users to its database , so its subdomain based multitenant application. How can I achieve similar approach? Moleculerjs is is awesome , so I want to remake my application with moleculerjs. I use mysql, I believe I should use sequelize for my application. Thank you in advance for your help.

epicbytes commented 4 years ago

I had such experience recently, the whole point is to make a custom adapter and process the variables, at some point I collect the variables, middleware forwards them further and replaces the adapter, or rather creates more connections and adds an adapter with an identifier that the before hook accesses it and already works with another database through the adapter, maybe this is not the most beautiful way, but I could not find another.

dragan-lalos commented 4 years ago

Hello,

I do not want to open a new issue, and I hope my problem is similar to this one. If not, I will open a new issue.

The rule said:

Database per service

Moleculer follows the one database per service pattern. To learn more about this design pattern and its implications, check this article. For multiple entities/tables per service approach, check the FAQ.

Then you have the rule:

At the moment, Moleculer DB only supports one model per service.

The first rule makes sense because it follows the Database per service pattern; the second I can also understand.

The problem is that the first rule is not working. I tried to have DB per service, and in MongooseAdapter, I pass different DB URI, but Moleculer somehow always finds a wat to initiate one DB and put all documents into one DB.

adapter: new MongooseAdapter(process.env.MONGO_URI_RAW_DATA || "")

Does anyone has this problem? Any idea what is wrong?

AndreMaz commented 4 years ago

Hey @dragan-lalos . You might check https://github.com/moleculerjs/moleculer-db/issues/132#issuecomment-540174139 I think this is want you want