Created
August 14, 2011 22:17
-
-
Save bluescreen303/1145382 to your computer and use it in GitHub Desktop.
node-mongodb-native double confirmation (untested! :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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