mongoosejs / mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
Apache License 2.0
10 stars 15 forks source link

feat: allow default lean options plugin config #41

Open DesignByOnyx opened 4 months ago

DesignByOnyx commented 4 months ago

Summary

This allows setting { getters: true } at a plugin level. This can be overridden at call time to disable getters if need be. Closes #33.

Examples

const testSchema = new mongoose.Schema({
  field: {
    type: String,
    get(val) { return `${val}-suffix`; },
  }
});
testSchema.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true } });

const TestModel = mongoose.model('gh-33', testSchema);
const entry = await TestModel.create({ field: 'value' });
const doc1 = await TestModel.findById(entry._id).lean();
assert.equal(doc1.field, 'value-suffix');

const doc2 = await TestModel.findById(entry._id).lean({ getters: false });
assert.equal(doc2.field, 'value');
Bderdot commented 1 week ago

Hi,

I'm very interested by such feature. Any clue when it would release?