backbone-db-mongodb
Advanced tools
Comparing version 0.2.18 to 0.2.19
55
index.js
@@ -103,3 +103,4 @@ var _ = require('lodash'), | ||
if (!Object.keys(searchAttrs).length) { | ||
var err = new Error('Cannot fetch model with given attributes'); | ||
var errMsg = util.format('Cannot fetch model %s with given attributes %s', model.type, JSON.stringify(searchAttrs)); | ||
var err = new Error(errMsg); | ||
return callback(err); | ||
@@ -123,10 +124,42 @@ } | ||
var reverseResults = false; | ||
var sortProperty; | ||
if (sort && (options.after_id || options.before_id)) { | ||
var keys; | ||
if ((options.after_id || options.before_id) && (keys = _.keys(sort)).length > 1) { | ||
return callback(new Error("before_id and after_id can only be used with single sort param")); | ||
} | ||
if (sort.id) { | ||
sortProperty = 'id'; | ||
} else { | ||
sortProperty = keys[0]; | ||
} | ||
} | ||
if (options.after_id) { | ||
query._id = { | ||
$gt: options.after_id | ||
}; | ||
if (sort && sort[sortProperty] === -1) { | ||
query._id = { | ||
$lt: options.after_id | ||
}; | ||
} else { | ||
query._id = { | ||
$gt: options.after_id | ||
}; | ||
} | ||
} else if (options.before_id) { | ||
query._id = { | ||
$lt: options.before_id | ||
}; | ||
if (sort && sort[sortProperty] === -1) { | ||
query._id = { | ||
$gt: options.before_id | ||
}; | ||
sort[sortProperty] = 1; | ||
reverseResults = true; | ||
} else { | ||
query._id = { | ||
$lt: options.before_id | ||
}; | ||
sort[sortProperty] = -1; | ||
reverseResults = true; | ||
} | ||
} | ||
@@ -145,2 +178,3 @@ if (query.id) { | ||
); | ||
this._getCollection(model, options, function(err, collection) { | ||
@@ -166,2 +200,7 @@ if (err) return callback(err); | ||
if (err || !results) return callback(err, results); | ||
if (reverseResults) { | ||
results = results.reverse(); | ||
} | ||
results = self._filter(results, model); | ||
@@ -292,2 +331,2 @@ callback(null, results); | ||
module.exports = Db.MongoDB = MongoDB; | ||
module.exports = Db.MongoDB = MongoDB; |
{ | ||
"name": "backbone-db-mongodb", | ||
"version": "0.2.18", | ||
"version": "0.2.19", | ||
"description": "MongoDB driver for Backbone.Db", | ||
@@ -20,3 +20,3 @@ "main": "index.js", | ||
"backbone": "~1.1.0", | ||
"backbone-db": "~0.4", | ||
"backbone-db": "~0.5", | ||
"backbone-promises": "~0.2", | ||
@@ -23,0 +23,0 @@ "debug": "~0.8", |
@@ -52,15 +52,19 @@ var _ = require('lodash'); | ||
value: 1, | ||
name: 'a' | ||
name: 'a', | ||
created_at: 10 | ||
}, { | ||
id: 2, | ||
value: 2, | ||
name: 'b' | ||
name: 'b', | ||
created_at: 20 | ||
}, { | ||
id: 3, | ||
value: 3, | ||
name: 'c' | ||
name: 'c', | ||
created_at: 25 | ||
}, { | ||
id: 4, | ||
value: 2, | ||
name: 'c' | ||
name: 'c', | ||
created_at: 35 | ||
}, ]; | ||
@@ -67,0 +71,0 @@ |
@@ -147,7 +147,74 @@ var setup = require('./setup'); | ||
it('should page through models with before_id', function() { | ||
var opts = { | ||
sort: 'id' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 1); | ||
assert(collection.at(1).id === 2); | ||
assert(collection.at(2).id === 3); | ||
assert(collection.at(3).id === 4); | ||
return collection.fetch({ | ||
sort: 'id', | ||
limit: 2, | ||
before_id: 4 | ||
}).then(function() { | ||
assert(collection.at(0).id === 2); | ||
assert(collection.at(1).id === 3); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with before_id and desc sort', function() { | ||
var opts = { | ||
sort: '-id' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: '-id', | ||
limit: 2, | ||
before_id: 1 | ||
}).then(function() { | ||
assert(collection.at(0).id === 3); | ||
assert(collection.at(1).id === 2); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with before_id (2) and desc sort and limit (1)', function() { | ||
var opts = { | ||
sort: '-id' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: '-id', | ||
limit: 1, | ||
before_id: 2 | ||
}).then(function() { | ||
assert.equal(collection.length, 1); | ||
assert(collection.at(0).id === 3); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with after_id', function() { | ||
var opts = { | ||
sort: 'value', | ||
limit: 2, | ||
after_id: testId | ||
sort: 'id' | ||
}; | ||
@@ -157,12 +224,21 @@ return collection | ||
.then(function() { | ||
var values = collection.pluck('value'); | ||
assert(values[0] === 2); | ||
assert(values[1] === 3); | ||
assert(collection.at(0).id === 1); | ||
assert(collection.at(1).id === 2); | ||
assert(collection.at(2).id === 3); | ||
assert(collection.at(3).id === 4); | ||
return collection.fetch({ | ||
sort: 'id', | ||
limit: 2, | ||
after_id: 2 | ||
}).then(function() { | ||
assert(collection.at(0).id === 3); | ||
assert(collection.at(1).id === 4); | ||
}); | ||
}); | ||
}); | ||
it('should fetch collections first page sorted descending', function() { | ||
it('should page through models with after_id & limit (1)', function() { | ||
var opts = { | ||
sort: '-value', | ||
limit: 2 | ||
sort: 'id' | ||
}; | ||
@@ -172,14 +248,175 @@ return collection | ||
.then(function() { | ||
var values = collection.pluck('value'); | ||
assert(values[0] === 3); | ||
assert(values[1] === 2); | ||
testId = collection.at(0).id; | ||
assert(collection.at(0).id === 1); | ||
assert(collection.at(1).id === 2); | ||
assert(collection.at(2).id === 3); | ||
assert(collection.at(3).id === 4); | ||
return collection.fetch({ | ||
sort: 'id', | ||
limit: 1, | ||
after_id: 2 | ||
}).then(function() { | ||
assert(collection.at(0).id === 3); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with before_id', function() { | ||
it('should page through models with after_id and desc sort', function() { | ||
var opts = { | ||
sort: '-id' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: '-id', | ||
limit: 2, | ||
after_id: 3 | ||
}).then(function() { | ||
assert.equal(collection.length, 2); | ||
assert(collection.at(0).id === 2); | ||
assert(collection.at(1).id === 1); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with after_id (3) and desc sort & limit (1)', function() { | ||
var opts = { | ||
sort: '-id' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: '-id', | ||
limit: 1, | ||
after_id: 3 | ||
}).then(function() { | ||
assert.equal(collection.length, 1); | ||
assert(collection.at(0).id === 2); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with before_id created_at sort', function() { | ||
var opts = { | ||
sort: 'created_at' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 1); | ||
assert(collection.at(1).id === 2); | ||
assert(collection.at(2).id === 3); | ||
assert(collection.at(3).id === 4); | ||
return collection.fetch({ | ||
sort: opts.sort, | ||
limit: 2, | ||
before_id: 4 | ||
}).then(function() { | ||
assert(collection.at(0).id === 2); | ||
assert(collection.at(1).id === 3); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with before_id and -created_at sort', function() { | ||
var opts = { | ||
sort: '-created_at' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: opts.sort, | ||
limit: 2, | ||
before_id: 1 | ||
}).then(function() { | ||
assert.equal(collection.length, 2); | ||
assert(collection.at(0).id === 3); | ||
assert(collection.at(1).id === 2); | ||
}); | ||
}); | ||
}); | ||
it('should fetch models with before_id and -created_at sort & limit (1)', function() { | ||
var opts = { | ||
sort: '-created_at', | ||
limit: 1, | ||
before_id: 1 | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert.equal(collection.length, 1); | ||
assert(collection.at(0).id === 2); | ||
}); | ||
}); | ||
it('should page through models with after_id created_at sort', function() { | ||
var opts = { | ||
sort: 'created_at' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 1); | ||
assert(collection.at(1).id === 2); | ||
assert(collection.at(2).id === 3); | ||
assert(collection.at(3).id === 4); | ||
return collection.fetch({ | ||
sort: opts.sort, | ||
after_id: 2 | ||
}).then(function() { | ||
assert.equal(collection.length, 2); | ||
assert(collection.at(0).id === 3); | ||
assert(collection.at(1).id === 4); | ||
}); | ||
}); | ||
}); | ||
it('should page through models with after_id and -created_at sort', function() { | ||
var opts = { | ||
sort: '-created_at' | ||
}; | ||
return collection | ||
.fetch(opts) | ||
.then(function() { | ||
assert(collection.at(0).id === 4); | ||
assert(collection.at(1).id === 3); | ||
assert(collection.at(2).id === 2); | ||
assert(collection.at(3).id === 1); | ||
return collection.fetch({ | ||
sort: opts.sort, | ||
limit: 2, | ||
after_id: 3 | ||
}).then(function() { | ||
assert(collection.at(0).id === 2); | ||
assert(collection.at(1).id === 1); | ||
}); | ||
}); | ||
}); | ||
it('should fetch collections first page sorted descending', function() { | ||
var opts = { | ||
sort: '-value', | ||
limit: 2, | ||
before_id: testId | ||
limit: 2 | ||
}; | ||
@@ -190,4 +427,5 @@ return collection | ||
var values = collection.pluck('value'); | ||
assert(values[0] === 2); | ||
assert(values[1] === 1); | ||
assert(values[0] === 3); | ||
assert(values[1] === 2); | ||
testId = collection.at(0).id; | ||
}); | ||
@@ -194,0 +432,0 @@ }); |
35353
1029
+ Addedbackbone-db@0.5.7(transitive)
+ Addedpff@0.2.0(transitive)
- Removedbackbone-db@0.4.25(transitive)
- Removedjsonquery@0.1.8(transitive)
Updatedbackbone-db@~0.5