DS#findAll
DS#findAll(resourceName[, params][, options])
The "R" in "CRUD", DS#findAll
is for retrieving a collections of items via an adapter. With completely default settings, it's probably going to end up making a GET request to /resource
, where resource
is the name of the resource, e.g. store.findAll('user')
. If you're working with the resource directly you can just do User.findAll()
.
DS#findAll
is asynchronous and returns a promise.
DS#findAll
queries are cached and keyed in the data store by JSON.stringify(params)
. So, by default, DS#findAll
first checks to see if the query has made been before, and if so (by default) will immediately resolve with the items that were returned the first time. JSData behaves in two different ways here depending on the boolean value of options.useFilter
, as explained next:
If useFilter: true
and query is cached, DS#findAll
calls DS#filter
with the params
argument. DS#fitler
applies JSData's query syntax to the collection of items from already cached DS#findAll
query, and resolves the promise immediately with the result. This is handy for catching items that were added to the store (or removed) since the first time the particular query was made.
If useFilter: false
(the default behavior) , DS#findAll' always returns what was received from the configured adapter, as it is. JSData's query syntax params
is ignored and has no effect on the result of cached query here.
If the query hasn't been made before or bypassCache
is true
, DS#findAll
ends up passing the params
and options
arguments to the configured adapter's findAll
method. For an http adapter, both params
and options.params
are merged and serialized to the url query string. You can use options.params
to pass backend specific query parameters to the http adapter. For JSData's query syntax, only params
argument has any meaning (options.params
is ignored here), and even then, it only matters if useFilter
is true
. When the adapter responds, cacheResponse: true
will inject the result into the store.
bypassCache: true
will ignore the cached query and force the specified collection to again be loaded via an adapter.
cacheResponse: false
will cause the result to not be injected in the store.
If the result is to be injected into the store, the options
argument will also be passed to DS#inject
when it is called.
You can call
DS#findAll
multiple ways
DS#findAll(resourceName[, params][, options])
Resource#findAll([params][, options])
- Where Resource was created byDS#defineResource
Argument | Type | Description |
---|---|---|
resourceName | string | The name of the resource to use. Unnecessary if calling findAll directly on a Resource. |
params (optional) | object | Query parameters for selecting items from the collection. This is your "where" clause, limit, offset, sort, etc. Default: {} . See Query Syntax. |
options (optional) | object | Configuration options. Settings are inherited from Resource and Global defaults. Will be passed through to the adapter's findAll method and DS#inject , if they are called. |
options.adapter | string | The name of a registered adapter to use. |
options.bypassCache | boolean | Bypass the cache and force the delegation to the adapter. |
options.maxAge | integer | Bypass the cached responses older than maxAge milliseconds. |
options.cacheResponse | boolean | Inject the resulting item into the store. |
options.useFilter | boolean | |
options.findAllStrategy | string | See findAllStrategy. Defaults to strategy. |
options.findAllFallbackAdapters | array (of strings) | See findAllFallbackAdapters. Defaults to fallbackAdapters. |
options.strategy | string | See strategy. |
options.fallbackAdapters | array (of strings) | See fallbackAdapters. |
Because options is passed through to an adapter's findAll method or DS#inject , then the options for those methods are valid options here as well. |
Examples
// find all comments
Comment.findAll().then(function (comments) {
// this will immediately resolve because the query is cached
return Comment.findAll();
}).then(function (comments) {
// find all comments of user 1
return Comment.findAll({ userId: 1 });
}).then(function (comments) {
// this will immediately resolve because the query is cached
return Comment.findAll({ userId: 1 });
}).then(function (comments) {
// force a new request
return Comment.findAll({ userId: 1 }, { bypassCache: true });
}).then(function (comments) {
// make a request if one isn't cached
// then resolve with the result of "Comment.filter({ userId: 1 })"
return Comment.findAll({ userId: 1 }, { useFilter: true });
}).then(function (comments) {
assert.deepEqual(comments, Comment.filter({ userId: 1 }));
});
// Find all users where age is greater than 30
User.findAll({
where: {
age: {
'>': 30
}
}
}).then(function (users) {
// users[0].age; 55 // etc., etc.
});
// Find all users where age is greater than 30
// but grab "page 3" and orderBy age descending
User.findAll({
where: {
age: {
'>': 30
}
},
limit: 10,
offset: 20,
orderBy: [
['age', 'DESC']
]
}).then(function (users) {
// ...
});
// Load relations as well.
// This does NOT WORK for the http adapter unless you specifically
// update your backend understand a "with" querystring parameter
// The "with" option works for all other adapters, assuming you've
// setup the relations correctly and your data supports them.
Post.findAll({
/* also could do:
where: {
authorId: {
'==': 78
}
},*/
authorId: 78, // where authorId is 78
limit: 4,
offset: 8,
orderBy: [
['created_at', 'DESC']
]
}, { 'with': ['comment', 'comment.user'] }).then(function (posts) {
posts[0].comments[0].user; // { ... }
posts[0].authorId; // 78
});
Need help?
Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.
Updated less than a minute ago