/* * MIT License * * Copyright (c) 2018-2020 Ardalan Amini * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import * as mongodb from "mongodb"; import OdinError from "../Error"; import { Event } from "./EventEmitter"; import Filter from "./Filter"; import Join from "./Join"; export declare type Callback = (error: OdinError, result: T) => void; export declare type Operator = "<" | "<=" | "=" | "<>" | ">=" | ">"; export declare type Order = "asc" | "desc"; export declare type Id = mongodb.ObjectId; export declare type FilterQuery = any> = (query: Filter) => Filter; export declare type JoinQuery = any> = (query: Join) => Join; export interface GroupQueryObject = any> { having: (field: string, operator: Operator | any, value?: any) => GroupQueryObject; } export declare type GroupQuery = any> = (query: GroupQueryObject) => void; export declare type Mapper = any> = (item: T, index: number, items: T[]) => any; export interface Iterator = any> { hasNext: () => Promise; next: () => Promise; [Symbol.asyncIterator]: () => AsyncIterator; } export default class DB = any> extends Filter { protected _connection: string; protected _query: mongodb.Collection; protected _collection: string; protected _pipeline: Array<{ [key: string]: any; }>; protected _mappers: Array>; get pipeline(): { [key: string]: any; }[]; constructor(_connection: string); protected _resetFilters(): this; /********************************** Event **********************************/ protected _emit(event: Event, data: T): boolean; /******************************** Collection *******************************/ static connection = any>(connection: string): DB; static collection = any>(collection: string): DB; collection(collection: string): this; /********************************** Extra **********************************/ aggregate(...objects: Record[] | Record[][]): this; /*********************************** Joins **********************************/ join(collection: string, query?: JoinQuery, as?: string): this; /********* Mapping, Ordering, Grouping, Limit, Offset & Pagination *********/ map(mapper: Mapper): this; orderBy(field: K, order?: Order): this; orderBy(field: string, order?: Order): this; orderBy(fields: { [field: string]: "asc" | "desc"; }): this; skip(offset: number): this; offset(offset: number): this; limit(limit: number): this; take(limit: number): this; paginate(page?: number, limit?: number): this; /********************************* Indexes *********************************/ indexes(): Promise; indexes(callback: Callback): void; index(fieldOrSpec: string | Record, options?: mongodb.IndexOptions): Promise; index(fieldOrSpec: string | Record, callback: Callback): void; index(fieldOrSpec: string | Record, options: mongodb.IndexOptions, callback: Callback): void; reIndex(): Promise; reIndex(callback: Callback): void; dropIndex(indexName: string, options?: mongodb.CommonOptions & { maxTimeMS?: number; }): Promise; dropIndex(indexName: string, options: mongodb.CommonOptions & { maxTimeMS?: number; }, callback: Callback): void; /*********************************** Read **********************************/ private _aggregate; private _filtersOnly; count(): Promise; count(callback: Callback): void; exists(): Promise; exists(callback: Callback): void; iterate(fields?: string[]): Iterator; get(fields?: Array): Promise; get(fields: Array, callback: Callback): void; get(callback: Callback): void; first(fields?: Array): Promise; first(fields: Array, callback: Callback): void; first(callback: Callback): void; value(field: K): Promise; value(field: string): Promise; value(field: K, callback: Callback): void; value(field: string, callback: Callback): void; pluck(field: K): Promise; pluck(field: string): Promise; pluck(field: K, callback: Callback): void; pluck(field: string, callback: Callback): void; max(field: K): Promise; max(field: string): Promise; max(field: K, callback: Callback): void; max(field: string, callback: Callback): void; min(field: K): Promise; min(field: string): Promise; min(field: K, callback: Callback): void; min(field: string, callback: Callback): void; avg(field: K): Promise; avg(field: string): Promise; avg(field: K, callback: Callback): void; avg(field: string, callback: Callback): void; /********************************** Inserts *********************************/ protected _insertMany(items: T[]): Promise; protected _insertMany(items: T[], callback: Callback): void; protected _insertOne(item: T): Promise; protected _insertOne(item: T, callback: Callback): void; insert(item: T | T[]): Promise; insert(item: T | T[], callback: Callback): void; insertGetId(item: T): Promise; insertGetId(item: T, callback: Callback): void; /********************************** Updates *********************************/ protected _update(update: Record, callback?: Callback, soft?: { type: "delete" | "restore"; field: string; value: Date; }): Promise; update(update: Partial): Promise; update(update: Partial, callback: Callback): void; increment(field: string, count?: number): Promise; increment(field: K, count?: number): Promise; increment(field: string, callback: Callback): void; increment(field: K, callback: Callback): void; increment(field: string, count: number, callback: Callback): void; decrement(field: string, count?: number): Promise; decrement(field: K, count?: number): Promise; decrement(field: string, callback: Callback): void; decrement(field: K, callback: Callback): void; decrement(field: string, count: number, callback: Callback): void; unset(fields: string[]): Promise; unset(fields: K[]): Promise; unset(fields: string[], callback: Callback): void; unset(fields: K[], callback: Callback): void; /********************************** Deletes *********************************/ delete(): Promise; delete(callback: Callback): void; }