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
28 lines (25 loc) • 1.01 kB
JavaScript
const exec = require('./exec/exec')
const Filter = require('./filter/filter')
const Sort = require('./sort/sort')
class List {
sortKeys = new Set(); searchKeys = new Set(); population = new Set(); selection = new Set();
constructor(Model) {
this.Model = Model;
this.tree = this.Model.schema.tree
this.navigation = true
}
addKeys(keys, set) {
keys.forEach(k => { this.tree[k] === undefined ? console.warn(`"${k}" not found in schema`) : set.add(k) });
return this
}
orderBy(...keys) { return this.addKeys(keys,this.sortKeys) }
searchIn(...keys) { return this.addKeys(keys,this.searchKeys) }
populate(...keys) { return this.addKeys(keys,this.population) }
select(...keys) { return this.addKeys(keys,this.selection) }
exec(query, search) {
if (!this.filterObj) this.filterObj = new Filter(this)
if(!this.sortObj) this.sortObj = new Sort(this)
return exec(query, search, this)
}
}
module.exports = List;