declare module "monk" { import { CollectionAggregationOptions, MongoCountPreferences, BulkWriteOpResultObject, FilterQuery, FindOneOptions, UpdateQuery, UpdateOneOptions, UpdateManyOptions, ReplaceOneOptions, CollectionInsertOneOptions, Cursor, FindOneAndDeleteOption, FindOneAndUpdateOption, FindOneAndReplaceOption, GeoHaystackSearchOptions, CollectionMapFunction, CollectionReduceFunction, MapReduceOptions, CommonOptions, DeleteWriteOpResultObject, ClientSession, CollStats, UpdateWriteOpResult, IndexOptions, CollectionBulkWriteOptions, BulkWriteOperation, MongoDistinctPreferences, CollectionCreateOptions, MongoClientOptions, } from "mongodb"; // Utils type SingleOrArray = T | Array; type WithID = { _id: IObjectID } & T; type Callback = (err: Error | null, data: T) => void; // Inputs type SingleMulti = { single?: boolean; multi?: boolean }; type CreateIndexInput = string | { [key in keyof T]?: 1 | -1 }; type CollectionInsertOneOptionsMonk = CollectionInsertOneOptions & { castIds: boolean; }; type DropIndexInput = CreateIndexInput & string[]; type DropIndexOptions = CommonOptions & { maxTimeMS?: number }; type FindOptions = FindOneOptions & { rawCursor?: boolean }; type RemoveOptions = CommonOptions & SingleMulti; type StatsOptions = { scale: number; session?: ClientSession }; // Returns type DropResult = "ns not found" | true; type DropIndexResult = { nIndexesWas: number; ok: 1 | 0 }; type DropIndexesResult = DropIndexResult & { msg?: string }; type FindRawResult = Cursor>; type FindResult = WithID[] & { readonly each: ( listener: ( record: T, cursor: { readonly close: () => void; readonly resume: () => void; readonly pause: () => void; } ) => any ) => any; }; type FindOneResult = WithID | null; type GeoHaystackSearchResult = T[]; type InsertResult = WithID; type IndexesResult = { [name: string]: [keyof T, 1 | -1][]; }; type UpdateResult = UpdateWriteOpResult["result"]; export type TMiddleware = ({ collection, monkInstance, }: { collection: ICollection; monkInstance: IMonkManager; }) => ( next: (args: Object, method: string) => Promise ) => (args: Object, method: string) => Promise; type CollectionOptions = { middlewares?: TMiddleware[]; }; export class IMonkManager { readonly _state: "closed" | "opening" | "open"; readonly on: (event: string, handler: (event: any) => any) => void; readonly addListener: (event: string, handler: (event: any) => any) => void; readonly once: (event: string, handler: (event: any) => any) => void; readonly removeListener: ( event: string, handler: (event: any) => any ) => void; readonly close: () => Promise; readonly listCollections: (query?: Object) => Promise>; get(name: string, options?: CollectionOptions): ICollection; create( name: string, creationOption?: CollectionCreateOptions, options?: CollectionOptions ): ICollection; readonly setDefaultCollectionOptions: ( collectionOptions?: CollectionOptions ) => void; readonly addMiddleware: (middleware: TMiddleware) => void; } type TQuery = string | Object; type TFields = string | Array; export class ICollection { readonly manager: IMonkManager; readonly name: string; options: Object; readonly middlewares: Array; aggregate( pipeline: Object[], options?: CollectionAggregationOptions ): Promise; aggregate( stages: Object[], options: CollectionAggregationOptions, callback: Callback ): void; bulkWrite( operations: BulkWriteOperation[], options?: CollectionBulkWriteOptions ): Promise; bulkWrite( operations: BulkWriteOperation[], options: CollectionBulkWriteOptions, callback: Callback ): void; /** * http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments */ count( query?: FilterQuery, options?: MongoCountPreferences ): Promise; count( query: FilterQuery, options: MongoCountPreferences, callback?: Callback ): void; createIndex( fields: CreateIndexInput, options?: IndexOptions ): Promise; createIndex( fields: CreateIndexInput, options: IndexOptions, callback: Callback ): void; distinct( field: K, query?: FilterQuery, options?: MongoDistinctPreferences ): Promise; distinct( field: K, query: FilterQuery, options: MongoDistinctPreferences, callback: Callback ): void; drop(): Promise; drop(callback: Callback): void; dropIndex( fields: DropIndexInput, options?: DropIndexOptions ): Promise; dropIndex( fields: DropIndexInput, options: DropIndexOptions, callback: Callback ): void; dropIndexes(): Promise; dropIndexes(callback?: Callback): void; // Raw find( query: FilterQuery, options: FindOptions & { rawCursor: true } ): Promise>; find( query: FilterQuery, options: FindOneOptions & { rawCursor: true }, callback: Callback> ): void; // Normal find( query?: FilterQuery, options?: FindOptions ): Promise>; find( query: FilterQuery, options: FindOneOptions, callback: Callback> ): void; findOne( query?: FilterQuery, options?: FindOneOptions ): Promise>; findOne( query: FilterQuery, options: FindOneOptions, callback: Callback> ): void; findOneAndDelete( query: FilterQuery, options?: FindOneAndDeleteOption ): Promise>; findOneAndDelete( query: FilterQuery, options: FindOneAndDeleteOption, callback: Callback> ): void; // Update findOneAndUpdate( query: FilterQuery, update: UpdateQuery | Partial, options?: FindOneAndUpdateOption & { replace?: false } ): Promise>; findOneAndUpdate( query: FilterQuery, update: UpdateQuery | Partial, options?: FindOneAndUpdateOption & { replace?: false }, callback?: Callback> ): void; // Replace findOneAndUpdate( query: FilterQuery, update: T, options?: FindOneAndReplaceOption & { replace: true } ): Promise>; findOneAndUpdate( query: FilterQuery, update: T, options: FindOneAndReplaceOption & { replace: true }, callback: Callback> ): void; geoHaystackSearch( x: number, y: number, options?: GeoHaystackSearchOptions ): Promise>; geoHaystackSearch( x: number, y: number, options: GeoHaystackSearchOptions, callback: Callback> ): void; /** @deprecated MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework. */ group( keys: any, condition: Object, initial: Object, reduce: Function, finalize: Function, command: Boolean, options?: Object ): Promise; group( keys: any, condition: Object, initial: Object, reduce: Function, finalize: Function, command: Boolean, options?: Object, callback?: Callback ): void; indexes(): Promise>; indexes(callback: Callback>): void; insert( data: SingleOrArray, options?: CollectionInsertOneOptionsMonk ): Promise>; insert( data: SingleOrArray, options: CollectionInsertOneOptionsMonk, callback: Callback> ): void; mapReduce( map: CollectionMapFunction, reduce: CollectionReduceFunction, options: MapReduceOptions ): Promise; mapReduce( map: CollectionMapFunction, reduce: CollectionReduceFunction, options: MapReduceOptions, callback: Callback ): void; remove( query?: FilterQuery, options?: RemoveOptions ): Promise; remove( query: FilterQuery, options: RemoveOptions, callback: Callback ): void; stats(options?: StatsOptions): Promise; stats(options: StatsOptions, callback: Callback): void; // single update( query: FilterQuery, update: UpdateQuery | Partial, options?: UpdateOneOptions & { single?: true, multi?: false, replace?: false} ): Promise; update( query: FilterQuery, update: UpdateQuery | Partial, options: UpdateOneOptions & { single?: true, multi?: false, replace?: false}, callback: Callback ): void; // multi update( query: FilterQuery, update: UpdateQuery | Partial, options?: UpdateManyOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false}) ): Promise; update( query: FilterQuery, update: UpdateQuery | Partial, options: UpdateOneOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false}), callback: Callback ): void; // replace update( query: FilterQuery, update: UpdateQuery | Partial, options?: ReplaceOneOptions & { single?: true, multi?: false, replace: true} ): Promise; update( query: FilterQuery, update: UpdateQuery | Partial, options: ReplaceOneOptions & { single?: true, multi?: false, replace: true}, callback: Callback ): void; } export interface IObjectID { readonly toHexString: () => string; readonly toString: () => string; } export function id(hexstring: string): IObjectID; // returns ObjectId export function id(obj: IObjectID): IObjectID; // returns ObjectId export function id(): IObjectID; // returns new generated ObjectId export function cast(obj?: Object | Array | any): any; export default function ( database: string | Array, options?: MongoClientOptions & { collectionOptions?: CollectionOptions; } ): Promise & IMonkManager; }