
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
bookshelf-entity
Advanced tools
Bookshelf plugin for controlling/formatting model output using json-entity
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.
npm install bookshelf-entity --save
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",
}
*/
FAQs
Bookshelf plugin for controlling/formatting model output using json-entity
We found that bookshelf-entity demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.