2.2.1 • Published 2 years ago

mongoose-lean-defaults v2.2.1

Weekly downloads
16,545
License
ISC
Repository
github
Last release
2 years ago

mongoose-lean-defaults

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

Run Tests Node.js Package

Install

npm install --save mongoose-lean-defaults

or

yarn add mongoose-lean-defaults

Usage

import mongooseLeanDefaults from 'mongoose-lean-defaults';
// const mongooseLeanDefaults = require('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'
 * }
 */
2.2.1

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago