From c4fd4d3a1f5ac2866794b22f165ad3fd5965bbc1 Mon Sep 17 00:00:00 2001 From: Felix Mosheev <9304194+felixmosh@users.noreply.github.com> Date: Mon, 31 Oct 2022 23:49:00 +0200 Subject: [PATCH] fix: Handle a case when redshift returns value of TOTAL=0 --- __tests__/specialCases.spec.js | 45 ++++++++++++++++++++++++++++++++++ lib/index.js | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 __tests__/specialCases.spec.js diff --git a/__tests__/specialCases.spec.js b/__tests__/specialCases.spec.js new file mode 100644 index 0000000..c84b296 --- /dev/null +++ b/__tests__/specialCases.spec.js @@ -0,0 +1,45 @@ +const knex = require('knex'); +const { getTracker, MockClient } = require('knex-mock-client'); +const { attachPaginate } = require('../lib'); +attachPaginate(); + +describe('special cases', () => { + let db; + let tracker; + beforeAll(() => { + db = knex({ + client: MockClient, + dialect: 'mysql', + }); + + tracker = getTracker(); + }); + + it('should support TOTAL as uppercase', async () => { + const total = 10; + tracker.on.select('select count(*) as `total` from').responseOnce({ TOTAL: `${total}` }); + tracker.on.select('select * from `tableName`').responseOnce([]); + + const data = await db('tableName').paginate({ isFromStart: true, perPage: 10 }); + + expect(data).toMatchObject({ + pagination: { + total, + }, + }); + }); + + it('should support TOTAL as uppercase with 0 value', async () => { + const total = 0; + tracker.on.select('select count(*) as `total` from').responseOnce({ TOTAL: total }); + tracker.on.select('select * from `tableName`').responseOnce([]); + + const data = await db('tableName').paginate({ isFromStart: true, perPage: 10 }); + + expect(data).toMatchObject({ + pagination: { + total, + }, + }); + }); +}); diff --git a/lib/index.js b/lib/index.js index 2e835ad..ef1dfd8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,7 +52,7 @@ module.exports.attachPaginate = function attachPaginate() { if (shouldFetchTotals) { const countResult = await countQuery.transacting(trx); - const total = +(countResult.TOTAL || countResult.total); + const total = +(countResult.TOTAL || countResult.total || 0); pagination = { total,