Skip to content

jose-cabral/mongoose-lean-defaults

 
 

Repository files navigation

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().

GitHub Actions Workflow Status GitHub release

Changelog

3.0.5

🪲 bugfixes

  • defaults were being applied to sub-schemas even if they were not required and not set
    • this resulted it non required fields to be set because of nested defaults

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'
 * }
 */

About

Attach defaults to the results of mongoose queries when using .lean()

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • TypeScript 97.3%
  • JavaScript 2.5%
  • Dockerfile 0.2%