Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

bradynpoulsen/waterline-params

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

waterline-params

waterline-params is designed to give you rails-like parameter helpers to secure your models.

Based on mongoose-params by Bradyn Poulsen.

Installation

$ npm install --save waterline-params

Synopsis

var Waterline = require('waterline');

var User = Waterline.Collection.extend({
  attributes: {
    name: {
      type: 'string'
    },
    email: {
      type: 'string',
      required: true
    },
    password: {
      type: 'string',
      required: true
    }
  }
});

require('waterline-params')(User, {
  permitted: 'name email'
});

Methods

#only(params)

Return only params properties for the document

console.log(bob.only('name', 'email')); // {name: 'Bob', email: '[email protected]'}

#except(params)

Return all properties except params for the document

console.log(bob.except('password')); // {name: 'Bob', email: '[email protected]'}

#assign(data, [override])

Filter and apply changes to the document

bob.assign({
  name: 'Bob Perry',
  password: 'new password'
});
console.log(bob); // {name: 'Bob Perry', email: '[email protected]', password: 'still the old password'}

#merge(data, [override])

Same as #assign but returns a copy of the document that the changes were made to

#safeUpdate(data, [override], [done])

Update the document using only permitted properties. Use override to set properties not in permitted.

bob.safeUpdate({
  email: '[email protected]',
  password: 'new password'
},function(err, updatedBob){
  console.log(updatedBob); // {name: 'Bob', email: '[email protected]', password: 'still the old password'}
});

Static Methods

#only(object, [callback], [thisArg])

See Lodash: _.pick.

#except(object, [callback], [thisArg])

See Lodash: _.omit.

#safeCreate(data, [override], [done])

Create document(s) using only permitted properties. Use override to set properties not in permitted.

user.safeCreate({
  name: 'Bob',
  email: '[email protected]',
  password: 'this password'
}, function(err, bob){
  console.log(err); // Error: `password` cannot be blank
});

// versus

user.safeCreate({
  name: 'Bob',
  email: '[email protected]'
}, {
  password: 'this password'
}, function(err, bob){
  console.log(bob); // {name: 'Bob', email: '[email protected]', password: 'this password'}
});

Configuration

permitted: {String|Array<String>}

String or Array<String> that contains the permitted parameters

These both give the same result:

var params = require('waterline-params');

params(Thing, {
  permitted: 'title description details'
});

params(Thing, {
  permitted: ['title', 'description', 'details']
});

License

The MIT License (MIT)

Copyright (c) 2015 Bradyn Poulsen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published