From e7f5417430a7eed8ebf06eb602ea58ada1101912 Mon Sep 17 00:00:00 2001 From: Birm Date: Mon, 21 Aug 2023 15:51:37 -0400 Subject: [PATCH] add findWithRegex --- caracal.js | 1 + handlers/dataHandlers.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/caracal.js b/caracal.js index 952fd08..f846e81 100644 --- a/caracal.js +++ b/caracal.js @@ -83,6 +83,7 @@ var HANDLERS = { "dataEnforce": auth.dataEnforce, "monitorCheck": monitor.check, "mongoFind": dataHandlers.General.find, + "mongoFindWithRegex": dataHandlers.General.findWithRegex, "mongoAdd": dataHandlers.General.add, "mongoUpdate": dataHandlers.General.update, "mongoDelete": dataHandlers.General.delete, diff --git a/handlers/dataHandlers.js b/handlers/dataHandlers.js index f0b2ce5..08e1fb4 100644 --- a/handlers/dataHandlers.js +++ b/handlers/dataHandlers.js @@ -15,6 +15,21 @@ General.find = function(db, collection) { }; }; +General.findWithRegex = function(db, collection) { + return function(req, res, next) { + var query = req.query; + for (let i in query) { + if (query.hasOwnProperty(i)) { + query[i] = new RegExp(query[i], 'i'); // case insensitive search + } + } + mongoDB.find(db, collection, query).then((x) => { + req.data = x; + next(); + }).catch((e) => next(e)); + }; +}; + General.get = function(db, collection) { return function(req, res, next) { var query = req.query;