Skip to content

Commit 858cc0a

Browse files
committed
add docs and test coverage for maps
1 parent 50b2670 commit 858cc0a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

test/types/schema.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,11 @@ async function gh14451() {
16931693
subdocProp: Date
16941694
})
16951695
},
1696-
docArr: [{ nums: [Number], times: [{ type: Date }] }]
1696+
docArr: [{ nums: [Number], times: [{ type: Date }] }],
1697+
myMap: {
1698+
type: Map,
1699+
of: String
1700+
}
16971701
});
16981702

16991703
const Test = model('Test', exampleSchema);
@@ -1706,6 +1710,7 @@ async function gh14451() {
17061710
subdoc?: {
17071711
subdocProp?: string | undefined | null
17081712
} | null,
1709-
docArr: { nums: number[], times: string[] }[]
1713+
docArr: { nums: number[], times: string[] }[],
1714+
myMap?: Record<string, string> | null | undefined
17101715
}>({} as TestJSON);
17111716
}

types/index.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,9 @@ declare module 'mongoose' {
706706
[K in keyof T]: FlattenProperty<T[K]>;
707707
};
708708

709+
/**
710+
* Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
711+
*/
709712
export type BufferToBinary<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
710713
[K in keyof T]: T[K] extends Buffer
711714
? mongodb.Binary
@@ -718,6 +721,9 @@ declare module 'mongoose' {
718721
: BufferToBinary<T[K]>;
719722
} : T;
720723

724+
/**
725+
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
726+
*/
721727
export type BufferToJSON<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
722728
[K in keyof T]: T[K] extends Buffer
723729
? { type: 'buffer', data: number[] }
@@ -730,6 +736,9 @@ declare module 'mongoose' {
730736
: BufferToBinary<T[K]>;
731737
} : T;
732738

739+
/**
740+
* Converts any ObjectId properties into strings for JSON serialization
741+
*/
733742
export type ObjectIdToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
734743
[K in keyof T]: T[K] extends mongodb.ObjectId
735744
? string
@@ -742,6 +751,9 @@ declare module 'mongoose' {
742751
: ObjectIdToString<T[K]>;
743752
} : T;
744753

754+
/**
755+
* Converts any Date properties into strings for JSON serialization
756+
*/
745757
export type DateToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
746758
[K in keyof T]: T[K] extends NativeDate
747759
? string
@@ -754,6 +766,9 @@ declare module 'mongoose' {
754766
: DateToString<T[K]>;
755767
} : T;
756768

769+
/**
770+
* Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents
771+
*/
757772
export type SubdocsToPOJOs<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
758773
[K in keyof T]: T[K] extends NativeDate
759774
? string

0 commit comments

Comments
 (0)