Socket
Socket
Sign inDemoInstall

mongoose-enumvalues

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-enumvalues

Mongoose plugin that provides convenient access to enum values


Version published
Weekly downloads
17
decreased by-41.38%
Maintainers
1
Install size
13.7 kB
Created
Weekly downloads
 

Readme

Source

EnumValues

Mongoose plugin that allows easy access to enum values. You can create virtuals, attach to your document, or modify your enum property in-place. Now with a TypeScript definition.

Installation

npm install [--save] mongoose-enumvalues

Examples

Setup

const mongoose   = require('mongoose');
const enumValues = require('mongoose-enumvalues');
const { Schema } = mongoose;
const UserSchema = new Schema({
  name: String,
  age: Number,
  role: {
    type: String,
    enum: ['admin', 'moderator', 'guest'],
    default: 'guest'
  },
  gender: {
    type: String,
    uppercase: true,
    enum: ['MALE', 'FEMALE'],
    default: 'FEMALE'
  },
  nesting: {
    enums: {
      type: String,
      enum: ['something', 'wicked', 'this', 'way', 'comes']
    }
  }
});

// specifics for each method below
const enumOptions = {};

UserSchema.plugin(enumValues, enumOptions);

module.exports = mongoose.model('User', UserSchema);

Virtuals

Automatically create virtual properties for enum value access.

const enumOptions = {
  virtual: {
    only: ['gender', 'role'],
    properties: {
      gender: 'genders',
      role: 'roleValues'
    }
  }
};

//...
user.genders
=> ['MALE', 'FEMALE']

user.roleValues
=> ['admin', 'moderator', 'guest']

Attach

Simply attach enum values to your documents, restricting which paths are included,
and which methods hooked: ['find', 'findOne']

const enumOptions = {
  find: true,
  findOne: true,
  attach: {
  //only: ['gender', 'role'], if omitted, determines properties via keys
    properties: {
      gender: {
        as: 'genderOptions',
        only: ['findOne'] // restricts attaching to 'findOne' method
      },
      role: { // will be attached on 'find' and 'findOne'
        as: 'roles'
      }
    }
  }
};

//...
user.genderOptions
=> ['MALE', 'FEMALE']

user.roles
=> ['admin', 'moderator', 'guest']

Modify

This option directly modifies the enum property to be an object, including the original value and the array of enum options.
In order for this to work correctly, lean() must be called.

const enumOptions = {
  find: true,
  findOne: true
  modify: true
};

user.gender
=> {
  values: ['MALE', 'FEMALE'],
  value: 'MALE'
}

user.role
=> {
  values: ['admin', 'moderator', 'guest'],
  value: 'guest'
}

user.nesting
=> {
  enums: {
    values: ['something', 'wicked', 'this', 'way', 'comes'],
    value: null
  }
}

// You may set modified values as such
user.nesting.enums = 'wicked';

// or
user.nesting.enums.value = 'wicked';



// Alternatively you may restrict the paths and the methods

const enumOptions = {
  modify: {
    only: ['role'],
    on: ['findOne']
  }
}

Caveats

Model.update bypasses any validations and middleware. Utilizing this method with modify will produce undesired results.
Due to the nature of Mongoose objects, modify must be used in conjunction with lean().
      Example: User.findOne({ username }).lean().then()...

Keywords

FAQs

Last updated on 30 Mar 2017

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