2.2.1 • Published 4 months ago
mongoose-lean-getters v2.2.1
mongoose-lean-getters
Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
This package requires Mongoose >= 7.1.0
. Do not use with Mongoose 7.0.x or 6.x.
Usage
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)
}
});
// Add this plugin to apply getters when using `lean()`.
schema.plugin(mongooseLeanGetters);
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:
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'
2.2.1
4 months ago
2.2.0
4 months ago
2.1.1
10 months ago
2.1.0
1 year ago
2.0.1
1 year ago
2.0.0
1 year ago
1.1.0
2 years ago
1.0.0
2 years ago
0.4.0
2 years ago
0.3.6
2 years ago
0.3.5
3 years ago
0.3.4
3 years ago
0.3.3
3 years ago
0.3.2
3 years ago
0.3.1
3 years ago
0.3.0
3 years ago
0.2.2
4 years ago
0.2.1
4 years ago
0.2.0
4 years ago
0.1.2
6 years ago
0.1.1
6 years ago
0.1.0
6 years ago