@@ -13,7 +13,7 @@ describe('mongoose-lean-getters', function() {
13
13
after ( ( ) => mongoose . disconnect ( ) ) ;
14
14
15
15
it ( 'with different types' , async function ( ) {
16
- const schema = mongoose . Schema ( {
16
+ const schema = new mongoose . Schema ( {
17
17
name : {
18
18
type : String ,
19
19
get : v => v != null ? v . toLowerCase ( ) : v
@@ -62,7 +62,7 @@ describe('mongoose-lean-getters', function() {
62
62
} ) ;
63
63
64
64
it ( 'only calls getters once with find() (gh-1)' , async function ( ) {
65
- const schema = mongoose . Schema ( {
65
+ const schema = new mongoose . Schema ( {
66
66
name : {
67
67
type : String ,
68
68
get : v => v + '123'
@@ -81,14 +81,14 @@ describe('mongoose-lean-getters', function() {
81
81
} ) ;
82
82
83
83
it ( 'avoids running getters on fields that are projected out (gh-9)' , async function ( ) {
84
- const childSchema = mongoose . Schema ( {
84
+ const childSchema = new mongoose . Schema ( {
85
85
name : {
86
86
type : String ,
87
87
get : v => v + '456'
88
88
}
89
89
} ) ;
90
90
91
- const schema = mongoose . Schema ( {
91
+ const schema = new mongoose . Schema ( {
92
92
name : {
93
93
type : String ,
94
94
get : v => v + '123'
@@ -114,22 +114,22 @@ describe('mongoose-lean-getters', function() {
114
114
} ) ;
115
115
116
116
it ( 'should call nested getters' , async function ( ) {
117
- const subChildSchema = mongoose . Schema ( {
117
+ const subChildSchema = new mongoose . Schema ( {
118
118
name : {
119
119
type : String ,
120
120
get : v => v + ' nested child'
121
121
}
122
122
} ) ;
123
123
124
- const childSchema = mongoose . Schema ( {
124
+ const childSchema = new mongoose . Schema ( {
125
125
name : {
126
126
type : String ,
127
127
get : v => v + ' child'
128
128
} ,
129
129
subChilren : [ subChildSchema ]
130
130
} ) ;
131
131
132
- const schema = mongoose . Schema ( {
132
+ const schema = new mongoose . Schema ( {
133
133
name : {
134
134
type : String ,
135
135
get : v => v + ' root'
@@ -209,12 +209,13 @@ describe('mongoose-lean-getters', function() {
209
209
} ) ;
210
210
211
211
it ( 'should work with arrays gh-22' , async function ( ) {
212
- const schema = mongoose . Schema ( {
212
+ const schema = new mongoose . Schema ( {
213
213
name : {
214
214
type : String
215
215
} ,
216
216
items : [ {
217
- text : { type : String , default : null , get : v => v . slice ( - 6 ) }
217
+ text : { type : String , default : null , get : v => v . slice ( - 6 ) } ,
218
+ num : Number ,
218
219
} ]
219
220
} ) ;
220
221
@@ -231,18 +232,21 @@ describe('mongoose-lean-getters', function() {
231
232
} , {
232
233
$push : {
233
234
items : {
234
- text : 'Lorem ipsum dolor sit amet'
235
+ text : 'Lorem ipsum dolor sit amet' ,
236
+ num : 1234 ,
235
237
}
236
238
}
237
- } , { new : true , projection : 'name items' } ) . lean ( { getters : true } ) ;
239
+ } , { new : true , projection : 'name items.text ' } ) . lean ( { getters : true } ) ;
238
240
239
241
const success = await Test . findOneAndUpdate ( {
240
242
name : 'Captain Jean-Luc Picard'
241
243
} ) . lean ( { getters : true } ) ;
242
244
243
245
await Test . deleteMany ( { } ) ;
244
- assert . equal ( success . items [ 0 ] . text , 't amet' ) ;
245
- assert . equal ( res . items [ 0 ] . text , 't amet' ) ;
246
+ assert . equal ( success . items [ 0 ] . text , 't amet' , 'Success text is wrong' ) ;
247
+ assert . equal ( success . items [ 0 ] . num , 1234 ) ;
248
+ assert . equal ( res . items [ 0 ] . text , 't amet' , 'Projected result is wrong' ) ;
249
+ assert . equal ( res . items [ 0 ] . num , undefined , 'num should be undefined' ) ;
246
250
} ) ;
247
251
248
252
it ( 'should call getters on schemas with discriminator' , async function ( ) {
@@ -401,6 +405,43 @@ describe('mongoose-lean-getters', function() {
401
405
assert . strictEqual ( doc . field , '1337' ) ;
402
406
} ) ;
403
407
408
+ it ( 'should call getters on nested schemas within discriminated models' , async ( ) => {
409
+ const nestedSchema = new mongoose . Schema ( {
410
+ num : {
411
+ type : mongoose . Types . Decimal128 ,
412
+ get : ( val ) => `${ val } `
413
+ }
414
+ } ) ;
415
+
416
+ const rootSchema = new mongoose . Schema ( {
417
+ // These properties are here as a control (these always worked as expected)
418
+ rootProp : { type : nestedSchema } ,
419
+ rootArray : { type : [ nestedSchema ] } ,
420
+ } ) ;
421
+ rootSchema . plugin ( mongooseLeanGetters ) ;
422
+
423
+ const discriminatedSchema = new mongoose . Schema ( {
424
+ // These props on the discriminated schemas were not having getters called
425
+ discriminatedProp : { type : nestedSchema } ,
426
+ discriminatedArray : { type : [ nestedSchema ] } ,
427
+ } ) ;
428
+
429
+ const RootModel = mongoose . model ( 'Root' , rootSchema ) ;
430
+ const DiscriminatedModel = RootModel . discriminator ( 'Discriminated' , discriminatedSchema ) ;
431
+
432
+ const entry = await DiscriminatedModel . create ( {
433
+ rootProp : { num : - 0.1111111111111111111 } ,
434
+ rootArray : [ { num : - 0.1111111111111111111 } ] ,
435
+ discriminatedProp : { num : - 0.222222222222222222 } ,
436
+ discriminatedArray : [ { num : - 0.333333333333333333 } ] ,
437
+ } ) ;
438
+
439
+ const found = await DiscriminatedModel . findById ( entry . _id ) . lean ( { getters : true } ) . exec ( ) ;
440
+ assert . equal ( typeof found . rootProp . num , 'string' , 'Root prop is not a string' ) ;
441
+ assert . equal ( typeof found . rootArray [ 0 ] . num , 'string' , 'Root array is not a string' ) ;
442
+ assert . equal ( typeof found . discriminatedProp . num , 'string' , 'Discriminated prop is not a string' ) ;
443
+ assert . equal ( typeof found . discriminatedArray [ 0 ] . num , 'string' , 'Discriminated array is not a string' ) ;
444
+ } ) ;
404
445
it ( 'allows defaultLeanOptions to be set and overridden at call time (#33)' , async ( ) => {
405
446
const testSchema = new mongoose . Schema ( {
406
447
field : {
0 commit comments