Drop-in serializer support for Express/Connect Node apps.
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.
Take the supplied payload of record(s) or error(s) and the supplied options and return a rendered a JSON response object.
the adapter to use when interrogating the payload models
the JSON response object
The list of attribute names that should be serialized. Attributes not included in this list will be omitted from the final rendered payload.
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:
records
: embed all related records (1-n relationships)record
: embed the related record (1-1 relationships)ids
: include only the ids of related records (1-n relationships)id
: include only the id of the related record (1-1 relationships)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.
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:
a model containing attributes to be serialized
an object with serialized attributes from the supplied record
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.
the key that should be used for this attribute name
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.
the value that should be rendered
, key, record
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:
config
- the config supplied in the relationships object (see
{@link Serializer#relationships})data
- contains any data that the adapter was able to return.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()
.
the record to extract relationships from
the serialized relationships