项目作者: oscaroox

项目描述 :
Objectionjs插件到白名单/黑名单模型属性
高级语言: TypeScript
项目地址: git://github.com/oscaroox/objection-visibility.git
创建时间: 2017-06-15T21:27:15Z
项目社区:https://github.com/oscaroox/objection-visibility

开源协议:

下载


workflow

objection-visibility

This plugin adds the ability to whitelist or blacklists model properties.

Installation

  1. npm install objection-visibility

Usage

You can enable this plugin by either setting the static property hidden or visible to your model.

It is possible to have a model which have both properties defined, note that the visible method is called first and the hidden method second.

Blacklist properties

To enable blacklisting on your model add the static property hidden on your model and return an array with the fields you want to blacklist.

The listed fields are gone after being serialized to json

  1. const Model = require('objection').Model
  2. const visibilityPlugin = require('objection-visibility').default;
  3. class User extends visibilityPlugin(Model) {
  4. static get hidden() {
  5. return ['hashed_password'];
  6. }
  7. }

Whitelist properties

To enable whitelisting on your model add a static property visible on your model and return an array with the fields you want to be whitelisted

The listed fields will be the only properties available after being serialized to json

  1. const Model = require('objection').Model
  2. const visibilityPlugin = require('objection-visibility').default;
  3. class User extends visibilityPlugin(Model) {
  4. static get visible() {
  5. return ['firstName', 'id']
  6. }
  7. }

Using with multiple models

Can be used on a base model and have it readily available on all your models

Models that dont have the static properties visible or hidden will remain untouched.

  1. // base.js
  2. class BaseModel extends visibilityPlugin(Model) {}
  3. // post.js
  4. class Post extends BaseModel {
  5. static get visible () {
  6. return ['description', 'title']
  7. }
  8. }
  9. // user.js
  10. class User extends BaseModel {
  11. static get hidden () {
  12. return ['hashedPassword']
  13. }
  14. }