Comparing version 0.1.6 to 0.1.7
0.1.7 / 2012-05-08 | ||
================== | ||
* Added global and collection-level options. | ||
* Enabled safe mode by default. | ||
* Improved error handling in tests. | ||
* Fixed `update` callback with safe: false. | ||
0.1.6 / 2012-05-06 | ||
@@ -3,0 +11,0 @@ ================== |
@@ -28,2 +28,3 @@ | ||
this.col = this.driver.collection(name); | ||
this.options = {}; | ||
} | ||
@@ -50,2 +51,21 @@ | ||
/** | ||
* Opts utility. | ||
*/ | ||
Collection.prototype.opts = function (opts) { | ||
opts = opts || {}; | ||
for (var i in this.manager.options) { | ||
if (!(i in opts) && !(i in this.options)) { | ||
opts[i] = this.manager.options[i]; | ||
} | ||
} | ||
for (var i in this.options) { | ||
if (!(i in opts)) { | ||
opts[i] = this.options[i]; | ||
} | ||
} | ||
return util.options(opts); | ||
}; | ||
/** | ||
* Set up indexes. | ||
@@ -132,10 +152,17 @@ * | ||
// query | ||
debug('%s update "%j" with "%j"', this.name, search, update); | ||
this.col.update( | ||
search | ||
, update | ||
, util.options(opts) | ||
, promise.fulfill | ||
); | ||
debug('%s update %j with %j', this.name, search, update); | ||
opts = this.opts(opts); | ||
var callback = opts.safe ? promise.fulfill : function () { | ||
// node-mongodb-native will send err=undefined and call the fn | ||
// in the same tick if safe: false | ||
var args = arguments; | ||
args[0] = args[0] || null; | ||
process.nextTick(function () { | ||
promise.fulfill.apply(promise, args); | ||
}); | ||
}; | ||
this.col.update(search, update, opts, callback); | ||
return promise; | ||
@@ -185,3 +212,3 @@ }; | ||
debug('%s remove "%j" with "%j"', this.name, search, update); | ||
this.col.update(search, util.options(opts), promise.fulfill); | ||
this.col.update(search, this.opts(opts), promise.fulfill); | ||
@@ -234,3 +261,3 @@ return promise; | ||
debug('%s findAndModify "%j" with "%j"', this.name, query.query, query.update); | ||
this.col.findAndModify(query, util.options(opts), promise.fulfill); | ||
this.col.findAndModify(query, this.opts(opts), promise.fulfill); | ||
@@ -267,3 +294,3 @@ return promise; | ||
debug('%s insert "%j"', this.name, data); | ||
this.col.insert(data, util.options(opts), function (err, docs) { | ||
this.col.insert(data, this.opts(opts), function (err, docs) { | ||
process.nextTick(function () { | ||
@@ -318,3 +345,3 @@ promise.fulfill.call(promise, err, docs ? docs[0] : docs); | ||
debug('%s find "%j"', this.name, query); | ||
var cursor = this.col.find(query, util.options(opts)); | ||
var cursor = this.col.find(query, this.opts(opts)); | ||
@@ -383,3 +410,3 @@ if (null == opts.stream) { | ||
debug('findOne "%j"', search); | ||
this.col.findOne(search, util.options(opts), promise.fulfill); | ||
this.col.findOne(search, this.opts(opts), promise.fulfill); | ||
@@ -386,0 +413,0 @@ return promise; |
@@ -56,2 +56,3 @@ | ||
this.collections = {}; | ||
this.options = { safe: true }; | ||
@@ -58,0 +59,0 @@ if (fn) { |
{ | ||
"name": "monk" | ||
, "version": "0.1.6" | ||
, "version": "0.1.7" | ||
, "main": "lib/monk.js" | ||
@@ -5,0 +5,0 @@ , "dependencies": { |
@@ -28,2 +28,4 @@ | ||
- Builds on top of [mongoskin](http://github.com/guileen/node-mongoskin) | ||
- Allows to set global options or collection-level options for queries. (eg: | ||
`safe` is `true` by default for all queries) | ||
@@ -168,2 +170,12 @@ ## How to use | ||
### Global options | ||
```js | ||
var db = require('monk')('localhost/mydb') | ||
db.options.multi = true; // global multi-doc update | ||
db.get('users').options.multi = false; // collection-level | ||
``` | ||
Monk sets `safe` to `true` by default. | ||
### Query debugging | ||
@@ -170,0 +182,0 @@ |
@@ -100,5 +100,5 @@ | ||
users.insert({ c: 'd' }, function (err, doc) { | ||
if (err) return done(err); | ||
expect(err).to.be(null); | ||
users.findById(doc._id, function (err, doc) { | ||
if (err) return done(err); | ||
expect(err).to.be(null); | ||
expect(doc.c).to.be('d'); | ||
@@ -111,2 +111,44 @@ done(); | ||
describe('updating', function () { | ||
it('should update', function (done) { | ||
users.insert({ d: 'e' }, function (err, doc) { | ||
expect(err).to.be(null); | ||
var p = users.update({ _id: doc._id }, { $set: { d: 'f' } }); | ||
p.complete(function (err) { | ||
expect(err).to.be(null); | ||
users.findById(doc._id, function (err, doc) { | ||
expect(err).to.be(null); | ||
expect(doc.d).to.be('f'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('options', function () { | ||
it('should allow defaults', function (done) { | ||
db.options.multi = true; | ||
users.update({}, { $set: { f: 'g' } }, function (err, num) { | ||
expect(err).to.be(null); | ||
expect(num).to.be.a('number'); // only a number of multi=true | ||
users.options.safe = false; | ||
users.options.multi = false; | ||
users.update({}, { $set: { g: 'h' } }, function (err, num) { | ||
expect(err).to.be(null); | ||
expect(num).to.be(undefined); | ||
users.options.safe = true; | ||
users.options.multi = true; | ||
users.update({}, { $set: { g: 'h' } }, { safe: false, multi: false }, function (err, num) { | ||
expect(err).to.be(null); | ||
expect(num).to.be(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('promises', function () { | ||
@@ -174,3 +216,3 @@ var Promise = monk.Promise; | ||
col.insert({}, function (err) { | ||
if (err) return done(err); | ||
expect(err).to.be(null); | ||
var p = col.drop(done) | ||
@@ -185,3 +227,3 @@ expect(p).to.be.a(Promise); | ||
users.drop(function (err) { | ||
if (err) return done(err); | ||
expect(err).to.be(null); | ||
indexes.drop(done); | ||
@@ -188,0 +230,0 @@ }); |
Sorry, the diff of this file is not supported yet
28296
837
218