diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9140dbf..f1cd100 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,12 +11,12 @@ jobs: strategy: fail-fast: false matrix: - node: [16, 18, 20] - os: [ubuntu-20.04] + node: [18, 20, 22] + os: [ubuntu-22.04] include: - - os: ubuntu-20.04 - mongo-os: ubuntu2004 - mongo: 5.0.2 + - os: ubuntu-22.04 + mongo-os: ubuntu2204 + mongo: 7.0.12 name: Node ${{ matrix.node }} MongoDB ${{ matrix.mongo }} steps: - uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3 @@ -36,6 +36,6 @@ jobs: printf "\n--timeout 8000" >> ./test/mocha.opts ./mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017 sleep 2 - mongod --version + ./mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin/mongod --version echo `pwd`/mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin >> $GITHUB_PATH - run: npm test diff --git a/index.js b/index.js index 8072ba8..56b6abe 100644 --- a/index.js +++ b/index.js @@ -117,11 +117,11 @@ function applyGettersToDoc(schema, doc) { val[i] = schematype.caster.applyGetters(val[i], doc); } } - } if (val && typeof val === 'object' && schematype.$isSingleNested) { + } else if (val && typeof val === 'object' && schematype.$isSingleNested) { applyGettersToDoc.call(this, schematype.schema, val); - } else { - mpath.set(path, val, doc); } + + mpath.set(path, val, doc); }); } diff --git a/test/index.test.js b/test/index.test.js index 9e7e696..048e786 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -550,4 +550,31 @@ describe('mongoose-lean-getters', function() { }); assert.strictEqual(account.name, 'HamoBoker'); }); + + it('applies single nested subdoc getters (gh-45)', async function() { + const priceSchema = new mongoose.Schema( + { + amount: mongoose.Schema.Types.Decimal128, + currency: String, + }, + { _id: false } + ); + + const itemSchema = new mongoose.Schema({ + name: String, + price: { + type: priceSchema, + get: v => ({ value: parseFloat(v.amount.toString()), unit: v.currency }) + }, + }); + itemSchema.plugin(mongooseLeanGetters); + const Item = mongoose.model('gh-45-single-nested-getters', itemSchema); + await Item.create({ + name: 'Test Product', + price: { amount: 123.45, currency: 'USD' }, + }); + + const lean = await Item.findOne().lean({ getters: true }); + assert.deepStrictEqual(lean.price, { value: 123.45, unit: 'USD' }); + }); });