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.

This commit is contained in:
Tom Butcher 2026-07-04 16:43:50 +01:00
parent e23ac3f6a3
commit 710bfc6de6

View File

@ -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 // New function to list unique property values
export const listPropertyValues = async ({ model, property, filter = {}, search = '' }) => { export const listPropertyValues = async ({ model, property, filter = {}, search = '' }) => {
let aggregateCommand = []; let aggregateCommand = [];