3.0.5 • Published 10 months ago

new-mongoose-lean-defaults v3.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

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

10 months ago

3.0.4

10 months ago

3.0.1

10 months ago

3.0.0

10 months ago