Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bookshelf-mask

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-mask

Mask Bookshelf.js models with json-mask

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

bookshelf-mask

This Bookshelf.js plugin enables you to define masks on your models and serialize to JSON based on its fields using the json-mask API.

Status

npm version node version build status coverage status

Installation

Install the package via npm:

$ npm install --save bookshelf-mask

Usage

Require and register the bookshelf-mask plugin:

var bookshelf = require('bookshelf')(knex);
var mask = require('bookshelf-mask');

bookshelf.plugin(mask);

Define masks on your models with masks class property:

var Author = bookshelf.Model.extend({
  posts: {
    return this.hasMany(Post);
  },
  tableName: 'Author'
}, {
  masks: {
    custom: 'id,name'
  }
});

If you're using ES6 class syntax, define masks as static property:

class Author extends bookshelf.Model {
  get tableName() {
    return 'Author';
  }

  posts() {
    return this.hasMany(Post);
  }

  static masks = {
    custom: 'id,name'
  }
}

Use the mask method to serialize the registered masks or pass the fields directly:

Author
  .forge({ name: 'foo' })
  .fetch({ withRelated: 'posts' })
  .then(function(model) {
    console.log(model.mask('custom'));
    // => { id: 1, name: 'foo' }
    console.log(model.mask('name,posts(title,body)'));
    // => { name: 'foo', posts: [{ title: 'biz', body: 'baz' }, { title: 'qux', body: 'qix' }]}
  });

The mask method can be applied to collections and the same options accepted in toJSON can also be provided.

Contributing

Contributions are welcome and greatly appreciated, so feel free to fork this repository and submit pull requests.

Setting up

  • Fork and clone the bookshelf-mask repository.
  • Duplicate test/knexfile.js.dist and and update it to your needs.
  • Make sure all the tests pass:
$ npm test

Linting

bookshelf-mask enforces linting using ESLint with the Seegno-flavored ESLint config. We recommend you to install an eslint plugin in your editor of choice, although you can run the linter anytime with:

$ eslint src test

Pull Request

Please follow these advices to simplify the pull request workflow:

  • If you add or enhance functionality, an update of README.md usage section should be part of the PR.
  • If your PR fixes a bug you should include tests that at least fail before your code changes and pass after.
  • Keep your branch rebased and fix all conflicts before submitting.
  • Make sure Travis build status is ok.

License

MIT

Keywords

FAQs

Package last updated on 26 Oct 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc