Giter Site home page Giter Site logo

mongoose-mass-assignment's Introduction

mongoose-mass-assign

This plugin provides two functions called #massAssign to your models. One static and one instance. You can use these to protect specific fields from web input. The plugin adds the schematype option 'protect' so you don't have to write any filtering code.

You can also get direct access to the filtering mechanism through the static massUpdate.

Install

npm install mongoose-mass-assign

Current version 0.4.0

Usage

Defining:

var massAssign = require('mongoose-mass-assign');

var UserSchema = new mongoose.Schema({
  name      : String,
  admin     : { type: Boolean, protect: true, default: false },
  verified  : { type: Boolean, protect: true, default: false }
});

UserSchema.plugin(massAssign);

var User = mongoose.model('User', UserSchema);

Using:

/** Static method, useful for creation **/

var user = User.massAssign({
  name: 'bhelx',
  admin: true
});

// user => { name: 'bhelx', admin: false, verified: false }

/** Instance method, useful for updating  **/
var user = new User;

user.massAssign({
  name: 'bhelx',
  admin: true
});

// user => { name: 'bhelx', admin: false, verified: false }

/** Static massUpdate method **/
var input = { name: 'bhelx', admin: 'true' };

User.update({ '_id': someId }, { $set: User.massUpdate(input) }, console.log);

For more details see test.js

Testing

npm install
make test

mongoose-mass-assignment's People

Contributors

andrewgrachov avatar bhelx avatar jwietelmann avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.