0.3.21 • Published 7 years ago

waterline-jsonapi v0.3.21

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

waterline-jsonapi

Build Status Coverage Status Code Climate Known Vulnerabilities

Create JSON API compliant payloads from Waterline blueprints and query results and get a promise in return.

npm i waterline-jsonapi --save

Usage

You need only your query results and your collection to get started. Assuming the example user and pet collections.

const Waterline_JSONAPI = require("waterline-jsonapi")

// The query
User
  .find()
  .populate("pets")

  // Create a JSON API payload from the results.
  .then(user_and_pets => {
    const jsonapi = new Waterline_JSONAPI(user_and_pets, User).generate()
    reply(jsonapi)
  })

// Or with a Promise

User
  .find()
  .populate("pets")

  // Create a JSON API payload from the results.
  .then(user_and_pets => {
    new Waterline_JSONAPI(user_and_pets, User).generate(true) // Note: the true here.

      // Reply with your data.
      .then(payload => reply(payload))

      // If there was an unlikely error, you can catch here.
      .catch(error => reply(error))
  })

The above will reply with a valid JSONAPI payload, the same works by passing in an Error or an object with is_error: true.

Links

Links are retrieved from your collections if they have any of the following functions.

All links functions are passed a single resource object and a meta object passed into the constructor.

get_self_link(resource, meta)
get_next_link(resource, meta)
get_last_link(resource, meta)
get_related_link(resource, meta)

Inside of the meta object is a context variable passed in by this library, it is a string and one of these values:

"related"
"single"
"list"

For you to return the appropriate link.

Meta

You can define metadata on your collection in the get_meta function. This function takes no arguments and should return an object.

API

This version is a 100% rebuild from the ground up and all previous API methods have been deprecated and removed as they were invalid.

MIT License © New World Code

0.3.21

7 years ago

0.3.20

7 years ago

0.3.19

8 years ago

0.3.18

8 years ago

0.3.17

8 years ago

0.3.16

8 years ago

0.3.15

8 years ago

0.3.14

8 years ago

0.3.13

8 years ago

0.3.12

8 years ago

0.3.11

8 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago