-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Auth support for useDb()
#8116
Comments
We are a handful of developers working on a multi-tenancy solution, and this has quickly become an issue. Each tenant has their own database with a specific user. Creating new connections or establishing and saving a connection for each request feels expensive. Is there another workaround for this until it's added in the mongoose library? Thanks in advance! |
Creating new connections for each tenant is generally the way to go. Switching on each request using |
Cool cool, we'll keep our approach as is then, thanks for the response 👍 |
I took a closer look and unfortunately this is not possible. The MongoDB Node.js driver deprecated support for additional |
In order to connect to different databases with different authentication requirements, you need to use multiple connections. |
@vkarpov15 What if you connect to one database with one set of credentials, but then try to |
@darinrogers Mongoose's docs on multi-tenant connections describe 2 patterns for multi tenancy: 1) shared connection pool using |
@vkarpov15 The problem we have is we've got many services, each of which talks to many (and an increasing number of) tenants. And our cluster only allows so many connections, so we quickly run out. I think our long-term solution will be an architectural change, where all or most tenant data goes through one or a few services. But that's beyond the scope of this thread. I was just curious if there were alternatives to username/password that would work with |
Yeah, unfortunately there's no way to make One potential alternative is to cap the number of sockets per connection: the MongoDB Node driver allows up to 100 sockets per connection by default. You can reduce that with the |
That's what we're doing and it does help. It prevents us from hitting a connection limit. But now, each service/tenant has a small number of sockets per connection and it's noticeably slowing things down. And this doesn't scale well. So I think an architectural change is in order. |
Re: #2874,
useDb()
should have the option to provide auth credentials in case the currently logged in user doesn't have access to the new database.https://mongoosejs.com/docs/api/connection.html#connection_Connection-useDb
The text was updated successfully, but these errors were encountered: