Class DS.Store

The store contains all of the data for records loaded from the server.
It is also responsible for creating instances of DS.Model that wrap
the individual data for a record, so that they can be bound to in your
Handlebars templates.

Define your application's store like this:

app/services/store.js

1
2
3
4

import DS from'ember-data';
exportdefault DS.Store.extend({
});

Most Ember.js applications will only have a single DS.Store that is
automatically created by their Ember.Application.

You can retrieve models from the store in several ways. To retrieve a record
for a specific id, use DS.Store's findRecord() method:

1
2

store.findRecord('person', 123).then(function(person) {
});

By default, the store will talk to your backend using a standard
REST mechanism. You can customize how the store talks to your
backend by specifying a custom adapter:

app/adapters/application.js

1
2
3
4

import DS from'ember-data';
exportdefault DS.Adapter.extend({
});

You can learn more about writing a custom adapter by reading the DS.Adapter
documentation.

Store createRecord() vs. push() vs. pushPayload()

The store provides multiple ways to create new record objects. They have
some subtle differences in their use which are detailed below:

createRecord is used for creating new
records on the client side. This will return a new record in the
created.uncommitted state. In order to persist this record to the
backend you will need to call record.save().

push is used to notify Ember Data's store of new or
updated records that exist in the backend. This will return a record
in the loaded.saved state. The primary use-case for store#push is
to notify Ember Data about record updates (full or partial) that happen
outside of the normal adapter methods (for example
SSE or Web
Sockets).

pushPayload is a convenience wrapper for
store#push that will deserialize payloads if the
Serializer implements a pushPayload method.

Note: When creating a new record using any of the above methods
Ember Data will update DS.RecordArrays such as those returned by
store#peekAll() or store#findAll(). This means any
data bindings or computed properties that depend on the RecordArray
will automatically be synced to include the new or updated record
values.

findAll
(modelName, options)
Promise

findAll asks the adapter's findAll method to find the records for the
given type, and returns a promise which will resolve with all records of
this type present in the store, even if the adapter only returns a subset
of them.

If no reload is indicated via the abovementioned ways, then the promise
immediately resolves with all the records currently loaded in the store.

Background Reloading

Optionally, if adapter.shouldBackgroundReloadAll evaluates to true,
then a background reload is started. Once this resolves, the array with
which the promise resolves, is updated automatically so it contains all the
records in the store:

See peekAll to get an array of current records in the
store, without waiting until a reload is finished.

Retrieving Related Model Records

If you use an adapter such as Ember's default
JSONAPIAdapter
that supports the JSON API specification and if your server
endpoint supports the use of an
'include' query parameter,
you can use findAll() to automatically retrieve additional records related to
those requested by supplying an include parameter in the options object.

For example, given a post model that has a hasMany relationship with a comment
model, when we retrieve all of the post records we can have the server also return
all of the posts' comments in the same request:

Multiple relationships can be requested using an include parameter consisting of a
comma-separated list (without white-space) while nested relationships can be specified
using a dot-separated sequence of relationship names. So to request both the posts'
comments and the authors of those comments the request would look like this:

If the record is not yet available, the store will ask the adapter's find
method to find the necessary data. If the record is already present in the
store, it depends on the reload behavior when the returned promise
resolves.

Preloading

You can optionally preload specific attributes and relationships that you know of
by passing them via the passed options.

For example, if your Ember route looks like /posts/1/comments/2 and your API route
for the comment also looks like /posts/1/comments/2 if you want to fetch the comment
without fetching the post you can pass in the post to the findRecord call:

1

store.findRecord('comment', 2, { preload: { post: 1 } });

If you have access to the post model you can also pass the model itself:

Reloading

The reload behavior is configured either via the passed options hash or
the result of the adapter's shouldReloadRecord.

If { reload: true } is passed or adapter.shouldReloadRecord evaluates
to true, then the returned promise resolves once the adapter returns
data, regardless if the requested record is already in the store:

Retrieving Related Model Records

If you use an adapter such as Ember's default
JSONAPIAdapter
that supports the JSON API specification and if your server
endpoint supports the use of an
'include' query parameter,
you can use findRecord() to automatically retrieve additional records related to
the one you request by supplying an include parameter in the options object.

For example, given a post model that has a hasMany relationship with a comment
model, when we retrieve a specific post we can have the server also return that post's
comments in the same request:

In this case, the post's comments would then be available in your template as
model.comments.

Multiple relationships can be requested using an include parameter consisting of a
comma-separated list (without white-space) while nested relationships can be specified
using a dot-separated sequence of relationship names. So to request both the post's
comments and the authors of those comments the request would look like this:

hasRecordForId
(modelName, id)
Boolean

This method returns true if a record for a given modelName and id is already
loaded in the store. Use this function to know beforehand if a findRecord()
will result in a request or that it will be a cache hit.

peekAll
(modelName)
DS.RecordArray

This method returns a filtered array that contains all of the
known records for a given type in the store.

Note that because it's just a filter, the result will contain any
locally created records of the type, however, it will not make a
request to the backend to retrieve additional records. If you
would like to request all the records from the backend please use
store.findAll.

Also note that multiple calls to peekAll for a given type will always
return the same RecordArray.

Example

1

let localPosts = store.peekAll('post');

peekRecord
(modelName, id)
DS.Model|null

This method will synchronously return the record if it is available in the store,
otherwise it will return null. A record is available if it has been fetched earlier, or
pushed manually into the store.

See findRecord if you would like to request this record from the backend.

If you're streaming data or implementing an adapter, make sure
that you have converted the incoming data into this form. The
store's normalize method is a convenience
helper for converting a json payload into the form Ember Data
expects.

1

store.push(store.normalize('person', data));

This method can be used both to push in brand new
records, as well as to update existing records.

pushPayload
(modelName, inputPayload)

Optionally, a model type used to determine which serializer will be used

inputPayload

Object

Push some raw data into the store.

This method can be used both to push in brand new
records, as well as to update existing records. You
can push in more than one type of object at once.
All objects should be in the format expected by the
serializer.

Note: the primary use case for store.queryRecord is when a single record
is queried and the id is not known beforehand. In all other cases
store.query and using the first item of the array is likely the preferred
way: