blackburn

Drop-in serializer support for Express/Connect Node apps.

Serializer Defined in lib/serializer.js

Serializers allow you to customize what data is returned the response, and apply simple transformations to it. They allow you to decouple what data is sent, with how that data is structured / rendered.

render (payloadoptions)

Take the supplied payload of record(s) or error(s) and the supplied options and return a rendered a JSON response object.

Arguments

Returns

(Object)

the JSON response object

attributes ()

The list of attribute names that should be serialized. Attributes not included in this list will be omitted from the final rendered payload.

relationships ()

An object with configuration on how to serialize relationships. Relationships that have no configuration present are omitted from the final rendered payload.

Out of the box, two options are supported:

strategy

It has one of four possible values:

What the embedded records or ids look like is up to each serializer to determine.

type

The model type of the related records.

Specific serializers may also accept additional options in the relationship configuration to customize their response format.

serializeAttributes (recordoptions)

The base Serializer class comes with a few methods defined that take advantage of some basic assumptions. serializeAttributes is one of them. You don't actually have to use these methods at all, but most types of serializers will find them helpful.

serializeAttributes takes a record, and return the attributes that should be rendered. Note that attributes are distinct from relationships. This can serialization customized in several ways:

Arguments

Returns

(Object)

an object with serialized attributes from the supplied record

serializeAttributeName (attributeName)

Take an attribute name and return the serialized key name. Useful for transforming or renaming attributes in the rendered payload, i.e. transforming snake_case to camelCase keys, or vice versa.

The default implementation returns the attribute name unchanged.

Arguments

Returns

(String)

the key that should be used for this attribute name

serializeAttributeValue (valuekeyrecord)

Take an attribute value and return the serialized value. Useful for changing how certain types of values are serialized, i.e. Date objects.

The default implementation returns the attribute's value unchanged.

Arguments

Returns

()

the value that should be rendered

, key, record

serializeRelationships (recordoptions)

The base Serializer class comes with a few methods defined that take advantage of some basic assumptions. serializeRelationships is one of them. You don't actually have to use these methods at all, but most types of serializers will find them helpful.

serializeRelationships takes a record and returns an object whose keys are the relationship names, and values are relationship descriptors containing the following information about the relationship:

It only returns relationships that have relationship config present, similar to how serializeAttributes() only returns attributes present in the Serializer.attributes whitelist.

You can tweak this serialization process by overriding either .serializeRelationshipName() or .serializeRelationshipValue().

Arguments

Returns

(Object)

the serialized relationships