dustcover
JSONAPI plugin for the bookshelfjs ORM
Features
The plan for dustcover is to provide a full featured jsonapi implementation for bookshelf with little to no configuration. It's very early (alpha) days at this point and the only thing thats even partially supported is jsonapi serialization. Stay tuned. Chip in if this interests you, PRs and collaboration welcome.
Installation
npm install dustcover --save
Usage
Require dependencies
const Bookshelf = const Knex = const dustcover =
Setup bookshelf
const bookshelf = const options = // Set the host that will be used in links during serialization // default: host omitted from links host: 'http://localhost:3000' // Specify whether models should be serialized by default, or // require opting in to serialization // when true, models will need to opt in to serialization by // defining a key `jsonapi` and setting it to true. (See below) // default: false optIn: falsebookshelf
Setup models
const Cat = bookshelfModel
Serializing models
dustcover overrides the serialize methods of models and collections such
that when you call collection.toJSON()
or model.toJSON()
you will get
jsonapi compatible data instead of just the usual attributes hash.
will output something like:
data: id: '1' type: 'cats' attributes: name: 'Boris' links: self: 'http://localhost:3000/cats/1' relationships: owner: links: related: 'http://localhost:3000/cats/1/owner' mice: links: related: 'http://localhost:3000/cats/1/mice'
toJSON
You can override a couple things when you call Change the type field like so:
cat
Remove or adjust relationship serialization like so:
cat
Force jsonapi serialization or non serialization like so: (see above for additional details)
cat