UNPKG

@everytravel/shared

Version:

A comprehensive shared package for Everytravel containing Mongoose models and CRUD operations for hotel booking, user management, and transaction handling. Updated with improved model syntax and enhanced error handling.

35 lines (28 loc) 991 B
/** * Read operations for Mongoose models */ export const getMany = async (Model, filter = {}, options = {}) => { try { const { sort = {}, limit, skip, populate } = options; let query = Model.find(filter); if (Object.keys(sort).length > 0) { query = query.sort(sort); } if (limit) query = query.limit(limit); if (skip) query = query.skip(skip); if (populate) query = query.populate(populate); return await query.exec(); } catch (error) { throw new Error(`Error fetching documents: ${error.message}`); } }; export const getOne = async (Model, filter, options = {}) => { try { const { populate } = options; let query = Model.findOne(filter); if (populate) query = query.populate(populate); return await query.exec(); } catch (error) { throw new Error(`Error fetching document: ${error.message}`); } };