mongoose-shorthand

2.0.1 • Public • Published

Mongoose Shorthand

Allows Mongoose models and schema to be created with less repetitive code.

Basic Usage

const schema = require('mongoose-shorthand');
const shorthand = schema();

shorthand('User', {
  schema: {
    ...
  }
})

Advanced Usage

Providing context

In certain situations it may be valuable to pass context to each schema as it is parsed. When setting up the shorthand parser, a parameter may be passed with the global context:

const schema = require('mongoose-shorthand');
const shorthand = schema({ hashRounds: 10 });

A function can be provided as the schema which will receive this context. In the example below, the hashRounds configuration value is used from the provided context to determine the bcrypt behavior:

module.exports = context => ({
  middleware: {
    preSave(next) {
      if (!this.isModified('password')) return next();
      bcrypt.hash(this.password, context.hashRounds, (err, hash) => {
        if (err) return next(err);
        this.password = hash;
        next();
      });
    },
  },
  schema: {
    username: String,
    password: String,
  },
});

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-shorthand

Weekly Downloads

315

Version

2.0.1

License

MIT

Unpacked Size

5.91 kB

Total Files

9

Last publish

Collaborators

  • ignigena