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

bookshelf-entity

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-entity

Bookshelf plugin for controlling/formatting model output using json-entity

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

bookshelf-entity

NPM Version Build Status Dependency Status Dev Dependency Status

Bookshelf plugin for controlling and formatting model serialization/output using json-entity. This plugin adds present/render (synonymous) methods to models and collections, both of which require an Entity to serialize the model. Since Entities only allow property whitelisting, you have very clear and detailed control over exactly which properties are exposed from your models. These methods also attempt to load any missing relations on your models to keep your model representations aligned. Entities also have a wealth of other formatting/modification options so you can make sure your API responses are perfect every time.

Installation

npm install bookshelf-entity --save

Usage

Apply the plugin:

const entity = require('bookshelf-entity');

bookshelf.plugin(entity);

Define an Entity:

// You can extend from bookshelf.Entity or install json-entity and use it directly
const UserEntity = bookshelf.Entity.extend({
  id: true,
  firstName: true,
  lastName: true,
  fullName(user) {
    return `${user.firstName} ${user.lastName}`;
  },
  location: { as: 'hometown', if: (user, options) => options.includeLocation },
  address: { using: AddressEntity }, // AddressEntity not shown
});

Specify Entity when calling present or render:

const User = Bookshelf.Model.extend({
  tableName: 'users',

  address() {
    return this.hasOne(Address);
  },
});

const user = User.forge({
  id: 1,
  firstName: 'Josh',
  lastName: 'Swan',
  location: 'San Francisco, CA',
  // Address not loaded
});

user.present({ entity: UserEntity }).then((obj) => {
  /*
      {
        id: 1,
        firstName: "Josh",
        lastName: "Swan",
        fullName: "Josh Swan",
        address: {
          line1: "123 Something St",
          city: "San Francisco",
          state: "CA",
          zip: "94104"
        }
      }
   */
});

user.render({ entity: UserEntity }, { includeLocation: true }).then((obj) => {
  /*
      {
        id: 1,
        firstName: "Josh",
        lastName: "Swan",
        fullName: "Josh Swan",
        hometown: "San Francisco, CA",
        address: {
          line1: "123 Something St",
          city: "San Francisco",
          state: "CA",
          zip: "94104"
        }
      }
   */
});

Optional: You can also specify a defaultEntity on your model as a fallback when present/render is invoked without specifying an Entity:

const User = bookshelf.Model.extend({
  defaultEntity: UserEntity,
});

For a quick synchronous representation, you can also call the represent method directly (it is called by present/render after loading any missing relations):

const user = User.forge({
  id: 1,
  firstName: 'Josh',
  lastName: 'Swan',
  location: 'San Francisco, CA',
});

user.represent(UserEntity, options);
/*
    {
      id: 1,
      firstName: "Josh",
      lastName: "Swan",
      fullName: "Josh Swan",
    }
 */

See json-entity for all available options

Keywords

FAQs

Package last updated on 09 Mar 2018

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