UNPKG

adonis-odm

Version:

A comprehensive MongoDB ODM for AdonisJS with Lucid-style API, type-safe relationships, embedded documents, and transaction support

191 lines (190 loc) 4.28 kB
/** * QueryUtilities - Utility methods for query building and processing * * This class contains utility methods for sorting, pagination, selection, * and other query building helpers. */ export class QueryUtilities { sortOptions = {}; limitValue; skipValue; selectFields; distinctField; groupByFields = []; havingConditions = {}; /** * Get current sort options */ getSortOptions() { return this.sortOptions; } /** * Get current limit value */ getLimitValue() { return this.limitValue; } /** * Get current skip value */ getSkipValue() { return this.skipValue; } /** * Get current select fields */ getSelectFields() { return this.selectFields; } /** * Get current distinct field */ getDistinctField() { return this.distinctField; } /** * Get current group by fields */ getGroupByFields() { return this.groupByFields; } /** * Get current having conditions */ getHavingConditions() { return this.havingConditions; } /** * Set sort options (used for cloning) */ setSortOptions(options) { this.sortOptions = { ...options }; } /** * Set limit value (used for cloning) */ setLimitValue(limit) { this.limitValue = limit; } /** * Set skip value (used for cloning) */ setSkipValue(skip) { this.skipValue = skip; } /** * Set select fields (used for cloning) */ setSelectFields(fields) { this.selectFields = fields ? { ...fields } : undefined; } /** * Set distinct field (used for cloning) */ setDistinctField(field) { this.distinctField = field; } /** * Set group by fields (used for cloning) */ setGroupByFields(fields) { this.groupByFields = [...fields]; } /** * Set having conditions (used for cloning) */ setHavingConditions(conditions) { this.havingConditions = { ...conditions }; } /** * Add a distinct field */ distinct(field) { this.distinctField = field; return this; } /** * Add group by fields */ groupBy(...fields) { this.groupByFields.push(...fields); return this; } /** * Add having condition */ having(field, operatorOrValue, value) { if (value === undefined) { this.havingConditions[field] = operatorOrValue; } else { this.havingConditions[field] = { [operatorOrValue]: value }; } return this; } /** * Add order by clause */ orderBy(field, direction = 'asc') { this.sortOptions[field] = direction === 'asc' ? 1 : -1; return this; } /** * Set limit */ limit(count) { this.limitValue = count; return this; } /** * Set skip */ skip(count) { this.skipValue = count; return this; } /** * Alias for skip */ offset(count) { return this.skip(count); } /** * Set pagination */ forPage(page, perPage) { this.skipValue = (page - 1) * perPage; this.limitValue = perPage; return this; } /** * Set select fields */ select(fields) { if (Array.isArray(fields)) { // Convert array to projection object this.selectFields = {}; fields.forEach((field) => { this.selectFields[field] = 1; }); } else { this.selectFields = { ...fields }; } return this; } /** * Clone the query utilities */ clone() { const cloned = new QueryUtilities(); cloned.setSortOptions(this.sortOptions); cloned.setLimitValue(this.limitValue); cloned.setSkipValue(this.skipValue); cloned.setSelectFields(this.selectFields); cloned.setDistinctField(this.distinctField); cloned.setGroupByFields(this.groupByFields); cloned.setHavingConditions(this.havingConditions); return cloned; } }