Skip to content

Commit 3d523e9

Browse files
committed
fix merge issues from #43 and bump ESlint
1 parent cefc3e6 commit 3d523e9

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

index.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,21 @@ function applyGettersToDoc(schema, doc) {
107107
// The path is not present (likely from projection)
108108
return;
109109
}
110-
111-
const pathExists = mpath.has(path, doc);
112-
if (pathExists) {
113-
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
114-
if (val && schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
115-
mpath.set(
116-
path,
117-
val.map(subdoc => {
118-
return schematype.caster.applyGetters(subdoc, doc);
119-
}),
120-
doc
121-
);
122-
} if (val && schematype.$isSingleNested) {
123-
applyGettersToDoc.call(this, schematype.schema, pathVal);
110+
111+
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
112+
if (val && schematype.$isMongooseArray) {
113+
if (schematype.$isMongooseDocumentArray) {
114+
val.forEach((subdoc) => applyGettersToDoc.call(this, schematype.schema, subdoc));
124115
} else {
125-
mpath.set(path, val, doc);
116+
for (let i = 0; i < val.length; ++i) {
117+
val[i] = schematype.caster.applyGetters(val[i], doc);
118+
}
126119
}
120+
} if (val && schematype.$isSingleNested) {
121+
applyGettersToDoc.call(this, schematype.schema, val);
122+
} else {
123+
mpath.set(path, val, doc);
127124
}
128-
129-
mpath.set(path, schematype.applyGetters(pathVal, doc, true), doc);
130125
});
131126
}
132127

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"acquit-ignore": "0.1.0",
3333
"acquit-markdown": "0.1.0",
3434
"co": "4.6.0",
35-
"eslint": "5.16.0",
35+
"eslint": "8.x",
3636
"istanbul": "0.4.5",
3737
"mocha": "5.2.x"
3838
},
@@ -47,7 +47,7 @@
4747
"eslint:recommended"
4848
],
4949
"parserOptions": {
50-
"ecmaVersion": 2018
50+
"ecmaVersion": 2020
5151
},
5252
"env": {
5353
"node": true,

test/index.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ describe('mongoose-lean-getters', function() {
430430
const DiscriminatedModel = RootModel.discriminator('Discriminated', discriminatedSchema);
431431

432432
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 }],
433+
rootProp: { num: -0.1111 },
434+
rootArray: [{ num: -0.1111 }],
435+
discriminatedProp: { num: -0.2222 },
436+
discriminatedArray: [{ num: -0.3333 }],
437437
});
438438

439439
const found = await DiscriminatedModel.findById(entry._id).lean({ getters: true }).exec();

0 commit comments

Comments
 (0)