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
18 lines (16 loc) • 853 B
JavaScript
const getPagination = require('als-pagination')
const mongoQuery = require('./mongo-query')
async function exec(query, search = {}, listObj) {
const { filterObj, sortObj, navigation } = listObj
let { currentPage = 0, pageSize = 10 } = query
currentPage = Number(currentPage); pageSize = Number(pageSize);
filterObj.getSearch(query, search)
const sort = sortObj.getSort(query)
const { total, items, error } = await mongoQuery(search, { currentPage, pageSize }, listObj, sort)
if (error) return { error }
const pagination = getPagination({ total, currentPage, pageSize }, { navigation })
const inputs = { filter:filterObj.inputs, sort: sortObj.input, pagination }
query.currentPage = currentPage; query.pageSize = pageSize;
return { query, items, error: null, total, inputs };
}
module.exports = exec