backbone-db-mongodb
Advanced tools
Comparing version 0.2.3 to 0.2.6
69
index.js
@@ -22,22 +22,3 @@ var _ = require('underscore'), | ||
function toMongoDoc(doc) { | ||
if (doc && doc._id) { | ||
if (typeof doc._id === "string" && doc._id.length === "24") { | ||
debug('converting _id to MongoID'); | ||
doc._id = new ObjectId(doc._id); | ||
} | ||
} | ||
return doc; | ||
} | ||
function fromMongoDoc(doc) { | ||
if (doc && doc._id) { | ||
if (typeof doc._id === "object" && doc._id.toString && doc._id.toString().length === "24") { | ||
debug('converting _id to MongoID'); | ||
doc._id = doc._id.toString(); | ||
} | ||
} | ||
return doc; | ||
} | ||
MongoDB.sync = Db.sync; | ||
@@ -57,2 +38,16 @@ _.extend(MongoDB.prototype, Db.prototype, { | ||
_getId: function(model) { | ||
var id; | ||
if(model && model.get) { | ||
id = model.get('_id'); | ||
} | ||
if(!id) { | ||
id = model.get(model.idAttribute); | ||
} | ||
if(typeof id === "string" && id.length === 24) { | ||
id = new ObjectId(id); | ||
} | ||
return id; | ||
}, | ||
findAll: function (model, options, callback) { | ||
@@ -75,3 +70,3 @@ options = options || {}; | ||
} | ||
debug('findAll', query, 'limit:', limit, 'offset:', offset, 'sort:', sort); | ||
debug('findAll %s: limit: %s, offset: %s, sort: %s', JSON.stringify(query), limit, offset, JSON.stringify(sort)); | ||
this._getCollection(model, options, function (err, collection) { | ||
@@ -85,5 +80,2 @@ if (err) return callback(err); | ||
.toArray(function (err, res) { | ||
if (res && res.length > 0) { | ||
res = _.map(res, fromMongoDoc); | ||
} | ||
callback(err, res); | ||
@@ -97,9 +89,9 @@ }); | ||
var query = options.where || { | ||
_id: model.get(model.idAttribute) | ||
_id: this._getId(model) | ||
}; | ||
debug('find', query); | ||
debug('find %s', JSON.stringify(query)); | ||
this._getCollection(model, options, function (err, col) { | ||
if (err) return callback(err); | ||
col.findOne(query, function (err, res) { | ||
return callback(err, fromMongoDoc(res)); | ||
return callback(err, res); | ||
}); | ||
@@ -127,4 +119,4 @@ }); | ||
createIdFn(function (err, id) { | ||
model.set(model.idAttribute, id.toString()); | ||
model.set('_id', id.toString()); | ||
model.set(model.idAttribute, id); | ||
model.set('_id', id); | ||
callback(err); | ||
@@ -144,5 +136,2 @@ }); | ||
} | ||
if (!model.has('_id') && model.has(model.idAttribute)) { | ||
model.set('_id', model.get(model.idAttribute)); | ||
} | ||
if (options.inc) { | ||
@@ -153,3 +142,6 @@ return this.inc(model, options, callback); | ||
if (err) return callback(err); | ||
collection.save(toMongoDoc(model.toJSON()), callback); | ||
model.set('_id', self._getId(model)); | ||
collection.save(model.toJSON(), function(err, res) { | ||
callback(err, model.toJSON()); | ||
}); | ||
}); | ||
@@ -160,3 +152,3 @@ }, | ||
var self = this; | ||
debug("destroy: " + model.get(model.idAttribute)); | ||
debug("destroy : " + model.get(model.idAttribute)); | ||
if (model.isNew()) { | ||
@@ -168,5 +160,4 @@ return false; | ||
if (err) return callback(err); | ||
var data = toMongoDoc(model.toJSON()); | ||
collection.remove({ | ||
_id: data._id | ||
_id: self._getId(model) | ||
}, function (err, res) { | ||
@@ -182,2 +173,3 @@ callback(err, res || options.ignoreFailures ? 1 : res); | ||
} | ||
var self = this; | ||
var attribute = options.inc.attribute; | ||
@@ -190,5 +182,4 @@ var amount = options.inc.amount || 1; | ||
if (err) return callback(err); | ||
var data = toMongoDoc(model.toJSON()); | ||
col.update({ | ||
_id: data._id | ||
_id: self._getId(model) | ||
}, { | ||
@@ -199,3 +190,5 @@ $inc: inc | ||
}, | ||
callback | ||
function (err, res) { | ||
callback(err, res || options.ignoreFailures ? 1 : res); | ||
} | ||
); | ||
@@ -202,0 +195,0 @@ }); |
{ | ||
"name": "backbone-db-mongodb", | ||
"version": "0.2.3", | ||
"version": "0.2.6", | ||
"description": "MongoDB driver for Backbone.Db", | ||
@@ -21,4 +21,4 @@ "main": "index.js", | ||
"backbone": "~1.1.0", | ||
"backbone-db": "~0.4.0", | ||
"backbone-promises": "~0.2.0", | ||
"backbone-db": "~0.4", | ||
"backbone-promises": "~0.2", | ||
"debug": "~0.7.2", | ||
@@ -25,0 +25,0 @@ "mongodb": "~1.3.19" |
@@ -7,3 +7,3 @@ var assert = require('assert'); | ||
describe('backbone-db-mongodb', function () { | ||
describe('backbone-db tests', function () { | ||
before(function (next) { | ||
@@ -27,2 +27,2 @@ var self = this; | ||
}); | ||
}); |
@@ -12,3 +12,5 @@ var assert = require('assert'); | ||
before(function (done) { | ||
setup.setupDb(done); | ||
setup.setupDb(function() { | ||
done(); | ||
}); | ||
}); | ||
@@ -25,10 +27,8 @@ | ||
'id_check': 1 | ||
}, { | ||
wait: true | ||
}) | ||
.then(function (m) { | ||
.done(function (m) { | ||
model = m; | ||
assert(m.get('id_check') === collection.at(0).get('id_check')); | ||
model = m; | ||
done(); | ||
}).otherwise(done); | ||
}, done); | ||
}); | ||
@@ -38,3 +38,3 @@ | ||
var m2 = new this.Model({ | ||
id: model.id | ||
id: model.get(model.idAttribute) | ||
}); | ||
@@ -62,5 +62,5 @@ m2.fetch().then(function (m) { | ||
.destroy() | ||
.then(function () { | ||
.done(function () { | ||
done(); | ||
}).otherwise(done); | ||
}, done); | ||
}); | ||
@@ -67,0 +67,0 @@ |
@@ -25,5 +25,5 @@ var assert = require('assert'); | ||
}); | ||
m.save().then(function () { | ||
m.save().done(function () { | ||
done(); | ||
}); | ||
}, done); | ||
}); | ||
@@ -118,3 +118,20 @@ | ||
it('should fail inc operation gracefully with ignoreFailures options', function (done) { | ||
model = new this.Model({ | ||
id: 'foo' | ||
}); | ||
model | ||
.save(null, { | ||
inc: { | ||
attribute: 'counter', | ||
amount: 1 | ||
}, | ||
ignoreFailures: true | ||
}) | ||
.then(function (m) { | ||
done(); | ||
}).otherwise(done); | ||
}); | ||
}); | ||
}); |
25096
692
Updatedbackbone-db@~0.4
Updatedbackbone-promises@~0.2