Skip to content

Commit d5f5afe

Browse files
committed
fix: don't apply getters if defaultLeanOptions is set but lean is false
Fix #46
1 parent 22171d0 commit d5f5afe

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ function applyGetters(schema, res) {
4242
if (res == null) {
4343
return;
4444
}
45+
if (!this._mongooseOptions?.lean) {
46+
return;
47+
}
4548
const { defaultLeanOptions } = this._mongooseLeanGettersOptions;
46-
const shouldCallGetters = this._mongooseOptions?.lean?.getters ?? defaultLeanOptions?.getters ?? false;
49+
const shouldCallGetters = this._mongooseOptions.lean.getters ?? defaultLeanOptions?.getters ?? false;
4750

4851
if (shouldCallGetters) {
4952
if (Array.isArray(res)) {

test/index.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ describe('mongoose-lean-getters', function() {
442442
assert.equal(typeof found.discriminatedProp.num, 'string', 'Discriminated prop is not a string');
443443
assert.equal(typeof found.discriminatedArray[0].num, 'string', 'Discriminated array is not a string');
444444
});
445-
it('allows defaultLeanOptions to be set and overridden at call time (#33)', async() => {
445+
446+
it('allows defaultLeanOptions to be set and overridden at call time (#33) (#46)', async() => {
446447
const testSchema = new mongoose.Schema({
447448
field: {
448449
type: String,
@@ -458,6 +459,10 @@ describe('mongoose-lean-getters', function() {
458459

459460
const doc2 = await TestModel.findById(entry._id).lean({ getters: false });
460461
assert.equal(doc2.field, 'value');
462+
463+
const doc3 = await TestModel.findById(entry._id);
464+
assert.equal(doc3.field, 'value-suffix');
465+
assert.equal(doc3.get('field', null, { getters: false }), 'value');
461466
});
462467

463468
it('should allow non-discriminated documents to be retrieved (#39)', async() => {

0 commit comments

Comments
 (0)