Skip to content

Commit 22171d0

Browse files
authored
Merge pull request #47 from mongoosejs/vkarpov15/gh-45
fix: apply single nested subdoc getter results
2 parents 9042902 + 073d55e commit 22171d0

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
node: [16, 18, 20]
15-
os: [ubuntu-20.04]
14+
node: [18, 20, 22]
15+
os: [ubuntu-22.04]
1616
include:
17-
- os: ubuntu-20.04
18-
mongo-os: ubuntu2004
19-
mongo: 5.0.2
17+
- os: ubuntu-22.04
18+
mongo-os: ubuntu2204
19+
mongo: 7.0.12
2020
name: Node ${{ matrix.node }} MongoDB ${{ matrix.mongo }}
2121
steps:
2222
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3
@@ -36,6 +36,6 @@ jobs:
3636
printf "\n--timeout 8000" >> ./test/mocha.opts
3737
./mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017
3838
sleep 2
39-
mongod --version
39+
./mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin/mongod --version
4040
echo `pwd`/mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin >> $GITHUB_PATH
4141
- run: npm test

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ function applyGettersToDoc(schema, doc) {
117117
val[i] = schematype.caster.applyGetters(val[i], doc);
118118
}
119119
}
120-
} if (val && typeof val === 'object' && schematype.$isSingleNested) {
120+
} else if (val && typeof val === 'object' && schematype.$isSingleNested) {
121121
applyGettersToDoc.call(this, schematype.schema, val);
122-
} else {
123-
mpath.set(path, val, doc);
124122
}
123+
124+
mpath.set(path, val, doc);
125125
});
126126
}
127127

test/index.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,31 @@ describe('mongoose-lean-getters', function() {
550550
});
551551
assert.strictEqual(account.name, 'HamoBoker');
552552
});
553+
554+
it('applies single nested subdoc getters (gh-45)', async function() {
555+
const priceSchema = new mongoose.Schema(
556+
{
557+
amount: mongoose.Schema.Types.Decimal128,
558+
currency: String,
559+
},
560+
{ _id: false }
561+
);
562+
563+
const itemSchema = new mongoose.Schema({
564+
name: String,
565+
price: {
566+
type: priceSchema,
567+
get: v => ({ value: parseFloat(v.amount.toString()), unit: v.currency })
568+
},
569+
});
570+
itemSchema.plugin(mongooseLeanGetters);
571+
const Item = mongoose.model('gh-45-single-nested-getters', itemSchema);
572+
await Item.create({
573+
name: 'Test Product',
574+
price: { amount: 123.45, currency: 'USD' },
575+
});
576+
577+
const lean = await Item.findOne().lean({ getters: true });
578+
assert.deepStrictEqual(lean.price, { value: 123.45, unit: 'USD' });
579+
});
553580
});

0 commit comments

Comments
 (0)