Socket
Socket
Sign inDemoInstall

mongoose-lean-defaults

Package Overview
Dependencies
223
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-lean-defaults

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


Version published
Weekly downloads
20K
increased by8.47%
Maintainers
1
Install size
98.8 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 25 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc