diff --git a/README.md b/README.md index 2ed495a..4a83e9a 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,15 @@ To use it you need `jugglingdb@0.2.x`. var schema = new Schema('mongodb'); ... ``` + You can also set some settings in your schema, as [write concern and journaling](http://docs.mongodb.org/manual/core/write-concern/): + ```javascript + var Schema = require('jugglingdb').Schema; + var schema = new Schema('mongodb', { + url: 'mongodb://localhost/myapp', + w: 1, + j: 1 + }); + ``` ## Running tests diff --git a/lib/mongodb.js b/lib/mongodb.js index 685fb6e..40d9991 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -49,6 +49,12 @@ exports.initialize = function initializeSchema(schema, callback) { s.safe = s.safe || false; + //write concern + s.w = s.w || 0; + + //journaling + s.j = s.j || false; + schema.adapter = new MongoDB(s, schema, callback); schema.ObjectID = ObjectID; }; @@ -76,7 +82,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, j: s.j }).open(function (err, client) { if (err) throw err; if (s.username && s.password) { var t = this; @@ -118,7 +124,7 @@ MongoDB.prototype.collection = function (name) { return this.collections[name]; }; -MongoDB.prototype.create = function (model, data, callback) { +MongoDB.prototype.create = function (model, data, callback, modelConstructor, options) { if (data.id === null) { delete data.id; } @@ -126,8 +132,19 @@ 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); + } }); }; @@ -161,7 +178,7 @@ MongoDB.prototype.find = function find(model, id, callback) { }; MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback) { - + // set data.id as a mongodb object if (data.id) { if (typeof data.id === "string") { @@ -178,7 +195,7 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback } this.collection(model).update({_id: data.id}, {$set: data}, {upsert: true, multi: false}, function(err, rowsAffected) { - callback(err, data); + callback(err, data); }); }; diff --git a/package.json b/package.json index f870e27..d46050f 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "make test" }, "dependencies": { - "mongodb": "=0.9.9" + "mongodb": ">=0.9.9" }, "devDependencies": { "jugglingdb": "https://github.com/1602/jugglingdb/archive/master.tar.gz",