diff --git a/index.d.ts b/index.d.ts index 85e67b6c9fc..73994372297 100644 --- a/index.d.ts +++ b/index.d.ts @@ -99,8 +99,8 @@ declare module 'mongoose' { */ export function isValidObjectId(v: any): boolean; - export function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; - export function model, TQueryHelpers = any>( + export function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; + export function model, TQueryHelpers = {}>( name: string, schema?: Schema, collection?: string, @@ -244,7 +244,7 @@ declare module 'mongoose' { /** Defines or retrieves a model. */ model(name: string, schema?: Schema, collection?: string): Model; - model, TQueryHelpers = {}>( + model, TQueryHelpers = undefined>( name: string, schema?: Schema, collection?: string, @@ -366,7 +366,7 @@ declare module 'mongoose' { getIndexes(): any; } - class Document { + class Document { constructor(doc?: any); /** This documents _id. */ @@ -435,11 +435,11 @@ declare module 'mongoose' { db: Connection; /** Removes this document from the db. */ - delete(options?: QueryOptions): Query; + delete(options?: QueryOptions): QueryWithHelpers; delete(options?: QueryOptions, cb?: (err: CallbackError, res: any) => void): void; /** Removes this document from the db. */ - deleteOne(options?: QueryOptions): Query; + deleteOne(options?: QueryOptions): QueryWithHelpers; deleteOne(options?: QueryOptions, cb?: (err: CallbackError, res: any) => void): void; /** Takes a populated field and returns it to its unpopulated state. */ @@ -594,7 +594,7 @@ declare module 'mongoose' { export const Model: Model; // eslint-disable-next-line no-undef - interface Model extends NodeJS.EventEmitter { + interface Model extends NodeJS.EventEmitter { new(doc?: any): T; aggregate(pipeline?: any[]): Aggregate>; @@ -624,12 +624,12 @@ declare module 'mongoose' { collection: Collection; /** Creates a `count` query: counts the number of documents that match `filter`. */ - count(callback?: (err: any, count: number) => void): Query; - count(filter: FilterQuery, callback?: (err: any, count: number) => void): Query; + count(callback?: (err: any, count: number) => void): QueryWithHelpers; + count(filter: FilterQuery, callback?: (err: any, count: number) => void): QueryWithHelpers; /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */ - countDocuments(callback?: (err: any, count: number) => void): Query; - countDocuments(filter: FilterQuery, callback?: (err: any, count: number) => void): Query; + countDocuments(callback?: (err: any, count: number) => void): QueryWithHelpers; + countDocuments(filter: FilterQuery, callback?: (err: any, count: number) => void): QueryWithHelpers; /** Creates a new document or documents */ create(doc: T | DocumentDefinition): Promise; @@ -665,14 +665,14 @@ declare module 'mongoose' { * Behaves like `remove()`, but deletes all documents that match `conditions` * regardless of the `single` option. */ - deleteMany(filter?: FilterQuery, options?: QueryOptions, callback?: (err: CallbackError) => void): Query; + deleteMany(filter?: FilterQuery, options?: QueryOptions, callback?: (err: CallbackError) => void): QueryWithHelpers; /** * Deletes the first document that matches `conditions` from the collection. * Behaves like `remove()`, but deletes at most one document regardless of the * `single` option. */ - deleteOne(filter?: FilterQuery, options?: QueryOptions, callback?: (err: CallbackError) => void): Query; + deleteOne(filter?: FilterQuery, options?: QueryOptions, callback?: (err: CallbackError) => void): QueryWithHelpers; /** * Sends `createIndex` commands to mongo for each index declared in the schema. @@ -693,10 +693,10 @@ declare module 'mongoose' { * equivalent to `findOne({ _id: id })`. If you want to query by a document's * `_id`, use `findById()` instead of `findOne()`. */ - findById(id: any, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): Query; + findById(id: any, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): QueryWithHelpers; /** Finds one document. */ - findOne(filter?: FilterQuery, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): Query; + findOne(filter?: FilterQuery, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): QueryWithHelpers; /** * Shortcut for creating a new Document from existing raw data, pre-saved in the DB. @@ -766,7 +766,7 @@ declare module 'mongoose' { /** Adds a `$where` clause to this query */ // eslint-disable-next-line @typescript-eslint/ban-types - $where(argument: string | Function): Query, T, TQueryHelpers>; + $where(argument: string | Function): QueryWithHelpers, T, TQueryHelpers>; /** Registered discriminators for this model. */ discriminators: { [name: string]: Model } | undefined; @@ -778,10 +778,10 @@ declare module 'mongoose' { discriminator(name: string, schema: Schema, value?: string): Model; /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */ - distinct(field: string, filter?: FilterQuery, callback?: (err: any, count: number) => void): Query, T, TQueryHelpers>; + distinct(field: string, filter?: FilterQuery, callback?: (err: any, count: number) => void): QueryWithHelpers, T, TQueryHelpers>; /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */ - estimatedDocumentCount(options?: QueryOptions, callback?: (err: any, count: number) => void): Query; + estimatedDocumentCount(options?: QueryOptions, callback?: (err: any, count: number) => void): QueryWithHelpers; /** * Returns true if at least one document exists in the database that matches @@ -791,37 +791,37 @@ declare module 'mongoose' { exists(filter: FilterQuery, callback: (err: any, res: boolean) => void): void; /** Creates a `find` query: gets a list of documents that match `filter`. */ - find(callback?: (err: any, docs: T[]) => void): Query, T, TQueryHelpers> & TQueryHelpers; - find(filter: FilterQuery, callback?: (err: any, docs: T[]) => void): Query, T, TQueryHelpers> & TQueryHelpers; - find(filter: FilterQuery, projection?: any | null, options?: QueryOptions | null, callback?: (err: any, docs: T[]) => void): Query, T, TQueryHelpers> & TQueryHelpers; + find(callback?: (err: any, docs: T[]) => void): QueryWithHelpers, T, TQueryHelpers>; + find(filter: FilterQuery, callback?: (err: any, docs: T[]) => void): QueryWithHelpers, T, TQueryHelpers>; + find(filter: FilterQuery, projection?: any | null, options?: QueryOptions | null, callback?: (err: any, docs: T[]) => void): QueryWithHelpers, T, TQueryHelpers>; /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */ - findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */ - findByIdAndRemove(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findByIdAndRemove(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */ - findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject, res: any) => void): Query, T, TQueryHelpers>; - findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query; - findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject, res: any) => void): QueryWithHelpers, T, TQueryHelpers>; + findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers; + findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */ - findOneAndDelete(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findOneAndDelete(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */ - findOneAndRemove(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findOneAndRemove(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */ - findOneAndReplace(filter: FilterQuery, replacement: DocumentDefinition, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query; - findOneAndReplace(filter?: FilterQuery, replacement?: DocumentDefinition, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findOneAndReplace(filter: FilterQuery, replacement: DocumentDefinition, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers; + findOneAndReplace(filter?: FilterQuery, replacement?: DocumentDefinition, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */ - findOneAndUpdate(filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject, res: any) => void): Query, T, TQueryHelpers>; - findOneAndUpdate(filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query; - findOneAndUpdate(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query; + findOneAndUpdate(filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject, res: any) => void): QueryWithHelpers, T, TQueryHelpers>; + findOneAndUpdate(filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers; + findOneAndUpdate(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers; - geoSearch(filter?: FilterQuery, options?: GeoSearchOptions, callback?: (err: CallbackError, res: Array) => void): Query, T, TQueryHelpers>; + geoSearch(filter?: FilterQuery, options?: GeoSearchOptions, callback?: (err: CallbackError, res: Array) => void): QueryWithHelpers, T, TQueryHelpers>; /** Executes a mapReduce command. */ mapReduce( @@ -829,10 +829,10 @@ declare module 'mongoose' { callback?: (err: any, res: any) => void ): Promise; - remove(filter?: any, callback?: (err: CallbackError) => void): Query; + remove(filter?: any, callback?: (err: CallbackError) => void): QueryWithHelpers; /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */ - replaceOne(filter?: FilterQuery, replacement?: DocumentDefinition, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query; + replaceOne(filter?: FilterQuery, replacement?: DocumentDefinition, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers; /** Schema the model uses. */ schema: Schema; @@ -841,18 +841,18 @@ declare module 'mongoose' { * @deprecated use `updateOne` or `updateMany` instead. * Creates a `update` query: updates one or many documents that match `filter` with `update`, based on the `multi` option. */ - update(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query; + update(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers; /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */ - updateMany(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query; + updateMany(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers; /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */ - updateOne(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query; + updateOne(filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers; /** Creates a Query, applies the passed conditions, and returns the Query. */ - where(path: string, val?: any): Query, T, TQueryHelpers>; - where(obj: object): Query, T, TQueryHelpers>; - where(): Query, T, TQueryHelpers>; + where(path: string, val?: any): QueryWithHelpers, T, TQueryHelpers>; + where(obj: object): QueryWithHelpers, T, TQueryHelpers>; + where(): QueryWithHelpers, T, TQueryHelpers>; } interface QueryOptions { @@ -1052,7 +1052,7 @@ declare module 'mongoose' { type SchemaPreOptions = { document?: boolean, query?: boolean }; type SchemaPostOptions = { document?: boolean, query?: boolean }; - class Schema = Model, SchemaDefinitionType = undefined> extends events.EventEmitter { + class Schema = Model, SchemaDefinitionType = undefined> extends events.EventEmitter { /** * Create a new schema */ @@ -1827,7 +1827,9 @@ declare module 'mongoose' { type ReturnsNewDoc = { new: true } | { returnOriginal: false }; - class Query { + type QueryWithHelpers = Query & THelpers; + + class Query { _mongooseOptions: MongooseQueryOptions; /** Executes the query */