Model

bookshelf-advanced-serialization. Model

new Model(attributes, optionsopt)

The usual Bookshelf model constructor, but it accepts an accessor option which will be set at this._accessor and passed to roleDeterminer() when toJSON is called.

Parameters:
Name Type Attributes Description
attributes Object
options Object <optional>
Properties
Name Type Description
accessor *

The accessor value to set

Source:

Members

rolesToVisibleProperties :Object.<string, Array.<string>>

An object that maps a role name to the array of properties of the model that should be visible to that role type. Reflects a whitelisting approach to serialization, as only properties listed in the array may be present in the serialization result. Not all properties listed in the array will necessarily be in the serialization result, however, if contextSpecificVisibleProperties is also being used.

Type:
  • Object.<string, Array.<string>>
Source:

Methods

roleDeterminer(accessor) → {string|Promise.<string>}

A function called by toJSON with this._accessor as its argument, to determine the accessor's role. This role is then looked up in rolesToVisibleProperties to identify the visible properties of the model. May return a promise--this allows asynchronously determining the role.

Parameters:
Name Type Description
accessor *

this._accessor

Source:
Returns:

The role or a promise resolving to the role.

Type
string | Promise.<string>

setAccessor(accessor)

Sets this._accessor. An alternative to setting the accessor via the model constructor. Useful if you already have a model, on which you want to set an accessor.

Parameters:
Name Type Description
accessor *

The accessor value to set

Source:

toJSON(optionsopt) → {Promise.<(Object|undefined)>}

The method for serializing a model.

Note that this method mutates models, first by loading relations per options.ensureRelationsLoaded and then by removing relations that are not among the model's visible properties (the latter improves serialization performance and helps to avoid infinite-looping / cycling of serialization). Such mutating behavior is, admittedly, not obvious from the method's name of toJSON alone. In using this plugin, you should therefore think of toJSON not as merely converting the existing model to a serialized form, but as transforming it according to your specifications, and then converting it to serialized form.

A model with no visible properties -- that is, where the list of properties that should be visible to the caller evaluates to empty -- will be serialized as undefined, and all such models in collections will be removed from the corresponding arrays in the serialization result. (N.B. A model with no visible properties is not the same as an empty model--the latter is a model with no attributes and no relations. An empty model that exists as a relation of another model is assumed to be due to this Bookshelf bug and is serialized as null. An empty model that exists otherwise (e.g. standalone, or in a standalone collection, or in a collection that exists as the relation of another model) is serialized as {}.)

Note also that the behavior of this method diverges from the standard Bookshelf behavior in that it does not remove null values for relations from the final serialized result.

Parameters:
Name Type Attributes Description
options Object <optional>

An optional object specifying how to customize the serialization result.

Properties
Name Type Attributes Description
contextSpecificVisibleProperties Object <optional>

An optional object specifying what properties of a model should be visible given the application context in which toJSON is being invoked. The object should be indexed first by table name, with values that are either (a) an array of visible property names; or (b) an object indexed by the possible context designations (i.e. the return values of options.contextDesignator), with values that are an array of visible property names. This object, potentially in combination with options.contextDesignator, is your mechanism for preventing infinite-looping / cycling of serialization, should your use case present that possibility.

ensureRelationsLoaded Object <optional>

An optional object analogous in form to options.contextSpecificVisibleProperties but whose values are arrays containing the relation names that it will be ensured are loaded on a model before serializing. Such relations will be loaded on the model if they are not already present.

contextDesignator function <optional>

A function which returns the context designation describing the context in which toJSON is being called. Only required if options.contextSpecificVisibleProperties or options.ensureRelationsLoaded index lists by context designation. May return a promise--this supports asynchronously determining the context designation. By default, the contextDesignator will be called with this.tableName, this._accessedAsRelationChain, and this.id as arguments. You may optionally customize these arguments by passing options.getEvaluatorArguments to the plugin.

accessor Object <optional>

A value representing who the model is being accessed by / whom it will be serialized for. Pass the accessor as an option here as an alternative to setting the accessor when the model is instantiated (see constructor) or using .setAccessor(). A value provided for this option takes precedence over an accessor value set on the model or any related models.

shallow boolean <optional>

Same as the standard Bookshelf option.

omitPivot boolean <optional>

Same as the standard Bookshelf option.

omitNew boolean <optional>

Same as the standard Bookshelf option.

Source:
Returns:

A promise resolving to the plain javascript object representing the model.

Type
Promise.<(Object|undefined)>
Documentation generated by JSDoc 3.4.1 on Mon Apr 17 2017 23:01:37 GMT-0400 (EDT) using a modified version of the Minami theme.