Skip to content

Commit 0e3b205

Browse files
prathamVaidyavkarpov15
authored andcommitted
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 8fb5ece commit 0e3b205

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
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) {
@@ -109,7 +109,17 @@ schema.post<Query<number, any>>('countDocuments', function(count, next) {
109109
});
110110

111111
schema.post<Query<ITest, ITest>>('findOneAndDelete', function(res, next) {
112-
expectType<ITest>(res);
112+
expectType<ITest | ModifyResult<ITest> | null>(res);
113+
next();
114+
});
115+
116+
schema.post<Query<ITest, ITest>>('findOneAndUpdate', function(res, next) {
117+
expectType<ITest | ModifyResult<ITest> | null>(res);
118+
next();
119+
});
120+
121+
schema.post<Query<ITest, ITest>>('findOneAndReplace', function(res, next) {
122+
expectType<ITest | ModifyResult<ITest> | null>(res);
113123
next();
114124
});
115125

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: 3 additions & 1 deletion
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 MongooseDistinctQueryMiddleware = 'count' | 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndRemove' | 'findOneAndReplace' | 'findOneAndUpdate' | 'replaceOne' | 'updateMany';
8+
type MongooseRawResultQueryMiddleware = 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
9+
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
@@ -548,21 +548,9 @@ declare module 'mongoose' {
548548
>;
549549
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
550550
id?: mongodb.ObjectId | any,
551-
options?: QueryOptions<TRawDocType> | null
552-
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
553-
554-
/** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
555-
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
556-
id: mongodb.ObjectId | any,
557-
options: QueryOptions<TRawDocType> & { lean: true }
558-
): QueryWithHelpers<
559-
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
560-
ResultDoc,
561-
TQueryHelpers,
562-
TRawDocType,
563-
'findOneAndDelete'
564-
>;
565-
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
551+
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
552+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
553+
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
566554
id?: mongodb.ObjectId | any,
567555
options?: QueryOptions<TRawDocType> | null
568556
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
@@ -584,11 +572,6 @@ declare module 'mongoose' {
584572
update: UpdateQuery<TRawDocType>,
585573
options: QueryOptions<TRawDocType> & { rawResult: true }
586574
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
587-
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
588-
id: mongodb.ObjectId | any,
589-
update: UpdateQuery<TRawDocType>,
590-
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
591-
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
592575
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
593576
id: mongodb.ObjectId | any,
594577
update: UpdateQuery<TRawDocType>,
@@ -615,6 +598,10 @@ declare module 'mongoose' {
615598
TRawDocType,
616599
'findOneAndDelete'
617600
>;
601+
findOneAndDelete<ResultDoc = THydratedDocumentType>(
602+
filter?: FilterQuery<TRawDocType>,
603+
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
604+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
618605
findOneAndDelete<ResultDoc = THydratedDocumentType>(
619606
filter?: FilterQuery<TRawDocType>,
620607
options?: QueryOptions<TRawDocType> | null
@@ -643,11 +630,6 @@ declare module 'mongoose' {
643630
replacement: TRawDocType | AnyObject,
644631
options: QueryOptions<TRawDocType> & { rawResult: true }
645632
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
646-
findOneAndReplace<ResultDoc = THydratedDocumentType>(
647-
filter: FilterQuery<TRawDocType>,
648-
replacement: TRawDocType | AnyObject,
649-
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
650-
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
651633
findOneAndReplace<ResultDoc = THydratedDocumentType>(
652634
filter: FilterQuery<TRawDocType>,
653635
replacement: TRawDocType | AnyObject,

0 commit comments

Comments
 (0)