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 DB connections #11186
Comments
Have you considered instantiating sequelize twice?
I never tested this, but I would guess it should work... |
I do something similar (I have 2 configs and 2 database model folders then export 2 different DB objects with relevant names) initially. I connect the first database and query for the IP and other info for that specific user's database, which I then use to connect to their database (second database model but specific to each user). My problem is that once I am connected to the second database I need a way to manage the connections to different database with the same models so I can verify a user before they are able to access that connection. |
Actually I didn't really understand your problem... What is the matter of having the same models? |
The database schema are the same so I would need the same models to apply |
I don't see why this prevents you from using what I suggested:
Can you try it? Not 2 configs and 2 database model folders but just one config and one database folder, with the only difference being the two Sequelize instances above. |
This way I only get one instance of each database, I need multiple instances of the second database because I need to connect to different IPs at different times. |
Ah, I get it now... I don't know how to help then, unfortunately... I actually don't have experience with using Sequelize with multiple connections (I tried to help with general ideas but now I think I can't really help further)... Good luck! Hopefully someone else will show up to help. |
No worries, thank you for your time and effort! |
Found a temporary solution...
I changed the index file to be a function and call it on every route. It seems to create a new instance of my second database while still associating all models. Any thoughts? |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
My team is dealing with a similar issue. Some details for context. The basic logic in the db API backend is:
So basically we create our own pool of sequelize instances with one per customer. The problem we are seeing is the multiple instances of sequelize seem to using the same mysql db connection. When we are running multiple db inserts for multiple customers, we end up with inserts to the wrong customer db. e.g. Customer A db inserts end up in the customer B db. While tracing through the code we've ensured that the correct sequelize instance is found in the array and is passed along for the right customer. We just can't make sense as to why the insert hits the wrong db. Here is the code for creating and retrieving the connections. Any ideas why this wouldn't work to allow a single sequelize instance per customer db and the connections seem to be mixed?
|
I'm not seeing where you're looping over the configs to add them to the pool. The other thing that I would recommend is to use an object instead of an array so you can do things like:
Unless I'm missing the point here... You have to have some kind of application logic that would determine which one from the pool to send the query to. |
Since this post we've change this and related code and have a new system in place to deal with the multiple databases. Thanks for the information. |
@John-Bitner it's an interesting concept using multiple databases... can you share what the new solution is? |
Firstly, thank you for a great package, it is always a huge help.
Below is my issue and possible feature request. If there is already a way to do this or another possible solution then please point me in the right direction, I have not found anything yet.
Is your feature request related to a problem? Please describe.
My app connects to one database to authenticate a user then get the IP and other DB info to connect to that users database for the remainder of their session (all databases are identical and would therefore point to the same table models). The issue is that once I connect to a database I am limited to only that connection.
Describe the solution you'd like
Ideally the pool functionality would allow me more control over which connections are active and would allow me to choose which user connects to which database. (I am using a jwt in axios headers)
Why should this be in Sequelize
Adds minor customization to the basic usage that would be beneficial to many.
Describe alternatives you've considered
I am using
myDB.sequelize.connectionManager.config.database = newDBName
currently for all fields I need to change as middle ware for every route.Usage example
Would allow me to create new connections if one doesn't exist then connect to the right db for the right user (allowing multiple instances to run concurrently)
Additional context
Thank you!
The text was updated successfully, but these errors were encountered: