@@ -41,24 +41,40 @@ export class DefinitionsFactory {
4141 }
4242
4343 private static inspectTypeDefinition (
44- options : mongoose . SchemaTypeOpts < unknown > | Function ,
44+ optionsOrType : mongoose . SchemaTypeOpts < unknown > | Function ,
4545 ) : PropOptions {
46- if ( typeof options === 'function' ) {
47- if ( this . isPrimitive ( options ) ) {
48- return options ;
49- } else if ( this . isMongooseSchemaType ( options ) ) {
50- return options ;
46+ if ( typeof optionsOrType === 'function' ) {
47+ if ( this . isPrimitive ( optionsOrType ) ) {
48+ return optionsOrType ;
49+ } else if ( this . isMongooseSchemaType ( optionsOrType ) ) {
50+ return optionsOrType ;
5151 }
52- return this . createForClass ( options as Type < unknown > ) ;
53- } else if ( typeof options . type === 'function' ) {
54- options . type = this . inspectTypeDefinition ( options . type ) ;
55- return options ;
56- } else if ( Array . isArray ( options ) ) {
57- return options . length > 0
58- ? [ this . inspectTypeDefinition ( options [ 0 ] ) ]
59- : options ;
52+
53+ const schemaDefinition = this . createForClass (
54+ optionsOrType as Type < unknown > ,
55+ ) ;
56+ const schemaMetadata = TypeMetadataStorage . getSchemaMetadataByTarget (
57+ optionsOrType as Type < unknown > ,
58+ ) ;
59+ if ( schemaMetadata ?. options ) {
60+ /**
61+ * When options are provided (e.g., `@Schema({ timestamps: true })`)
62+ * create a new nested schema for a subdocument
63+ * @ref https://mongoosejs.com/docs/subdocs.html
64+ **/
65+
66+ return new mongoose . Schema ( schemaDefinition , schemaMetadata . options ) ;
67+ }
68+ return schemaDefinition ;
69+ } else if ( typeof optionsOrType . type === 'function' ) {
70+ optionsOrType . type = this . inspectTypeDefinition ( optionsOrType . type ) ;
71+ return optionsOrType ;
72+ } else if ( Array . isArray ( optionsOrType ) ) {
73+ return optionsOrType . length > 0
74+ ? [ this . inspectTypeDefinition ( optionsOrType [ 0 ] ) ]
75+ : optionsOrType ;
6076 }
61- return options ;
77+ return optionsOrType ;
6278 }
6379
6480 private static isPrimitive ( type : Function ) {
0 commit comments