Skip to content

popodidi/bookshelf-plugin-mode

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

bookshelf-plugin-mode

NPM version Build Status

bookshelf-plugin-mode is inspired by visibility plugin, providing functionality to specify different modes with corresponding visible/hidden fields of model.

install

npm install --save bookshelf-plugin-mode

usage

add plugin

var mode = require('bookshelf-plugin-mode');
bookshelf.plugin(mode);

define model modes

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']
    }
  }
});

use mode(...) in query

new User().mode('info').fetch();
// {name: "username", tel: "user_tel", email: "user_email}
new User().mode('test').fetch();
// {name: "username, tel: "user_tel"}

use mode in related

new User().fetch({
   originalOptions: {
     require: true,
     withRelated: ['relatedTable', 'photo']
   },
    modeOptions: {
     'relatedTable': 'mode',
     'photo': 'info'
   }
});

About

bookshelf-plugin-mode is inspired by visibility plugin, providing functionality to specify different modes with corresponding visible/hidden fields of model.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published