Norse Polytheistic Mythology

    mongoose-lean-defaults
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    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

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

    Install

    npm i mongoose-lean-defaults

    DownloadsWeekly Downloads

    25,129

    Version

    2.0.1

    License

    ISC

    Unpacked Size

    10.2 kB

    Total Files

    6

    Last publish

    Collaborators

    • douglasgabr