how to create multiple mongoose connection and use the existing mongoose connection if already made #9858
-
My requirement is that- i want to create new mongo database connection for each subdomain as subdomain name is coming with request's header ---- i have done this as i pass that subdomain name into await mongoose.createConnection(mongodb://localhost/${subdomainName}), but with this approach if 500 request coming from a subdomain then 500 times mongoose connection is made--- i want only while first request mongoose connection should be created and while remaining 499 request that existing connection should be used -- because there is no meaning of creating new connection on each request if already have the same connection EXAMPLE STEP1: if there is a request coming from subdomain "today.mydomain.com" then it creates a new mongodb connection and db name as "today" --- THAT IS OK STEP 2: if next time any other request is coming from same domain then again connection is made but this time this time new mongoose connection should not be created ----- but existing connection of today db which was created in first request should be used But i want if there is already mongodb connection made for a subdomain (today.mydomain.com) and next time when any request coming from that subdomain at that time new mongoose connection should not be created --- the existing connection which is already created WHILE FIRST REQUEST FROM THAT SUB DOMAIN should be used IS THERE ANY WAY THAT MONGOOSE PROVIDE WHERE I CAN FIND THERE IS ALREADY CONNECTION IS MADE FOR THE DOMAIN IF IT WILL RETURN TRUE THEN I WOULD USE THAT CONNECTION WITHOUT CREATING NEW ELSE i would create a new connection I AM USING FOLLOWING CODE-- |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Why don't you just store a Map from db names to Mongoose connections? |
Beta Was this translation helpful? Give feedback.
Why don't you just store a Map from db names to Mongoose connections?