Skip to content

isplasher-forks/mongoose-lean-defaults

 
 

Repository files navigation

mongoose-lean-defaults

Attach defaults to the results of mongoose queries when using .lean(). Highly inspired by mongoose-lean-virtuals.

Install

npm install --save mongoose-lean-defaults

or

yarn add mongoose-lean-defaults

Usage

const mongooseLeanDefaults = require('mongoose-lean-defaults')

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

No packages published

Languages

  • JavaScript 99.6%
  • Dockerfile 0.4%