3.0.5 • Published 1 year ago
new-mongoose-lean-defaults v3.0.5
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
defaultswere being applied to sub-schemas even if they werenot requiredand 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-defaultsor
yarn add new-mongoose-lean-defaultsUsage
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'
* }
*/