Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function create using write concern #26

Merged
merged 4 commits into from
Apr 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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