Skip to content

Plugin: Visibility

David Furlong edited this page Mar 7, 2018 · 7 revisions

If visible or hidden attributes are defined on the model as arrays, the specified fields are hidden / shown as appropriate when calling toJSON.

See #187

var Knex = require('knex');
var Bookshelf = require('bookshelf')
  , Db = {}
  , Orm
  , config
  ;

config = {
  client: 'postgresql',
  connection: {
    host     : 'example.com',
    port     : '5432',
    user     : 'someuser',
    password : 'somesecret',
    database : 'dev',
    charset  : 'UTF8_GENERAL_CI'
  }
}
Orm = Bookshelf(Knex(config));

Orm.plugin('visibility');

Db.Users = Orm.Model.extend({
  tableName: 'users'
, hidden: ['password', 'salt', 'secret', 'hashtype']
});

Using visible will whitelist and only those attributes will serialize. Using hidden will blacklist only those fields will be hidden. If both are specified then it will be whitelist by default, but hidden will override fields in visible.

Using toJSON({ visibility: false }) will disable the visibility plugin for this toJSON call

Clone this wiki locally