diff --git a/test/types/middleware.test.ts b/test/types/middleware.test.ts index a0f3e53caa1..47fa0ed03d6 100644 --- a/test/types/middleware.test.ts +++ b/test/types/middleware.test.ts @@ -1,4 +1,4 @@ -import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction } from 'mongoose'; +import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult } from 'mongoose'; import { expectError, expectType, expectNotType, expectAssignable } from 'tsd'; const preMiddlewareFn: PreSaveMiddlewareFunction = function(next, opts) { @@ -109,7 +109,17 @@ schema.post>('countDocuments', function(count, next) { }); schema.post>('findOneAndDelete', function(res, next) { - expectType(res); + expectType | null>(res); + next(); +}); + +schema.post>('findOneAndUpdate', function(res, next) { + expectType | null>(res); + next(); +}); + +schema.post>('findOneAndReplace', function(res, next) { + expectType | null>(res); next(); }); diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 7865a5e6855..fe68d015f07 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -673,9 +673,6 @@ async function gh13705() { const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { lean: true }); expectType(findByIdAndDeleteRes); - const findByIdAndRemoveRes = await TestModel.findByIdAndRemove('0'.repeat(24), { lean: true }); - expectType(findByIdAndRemoveRes); - const findByIdAndUpdateRes = await TestModel.findByIdAndUpdate('0'.repeat(24), {}, { lean: true }); expectType(findByIdAndUpdateRes); @@ -709,6 +706,16 @@ async function gh13746() { expectType(findOneAndUpdateRes.lastErrorObject?.updatedExisting); expectType(findOneAndUpdateRes.lastErrorObject?.upserted); expectType(findOneAndUpdateRes.ok); + + const findOneAndDeleteRes = await TestModel.findOneAndDelete({ _id: '0'.repeat(24) }, { includeResultMetadata: true }); + expectType(findOneAndDeleteRes.lastErrorObject?.updatedExisting); + expectType(findOneAndDeleteRes.lastErrorObject?.upserted); + expectType(findOneAndDeleteRes.ok); + + const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { includeResultMetadata: true }); + expectType(findByIdAndDeleteRes.lastErrorObject?.updatedExisting); + expectType(findByIdAndDeleteRes.lastErrorObject?.upserted); + expectType(findByIdAndDeleteRes.ok); } function gh13904() { diff --git a/types/index.d.ts b/types/index.d.ts index 3f94524ba98..d8d9e0c629a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -336,6 +336,7 @@ declare module 'mongoose' { post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction): this; post(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction): this; // this = Query + post>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction | ModifyResult>>): this; post>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction>): this; post>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: PostMiddlewareFunction>): this; post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction>): this; diff --git a/types/middlewares.d.ts b/types/middlewares.d.ts index 43ca1974b81..9302b9b7d48 100644 --- a/types/middlewares.d.ts +++ b/types/middlewares.d.ts @@ -5,7 +5,9 @@ declare module 'mongoose' { type MongooseDistinctDocumentMiddleware = 'save' | 'init' | 'validate'; type MongooseDocumentMiddleware = MongooseDistinctDocumentMiddleware | MongooseQueryAndDocumentMiddleware; - type MongooseDistinctQueryMiddleware = 'count' | 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndRemove' | 'findOneAndReplace' | 'findOneAndUpdate' | 'replaceOne' | 'updateMany'; + type MongooseRawResultQueryMiddleware = 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete'; + type MongooseDistinctQueryMiddleware = 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndReplace' | 'findOneAndUpdate' | 'replaceOne' | 'updateMany'; + type MongooseDefaultQueryMiddleware = MongooseDistinctQueryMiddleware | 'updateOne' | 'deleteOne'; type MongooseQueryMiddleware = MongooseDistinctQueryMiddleware | MongooseQueryAndDocumentMiddleware; diff --git a/types/models.d.ts b/types/models.d.ts index cfa23bc40da..ddb28542ed7 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -548,21 +548,9 @@ declare module 'mongoose' { >; findByIdAndDelete( id?: mongodb.ObjectId | any, - options?: QueryOptions | null - ): QueryWithHelpers; - - /** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */ - findByIdAndRemove( - id: mongodb.ObjectId | any, - options: QueryOptions & { lean: true } - ): QueryWithHelpers< - GetLeanResultType | null, - ResultDoc, - TQueryHelpers, - TRawDocType, - 'findOneAndDelete' - >; - findByIdAndRemove( + options?: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>; + findByIdAndDelete( id?: mongodb.ObjectId | any, options?: QueryOptions | null ): QueryWithHelpers; @@ -584,11 +572,6 @@ declare module 'mongoose' { update: UpdateQuery, options: QueryOptions & { rawResult: true } ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>; - findByIdAndUpdate( - id: mongodb.ObjectId | any, - update: UpdateQuery, - options: QueryOptions & { includeResultMetadata: true } - ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>; findByIdAndUpdate( id: mongodb.ObjectId | any, update: UpdateQuery, @@ -615,6 +598,10 @@ declare module 'mongoose' { TRawDocType, 'findOneAndDelete' >; + findOneAndDelete( + filter?: FilterQuery, + options?: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>; findOneAndDelete( filter?: FilterQuery, options?: QueryOptions | null @@ -643,11 +630,6 @@ declare module 'mongoose' { replacement: TRawDocType | AnyObject, options: QueryOptions & { rawResult: true } ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>; - findOneAndReplace( - filter: FilterQuery, - replacement: TRawDocType | AnyObject, - options: QueryOptions & { includeResultMetadata: true } - ): QueryWithHelpers, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>; findOneAndReplace( filter: FilterQuery, replacement: TRawDocType | AnyObject,