You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to restrict model access to one or more connections with the hope of limiting access based on user permissions. I have the code working, but when I remove the mongoose.connect, it slows everything down. The times are in the 20-50 seconds on my local desktop in development mode. However, If I add mongoose.connect, everything is under a second. Any help is appreciated.
db.js
"use strict";varmongoose=require('mongoose');varlogger=require('winston');varnconf=require('nconf');vardbUrl=nconf.get("database:acl:url");mongoose.connect(dbUrl,function(err){if(err){logger.error('Error connecting to DB from mongoose',err);throwerr;}logger.info('Mongoose is connected to '+dbUrl);});module.exports=mongoose;varhostReadOnly=nconf.get("database:read-only:host");vardatabaseReadOnly=nconf.get("database:read-only:database");varportReadOnly=nconf.get("database:read-only:port");varoptionsReadOnly=nconf.get("database:read-only:options");varreadOnlyConnection=mongoose.createConnection(hostReadOnly,databaseReadOnly,portReadOnly,optionsReadOnly);readOnlyConnection.on('error',function(err){if(err){logger.error('Error with Read-Only DB from mongoose',err);throwerr;}});readOnlyConnection.on('connected',function(err){if(err){logger.error('Error connecting with Read-Only DB from mongoose',err);throwerr;}logger.info('Mongoose is connected to Read-Only DB via '+hostReadOnly+':'+portReadOnly+'/'+databaseReadOnly);});varhostReadWrite=nconf.get("database:read-write:host");vardatabaseReadWrite=nconf.get("database:read-write:database");varportReadWrite=nconf.get("database:read-write:port");varoptionsReadWrite=nconf.get("database:read-write:options");varreadWriteConnection=mongoose.createConnection(hostReadWrite,databaseReadWrite,portReadWrite,optionsReadWrite);readWriteConnection.on('error',function(err){if(err){logger.error('Error with Read-Write DB from mongoose',err);throwerr;}});readWriteConnection.on('connected',function(err){if(err){logger.error('Error connecting with Read-Write DB from mongoose',err);throwerr;}logger.info('Mongoose is connected to Read-Write DB via '+hostReadWrite+':'+portReadWrite+'/'+databaseReadWrite);});varhostAdmin=nconf.get("database:admin:host");vardatabaseAdmin=nconf.get("database:admin:database");varportAdmin=nconf.get("database:admin:port");varoptionsAdmin=nconf.get("database:admin:options");varadminConnection=mongoose.createConnection(hostAdmin,databaseAdmin,portAdmin,optionsAdmin);adminConnection.on('error',function(err){if(err){logger.error('Error with Admin DB from mongoose',err);throwerr;}});adminConnection.on('connected',function(err){if(err){logger.error('Error connecting with Admin DB from mongoose',err);throwerr;}logger.info('Mongoose is connected to Admin DB via '+hostAdmin+':'+portAdmin+'/'+databaseAdmin);});module.exports.readOnlyConnection=readOnlyConnection;module.exports.readWriteConnection=readWriteConnection;module.exports.adminConnection=adminConnection;
@vkarpov15 I appreciate you tagging it with help and question, but I found the issue. Express in our code is using the default Mongoose connection to store session info, which was causing the problem. After creating its own connection, the times went back to normal. I hope people can benefit from this problem.
I am trying to restrict model access to one or more connections with the hope of limiting access based on user permissions. I have the code working, but when I remove the mongoose.connect, it slows everything down. The times are in the 20-50 seconds on my local desktop in development mode. However, If I add mongoose.connect, everything is under a second. Any help is appreciated.
db.js
permission.js
I access the model with the following code.
The text was updated successfully, but these errors were encountered: