The interface for read operations is a simpler version of the
ActiveRecord Query Interface.
Instead of generating SQL, we’ll be generating JSONAPI requests.
Basic Finders
Execute queries with .all(), find(), or .first():
Typescript
Javascript
GET /posts
Typescript
Javascript
GET /posts/123
Typescript
Javascript
GET /posts?page[size]=1
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:
Typescript
Javascript
/posts?sort=-name&filter[important]=true
/posts?sort=-name&filter[important]=false
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:
Typescript
Javascript
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.
Typescript
Javascript
/posts
Hopefully you’re running in an environment that supports
ES7’s Async/Await. This makes things even easier: