From 1456241f8ff4e589e8b7567fd305f92a3a98fbe4 Mon Sep 17 00:00:00 2001 From: virgilchiriac <17074330+virgilchiriac@users.noreply.github.com> Date: Thu, 11 Jan 2024 13:48:02 +0100 Subject: [PATCH] BC-6231 - fix feathers $limit=false (#4690) Co-authored-by: SevenWaysDP --- src/utils/feathers-mongoose/service.js | 11 ++++++++ test/services/school/index.test.js | 36 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/utils/feathers-mongoose/service.js b/src/utils/feathers-mongoose/service.js index 04522a0756f..f2c1a860394 100644 --- a/src/utils/feathers-mongoose/service.js +++ b/src/utils/feathers-mongoose/service.js @@ -462,6 +462,17 @@ class Service extends AdapterBase { filterQuery(params) { const options = this.getOptions(params); + + // $limit=false - should return all records with structure as with pagination (total, limit, skip, data) + const paginateNoLimit = { + default: undefined, + max: undefined, + }; + if (params.query && (params.query.$limit === 'false' || params.query.$limit === false)) { + options.paginate = paginateNoLimit; + params.query.$limit = undefined; + } + const { $select, $sort, $limit: _limit, $skip = 0, $populate, ...query } = params.query || {}; const $limit = getLimit(_limit, options.paginate); diff --git a/test/services/school/index.test.js b/test/services/school/index.test.js index 93c8bebea67..f1817289b53 100644 --- a/test/services/school/index.test.js +++ b/test/services/school/index.test.js @@ -575,6 +575,42 @@ describe('school service', () => { }); }); +describe('find schools', () => { + let app; + let server; + let schoolsService; + + before(async () => { + app = await appPromise(); + server = await app.listen(); + schoolsService = app.service('schools'); + }); + + after(async () => { + await server.close(); + }); + + afterEach(async () => { + await testObjects.cleanup(); + }); + + beforeEach('set data samples', async () => { + await testObjects.createTestSchool({}); + await testObjects.createTestSchool({}); + await testObjects.createTestSchool({}); + }); + + it('find with pagination and limit', async () => { + const result = await schoolsService.find({ query: { $limit: 2 } }); + expect(result.data.length).to.be.equal(2); + }); + + it('find should return all schools when $limit = false', async () => { + const result = await schoolsService.find({ query: { $limit: false } }); + expect(result.data.length).to.be.equal(result.total); + }); +}); + describe('years service', () => { let app; let server;