I've been evaluating Mongoose (an ORM for node.js which uses MongoDB for persistent storage).
What I'd like to do is make sure that the app can run when the DB is not up when the app starts, and also handles the DB going down intelligently.
Currently my test app which does not work in either case does this:
var mongoose_connection = mongoose.createConnection(DATABASE_URL, {server:{poolSize:4}});
Then I use that connection when making Models.
In the case when the DB is down at the start of the app then any save() calls on instances silently fail with no error. If the DB comes back up they never get written.
So I would need to detect that the connection never happened and have the app be able to tell that at runtime so that I can handle it somehow.
When the DB goes down after the app has started though the save() calls still do not cause errors but they are queued and the written when the DB comes back.
That seems fine except I'd like to hook into the API to get events when the DB is down and to query how many save's are queued. At some point I might have so many queued events that I would want to just stop making new ones and have the app back off.
The connect() method provided by the Mongoose supports both JavaScript promises and async-await syntax.
connect() is use, whereas if there is multiple instance of connection mongoose. createConnection() is used. Hope someone can clarify more about this.
You can connect to MongoDB with the mongoose. connect() method. mongoose. connect('mongodb://localhost:27017/myapp');
Mongoose is similar to an ORM (Object-Relational Mapper) you would use with a relational database. Both ODMs and ORMs can make your life easier with built-in structure and methods. The structure of an ODM or ORM will contain business logic that helps you organize data.
Case #1: db is down on app startup. there is a minor bug preventing this use case that I'm fixing now. However, here is the work-around:
var db = mongoose.createConnection();
db.on('error', function (err) {
if (err) // couldn't connect
// hack the driver to allow re-opening after initial network error
db.db.close();
// retry if desired
connect();
});
function connect () {
db.open('localhost', 'dbname');
}
connect();
https://gist.github.com/2878607
An ugly but working gist. First shutdown mongo, then run this gist.
Notice the connection failures.
Then start mongo and see all of the queued up inserts complete and dumped to the console. Writes and finds will begin.
Shutdown mongo, notice that the inserts and finds are being attempted but no callbacks are executed.
Restart mongo. Notice all of the queued inserts and finds are completed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With