Skip to content

Commit

Permalink
feat: allow default lean options plugin config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wheale committed Jul 16, 2024
1 parent 32e828a commit 1413c92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,29 @@ await Model.create({ name: 'Captain Jean-Luc Picard' });
const doc = await Model.findOne().lean({ getters: true });
doc.name; // 'Picard'
```

You may also set the default lean options to always use getters:

```javascript
const mongoose = require('mongoose');
const mongooseLeanGetters = require('mongoose-lean-getters');

const schema = mongoose.Schema({
name: {
type: String,
// Get the last 6 characters of the string
get: v => v.slice(-6)
}
});
// Set the default options for all lean queries
schema.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true }});

await Model.create({ name: 'Captain Jean-Luc Picard' });

const doc = await Model.findOne().lean();
doc.name; // 'Picard'

// You may also set getters: false at call time
const doc2 = await Model.findOne().lean({ getters: false });
doc2.name; // 'Captain Jean-Luc Picard'
```
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

declare module 'mongoose-lean-getters' {
import mongoose = require('mongoose');
export default function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
export function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
}
export type LeanGettersOptions = { defaultLeanOptions?: { getters: boolean } };
export default function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: LeanGettersOptions): void;
export function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: LeanGettersOptions): void;
}
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const mpath = require('mpath');

module.exports = function mongooseLeanGetters(schema) {
const fn = applyGettersMiddleware(schema);
module.exports = function mongooseLeanGetters(schema, options) {
const fn = applyGettersMiddleware(schema, options);
// Use `pre('find')` so this also works with `cursor()`
// and `eachAsync()`, because those do not call `post('find')`
schema.pre('find', function() {
Expand Down Expand Up @@ -31,8 +31,9 @@ module.exports = function mongooseLeanGetters(schema) {
schema.post('findOneAndReplace', fn);
};

function applyGettersMiddleware(schema) {
function applyGettersMiddleware(schema, options) {
return function(res) {
this._mongooseLeanGettersOptions = options || {};
applyGetters.call(this, schema, res);
};
}
Expand All @@ -41,7 +42,14 @@ function applyGetters(schema, res, path) {
if (res == null) {
return;
}
if (this._mongooseOptions.lean && this._mongooseOptions.lean.getters) {
const { defaultLeanOptions } = this._mongooseLeanGettersOptions;
const shouldCallGetters = this._mongooseOptions.lean && (
this._mongooseOptions.lean.getters ||
// Allow the default options to be overridden with `.lean({ getters: false })`
(defaultLeanOptions && defaultLeanOptions.getters && this._mongooseOptions.lean.getters !== false)
);

if (shouldCallGetters) {
if (Array.isArray(res)) {
const len = res.length;
for (let i = 0; i < len; ++i) {
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,22 @@ describe('mongoose-lean-getters', function() {
assert.equal(typeof doc.field, 'string');
assert.strictEqual(doc.field, '1337');
});

it('allows defaultLeanOptions to be set and overridden at call time (#33)', async() => {
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');
});
});

0 comments on commit 1413c92

Please sign in to comment.