Skip to content

Commit

Permalink
Merge pull request #15160 from Automattic/vkarpov15/gh-15158
Browse files Browse the repository at this point in the history
types: avoid BufferToBinary<> wiping lean types when passed to generic functions
  • Loading branch information
vkarpov15 authored Jan 8, 2025
2 parents b5752f6 + 15a6595 commit e56b046
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
26 changes: 26 additions & 0 deletions test/types/lean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,29 @@ async function gh15122() {
testFn(parentDoc);
}
}

async function gh15158() {
type FooBar = {
value: string;
};

const createSomeModelAndDoSomething = async <T extends FooBar>() => {
const TestSchema = new Schema<T>({
value: { type: String }
});

const FooBarModel = model<T>('test', TestSchema);

const item = await FooBarModel.findOne().lean();

if (!item) return;

doSomeThing(item);
};

const doSomeThing = <T extends FooBar>(item: T) => {
console.log(item);
};

createSomeModelAndDoSomething();
}
22 changes: 13 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,14 @@ declare module 'mongoose' {
[K in keyof T]: FlattenProperty<T[K]>;
};

export type BufferToBinaryProperty<T> = T extends Buffer
? mongodb.Binary
: T extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
: BufferToBinary<T>;

/**
* Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
*/
Expand All @@ -719,15 +727,11 @@ declare module 'mongoose' {
? T
: T extends TreatAsPrimitives
? T
: T extends Record<string, any> ? {
[K in keyof T]: T[K] extends Buffer
? mongodb.Binary
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
: BufferToBinary<T[K]>;
} : T;
: T extends Record<string, any>
? {
[K in keyof T]: BufferToBinaryProperty<T[K]>
}
: T;

/**
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
Expand Down
2 changes: 1 addition & 1 deletion types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ declare module 'mongoose' {
type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';

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

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

0 comments on commit e56b046

Please sign in to comment.