Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
victor.zeroglosa committed Apr 21, 2014
1 parent 1dafcd8 commit 0459b2a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -118,16 +121,26 @@ 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;
}
if (data.id) {
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);
});
};

Expand Down

0 comments on commit 0459b2a

Please sign in to comment.