Skip to content
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
74 changes: 40 additions & 34 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,47 +712,53 @@ 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>;
export type BufferToBinaryProperty<T> = unknown extends Buffer
? 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
*/
export type BufferToBinary<T> = T extends Buffer
? mongodb.Binary
: T extends Document
? T
: T extends TreatAsPrimitives
? T
: T extends Record<string, any>
? {
[K in keyof T]: BufferToBinaryProperty<T[K]>
}
: T;
export type BufferToBinary<T> = unknown extends Buffer
? T
: T extends Buffer
? mongodb.Binary
: T extends Document
? T
: T extends TreatAsPrimitives
? 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
*/
export type BufferToJSON<T> = T extends Buffer
? { type: 'buffer', data: number[] }
: T extends Document
? T
: T extends TreatAsPrimitives
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
*/
export type BufferToJSON<T> = unknown extends Buffer
? T
: T extends Buffer
? { type: 'buffer', data: number[] }
: T extends Document
? T
: T extends Record<string, any> ? {
[K in keyof T]: T[K] extends Buffer
? { type: 'buffer', data: number[] }
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<SubdocType>
: BufferToBinary<T[K]>;
} : T;
: T extends TreatAsPrimitives
? T
: T extends Record<string, any> ? {
[K in keyof T]: T[K] extends Buffer
? { type: 'buffer', data: number[] }
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<SubdocType>
: BufferToBinary<T[K]>;
} : T;

/**
* Converts any ObjectId properties into strings for JSON serialization
Expand Down
12 changes: 8 additions & 4 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@ type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
? true
: T extends Types.Decimal128
? true
: T extends Buffer
: T extends NativeDate
? true
: T extends NativeDate
: T extends (typeof Schema.Types.Mixed)
? true
: T extends (typeof Schema.Types.Mixed)
: IfEquals<T, Schema.Types.ObjectId, true, false> extends true
? true
: IfEquals<T, Schema.Types.ObjectId, true, false>;
: unknown extends Buffer
? false
: T extends Buffer
? true
: false;

/**
* @summary Resolve path type by returning the corresponding type.
Expand Down
Loading