Skip to content

Instantly share code, notes, and snippets.

@bluescreen303
Created August 14, 2011 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluescreen303/1145382 to your computer and use it in GitHub Desktop.
Save bluescreen303/1145382 to your computer and use it in GitHub Desktop.
node-mongodb-native double confirmation (untested! :)
Db.prototype.checkoutRawConnection = function() {
var conn = this.serverConfig.checkoutWriter();
return conn.pool[conn.poolIndex++ % conn.pool.length];
};
Db.prototype.lastErrorOnConnection(connection, options, callback) {
if ('function' === typeof options) callback = options, options = {};
var command = DbCommand.createGetLastErrorCommand(options, this);
this.executeCommand(command, {writer: connection}, function(err, error) {
callback(err, error && error.documents);
});
};
// to perform a double-callbacked insert, check out a connection
var conn = db.checkoutRawConnection();
// perform the insert, but mandate it uses our connection
// little dirty, piggybacks on safetyoptions, works for update too
db.insert(docs, {safe:{writer: conn}}, function(err, objects) {
if(!err) {
// here we can call back to calling code because we succeeded
// mongo accepted the docs and other connections will see them
}
});
// now we place an additional callback for the insert
// that gets triggered later than the one above
db.lastErrorOnConnection(conn, {fsync: true, w:2}, function(err, errdocs) {
// here we can notify externals because
// we're certain now everything is safely persisted
// and at least on 2 machines.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment