Skip to content

Commit

Permalink
Merge pull request #13747 from Idnan/master
Browse files Browse the repository at this point in the history
fix: missing typings for option includeResultMetadata
  • Loading branch information
vkarpov15 committed Aug 22, 2023
2 parents 812b014 + 8156275 commit acbd957
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
24 changes: 23 additions & 1 deletion test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import mongoose, {
} from 'mongoose';
import { expectAssignable, expectError, expectType } from 'tsd';
import { AutoTypedSchemaType, autoTypedSchema } from './schema.test';
import { UpdateOneModel, ChangeStreamInsertDocument } from 'mongodb';
import { UpdateOneModel, ChangeStreamInsertDocument, ObjectId } from 'mongodb';

function rawDocSyntax(): void {
interface ITest {
Expand Down Expand Up @@ -673,3 +673,25 @@ async function gh13705() {
const findOneAndUpdateRes = await TestModel.findOneAndUpdate({}, {}, { lean: true });
expectType<ExpectedLeanDoc | null>(findOneAndUpdateRes);
}

async function gh13746() {
const schema = new Schema({ name: String });
const TestModel = model('Test', schema);

type OkType = 0 | 1;

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

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

const findOneAndUpdateRes = await TestModel.findOneAndUpdate({ _id: '0'.repeat(24) }, {}, { includeResultMetadata: true });
expectType<boolean | undefined>(findOneAndUpdateRes.lastErrorObject?.updatedExisting);
expectType<ObjectId | undefined>(findOneAndUpdateRes.lastErrorObject?.upserted);
expectType<OkType>(findOneAndUpdateRes.ok);
}
15 changes: 15 additions & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@ 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 Down Expand Up @@ -611,6 +616,11 @@ 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 Expand Up @@ -639,6 +649,11 @@ declare module 'mongoose' {
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { rawResult: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
filter: FilterQuery<TRawDocType>,
update: UpdateQuery<TRawDocType>,
Expand Down

0 comments on commit acbd957

Please sign in to comment.