@warlock.js/cascade
Version:
ORM for managing databases
409 lines • 12.2 kB
TypeScript
import type { GenericObject } from "@mongez/reinforcements";
import type { ChunkCallback, CursorPagination, CursorPaginationResults, Filter, PaginationListing } from "../model";
import { ModelEvents } from "../model/model-events";
import { GroupByPipeline } from "./GroupByPipeline";
import type { LookupPipelineOptions } from "./LookupPipeline";
import type { UnwindOptions } from "./UnwindPipeline";
import type { FilterOptions, FilterStructure } from "./filters/types";
import type { Pipeline } from "./pipeline";
import type { WhereOperator } from "./types";
export declare class Aggregate {
readonly collection: string;
/**
* Collection pipelines
*/
pipelines: (Pipeline | GenericObject)[];
/**
* Aggregate events
*/
static _events: ModelEvents;
/**
* Query manager
*/
query: import("../query").Query;
/**
* Constructor
*/
constructor(collection: string);
/**
* Get the events instance
*/
static events(): ModelEvents;
/**
* Sort by the given column
*/
sort(column: string, direction?: "asc" | "desc"): this;
/**
* @alias sort
*/
orderBy(column: string, direction?: "asc" | "desc"): this;
/**
* Order by descending
*/
sortByDesc(column: string): this;
/**
* Order by descending
*/
orderByDesc(column: string): this;
/**
* Sort by multiple columns
*/
sortBy(columns: Record<string, "desc" | "asc">): this;
/**
* Sort randomly
*/
random(limit?: number): this;
/**
* Order by latest created records
*/
latest(column?: string): this;
/**
* Order by oldest created records
*/
oldest(column?: string): this;
/**
* Group by aggregate
*/
groupBy(GroupByPipeline: GroupByPipeline): this;
groupBy(GroupByPipeline: GenericObject, groupByData?: GenericObject): this;
groupBy(groupByColumns: string[], groupByData?: GenericObject): this;
groupBy(groupBy_id: string | null): this;
groupBy(groupBy_id: string | null, groupByData: GenericObject): this;
/**
* Group by year
*/
groupByYear(column: string, groupByData?: GenericObject): this;
/**
* Group by month and year
*/
groupByMonthAndYear(column: string, groupByData?: GenericObject): this;
/**
* Group by month only
*/
groupByMonth(column: string, groupByData?: GenericObject): this;
/**
* Group by day, month and year
*/
groupByDate(column: string, groupByData?: GenericObject): this;
/**
* Group by week and year
*/
groupByWeek(column: string, groupByData?: GenericObject): this;
/**
* Group by day only
*/
groupByDayOfMonth(column: string, groupByData?: GenericObject): this;
/**
* Pluck only the given column
*/
pluck(column: string): Promise<any[]>;
/**
* Get average of the given column
*/
avg(column: string): Promise<any>;
/**
* {@alias} avg
*/
average(column: string): Promise<any>;
/**
* Sum values of the given column
*/
sum(column: string): Promise<any>;
/**
* Get minimum value of the given column
*/
min(column: string): Promise<any>;
/**
* Get maximum value of the given column
*/
max(column: string): Promise<any>;
/**
* Get distinct value for the given column using aggregation
*/
distinct<T = any>(column: string): Promise<T[]>;
/**
* {@alias} distinct
*/
unique<T = any>(column: string): Promise<T[]>;
/**
* Get distinct values that are not empty
*/
distinctHeavy<T = any>(column: string): Promise<T[]>;
/**
* {@alias} distinctHeavy
*/
uniqueHeavy<T = any>(column: string): Promise<T[]>;
/**
* Get values list of the given column
*/
values<T = any>(column: string): Promise<T>;
/**
* Limit the number of results
*/
limit(limit: number): this;
/**
* Skip the given number of results
*/
skip(skip: number): this;
/**
* Select the given columns
*/
select(...columns: string[]): this;
select(columns: string[] | Record<string, 0 | 1 | boolean>): this;
/**
* Deselect the given columns
*/
deselect(columns: string[]): this;
/**
* Unwind/Extract the given column
*/
unwind(column: string, options?: UnwindOptions): this;
/**
* Add where stage
*/
where(column: string, value: any): this;
where(column: string, operator: WhereOperator, value: any): this;
where(column: GenericObject): this;
/**
* Add comparison between two or more columns
*/
whereColumns(column1: string, operator: WhereOperator, ...otherColumns: string[]): this;
/**
* Or Where stage
*/
orWhere(...operations: GenericObject[]): this;
orWhere(...operations: [column: string, value: any][]): this;
/**
* Perform a text search
* Please note that this method will add the `match` stage to the beginning of the pipeline
* Also it will add `score` field to the result automatically
*
* @warning This method will not work if the collection is not indexed for text search
*/
textSearch(query: string, moreFilters?: GenericObject): this;
/**
* Where null
*/
whereNull(column: string): this;
/**
* Check if the given column array has the given value or it is empty
* Empty means either the array column does not exists or exists but empty
*
* @usecase for when to use this method is when you have lessons collection and you want to get all lessons that either does not have column `allowedStudents`
* or has an empty array of `allowedStudents` or the `allowedStudents` column has the given student id
*
* Passing third argument empty means we will check directly in the given array (not array of objects in this case)
*/
whereArrayHasOrEmpty(column: string, value: any, key?: string): this;
/**
* Check if the given column array does not have the given value or it is empty.
* Empty means either the array column does not exist or exists but is empty.
*
* @usecase This method is useful when you have a collection, such as `lessons`, and you want to retrieve all lessons that either column `excludedStudents` does not contain the specified student id,
* have an empty array for `excludedStudents`, or the `excludedStudents` does not exist.
*/
whereArrayNotHaveOrEmpty(column: string, value: any, key?: string): this;
/**
* Where not null
*/
whereNotNull(column: string): this;
/**
* Where like operator
*/
whereLike(column: string, value: string): this;
/**
* Where not like operator
*/
whereNotLike(column: string, value: string): this;
/**
* Where column starts with the given value
*/
whereStartsWith(column: string, value: string | number): this;
/**
* Where column not starts with the given value
*/
whereNotStartsWith(column: string, value: string | number): this;
/**
* Where column ends with the given value
*/
whereEndsWith(column: string, value: string | number): this;
/**
* Where column not ends with the given value
*/
whereNotEndsWith(column: string, value: string | number): this;
/**
* Where between operator
*/
whereBetween(column: string, value: [any, any]): this;
/**
* Where date between operator
*/
whereDateBetween(column: string, value: [Date, Date]): this;
/**
* Where date not between operator
*/
whereDateNotBetween(column: string, value: [Date, Date]): this;
/**
* Where not between operator
*/
whereNotBetween(column: string, value: [any, any]): this;
/**
* Where exists operator
*/
whereExists(column: string): this;
/**
* Where not exists operator
*/
whereNotExists(column: string): this;
/**
* Where size operator
*/
whereSize(column: string, size: number): this;
whereSize(column: string, operator: ">" | ">=" | "=" | "<" | "<=", size: number): this;
/**
* Add project pipeline
*
*/
project(data: Record<string, any>): this;
/**
* Where in operator
* If value is a string, it will be treated as a column name
*/
whereIn(column: string, values: string | any[]): this;
/**
* Where not in operator
* If value is a string, it will be treated as a column name
*/
whereNotIn(column: string, values: string | any[]): this;
/**
* // TODO: Make a proper implementation
* Where location near
*/
whereNear(column: string, value: [number, number], _distance: number): this;
/**
* // TODO: Make a proper implementation
* Get nearby location between the given min and max distance
*/
whereNearByIn(column: string, value: [number, number], _minDistance: number, _maxDistance: number): Promise<this>;
/**
* Lookup the given collection
*/
lookup(options: LookupPipelineOptions): this;
/**
* Add field to the pipeline
*/
addField(field: string, value: any): this;
/**
* Add fields to the pipeline
*/
addFields(fields: GenericObject): this;
/**
* Get new pipeline instance
*/
pipeline(...pipelines: Pipeline[]): this;
/**
* Unshift pipeline to the beginning of the pipelines
*/
unshiftPipelines(pipelines: Pipeline[]): this;
/**
* Add mongodb plain stage
*/
addPipeline(pipeline: any): this;
/**
* Add mongodb plain stages
*/
addPipelines(pipelines: any[]): this;
/**
* Get pipelines
*/
getPipelines(): (GenericObject | Pipeline)[];
/**
* Determine if record exists
*/
exists(): Promise<boolean>;
/**
* {@inheritdoc}
*/
toJSON(): any[];
/**
* Get only first result
*/
first(mapData?: (data: any) => any): Promise<any>;
/**
* Get last result
*/
last(filters?: Filter): Promise<any>;
/**
* Delete records
*/
delete(): Promise<number>;
/**
* Get the data
*/
get(mapData?: (data: any) => any): Promise<any[]>;
/**
* Chunk documents based on the given limit
*/
chunk<T = any>(limit: number, callback: ChunkCallback<T>, mapData?: (data: any) => any): Promise<void>;
/**
* Paginate records based on the given filter
*/
paginate<T = any>(page?: number, limit?: number, mapData?: (data: any) => T): Promise<PaginationListing<T>>;
/**
* Use cursor pagination-based for better performance
*/
cursorPaginate<T = any>(options: CursorPagination, mapData?: (data: any) => T): Promise<CursorPaginationResults<T>>;
/**
* Explain the query
*/
explain(): Promise<any>;
/**
* Update the given data
*/
update(data: any): Promise<any>;
/**
* Increment the given column
*/
increment(column: string | string[] | Record<string, number>, value?: number): Promise<any>;
/**
* Decrement the given column(s)
*/
decrement(column: string, value?: number): Promise<any>;
/**
* Multiply the given column(s)
*/
multiply(column: string | string[] | Record<string, number>, value: number): Promise<any>;
/**
* Divide the given column(s)
*/
divide(column: string | string[] | Record<string, number>, value: number): Promise<any>;
/**
* Unset the given columns
*/
unset(...columns: string[]): Promise<any>;
/**
* Execute the query
*/
execute(): Promise<any>;
/**
* Count the results
*/
count(): Promise<number>;
/**
* Parse pipelines
*/
parse(): any[];
/**
* Reset the pipeline
*/
reset(): this;
/**
* Clone the aggregate class
*/
clone(): this;
/**
* Apply filters to the query
*/
applyFilters(filters: FilterStructure, data?: Record<string, any>, options?: FilterOptions): this;
}
//# sourceMappingURL=aggregate.d.ts.map