From 0459b2a8a657a80f991f9a7723acd0f624841ea3 Mon Sep 17 00:00:00 2001 From: "victor.zeroglosa" Date: Mon, 21 Apr 2014 03:51:06 -0300 Subject: [PATCH] fix #https://github.com/1602/compound/issues/619 --- lib/mongodb.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 685fb6e..1dc5f0e 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -49,6 +49,9 @@ exports.initialize = function initializeSchema(schema, callback) { s.safe = s.safe || false; + //write concern + s.w = s.w || 0; + schema.adapter = new MongoDB(s, schema, callback); schema.ObjectID = ObjectID; }; @@ -76,7 +79,7 @@ function MongoDB(s, schema, callback) { server = new mongodb.Server(s.host, s.port, {}); } - new mongodb.Db(s.database, server, { safe: s.safe }).open(function (err, client) { + new mongodb.Db(s.database, server, { safe: s.safe, w: s.w }).open(function (err, client) { if (err) throw err; if (s.username && s.password) { var t = this; @@ -118,7 +121,7 @@ MongoDB.prototype.collection = function (name) { return this.collections[name]; }; -MongoDB.prototype.create = function (model, data, callback) { +MongoDB.prototype.create = function (model, data, callback, options) { if (data.id === null) { delete data.id; } @@ -126,8 +129,18 @@ MongoDB.prototype.create = function (model, data, callback) { data._id = data.id; delete data.id; } - this.collection(model).insert(data, {}, function (err, m) { - callback(err, err ? null : m[0]._id); + if (!options) { + options = {}; + } + + //enable write concern if it's not specified + options.w = options.w || 1; + + this.collection(model).insert(data, options, function (err, m) { + if (err || options.w < 1) + callback(err, null); + else + callback(err, m[0]._id); }); };