0

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.

Jester
  • 399
  • 2
  • 3
  • 15
  • 4
    Unless I'm missing some part of the question, doesn't mongoose's connection pool handle asynchronous connections automatically? See the [connection](http://mongoosejs.com/docs/connections.html) docs. – Ash Feb 14 '16 at 14:41
  • 2
    As Ashley B said, Mongoose handle all these async thing. Still not satisfied, you can [mongoose-and-multiple-database-in-single-node-js-project](http://stackoverflow.com/questions/19474712/mongoose-and-multiple-database-in-single-node-js-project) – xdeepakv Feb 14 '16 at 14:58

1 Answers1

1

Write-up from my comment:

Mongoose handles async connections by default using connection pools. You can specify the pool size like so:

var options = {
  server: { poolSize: 10 } // default is '5'
}

mongoose.connect('mongodb://username:password@host:port/database', options);

A connection will be used from the connection pool and then released back to the pool once the transaction has been completed.

Ash
  • 6,483
  • 7
  • 29
  • 37
  • Cheers Ashley - that's pretty much what I needed to know! And the tips from that other page Deepak was very useful. With the changes made in the options object, working a lot more smoother now. – Jester Feb 16 '16 at 13:28
  • No problem, if you want to make it a bit cleaner (if you have no other options) you can also just add it to the mongo connection uri like so: `mongodb://username:password@host:port/database?poolSize=4` – Ash Feb 16 '16 at 17:03