backbone-db-mongodb
Advanced tools
Comparing version 0.2.16 to 0.2.17
109
index.js
var _ = require('lodash'), | ||
Db = require('backbone-db'), | ||
debug = require('debug')('backbone-db-mongodb'), | ||
ObjectId = require('mongodb').BSONPure.ObjectID; | ||
ObjectId = require('mongodb').BSONPure.ObjectID, | ||
util = require('util'); | ||
@@ -23,7 +24,5 @@ function MongoDB(client) { | ||
if (_.isArray(sortProp)) { | ||
var sortOpts = _.extend.apply(null, | ||
[{}].concat(_.map(sortProp, function(prop) { | ||
return _convert(prop); | ||
})) | ||
); | ||
var sortOpts = _.extend.apply(null, [{}].concat(_.map(sortProp, function(prop) { | ||
return _convert(prop); | ||
}))); | ||
return sortOpts; | ||
@@ -38,3 +37,3 @@ } else { | ||
_.extend(MongoDB.prototype, Db.prototype, { | ||
_getCollection: function (model, options, callback) { | ||
_getCollection: function(model, options, callback) { | ||
if (options && options.mongo_collection) { | ||
@@ -53,3 +52,3 @@ this.client.collection(options.mongo_collection, callback); | ||
var self = this; | ||
if(Array.isArray(res)) { | ||
if (Array.isArray(res)) { | ||
return _.map(res, function(item) { | ||
@@ -62,4 +61,4 @@ return self._filter(item, model); | ||
if(res && res._id) { | ||
if(!res[idAttr]) { | ||
if (res && res._id) { | ||
if (!res[idAttr]) { | ||
res[idAttr] = res._id; | ||
@@ -75,12 +74,12 @@ } | ||
if(model && model.get) { | ||
if (model && model.get) { | ||
id = model.get(model.idAttribute); | ||
} | ||
if(!id) { | ||
if (!id) { | ||
id = model.get('_id'); | ||
} | ||
if(!id && model) { | ||
if (!id && model) { | ||
id = model.id || model._id; | ||
} | ||
if(typeof id === 'string' && id.length === 24) { | ||
if (typeof id === 'string' && id.length === 24) { | ||
try { | ||
@@ -95,3 +94,3 @@ id = new ObjectId(id); | ||
findAll: function (model, options, callback) { | ||
findAll: function(model, options, callback) { | ||
options = options || {}; | ||
@@ -105,3 +104,3 @@ | ||
_.each(objectKeys, function(attr) { | ||
if(indexedKeys.indexOf(attr) > -1) { | ||
if (indexedKeys.indexOf(attr) > -1) { | ||
searchAttrs[attr] = model.get(attr); | ||
@@ -137,3 +136,3 @@ } | ||
debug('findAll %s: limit: %s, offset: %s, sort: %s', JSON.stringify(query), limit, offset, JSON.stringify(sort)); | ||
this._getCollection(model, options, function (err, collection) { | ||
this._getCollection(model, options, function(err, collection) { | ||
if (err) return callback(err); | ||
@@ -144,22 +143,23 @@ var q = collection | ||
.limit(limit); | ||
if (sort) { | ||
q.sort(sort); | ||
if (sort) { | ||
q.sort(sort); | ||
} | ||
q.toArray(function(err, results) { | ||
if (!model.model) { | ||
if (!results || results.length === 0) { | ||
var errorMsg = util.format('%s (%s) not found (read)', model.type, model.id); | ||
err = err || new Db.errors.NotFoundError(errorMsg); | ||
} else { | ||
results = self._filter(results, model); | ||
} | ||
return callback(err, results && results.length && results[0]); | ||
} | ||
q.toArray(function (err, results) { | ||
if(!model.model) { | ||
if(!results || results.length === 0) { | ||
err = err || new Error('not found'); | ||
} else { | ||
results = self._filter(results, model); | ||
} | ||
return callback(err, results && results.length && results[0]); | ||
} | ||
if(err || !results) return callback(err, results); | ||
results = self._filter(results, model); | ||
callback(null, results); | ||
}); | ||
if (err || !results) return callback(err, results); | ||
results = self._filter(results, model); | ||
callback(null, results); | ||
}); | ||
}); | ||
}, | ||
find: function (model, options, callback) { | ||
find: function(model, options, callback) { | ||
options = options || {}; | ||
@@ -172,6 +172,6 @@ var self = this; | ||
debug('find %s', JSON.stringify(query)); | ||
this._getCollection(model, options, function (err, col) { | ||
this._getCollection(model, options, function(err, col) { | ||
if (err) return callback(err); | ||
col.findOne(query, function (err, res) { | ||
if(err) return callback(err); | ||
col.findOne(query, function(err, res) { | ||
if (err) return callback(err); | ||
res = self._filter(res, model); | ||
@@ -183,6 +183,6 @@ return callback(err, res); | ||
create: function (model, options, callback) { | ||
create: function(model, options, callback) { | ||
var self = this; | ||
if (model.isNew()) { | ||
this.createId(model, options, function (err) { | ||
this.createId(model, options, function(err) { | ||
debug('create: %s', model.id); | ||
@@ -197,6 +197,6 @@ if (err) callback(err); | ||
createId: function (model, options, callback) { | ||
createId: function(model, options, callback) { | ||
debug('createId'); | ||
var createIdFn = model.createId ? model.createId.bind(model) : this._createDefaultId; | ||
createIdFn(function (err, id) { | ||
createIdFn(function(err, id) { | ||
model.set(model.idAttribute, id); | ||
@@ -207,7 +207,7 @@ callback(err); | ||
_createDefaultId: function (callback) { | ||
_createDefaultId: function(callback) { | ||
callback(null, new ObjectId()); | ||
}, | ||
update: function (model, options, callback) { | ||
update: function(model, options, callback) { | ||
var self = this; | ||
@@ -221,3 +221,3 @@ if (model.isNew()) { | ||
debug('update: %s %s', model.type, model.id); | ||
this._getCollection(model, options, function (err, collection) { | ||
this._getCollection(model, options, function(err, collection) { | ||
if (err) return callback(err); | ||
@@ -227,3 +227,10 @@ var data = model.toJSON(options); | ||
delete data._id; | ||
collection.update({_id: id}, {'$set': data}, {upsert: true, multi: false}, function(err, res) { | ||
collection.update({ | ||
_id: id | ||
}, { | ||
'$set': data | ||
}, { | ||
upsert: true, | ||
multi: false | ||
}, function(err, res) { | ||
if (id && model.idAttribute === '_id') { | ||
@@ -237,3 +244,3 @@ data._id = id; | ||
destroy: function (model, options, callback) { | ||
destroy: function(model, options, callback) { | ||
var self = this; | ||
@@ -245,7 +252,7 @@ debug('destroy %s', model.get(model.idAttribute)); | ||
this._getCollection(model, options, function (err, collection) { | ||
this._getCollection(model, options, function(err, collection) { | ||
if (err) return callback(err); | ||
collection.remove({ | ||
_id: self._getId(model) | ||
}, function (err, res) { | ||
}, function(err, res) { | ||
callback(err, res || options.ignoreFailures ? 1 : res); | ||
@@ -256,3 +263,3 @@ }); | ||
inc: function (model, options, callback) { | ||
inc: function(model, options, callback) { | ||
if (!options || !options.inc || !options.inc.attribute) { | ||
@@ -267,3 +274,3 @@ throw new Error('inc settings must be defined'); | ||
debug('inc:' + JSON.stringify(inc)); | ||
this._getCollection(model, options, function (err, col) { | ||
this._getCollection(model, options, function(err, col) { | ||
if (err) return callback(err); | ||
@@ -277,3 +284,3 @@ col.update({ | ||
}, | ||
function (err, res) { | ||
function(err, res) { | ||
@@ -287,2 +294,2 @@ callback(err, res || options.ignoreFailures ? 1 : res); | ||
module.exports = Db.MongoDB = MongoDB; | ||
module.exports = Db.MongoDB = MongoDB; |
{ | ||
"name": "backbone-db-mongodb", | ||
"version": "0.2.16", | ||
"version": "0.2.17", | ||
"description": "MongoDB driver for Backbone.Db", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,8 +6,8 @@ var setup = require('./setup'); | ||
describe('MongoDB', function () { | ||
describe('MongoDB', function() { | ||
var id; | ||
before(function (next) { | ||
before(function(next) { | ||
var self = this; | ||
setup.setupDb(function () { | ||
setup.setupDb(function() { | ||
self.Model = this.Model; | ||
@@ -20,14 +20,14 @@ self.Collection = this.Collection; | ||
after(function (next) { | ||
after(function(next) { | ||
setup.clearDb(next); | ||
}); | ||
after(function (done) { | ||
after(function(done) { | ||
setup.clearDb(done); | ||
}); | ||
describe('#Model', function () { | ||
describe('#Model', function() { | ||
var model; | ||
it('should .save model with given id', function () { | ||
it('should .save model with given id', function() { | ||
var deferred = Promised.when.defer(); | ||
@@ -40,3 +40,3 @@ model = new this.Model({ | ||
model.db.createId(model, {}, function(err) { | ||
if(err) return deferred.reject(err); | ||
if (err) return deferred.reject(err); | ||
model.save().then(deferred.resolve); | ||
@@ -47,3 +47,3 @@ }); | ||
it('should fetch saved model', function () { | ||
it('should fetch saved model', function() { | ||
var m2 = new this.Model({ | ||
@@ -53,3 +53,3 @@ id: model.get(model.idAttribute) | ||
return m2.fetch() | ||
.then(function () { | ||
.then(function() { | ||
assert.equal(m2.get('asd'), 'das'); | ||
@@ -60,3 +60,3 @@ assert.equal(m2.get('counter'), 2); | ||
it('should .save model without id', function () { | ||
it('should .save model without id', function() { | ||
var m = new this.Model({ | ||
@@ -66,3 +66,3 @@ data: 'foo', | ||
}); | ||
return m.save().then(function (m) { | ||
return m.save().then(function(m) { | ||
id = m.get(m.idAttribute); | ||
@@ -72,3 +72,3 @@ }); | ||
it('should fetch saved model', function () { | ||
it('should fetch saved model', function() { | ||
model = new this.Model({ | ||
@@ -78,3 +78,3 @@ id: id | ||
return model.fetch() | ||
.then(function () { | ||
.then(function() { | ||
assert.equal(model.get('data'), 'foo'); | ||
@@ -85,3 +85,3 @@ assert.equal(model.get('counter'), 5); | ||
it('should update model', function () { | ||
it('should update model', function() { | ||
model.set('data', 'new'); | ||
@@ -91,3 +91,3 @@ return model.save(); | ||
it('should fetch updated model', function () { | ||
it('should fetch updated model', function() { | ||
model = new this.Model({ | ||
@@ -97,3 +97,3 @@ id: id | ||
return model.fetch() | ||
.then(function () { | ||
.then(function() { | ||
assert.equal(model.get('data'), 'new'); | ||
@@ -103,3 +103,3 @@ }); | ||
it('should inc model attribute', function () { | ||
it('should inc model attribute', function() { | ||
model = new this.Model({ | ||
@@ -117,3 +117,3 @@ id: id | ||
it('should check that attribute was increased', function () { | ||
it('should check that attribute was increased', function() { | ||
model = new this.Model({ | ||
@@ -124,3 +124,3 @@ id: id | ||
.fetch() | ||
.then(function () { | ||
.then(function() { | ||
assert.equal(model.get('counter'), 6); | ||
@@ -131,3 +131,3 @@ assert.equal(model.get('data'), 'new'); | ||
it('should fail inc operation gracefully with ignoreFailures options', function () { | ||
it('should fail inc operation gracefully with ignoreFailures options', function() { | ||
var m = new this.Model({ | ||
@@ -145,3 +145,3 @@ id: 'foo' | ||
}); | ||
it('should use model.idAttribute as _id but not add it to attributes', function() { | ||
@@ -153,3 +153,3 @@ assert.ok(model.get('_id') === undefined); | ||
var m = new this.Model({ | ||
id: ''+model.get(model.idAttribute) | ||
id: '' + model.get(model.idAttribute) | ||
}); | ||
@@ -156,0 +156,0 @@ return m.fetch(); |
27237
751