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

types: add __v to lean() result type and ModifyResult #14990

Merged
merged 1 commit into from
Oct 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ async function gh12959() {

const doc = await Model.findById('id').orFail();
expectType<Types.ObjectId>(doc._id);
expectType<number | undefined>(doc.__v);
expectType<number>(doc.__v);

expectError(doc.subdocArray[0].__v);
}
Expand Down
6 changes: 3 additions & 3 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ async function gh13151() {

const TestModel = model<ITest>('Test', TestSchema);
const test = await TestModel.findOne().lean();
expectType<ITest & { _id: Types.ObjectId } | null>(test);
expectType<ITest & { _id: Types.ObjectId } & { __v: number } | null>(test);
if (!test) return;
expectType<ITest & { _id: Types.ObjectId }>(test);
expectType<ITest & { _id: Types.ObjectId } & { __v: number }>(test);
}

function gh13206() {
Expand Down Expand Up @@ -661,7 +661,7 @@ async function gh13705() {
const schema = new Schema({ name: String });
const TestModel = model('Test', schema);

type ExpectedLeanDoc = (mongoose.FlattenMaps<{ name?: string | null }> & { _id: mongoose.Types.ObjectId });
type ExpectedLeanDoc = (mongoose.FlattenMaps<{ name?: string | null }> & { _id: mongoose.Types.ObjectId } & { __v: number });

const findByIdRes = await TestModel.findById('0'.repeat(24), undefined, { lean: true });
expectType<ExpectedLeanDoc | null>(findByIdRes);
Expand Down
1 change: 0 additions & 1 deletion test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ async function _11532() {

if (!leanResult) return;
expectType<string>(leanResult.child.name);
expectError(leanResult?.__v);
}

async function gh11710() {
Expand Down
10 changes: 10 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,3 +1714,13 @@ async function gh14451() {
myMap?: Record<string, string> | null | undefined
}>({} as TestJSON);
}

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

const doc = await TestModel.findOne().orFail();
expectType<number>(doc.__v);
const leanDoc = await TestModel.findOne().lean().orFail();
expectType<number>(leanDoc.__v);
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ declare module 'mongoose' {

export type Default__v<T> = T extends { __v?: infer U }
? T
: T & { __v?: number };
: T & { __v: number };

/** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
export type HydratedDocument<
Expand Down
2 changes: 1 addition & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ declare module 'mongoose' {
}

interface ModifyResult<T> {
value: Require_id<T> | null;
value: Default__v<Require_id<T>> | null;
/** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */
lastErrorObject?: {
updatedExisting?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ declare module 'mongoose' {
type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';

type GetLeanResultType<RawDocType, ResultType, QueryOp> = QueryOp extends QueryOpThatReturnsDocument
? (ResultType extends any[] ? Require_id<BufferToBinary<FlattenMaps<RawDocType>>>[] : Require_id<BufferToBinary<FlattenMaps<RawDocType>>>)
? (ResultType extends any[] ? Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>[] : Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>)
: ResultType;

type MergePopulatePaths<RawDocType, ResultType, QueryOp, Paths, TQueryHelpers, TInstanceMethods = Record<string, never>> = QueryOp extends QueryOpThatReturnsDocument
Expand Down