bookshelf-plugin-mode is inspired by visibility plugin, providing functionality to specify different modes with corresponding visible/hidden fields of model.
npm install --save bookshelf-plugin-mode
var mode = require('bookshelf-plugin-mode');
bookshelf.plugin(mode);
const User = bookshelf.Model.extend({
tableName: "user",
idAttribute: "user_id",
serialize: function () {
return {
id: this.get('user_id'),
name: this.get('name'),
tel: this.get('tel'),
email: this.get('email')
}
},
modes: {
info: {
hidden: ['id']
},
test: {
visible: ['name','tel']
}
}
});
new User().mode('info').fetch();
// {name: "username", tel: "user_tel", email: "user_email}
new User().mode('test').fetch();
// {name: "username, tel: "user_tel"}
new User().fetch({
originalOptions: {
require: true,
withRelated: ['relatedTable', 'photo']
},
modeOptions: {
'relatedTable': 'mode',
'photo': 'info'
}
});