Skip to content

Commit

Permalink
Merge pull request #14336 from Automattic/vkarpov15/gh-14303
Browse files Browse the repository at this point in the history
types(model): correct return type for findOneAndUpdate with `includeResultMetadata` and `lean` set
  • Loading branch information
vkarpov15 committed Feb 7, 2024
2 parents 3eceee6 + 69d113f commit 870b94f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion test/types/models.test.ts
Expand Up @@ -17,7 +17,7 @@ import mongoose, {
} from 'mongoose';
import { expectAssignable, expectError, expectType } from 'tsd';
import { AutoTypedSchemaType, autoTypedSchema } from './schema.test';
import { UpdateOneModel, ChangeStreamInsertDocument, ObjectId } from 'mongodb';
import { ModifyResult, UpdateOneModel, ChangeStreamInsertDocument, ObjectId } from 'mongodb';

function rawDocSyntax(): void {
interface ITest {
Expand Down Expand Up @@ -684,6 +684,9 @@ async function gh13705() {

const findOneAndUpdateRes = await TestModel.findOneAndUpdate({}, {}, { lean: true });
expectType<ExpectedLeanDoc | null>(findOneAndUpdateRes);

const findOneAndUpdateResWithMetadata = await TestModel.findOneAndUpdate({}, {}, { lean: true, includeResultMetadata: true });
expectAssignable<ModifyResult<ExpectedLeanDoc>>(findOneAndUpdateResWithMetadata);
}

async function gh13746() {
Expand Down
25 changes: 21 additions & 4 deletions types/models.d.ts
Expand Up @@ -592,6 +592,17 @@ declare module 'mongoose' {
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;

/** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
): QueryWithHelpers<
ModifyResult<TRawDocType>,
ResultDoc,
TQueryHelpers,
TRawDocType,
'findOneAndUpdate'
>;
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
id: mongodb.ObjectId | any,
update: UpdateQuery<TRawDocType>,
Expand Down Expand Up @@ -675,9 +686,9 @@ declare module 'mongoose' {
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { lean: true }
options: QueryOptions<TRawDocType> & { includeResultMetadata: true, lean: true }
): QueryWithHelpers<
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
ModifyResult<TRawDocType>,
ResultDoc,
TQueryHelpers,
TRawDocType,
Expand All @@ -686,8 +697,14 @@ declare module 'mongoose' {
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
options: QueryOptions<TRawDocType> & { lean: true }
): QueryWithHelpers<
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
ResultDoc,
TQueryHelpers,
TRawDocType,
'findOneAndUpdate'
>;
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
Expand Down

0 comments on commit 870b94f

Please sign in to comment.