Skip to content

Commit

Permalink
add pagniated find explicitly differently from find
Browse files Browse the repository at this point in the history
  • Loading branch information
birm committed Apr 24, 2024
1 parent 0e959c7 commit 7209034
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var HANDLERS = {
},
"monitorCheck": monitor.check,
"mongoFind": dataHandlers.General.find,
"mongoPaginatedFind": dataHandlers.General.paginatedFind,
"mongoFindWithRegex": dataHandlers.General.findWithRegex,
"mongoAdd": dataHandlers.General.add,
"mongoUpdate": dataHandlers.General.update,
Expand Down
10 changes: 10 additions & 0 deletions handlers/dataHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ General.find = function(db, collection) {
};
};

General.paginatedFind = function(db, collection) {
return function(req, res, next) {
var query = req.query;
mongoDB.paginatedFind(db, collection, query).then((x) => {
req.data = x;
next();
}).catch((e) => next(e));
};
};

General.count = function(db, collection) {
return function(req, res, next) {
var query = req.query;
Expand Down
44 changes: 40 additions & 4 deletions service/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class Mongo {
query = transformIdToObjectId(query);

const collection = getConnection(database).collection(collectionName);
let { _page = 0, _pageSize = 1000, ...filterQuery } = query;
const _skip = _page * _pageSize;
_pageSize = parseInt(_pageSize, 10);
const data = await collection.find(filterQuery).skip(_skip).limit(_pageSize).toArray();
const data = await collection.find(query).toArray();

/** allow caller method to toggle response transformation */
if (transform) {
Expand All @@ -45,6 +42,44 @@ class Mongo {
}
}

/**
* Runs the MongoDB find() method to fetch documents with pagination.
*
* @async
* @param {string} database Name of the database
* @param {string} collectionName Name of the collection to run operation on
* @param {document} query Specifies selection filter using query operators.
* To return all documents in a collection, omit this parameter or pass an empty document ({}).
* @param {boolean} [transform=false] check to transform the IDs to ObjectID in response
*
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/ Read MongoDB Reference}
*/
static async paginatedFind(database, collectionName, query, transform = true) {
try {
query = transformIdToObjectId(query);

const collection = getConnection(database).collection(collectionName);
let { _page = 0, _pageSize = 1000, ...filterQuery } = query;
const _skip = _page * _pageSize;
_pageSize = parseInt(_pageSize, 10);
const data = await collection.find(filterQuery).skip(_skip).limit(_pageSize).toArray();

/** allow caller method to toggle response transformation */
if (transform) {
data.forEach((x) => {
x["_id"] = {
$oid: x["_id"],
};
});
}

return data;
} catch (e) {
console.error(e);
throw e;
}
}

/**
* Runs the MongoDB count() method to count documents.
*
Expand Down Expand Up @@ -232,6 +267,7 @@ class Mongo {
module.exports = {
add: Mongo.add,
find: Mongo.find,
paginatedFind: Mongo.paginatedFind,
update: Mongo.update,
delete: Mongo.delete,
aggregate: Mongo.aggregate,
Expand Down

0 comments on commit 7209034

Please sign in to comment.