Allows skip or offset

See https://github.com/strongloop/loopback/issues/336
This commit is contained in:
Raymond Feng 2014-06-17 09:07:55 -07:00
parent 0c13f8c23a
commit 4b9d98c6ce
4 changed files with 19 additions and 8 deletions

View File

@ -306,9 +306,9 @@ Memory.prototype.all = function all(model, filter, callback) {
} }
// limit/skip // limit/skip
filter.skip = filter.skip || 0; var skip = filter.skip || filter.offset || 0;
filter.limit = filter.limit || nodes.length; var limit = filter.limit || nodes.length;
nodes = nodes.slice(filter.skip, filter.skip + filter.limit); nodes = nodes.slice(skip, skip + limit);
} }
process.nextTick(function () { process.nextTick(function () {

View File

@ -432,7 +432,7 @@ DataAccessObject._normalize = function (filter) {
} }
filter.limit = limit; filter.limit = limit;
filter.offset = offset; filter.offset = offset;
delete filter.skip; filter.skip = offset;
} }
// normalize fields as array of included property names // normalize fields as array of included property names

View File

@ -72,15 +72,26 @@ describe('basic-querying', function () {
}); });
}); });
it('should query offset collection with limit', function (done) { it('should query collection with skip & limit', function (done) {
User.find({skip: 1, limit: 4}, function (err, users) { User.find({skip: 1, limit: 4, order: 'seq'}, function (err, users) {
should.exists(users); should.exists(users);
should.not.exists(err); should.not.exists(err);
users[0].seq.should.be.eql(1);
users.should.have.lengthOf(4); users.should.have.lengthOf(4);
done(); done();
}); });
}); });
it('should query collection with offset & limit', function (done) {
User.find({offset: 2, limit: 3}, function (err, users) {
should.exists(users);
should.not.exists(err);
users[0].seq.should.be.eql(2);
users.should.have.lengthOf(3);
done();
});
});
it('should query filtered collection', function (done) { it('should query filtered collection', function (done) {
User.find({where: {role: 'lead'}}, function (err, users) { User.find({where: {role: 'lead'}}, function (err, users) {
should.exists(users); should.exists(users);

View File

@ -1198,12 +1198,12 @@ describe('DataAccessObject', function () {
it('should normalize limit/offset/skip', function () { it('should normalize limit/offset/skip', function () {
filter = model._normalize({limit: '10', skip: 5}); filter = model._normalize({limit: '10', skip: 5});
assert.deepEqual(filter, {limit: 10, offset: 5}); assert.deepEqual(filter, {limit: 10, offset: 5, skip: 5});
}); });
it('should set the default value for limit', function () { it('should set the default value for limit', function () {
filter = model._normalize({skip: 5}); filter = model._normalize({skip: 5});
assert.deepEqual(filter, {limit: 100, offset: 5}); assert.deepEqual(filter, {limit: 100, offset: 5, skip: 5});
}); });
it('should skip GeoPoint', function () { it('should skip GeoPoint', function () {