-
Notifications
You must be signed in to change notification settings - Fork 25
Retrieving
The most simple query is to retrieve all objects of a certain kind.
Artist.get()
Because this is not a database query but an API query, results of multiple objects are always paginated. Artist.get()
retrieves the first page. Other pages are retrieved like this:
Artist.get(3)
...which retrieves the third page. The default page size of the underlying API can be overridden; how this is done is explained here.
A single object can be retrieved either by ID
Artist.find(123)
Which can also be a string
Artist.find('tomaskalnoky')
Or a single object is retrieved by taking only the first result of some query
Artist.first()
A filter can be applied on a property of the object you are trying to retrieve.
Artist
.where('gender', 'm')
.get()
The following query would return the first page of artists ordered by their birthdate in ascending order.
Artist
.orderBy('birthDate', 'asc')
.get()
We can also write .orderBy('birthDate')
; it would yield the same result as ascending order is the default. To use descending order, write
Artist
.orderBy('birthDate', 'desc')
.get()
It could be that you are only interested in the 10 youngest Artists
. Limit your number of query results by writing
Artist
.orderBy('birthDate', 'desc')
.limit(10)
.get()
The limit
clause also works without the orderBy
clause, of course.
From the response object, queried objects can be retrieved like this.
Artist
.get()
.then(function (PluralResponse response) {
let artists: Artist[] = response.getData();
});
One can also directly access included models with getIncluded()
as follows follows
Artist
.get()
.include('albums')
.then(function (PluralResponse response) {
let albums: Album[] = response.getIncluded();
});
Note that getIncluded()
returns all included models, even when there are multiple includes. So in
Artist
.get()
.include('albums.songs')
.then(function (PluralResponse response) {
let albumsAndSongs: Model[] = response.getIncluded();
});
the array albumsAndSongs
contains both Album
and Song
objects.