Skip to content

Commit

Permalink
Fix: Mongoose types incorrect for when includeResultMetadata: true is…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
prathamVaidya authored and vkarpov15 committed Nov 16, 2023
1 parent 8fb5ece commit 0e3b205
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
14 changes: 12 additions & 2 deletions 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<Document> = function(next, opts) {
Expand Down Expand Up @@ -109,7 +109,17 @@ schema.post<Query<number, any>>('countDocuments', function(count, next) {
});

schema.post<Query<ITest, ITest>>('findOneAndDelete', function(res, next) {
expectType<ITest>(res);
expectType<ITest | ModifyResult<ITest> | null>(res);
next();
});

schema.post<Query<ITest, ITest>>('findOneAndUpdate', function(res, next) {
expectType<ITest | ModifyResult<ITest> | null>(res);
next();
});

schema.post<Query<ITest, ITest>>('findOneAndReplace', function(res, next) {
expectType<ITest | ModifyResult<ITest> | null>(res);
next();
});

Expand Down
13 changes: 10 additions & 3 deletions test/types/models.test.ts
Expand Up @@ -673,9 +673,6 @@ async function gh13705() {
const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { lean: true });
expectType<ExpectedLeanDoc | null>(findByIdAndDeleteRes);

const findByIdAndRemoveRes = await TestModel.findByIdAndRemove('0'.repeat(24), { lean: true });
expectType<ExpectedLeanDoc | null>(findByIdAndRemoveRes);

const findByIdAndUpdateRes = await TestModel.findByIdAndUpdate('0'.repeat(24), {}, { lean: true });
expectType<ExpectedLeanDoc | null>(findByIdAndUpdateRes);

Expand Down Expand Up @@ -709,6 +706,16 @@ async function gh13746() {
expectType<boolean | undefined>(findOneAndUpdateRes.lastErrorObject?.updatedExisting);
expectType<ObjectId | undefined>(findOneAndUpdateRes.lastErrorObject?.upserted);
expectType<OkType>(findOneAndUpdateRes.ok);

const findOneAndDeleteRes = await TestModel.findOneAndDelete({ _id: '0'.repeat(24) }, { includeResultMetadata: true });
expectType<boolean | undefined>(findOneAndDeleteRes.lastErrorObject?.updatedExisting);
expectType<ObjectId | undefined>(findOneAndDeleteRes.lastErrorObject?.upserted);
expectType<OkType>(findOneAndDeleteRes.ok);

const findByIdAndDeleteRes = await TestModel.findByIdAndDelete('0'.repeat(24), { includeResultMetadata: true });
expectType<boolean | undefined>(findByIdAndDeleteRes.lastErrorObject?.updatedExisting);
expectType<ObjectId | undefined>(findByIdAndDeleteRes.lastErrorObject?.upserted);
expectType<OkType>(findByIdAndDeleteRes.ok);
}

function gh13904() {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -336,6 +336,7 @@ declare module 'mongoose' {
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
post<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction<T, T>): this;
// this = Query
post<T = Query<any, any>>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction<T, null | QueryResultType<T> | ModifyResult<QueryResultType<T>>>): this;
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
post<T = Query<any, any>>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
post<T = Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
Expand Down
4 changes: 3 additions & 1 deletion types/middlewares.d.ts
Expand Up @@ -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;

Expand Down
32 changes: 7 additions & 25 deletions types/models.d.ts
Expand Up @@ -548,21 +548,9 @@ declare module 'mongoose' {
>;
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
id?: mongodb.ObjectId | any,
options?: QueryOptions<TRawDocType> | null
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;

/** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
id: mongodb.ObjectId | any,
options: QueryOptions<TRawDocType> & { lean: true }
): QueryWithHelpers<
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
ResultDoc,
TQueryHelpers,
TRawDocType,
'findOneAndDelete'
>;
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
id?: mongodb.ObjectId | any,
options?: QueryOptions<TRawDocType> | null
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
Expand All @@ -584,11 +572,6 @@ declare module 'mongoose' {
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { rawResult: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
id: mongodb.ObjectId | any,
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
id: mongodb.ObjectId | any,
update: UpdateQuery<TRawDocType>,
Expand All @@ -615,6 +598,10 @@ declare module 'mongoose' {
TRawDocType,
'findOneAndDelete'
>;
findOneAndDelete<ResultDoc = THydratedDocumentType>(
filter?: FilterQuery<TRawDocType>,
options?: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
findOneAndDelete<ResultDoc = THydratedDocumentType>(
filter?: FilterQuery<TRawDocType>,
options?: QueryOptions<TRawDocType> | null
Expand Down Expand Up @@ -643,11 +630,6 @@ declare module 'mongoose' {
replacement: TRawDocType | AnyObject,
options: QueryOptions<TRawDocType> & { rawResult: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
findOneAndReplace<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
replacement: TRawDocType | AnyObject,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
findOneAndReplace<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
replacement: TRawDocType | AnyObject,
Expand Down

0 comments on commit 0e3b205

Please sign in to comment.