From 4b9d98c6ce2ec4bf32ac9d0e1b6ef6589aac8d6b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 17 Jun 2014 09:07:55 -0700 Subject: [PATCH] Allows skip or offset See https://github.com/strongloop/loopback/issues/336 --- lib/connectors/memory.js | 6 +++--- lib/dao.js | 2 +- test/basic-querying.test.js | 15 +++++++++++++-- test/loopback-dl.test.js | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index ab54f959..b0c81ca1 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -306,9 +306,9 @@ Memory.prototype.all = function all(model, filter, callback) { } // limit/skip - filter.skip = filter.skip || 0; - filter.limit = filter.limit || nodes.length; - nodes = nodes.slice(filter.skip, filter.skip + filter.limit); + var skip = filter.skip || filter.offset || 0; + var limit = filter.limit || nodes.length; + nodes = nodes.slice(skip, skip + limit); } process.nextTick(function () { diff --git a/lib/dao.js b/lib/dao.js index d4bd40de..c68a114a 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -432,7 +432,7 @@ DataAccessObject._normalize = function (filter) { } filter.limit = limit; filter.offset = offset; - delete filter.skip; + filter.skip = offset; } // normalize fields as array of included property names diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index eaccb348..cfb51be8 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -72,15 +72,26 @@ describe('basic-querying', function () { }); }); - it('should query offset collection with limit', function (done) { - User.find({skip: 1, limit: 4}, function (err, users) { + it('should query collection with skip & limit', function (done) { + User.find({skip: 1, limit: 4, order: 'seq'}, function (err, users) { should.exists(users); should.not.exists(err); + users[0].seq.should.be.eql(1); users.should.have.lengthOf(4); 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) { User.find({where: {role: 'lead'}}, function (err, users) { should.exists(users); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index d1d2b0a8..7b5eb2ab 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -1198,12 +1198,12 @@ describe('DataAccessObject', function () { it('should normalize limit/offset/skip', function () { 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 () { 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 () {