The interface for read operations is a simpler version of the
ActiveRecord Query Interface.
Instead of generating SQL, we’ll be generating JSONAPI requests.
Execute queries with .all(), find(), or .first():
Composable Queries with Scopes
The beauty of ORMs is their ability to compose queries. We’ll be doing
this by chaining together Scopes (query fragments). All of the methods
you see on this page can be chained together - the request will not fire
until the chain ends with all(), first(), or find. Example:
In practice, you’ll probably have some scopes you want to re-use across
different contexts. A best practice is to store these scopes as class
methods (static methods) in the model:
The meta information of the
JSONAPI response is available as a POJO on the response:
Promises and Async/Await
The result of all(), first() or find is a Promise. The promise will resolve to a Response object.
A Response object has three keys - data, meta, and raw. data - the one
you’ll be using the most - will be a Model instance (or array of
Model) instances. meta will be the Meta Information returned by the API (mostly used for statistics in our case). raw is only used to introspect the raw response document.
Hopefully you’re running in an environment that supports
ES7’s Async/Await. This makes things even easier: