feathers-hooks-jsonapify
Feathers hook for outputting data in a JSON-API-compliant way.
Currently works great with Sequelize as an adapter. There are plans to support more adapters in the future.
Installing
Simply run npm install --save feathers-hooks-jsonapify
and you're good to go!
Usage
This hook is intended to use with feathers-rest
, since it'll convert that provider's response to a JSON-API compliant document.
Require the hook:
const jsonapify = ;
Then choose how to implement it.
Tied up to a service
app;
As a global hook
app;
Relationships
Available since: v0.1.4
feathers-hooks-jsonapify
will automatically detect metadata for relationships in the model. It'll create an included
top-level array in the document when the hook is called via find
.
Currently working and tested with
belongsTo
andhasMany
associations. This feature works only with a Sequelize adapter.
Example document for a self-referencing model
Pagination
Available since: v0.1.4
The hook will also detect if hook.result.skip
, hook.result.limit
and hook.result.total
are available as part of the feathers-rest
provider. If available, it'll create first
, prev
, next
and last
links accordingly.
The raw pagination data is moved to a meta
object.
Example document with pagination links
Plain Object Serialization (POS) 🆕
Available since: v0.1.8
Common Object
arrays can also be jsonapified
for any custom service's result
:
Multiple objects
// Sample hook result, with multiple objects, from a `person` custom service.hookresult = firstName: 'Joel' lastName: 'Villarreal' isEnabled: true firstName: 'Alejandro' lastName: 'Bertoldi' isEnabled: false;
JSONAPIfied result:
Single object
// Sample hook result, with a single object in an array, from a `person` custom service.hookresult = firstName: 'Joel' lastName: 'Villarreal' isEnabled: true; // same as:hookresult = firstName: 'Joel' lastName: 'Villarreal' isEnabled: true;
JSONAPIfied result:
Identifier and type mapping
The jsonapify
hook receives an options
object that accepts two settings for POS:
identifierKey
: the name of the property to convert intoid
typeKey
: the name of the property to convert intotype
identifierKey
?
What happens if I don't use The hook's got your back. Using crypto.createHash
, it creates a unique SHA-256 digest using the contents of the object.
typeKey
?
What happens if I don't use The hook will use the service's name (hook.service.options.name
) as each model's type.
TODOs
Check out the issues.
Feel like contributing?
Knock yourself out! Fork the repo and make a PR.
Licence
MIT