Have to improve on my current Express/Mongoose server, thinking I may need to use mongoose.createConnection
over mongoose.connect
, and perhaps maintaining an array of connections, and deleting the indexes as the connection dies - so I need some input from your collective minds.
The script is essentially built up like this:
// Constants
var MONGO_SERVER = "mongodb://user:pass@localhost/dbname";
// Connection Handlers
function checkConnection(cb) {
if (mongoose.Connection.STATES.connected !== mongoose.connection.readyState) connectMongo(cb());
else cb();
}
function connectMongo(cb) {
db = mongoose.connect(MONGO_SERVER,function(err){if (err) throw err; else if (typeof cb==="function") cb();});
}
// Setup connection
mongoose = require('mongoose');
var db = {};
connectMongo();
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
// Schemas...
var UsersSchema = new Schema({
display_name : String,
email : String
});
//...
var users = mongoose.model('Users',UsersSchema);
//...
function login(conn,obj,link_id) {
checkConnection(function() {
try {
//...
}
catch (ex) {
console.log(ex.message);
}
});
}
Now, this works, but seeing some performance issues and some queries appear to be hanging tells me I need to re-shape this type of execution, and maybe making var db = {}
to be an array, and using createConnection
pushed to the end of it. Don't need a direct answer, just a nudge in the right direction.
Thanks.