Skip to content
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 dynamic connections (500 or more) #7504

Closed
gustavoportaluppi opened this issue Apr 10, 2017 · 4 comments
Closed

Multiple dynamic connections (500 or more) #7504

gustavoportaluppi opened this issue Apr 10, 2017 · 4 comments

Comments

@gustavoportaluppi
Copy link

I have an API in nodeJS and need to connect dynamically to different PostgreSQL databases (500 or more connections), all on the same server. I have a default database for all users, which will save the basic data for authentication. After being authenticated, the user's stream will be directed to another database (each user will have access to a specific database).

I could instantiate the Sequelize for each connection, but I think it will be dangerous for app performance. Is there any other solution / resource so I can access different databases through the same instance of Sequelize?

@StyleT
Copy link

StyleT commented Apr 11, 2017

Hi! I would suggest you not to use ORM for such purpose & stick to the simple PostgreSQL library.

@gustavoportaluppi
Copy link
Author

Yes, this is a solution, but an ORM would make it a lot easier.
I'm searching on Sequelize and found the option to change the "searchPath".
My application is multi-tenancy, and I think that's exactly what I need.
I have hundreds of databases, all with the same models.

models.user.findAll ({searchPath: 'base2'});

When executing the above code, I get this error: Unhandled rejection SequelizeDatabaseError: relation "users" does not exist.
Do you have any idea what it might be?

@sushantdhiman
Copy link
Contributor

Whenever an user joins your application you need to open a connection to new database, you can initialize one sequelize instance with max pool set to 1. This will keep only one connection open for that specific user (and its database).

Although I feel this is not a good approach, you should use schema instead.

If your application need multi tenancy you can use schemas as well, thats what searchPath sets. A schema works like a way to organize tables inside database.

I have to warn you schema support is lacking in Sequelize but can we worked with.

Lastly error you are facing is because there is no users table in base2 schema.

@sandeep0009
Copy link

hey i got the same issue will anyone guide me right solution because i have multiple tenant and i want access particular instance of them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants