Skip to content

Commit 5f7065d

Browse files
authored
Merge pull request #41 from DesignByOnyx/33-default-lean-options
feat: allow default lean options plugin config
2 parents 3ed0fd8 + 682bb54 commit 5f7065d

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,29 @@ await Model.create({ name: 'Captain Jean-Luc Picard' });
2424
const doc = await Model.findOne().lean({ getters: true });
2525
doc.name; // 'Picard'
2626
```
27+
28+
You may also set the default lean options to always use getters:
29+
30+
```javascript
31+
const mongoose = require('mongoose');
32+
const mongooseLeanGetters = require('mongoose-lean-getters');
33+
34+
const schema = mongoose.Schema({
35+
name: {
36+
type: String,
37+
// Get the last 6 characters of the string
38+
get: v => v.slice(-6)
39+
}
40+
});
41+
// Set the default options for all lean queries
42+
schema.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true }});
43+
44+
await Model.create({ name: 'Captain Jean-Luc Picard' });
45+
46+
const doc = await Model.findOne().lean();
47+
doc.name; // 'Picard'
48+
49+
// You may also set getters: false at call time
50+
const doc2 = await Model.findOne().lean({ getters: false });
51+
doc2.name; // 'Captain Jean-Luc Picard'
52+
```

index.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
declare module 'mongoose-lean-getters' {
55
import mongoose = require('mongoose');
6-
export default function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
7-
export function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
8-
}
6+
export type LeanGettersOptions = { defaultLeanOptions?: { getters: boolean } };
7+
export default function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: LeanGettersOptions): void;
8+
export function mongooseLeanGetters(schema: mongoose.Schema<any, any, any, any>, opts?: LeanGettersOptions): void;
9+
}

index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const mpath = require('mpath');
44

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

34-
function applyGettersMiddleware(schema) {
34+
function applyGettersMiddleware(schema, options) {
3535
return function(res) {
36+
this._mongooseLeanGettersOptions = options || {};
3637
applyGetters.call(this, schema, res);
3738
};
3839
}
@@ -41,7 +42,10 @@ function applyGetters(schema, res, path) {
4142
if (res == null) {
4243
return;
4344
}
44-
if (this._mongooseOptions.lean && this._mongooseOptions.lean.getters) {
45+
const { defaultLeanOptions } = this._mongooseLeanGettersOptions;
46+
const shouldCallGetters = this._mongooseOptions?.lean?.getters ?? defaultLeanOptions?.getters ?? false;
47+
48+
if (shouldCallGetters) {
4549
if (Array.isArray(res)) {
4650
const len = res.length;
4751
for (let i = 0; i < len; ++i) {

test/index.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ describe('mongoose-lean-getters', function() {
401401
assert.strictEqual(doc.field, '1337');
402402
});
403403

404+
it('allows defaultLeanOptions to be set and overridden at call time (#33)', async() => {
405+
const testSchema = new mongoose.Schema({
406+
field: {
407+
type: String,
408+
get(val) { return `${val}-suffix`; },
409+
}
410+
});
411+
testSchema.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true } });
412+
413+
const TestModel = mongoose.model('gh-33', testSchema);
414+
const entry = await TestModel.create({ field: 'value' });
415+
const doc1 = await TestModel.findById(entry._id).lean();
416+
assert.equal(doc1.field, 'value-suffix');
417+
418+
const doc2 = await TestModel.findById(entry._id).lean({ getters: false });
419+
assert.equal(doc2.field, 'value');
420+
});
421+
404422
it('should allow non-discriminated documents to be retrieved (#39)', async() => {
405423
const baseSchema = new mongoose.Schema({ foo: String });
406424
baseSchema.plugin(mongooseLeanGetters);

0 commit comments

Comments
 (0)