Skip to content

Commit

Permalink
refactor: cleanup some code style
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Dec 23, 2024
1 parent 155c09c commit d1e4a79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
10 changes: 6 additions & 4 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ function fakeOracleColumnNameMapping(obj) {
return obj;
}

return Object.entries(obj).reduce((result, [key, value]) => {
result[key === 'total' && !isPagination(obj) ? key.toUpperCase() : snakecase(key)] = value;
return result;
}, {});
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => {
const newKey = key === 'total' && !isPagination(obj) ? key.toUpperCase() : snakecase(key);
return [newKey, value];
})
);
}

const db = knex({
Expand Down
46 changes: 21 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,51 @@ module.exports.attachPaginate = function attachPaginate() {
}

const shouldFetchTotals = isLengthAware || currentPage === 1 || isFromStart;
let pagination = {};
let countQuery = null;

const offset = isFromStart ? 0 : (currentPage - 1) * perPage;
const limit = isFromStart ? perPage * currentPage : perPage;

let pagination = {
perPage,
currentPage,
from: offset,
to: undefined, // will be assigned when we fetch the data
};

const postProcessResponse =
typeof this.client.config.postProcessResponse === 'function'
? this.client.config.postProcessResponse
: function (key) {
return key;
};

if (shouldFetchTotals) {
countQuery = new this.constructor(this.client)
.count('* as total')
.from(this.clone().offset(0).clearOrder().as('count__query__'))
.first()
.debug(this._debug);
}
: (key) => key;

const originalQuery = shouldFetchTotals ? this.clone() : null;

// This will paginate the data itself
this.offset(offset).limit(limit);

return this.client.transaction(async (trx) => {
const result = await this.transacting(trx);
const data = await this.transacting(trx);
pagination.to = offset + data.length;

if (shouldFetchTotals) {
const countResult = await countQuery.transacting(trx);
const countResult = await new this.constructor(this.client)
.count('* as total')
.from(originalQuery.clear('offset').clearOrder().as('count__query__'))
.first()
.transacting(trx)
.debug(this._debug);

const total = +(countResult.TOTAL || countResult.total || 0);
const lastPage = Math.ceil(total / perPage);
pagination = {
...pagination,
total,
lastPage,
prevPage: currentPage > 1 ? currentPage - 1 : null,
nextPage: currentPage < lastPage ? currentPage + 1 : null,
};
}

// Add pagination data to paginator object
pagination = postProcessResponse({
...pagination,
perPage,
currentPage,
from: offset,
to: offset + result.length,
});

return { data: result, pagination };
return { data, pagination: postProcessResponse(pagination) };
});
}

Expand Down

0 comments on commit d1e4a79

Please sign in to comment.