Skip to content

Commit 72b4094

Browse files
Fix: Mongoose types incorrect for when includeResultMetadata: true is set (#14078)
* Added Seperate type for Middlewares that support includeResultMetadata * Added Return Type as Raw Result when includeResultMetadata is TRUE for all middlewares * Added Seperate post query type for middlewares that support includeResultMetadata * Added Seperate post query type for middlewares that support includeResultMetadata, Fixed Lint * Updated MongooseRawResultQueryMiddleware and removed duplicate and redundant middlewares * Removed 'findByIdAndRemove' type definations : DEPRECATED, Fixed Linting Errors * Removed test cases for 'findByIdAndRemove', Added test case for 'findOneAndDeleteRes', 'findByIdAndDeleteRes' * Updated findOneAndDelete, Added test case for findOneAndUpdate, findOneAndReplace middlewares
1 parent 4e78234 commit 72b4094

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

test/types/middleware.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction } from 'mongoose';
1+
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult } from 'mongoose';
22
import { expectError, expectType, expectNotType, expectAssignable } from 'tsd';
33

44
const preMiddlewareFn: PreSaveMiddlewareFunction<Document> = function(next, opts) {
@@ -103,7 +103,17 @@ schema.post<Query<number, any>>('countDocuments', function(count, next) {
103103
});
104104

105105
schema.post<Query<ITest, ITest>>('findOneAndDelete', function(res, next) {
106-
expectType<ITest>(res);
106+
expectType<ITest | ModifyResult<ITest> | null>(res);
107+
next();
108+
});
109+
110+
schema.post<Query<ITest, ITest>>('findOneAndUpdate', function(res, next) {
111+
expectType<ITest | ModifyResult<ITest> | null>(res);
112+
next();
113+
});
114+
115+
schema.post<Query<ITest, ITest>>('findOneAndReplace', function(res, next) {
116+
expectType<ITest | ModifyResult<ITest> | null>(res);
107117
next();
108118
});
109119

test/types/models.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,6 @@ async function gh13705() {
673673
const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { lean: true });
674674
expectType<ExpectedLeanDoc | null>(findByIdAndDeleteRes);
675675

676-
const findByIdAndRemoveRes = await TestModel.findByIdAndRemove('0'.repeat(24), { lean: true });
677-
expectType<ExpectedLeanDoc | null>(findByIdAndRemoveRes);
678-
679676
const findByIdAndUpdateRes = await TestModel.findByIdAndUpdate('0'.repeat(24), {}, { lean: true });
680677
expectType<ExpectedLeanDoc | null>(findByIdAndUpdateRes);
681678

@@ -709,6 +706,16 @@ async function gh13746() {
709706
expectType<boolean | undefined>(findOneAndUpdateRes.lastErrorObject?.updatedExisting);
710707
expectType<ObjectId | undefined>(findOneAndUpdateRes.lastErrorObject?.upserted);
711708
expectType<OkType>(findOneAndUpdateRes.ok);
709+
710+
const findOneAndDeleteRes = await TestModel.findOneAndDelete({ _id: '0'.repeat(24) }, { includeResultMetadata: true });
711+
expectType<boolean | undefined>(findOneAndDeleteRes.lastErrorObject?.updatedExisting);
712+
expectType<ObjectId | undefined>(findOneAndDeleteRes.lastErrorObject?.upserted);
713+
expectType<OkType>(findOneAndDeleteRes.ok);
714+
715+
const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { includeResultMetadata: true });
716+
expectType<boolean | undefined>(findByIdAndDeleteRes.lastErrorObject?.updatedExisting);
717+
expectType<ObjectId | undefined>(findByIdAndDeleteRes.lastErrorObject?.upserted);
718+
expectType<OkType>(findByIdAndDeleteRes.ok);
712719
}
713720

714721
function gh13904() {

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ declare module 'mongoose' {
336336
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
337337
post<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction<T, T>): this;
338338
// this = Query
339+
post<T = Query<any, any>>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction<T, null | QueryResultType<T> | ModifyResult<QueryResultType<T>>>): this;
339340
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
340341
post<T = Query<any, any>>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
341342
post<T = Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;

types/middlewares.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ declare module 'mongoose' {
55
type MongooseDistinctDocumentMiddleware = 'save' | 'init' | 'validate';
66
type MongooseDocumentMiddleware = MongooseDistinctDocumentMiddleware | MongooseQueryAndDocumentMiddleware;
77

8+
type MongooseRawResultQueryMiddleware = 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
89
type MongooseDistinctQueryMiddleware = 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndReplace' | 'findOneAndUpdate' | 'replaceOne' | 'updateMany';
10+
911
type MongooseDefaultQueryMiddleware = MongooseDistinctQueryMiddleware | 'updateOne' | 'deleteOne';
1012
type MongooseQueryMiddleware = MongooseDistinctQueryMiddleware | MongooseQueryAndDocumentMiddleware;
1113

types/models.d.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -542,21 +542,9 @@ declare module 'mongoose' {
542542
>;
543543
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
544544
id?: mongodb.ObjectId | any,
545-
options?: QueryOptions<TRawDocType> | null
546-
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
547-
548-
/** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
549-
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
550-
id: mongodb.ObjectId | any,
551-
options: QueryOptions<TRawDocType> & { lean: true }
552-
): QueryWithHelpers<
553-
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
554-
ResultDoc,
555-
TQueryHelpers,
556-
TRawDocType,
557-
'findOneAndDelete'
558-
>;
559-
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
545+
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
546+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
547+
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
560548
id?: mongodb.ObjectId | any,
561549
options?: QueryOptions<TRawDocType> | null
562550
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
@@ -578,11 +566,6 @@ declare module 'mongoose' {
578566
update: UpdateQuery<TRawDocType>,
579567
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
580568
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
581-
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
582-
id: mongodb.ObjectId | any,
583-
update: UpdateQuery<TRawDocType>,
584-
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
585-
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
586569
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
587570
id: mongodb.ObjectId | any,
588571
update: UpdateQuery<TRawDocType>,
@@ -609,6 +592,10 @@ declare module 'mongoose' {
609592
TRawDocType,
610593
'findOneAndDelete'
611594
>;
595+
findOneAndDelete<ResultDoc = THydratedDocumentType>(
596+
filter?: FilterQuery<TRawDocType>,
597+
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
598+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
612599
findOneAndDelete<ResultDoc = THydratedDocumentType>(
613600
filter?: FilterQuery<TRawDocType>,
614601
options?: QueryOptions<TRawDocType> | null
@@ -631,11 +618,6 @@ declare module 'mongoose' {
631618
replacement: TRawDocType | AnyObject,
632619
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
633620
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
634-
findOneAndReplace<ResultDoc = THydratedDocumentType>(
635-
filter: FilterQuery<TRawDocType>,
636-
replacement: TRawDocType | AnyObject,
637-
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
638-
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
639621
findOneAndReplace<ResultDoc = THydratedDocumentType>(
640622
filter: FilterQuery<TRawDocType>,
641623
replacement: TRawDocType | AnyObject,

0 commit comments

Comments
 (0)