Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Mongoose types incorrect for when includeResultMetadata: true is set #14078

Merged
merged 11 commits into from Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -103,7 +103,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
2 changes: 2 additions & 0 deletions types/middlewares.d.ts
Expand Up @@ -5,7 +5,9 @@ declare module 'mongoose' {
type MongooseDistinctDocumentMiddleware = 'save' | 'init' | 'validate';
type MongooseDocumentMiddleware = MongooseDistinctDocumentMiddleware | MongooseQueryAndDocumentMiddleware;

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 @@ -542,21 +542,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 @@ -578,11 +566,6 @@ declare module 'mongoose' {
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>,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
id: mongodb.ObjectId | any,
update: UpdateQuery<TRawDocType>,
Expand All @@ -609,6 +592,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 All @@ -631,11 +618,6 @@ declare module 'mongoose' {
replacement: TRawDocType | AnyObject,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
findOneAndReplace<ResultDoc = THydratedDocumentType>(
prathamVaidya marked this conversation as resolved.
Show resolved Hide resolved
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