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: missing typings for option includeResultMetadata #13747

Merged
merged 1 commit into from
Aug 22, 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
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