UNPKG

als-mongo-list

Version:

A flexible, lightweight MongoDB query utility for Node.js applications. Simplifies database operations with intuitive filtering, pagination, sorting, and field selection. Ideal for REST API endpoints, providing a primary List class that abstracts complex

14 lines (13 loc) 604 B
async function query(search, { currentPage, pageSize }, { Model, population, selection }, sort) { try { const total = await Model.countDocuments(search) const skip = pageSize * currentPage const mongoQuery = Model.find(search).limit(pageSize).skip(skip) if (selection.size) mongoQuery.select(Array.from(selection)) if (population.size) mongoQuery.populate(Array.from(population)) if (sort) mongoQuery.sort(sort); const items = await mongoQuery.exec() return { total, items } } catch (error) { return { error } } } module.exports = query