@@ -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,76 @@ 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+ */
727+ export type BufferToJSON < T > = T extends TreatAsPrimitives ? T : T extends Record < string , any > ? {
728+ [ K in keyof T ] : T [ K ] extends Buffer
729+ ? { type : 'buffer' , data : number [ ] }
730+ : T [ K ] extends ( Buffer | null | undefined )
731+ ? { type : 'buffer' , data : number [ ] } | null | undefined
732+ : T [ K ] extends Types . DocumentArray < infer ItemType >
733+ ? Types . DocumentArray < BufferToBinary < ItemType > >
734+ : T [ K ] extends Types . Subdocument < unknown , unknown , infer SubdocType >
735+ ? HydratedSingleSubdocument < SubdocType >
736+ : BufferToBinary < T [ K ] > ;
737+ } : T ;
738+
739+ /**
740+ * Converts any ObjectId properties into strings for JSON serialization
741+ */
742+ export type ObjectIdToString < T > = T extends TreatAsPrimitives ? T : T extends Record < string , any > ? {
743+ [ K in keyof T ] : T [ K ] extends mongodb . ObjectId
744+ ? string
745+ : T [ K ] extends ( mongodb . ObjectId | null | undefined )
746+ ? string | null | undefined
747+ : T [ K ] extends Types . DocumentArray < infer ItemType >
748+ ? Types . DocumentArray < ObjectIdToString < ItemType > >
749+ : T [ K ] extends Types . Subdocument < unknown , unknown , infer SubdocType >
750+ ? HydratedSingleSubdocument < ObjectIdToString < SubdocType > >
751+ : ObjectIdToString < T [ K ] > ;
752+ } : T ;
753+
754+ /**
755+ * Converts any Date properties into strings for JSON serialization
756+ */
757+ export type DateToString < T > = T extends TreatAsPrimitives ? T : T extends Record < string , any > ? {
758+ [ K in keyof T ] : T [ K ] extends NativeDate
759+ ? string
760+ : T [ K ] extends ( NativeDate | null | undefined )
761+ ? string | null | undefined
762+ : T [ K ] extends Types . DocumentArray < infer ItemType >
763+ ? Types . DocumentArray < DateToString < ItemType > >
764+ : T [ K ] extends Types . Subdocument < unknown , unknown , infer SubdocType >
765+ ? HydratedSingleSubdocument < DateToString < SubdocType > >
766+ : DateToString < T [ K ] > ;
767+ } : T ;
768+
769+ /**
770+ * Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents
771+ */
772+ export type SubdocsToPOJOs < T > = T extends TreatAsPrimitives ? T : T extends Record < string , any > ? {
773+ [ K in keyof T ] : T [ K ] extends NativeDate
774+ ? string
775+ : T [ K ] extends ( NativeDate | null | undefined )
776+ ? string | null | undefined
777+ : T [ K ] extends Types . DocumentArray < infer ItemType >
778+ ? ItemType [ ]
779+ : T [ K ] extends Types . Subdocument < unknown , unknown , infer SubdocType >
780+ ? SubdocType
781+ : SubdocsToPOJOs < T [ K ] > ;
782+ } : T ;
783+
784+ export type JSONSerialized < T > = SubdocsToPOJOs <
785+ FlattenMaps <
786+ BufferToJSON <
787+ ObjectIdToString <
788+ DateToString < T >
789+ >
790+ >
791+ >
792+ > ;
793+
721794 /**
722795 * Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it
723796 * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
0 commit comments