From 710bfc6de63a6ea16ef6adda4db53dd40b0adcc0 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sat, 4 Jul 2026 16:43:50 +0100 Subject: [PATCH] Added searchObjects function to database module for enhanced querying capabilities, allowing searches by reference, ID, or text. Improved error handling and result formatting in the search process. --- src/database/database.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/database/database.js b/src/database/database.js index 7b81b71..292bef6 100644 --- a/src/database/database.js +++ b/src/database/database.js @@ -416,6 +416,34 @@ export const listObjects = async ({ } }; +export const searchObjects = async ({ model, search, populate = [] }) => { + try { + const isRefOrIdSearch = search.match(/^.{3}:/); + let query = null; + if (isRefOrIdSearch) { + const lookupValue = search.split(':')[1]; + if (lookupValue.length <= 12) { + query = model.find({ _reference: lookupValue }); + } else { + query = model.find({ _id: lookupValue }); + } + } else { + query = model.find({ $text: { $search: search } }); + } + if (populate) { + for (const pop of populate) { + query = query.populate(pop); + } + } + const queryResult = await query.lean(); + const result = queryResult.map((item) => expandObjectIds(item)); + return result; + } catch (error) { + logger.error('Object search error:', error); + return { error: error, code: 500 }; + } +}; + // New function to list unique property values export const listPropertyValues = async ({ model, property, filter = {}, search = '' }) => { let aggregateCommand = [];