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 = [];