Skip to content

Commit

Permalink
Merge pull request #26 from victor0402/master
Browse files Browse the repository at this point in the history
function create using write concern
  • Loading branch information
1602 committed Apr 22, 2014
2 parents 1dafcd8 + 3d4c4ff commit 47f3433
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ To use it you need `[email protected]`.
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

Expand Down
29 changes: 23 additions & 6 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -118,16 +124,27 @@ 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;
}
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 Expand Up @@ -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") {
Expand All @@ -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);
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 47f3433

Please sign in to comment.