new-mongoose-lean-defaults
v3.0.5
Published
Attach defaults to the results of mongoose queries when using `.lean()`
Downloads
7
Maintainers
Readme
new-mongoose-lean-defaults
This a fork of the original mongoose-lean-defaults project. Due to the project's current inactivity, this fork has been created to address the current issues.
Attach defaults to the results of mongoose queries when using .lean()
.
Changelog
3.0.5
🪲 bugfixes
defaults
were being applied to sub-schemas even if they werenot required
and not set- this resulted it non required fields to be set because of nested
defaults
- this resulted it non required fields to be set because of nested
Install
npm install --save new-mongoose-lean-defaults
or
yarn add new-mongoose-lean-defaults
Usage
import mongooseLeanDefaults from 'new-mongoose-lean-defaults';
// const mongooseLeanDefaults = require('new-mongoose-lean-defaults').default;
const userSchema = new mongoose.Schema({
name: {
type: String,
default: 'Bob',
},
});
// documents will only have `name` field on database
// Later
const updatedUserSchema = new mongoose.Schema({
name: {
type: String,
default: 'Bob',
},
country: {
type: String,
default: 'USA',
},
});
// `.find().lean()` will return documents without `country` field
updatedUserSchema.plugin(mongooseLeanDefaults);
// You must pass `defaults: true` to `.lean()`
const bob = await UserModel.findOne().lean({ defaults: true });
/**
* bob = {
* _id: ...,
* name: 'Bob',
* country: 'USA'
* }
*/