Skip to content

Instantly share code, notes, and snippets.

@mostafa-hz
Last active May 27, 2023 18:57
Show Gist options
  • Save mostafa-hz/425b38bbd8e1770b86a04fdf6baf004c to your computer and use it in GitHub Desktop.
Save mostafa-hz/425b38bbd8e1770b86a04fdf6baf004c to your computer and use it in GitHub Desktop.
A function to generate keyset paginated queries for mongodb
function generatePaginationQuery(query, sort, nextKey) {
const sortField = sort == null ? null : sort[0];
function nextKeyFn(items) {
if (items.length === 0) {
return null;
}
const item = items[items.length - 1];
if (sortField == null) {
return { _id: item._id };
}
return { _id: item._id, [sortField]: item[sortField] };
}
if (nextKey == null) {
return { paginatedQuery: query, nextKeyFn };
}
let paginatedQuery = query;
if (sort == null) {
paginatedQuery._id = { $gt: nextKey._id };
return { paginatedQuery, nextKey };
}
const sortOperator = sort[1] === 1 ? "$gt" : "$lt";
const paginationQuery = [
{ [sortField]: { [sortOperator]: nextKey[sortField] } },
{
$and: [
{ [sortField]: nextKey[sortField] },
{ _id: { [sortOperator]: nextKey._id } }
]
}
];
if (paginatedQuery.$or == null) {
paginatedQuery.$or = paginationQuery;
} else {
paginatedQuery = { $and: [query, { $or: paginationQuery }] };
}
return { paginatedQuery, nextKeyFn };
}
@vijay122
Copy link

That works 👏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment