#

API Docs for: v3.8.1
Show: Inherited Protected Private Deprecated

# DS.Store Class

Extends Ember.Service
Defined in: `addon/-private/system/store.js:110`
Module: ember-data

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:

    import DS from 'ember-data';

    export default 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:

    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:

    import DS from 'ember-data';

    export default 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.RecordArray`s 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.

- Index
- Methods
- Properties

## Item Index

### Methods

- _buildInternalModel
- _didUpdateAll
- _fetchAll
- _fetchRecord
- _generateId
- _internalModelsFor
- _load
- _reloadRecord
- _removeFromIdMap
- adapterFor
- createRecord
- deleteRecord
- didSaveRecord
- find
- findAll
- findBelongsTo
- findByIds
- findHasMany
- findMany
- findRecord
- flushPendingSave
- getReference
- hasRecordForId
- init
- modelFor
- normalize
- peekAll
- peekRecord
- push
- pushPayload
- query
- queryRecord
- recordForId
- recordWasError
- recordWasInvalid
- scheduleSave
- serializerFor
- setRecordId
- unloadAll
- unloadRecord

### Properties

- adapter
- defaultAdapter

## Methods

### `_buildInternalModel`

(

- `modelName`
- `id`
- `data`

)
InternalModel private

Defined in `addon/-private/system/store.js:2916`

Build a brand new record for a given type, ID, and initial data.

#### Parameters:

- `modelName` String
- `id` String
- `data` Object

#### Returns:

InternalModel:

internal model

### `_didUpdateAll`

(

- `modelName`

)
private

Defined in `addon/-private/system/store.js:2062`

#### Parameters:

- `modelName` String

### `_fetchAll`

(

- `modelName`
- `array`

)
Promise private

Defined in `addon/-private/system/store.js:2021`

#### Parameters:

- `modelName` DS.Model
- `array` DS.RecordArray

#### Returns:

Promise:

promise

### `_fetchRecord`

(

- `internalModel`

)
Promise private

Defined in `addon/-private/system/store.js:860`

This method is called by `findRecord` if it discovers that a particular type/id pair hasn't been loaded yet to kick off a request to the adapter.

#### Parameters:

- `internalModel` InternalModel

  model

#### Returns:

Promise:

promise

### `_generateId`

(

- `modelName`
- `properties`

)
String private

Defined in `addon/-private/system/store.js:404`

If possible, this method asks the adapter to generate an ID for a newly created record.

#### Parameters:

- `modelName` String
- `properties` Object

  from the new record

#### Returns:

String:

if the adapter can generate one, an ID

### `_internalModelsFor`

(

- `modelName`

)
Object private

Defined in `addon/-private/system/store.js:2333`

Returns a map of IDs to client IDs for a given modelName.

#### Parameters:

- `modelName` String

#### Returns:

Object:

recordMap

### `_load`

(

- `data`

)
private

Defined in `addon/-private/system/store.js:2354`

This internal method is used by `push`.

#### Parameters:

- `data` Object

### `_reloadRecord`

(

- `internalModel`
- `options`

)
Promise private

Defined in `addon/-private/system/store.js:1194`

This method is called by the record's `reload` method.

This method calls the adapter's `find` method, which returns a promise. When that promise resolves, `_reloadRecord` will resolve the promise returned by the record's `reload`.

#### Parameters:

- `internalModel` DS.Model
- `options` Object

  optional to include adapterOptions

#### Returns:

Promise:

promise

### `_removeFromIdMap`

(

- `internalModel`

)
private

Defined in `addon/-private/system/store.js:2985`

When a record is destroyed, this un-indexes it and removes it from any record arrays so it can be GCed.

#### Parameters:

- `internalModel` InternalModel

### `adapterFor`

(

- `modelName`

)
public

Defined in `addon/-private/system/store.js:3005`

Returns an instance of the adapter for a given type. For example, `adapterFor('person')` will return an instance of `App.PersonAdapter`.

If no `App.PersonAdapter` is found, this method will look for an `App.ApplicationAdapter` (the default adapter for your entire application).

If no `App.ApplicationAdapter` is found, it will return the value of the `defaultAdapter`.

#### Parameters:

- `modelName` String

#### Returns:

DS.Adapter

### `createRecord`

(

- `modelName`
- `inputProperties`

)
DS.Model

Defined in `addon/-private/system/store.js:331`

Create a new record in the current store. The properties passed to this method are set on the newly created record.

To create a new instance of a `Post`:

    store.createRecord('post', {
      title: 'Ember is awesome!'
    });

To create a new instance of a `Post` that has a relationship with a `User` record:

    let user = this.store.peekRecord('user', 1);
    store.createRecord('post', {
      title: 'Ember is awesome!',
      user: user
    });

#### Parameters:

- `modelName` String
- `inputProperties` Object

  a hash of properties to set on the newly created record.

#### Returns:

DS.Model:

record

### `deleteRecord`

(

- `record`

)

Defined in `addon/-private/system/store.js:429`

For symmetry, a record can be deleted via the store.

Example

    let post = store.createRecord('post', {
      title: 'Ember is awesome!'
    });

    store.deleteRecord(post);

#### Parameters:

- `record` DS.Model

### `didSaveRecord`

(

- `internalModel`
- `data`

)
private

Defined in `addon/-private/system/store.js:2208`

This method is called once the promise returned by an adapter's `createRecord`, `updateRecord` or `deleteRecord` is resolved.

If the data provides a server-generated ID, it will update the record and the store's indexes.

#### Parameters:

- `internalModel` InternalModel

  the in-flight internal model

- `data` Object

  optional data (see above)

### `find`

(

- `modelName`
- `id`
- `options`

)
Promise private

Defined in `addon/-private/system/store.js:478`

#### Parameters:

- `modelName` String
- `id` String | Integer
- `options` Object

#### Returns:

Promise:

promise

### `findAll`

(

- `modelName`
- `options`

)
Promise

Defined in `addon/-private/system/store.js:1810`

Available since 1.13.0

`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.

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findAll('author');
      }
    });

When the returned promise resolves depends on the reload behavior, configured via the passed `options` hash and the result of the adapter's `shouldReloadAll` method.

### Reloading

If `{ reload: true }` is passed or `adapter.shouldReloadAll` evaluates to `true`, then the returned promise resolves once the adapter returns data, regardless if there are already records in the store:

    store.push({
      data: {
        id: 'first',
        type: 'author'
      }
    });

    // adapter#findAll resolves with
    // [
    //   {
    //     id: 'second',
    //     type: 'author'
    //   }
    // ]
    store.findAll('author', { reload: true }).then(function(authors) {
      authors.getEach('id'); // ['first', 'second']
    });

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:

    import DS from 'ember-data';
    export default DS.Adapter.extend({
      shouldReloadAll(store, snapshotsArray) {
        return false;
      },

      shouldBackgroundReloadAll(store, snapshotsArray) {
        return true;
      }
    });

    // ...

    store.push({
      data: {
        id: 'first',
        type: 'author'
      }
    });

    let allAuthors;
    store.findAll('author').then(function(authors) {
      authors.getEach('id'); // ['first']

      allAuthors = authors;
    });

    // later, once adapter#findAll resolved with
    // [
    //   {
    //     id: 'second',
    //     type: 'author'
    //   }
    // ]

    allAuthors.getEach('id'); // ['first', 'second']

If you would like to force or prevent background reloading, you can set a boolean value for `backgroundReload` in the options object for `findAll`.

    import Route from '@ember/routing/route';

    export default Route.extend({
      model() {
        return this.store.findAll('post', { backgroundReload: false });
      }
    });

If you pass an object on the `adapterOptions` property of the options argument it will be passed to you adapter via the `snapshotRecordArray`

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findAll('post', {
          adapterOptions: { subscribe: false }
        });
      }
    });

    import MyCustomAdapter from './custom-adapter';

    export default MyCustomAdapter.extend({
      findAll(store, type, sinceToken, snapshotRecordArray) {
        if (snapshotRecordArray.adapterOptions.subscribe) {
          // ...
        }
        // ...
      }
    });

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:

    import Route from '@ember/routing/route';

    export default Route.extend({
      model() {
        return this.store.findAll('post', { include: '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 posts' comments and the authors of those comments the request would look like this:

    import Route from '@ember/routing/route';

    export default Route.extend({
      model() {
        return this.store.findAll('post', { include: 'comments,comments.author' });
      }
    });

See query to only get a subset of records from the server.

#### Parameters:

- `modelName` String
- `options` Object

#### Returns:

Promise:

promise

### `findBelongsTo`

(

- `internalModel`
- `link`
- `relationship`

)
Promise private

Defined in `addon/-private/system/store.js:1447`

#### Parameters:

- `internalModel` InternalModel
- `link` Any
- `relationship` Relationship

#### Returns:

Promise:

promise

### `findByIds`

(

- `modelName`
- `ids`

)
Promise private

Defined in `addon/-private/system/store.js:827`

This method makes a series of requests to the adapter's `find` method and returns a promise that resolves once they are all loaded.

#### Parameters:

- `modelName` String
- `ids` Array

#### Returns:

Promise:

promise

### `findHasMany`

(

- `internalModel`
- `link`
- `relationship`

)
Promise private

Defined in `addon/-private/system/store.js:1339`

If a relationship was originally populated by the adapter as a link (as opposed to a list of IDs), this method is called when the relationship is fetched.

The link (which is usually a URL) is passed through unchanged, so the adapter can make whatever request it wants.

The usual use-case is for the server to register a URL as a link, and then use that URL in the future to make a request for the relationship.

#### Parameters:

- `internalModel` InternalModel
- `link` Any
- `relationship` (Relationship)

#### Returns:

Promise:

promise

### `findMany`

(

- `internalModels`

)
Promise private

Defined in `addon/-private/system/store.js:1320`

#### Parameters:

- `internalModels` Array

#### Returns:

Promise:

promise

### `findRecord`

(

- `modelName`
- `id`
- `[options]`
- `preload`

)
Promise

Defined in `addon/-private/system/store.js:521`

Available since 1.13.0

This method returns a record for a given type and id combination.

The `findRecord` method will always resolve its promise with the same object for a given type and `id`.

The `findRecord` method will always return a promise that will be resolved with the record.

Example

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findRecord('post', params.post_id);
      }
    });

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:

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

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

    store.findRecord('post', 1).then(function (myPostModel) {
      store.findRecord('comment', 2, { post: myPostModel });
    });

### 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:

    store.push({
      data: {
        id: 1,
        type: 'post',
        revision: 1
      }
    });

    // adapter#findRecord resolves with
    // [
    //   {
    //     id: 1,
    //     type: 'post',
    //     revision: 2
    //   }
    // ]
    store.findRecord('post', 1, { reload: true }).then(function(post) {
      post.get('revision'); // 2
    });

If no reload is indicated via the abovementioned ways, then the promise immediately resolves with the cached version in the store.

### Background Reloading

Optionally, if `adapter.shouldBackgroundReloadRecord` evaluates to `true`, then a background reload is started, which updates the records' data, once it is available:

    // app/adapters/post.js
    import ApplicationAdapter from "./application";

    export default ApplicationAdapter.extend({
      shouldReloadRecord(store, snapshot) {
        return false;
      },

      shouldBackgroundReloadRecord(store, snapshot) {
        return true;
      }
    });

    // ...

    store.push({
      data: {
        id: 1,
        type: 'post',
        revision: 1
      }
    });

    let blogPost = store.findRecord('post', 1).then(function(post) {
      post.get('revision'); // 1
    });

    // later, once adapter#findRecord resolved with
    // [
    //   {
    //     id: 1,
    //     type: 'post',
    //     revision: 2
    //   }
    // ]

    blogPost.get('revision'); // 2

If you would like to force or prevent background reloading, you can set a boolean value for `backgroundReload` in the options object for `findRecord`.

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findRecord('post', params.post_id, { backgroundReload: false });
      }
    });

If you pass an object on the `adapterOptions` property of the options argument it will be passed to you adapter via the snapshot

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findRecord('post', params.post_id, {
          adapterOptions: { subscribe: false }
        });
      }
    });

    import MyCustomAdapter from './custom-adapter';

    export default MyCustomAdapter.extend({
      findRecord(store, type, id, snapshot) {
        if (snapshot.adapterOptions.subscribe) {
          // ...
        }
        // ...
      }
    });

See peekRecord to get the cached version of a record.

### 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:

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findRecord('post', params.post_id, { include: 'comments' });
      }
    });

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:

    import Route from '@ember/routing/route';

    export default Route.extend({
      model(params) {
        return this.store.findRecord('post', params.post_id, { include: 'comments,comments.author' });
      }
    });

#### Parameters:

- `modelName` String
- `id` (String | Integer)
- `[options]` Object optional
- `preload` Object
  - optional set of attributes and relationships passed in either as IDs or as actual models

#### Returns:

Promise:

promise

### `flushPendingSave`

() private

Defined in `addon/-private/system/store.js:2174`

This method is called at the end of the run loop, and flushes any records passed into `scheduleSave`

### `getReference`

(

- `modelName`
- `id`

)
RecordReference

Defined in `addon/-private/system/store.js:1102`

Available since 2.5.0

Get the reference for the specified record.

Example

    let userRef = store.getReference('user', 1);

    // check if the user is loaded
    let isLoaded = userRef.value() !== null;

    // get the record of the reference (null if not yet available)
    let user = userRef.value();

    // get the identifier of the reference
    if (userRef.remoteType() === 'id') {
    let id = userRef.id();
    }

    // load user (via store.find)
    userRef.load().then(...)

    // or trigger a reload
    userRef.reload().then(...)

    // provide data for reference
    userRef.push({ id: 1, username: '@user' }).then(function(user) {
      userRef.value() === user;
    });

#### Parameters:

- `modelName` String
- `id` String | Integer

#### Returns:

RecordReference:

### `hasRecordForId`

(

- `modelName`
- `id`

)
Boolean

Defined in `addon/-private/system/store.js:1221`

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.

Example

    store.hasRecordForId('post', 1); // false
    store.findRecord('post', 1).then(function() {
      store.hasRecordForId('post', 1); // true
    });

#### Parameters:

- `modelName` String
- `id` (String | Integer)

#### Returns:

Boolean:

### `init`

() private

Defined in `addon/-private/system/store.js:184`

### `modelFor`

(

- `modelName`

)
DS.Model

Defined in `addon/-private/system/store.js:2379`

Returns the model class for the particular `modelName`.

The class of a model might be useful if you want to get a list of all the relationship names of the model, see `relationshipNames` for example.

#### Parameters:

- `modelName` String

#### Returns:

DS.Model:

### `normalize`

(

- `modelName`
- `payload`

)
Object

Defined in `addon/-private/system/store.js:2876`

`normalize` converts a json payload into the normalized form that push expects.

Example

    socket.on('message', function(message) {
      let modelName = message.model;
      let data = message.data;
      store.push(store.normalize(modelName, data));
    });

#### Parameters:

- `modelName` String

  The name of the model type for this payload

- `payload` Object

#### Returns:

Object:

The normalized payload

### `peekAll`

(

- `modelName`

)
DS.RecordArray

Defined in `addon/-private/system/store.js:2072`

Available since 1.13.0

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

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

#### Parameters:

- `modelName` String

#### Returns:

DS.RecordArray:

### `peekRecord`

(

- `modelName`
- `id`

)
DS.Model | Null

Defined in `addon/-private/system/store.js:1148`

Available since 1.13.0

Get a record by a given type and ID without triggering a fetch.

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.

Note: This is a synchronous method and does not return a promise.

    let post = store.peekRecord('post', 1);

    post.get('id'); // 1

#### Parameters:

- `modelName` String
- `id` String | Integer

#### Returns:

DS.Model | Null:

record

### `push`

(

- `data`

)
DS.Model | Array

Defined in `addon/-private/system/store.js:2455`

Push some data for a given type into the store.

This method expects normalized JSON API document. This means you have to follow JSON API specification with few minor adjustments:

- record's `type` should always be in singular, dasherized form
- members (properties) should be camelCased

Your primary data should be wrapped inside `data` property:

    store.push({
      data: {
        // primary data for single record of type Person
        id: '1',
        type: 'person',
        attributes: {
          firstName: 'Daniel',
          lastName: 'Kmak'
        }
      }
    });

Demo.

`data` property can also hold an array (of records):

    store.push({
      data: [
        // an array of records
        {
          id: '1',
          type: 'person',
          attributes: {
            firstName: 'Daniel',
            lastName: 'Kmak'
          }
        },
        {
          id: '2',
          type: 'person',
          attributes: {
            firstName: 'Tom',
            lastName: 'Dale'
          }
        }
      ]
    });

Demo.

There are some typical properties for `JSONAPI` payload:

- `id` - mandatory, unique record's key
- `type` - mandatory string which matches `model`'s dasherized name in singular form
- `attributes` - object which holds data for record attributes - `DS.attr`'s declared in model
- `relationships` - object which must contain any of the following properties under each relationships' respective key (example path is `relationships.achievements.data`):
  - `links`
  - `data` - place for primary data
  - `meta` - object which contains meta-information about relationship

For this model:

    import DS from 'ember-data';

    export default DS.Model.extend({
      firstName: DS.attr('string'),
      lastName: DS.attr('string'),

      children: DS.hasMany('person')
    });

To represent the children as IDs:

    {
      data: {
        id: '1',
        type: 'person',
        attributes: {
          firstName: 'Tom',
          lastName: 'Dale'
        },
        relationships: {
          children: {
            data: [
              {
                id: '2',
                type: 'person'
              },
              {
                id: '3',
                type: 'person'
              },
              {
                id: '4',
                type: 'person'
              }
            ]
          }
        }
      }
    }

Demo.

To represent the children relationship as a URL:

    {
      data: {
        id: '1',
        type: 'person',
        attributes: {
          firstName: 'Tom',
          lastName: 'Dale'
        },
        relationships: {
          children: {
            links: {
              related: '/people/1/children'
            }
          }
        }
      }
    }

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.

    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.

#### Parameters:

- `data` Object

#### Returns:

DS.Model | Array:

the record(s) that was created or updated.

### `pushPayload`

(

- `modelName`
- `inputPayload`

)

Defined in `addon/-private/system/store.js:2726`

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.

    import DS from 'ember-data';

    export default DS.ActiveModelSerializer;

    let pushData = {
      posts: [
        { id: 1, post_title: "Great post", comment_ids: [2] }
      ],
      comments: [
        { id: 2, comment_body: "Insightful comment" }
      ]
    }

    store.pushPayload(pushData);

By default, the data will be deserialized using a default serializer (the application serializer if it exists).

Alternatively, `pushPayload` will accept a model type which will determine which serializer will process the payload.

    import DS from 'ember-data';

    export default DS.ActiveModelSerializer;

    import DS from 'ember-data';

    export default DS.JSONSerializer;

    store.pushPayload(pushData); // Will use the application serializer
    store.pushPayload('post', pushData); // Will use the post serializer

#### Parameters:

- `modelName` String

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

- `inputPayload` Object

### `query`

(

- `modelName`
- `query`
- `options`

)
Promise

Defined in `addon/-private/system/store.js:1568`

Available since 1.13.0

This method delegates a query to the adapter. This is the one place where adapter-level semantics are exposed to the application.

Each time this method is called a new request is made through the adapter.

Exposing queries this way seems preferable to creating an abstract query language for all server-side queries, and then require all adapters to implement them.

If you do something like this:

    store.query('person', { page: 1 });

The request made to the server will look something like this:

    GET "/api/v1/person?page=1"

If you do something like this:

    store.query('person', { ids: [1, 2, 3] });

The request made to the server will look something like this:

    GET "/api/v1/person?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"
    decoded: "/api/v1/person?ids[]=1&ids[]=2&ids[]=3"

This method returns a promise, which is resolved with an `AdapterPopulatedRecordArray` once the server returns.

#### Parameters:

- `modelName` String
- `query` Any

  an opaque query to be used by the adapter

- `options` Object

  optional, may include `adapterOptions` hash which will be passed to adapter.query

#### Returns:

Promise:

promise

### `queryRecord`

(

- `modelName`
- `query`
- `options`

)
Promise

Defined in `addon/-private/system/store.js:1670`

Available since 1.13.0

This method makes a request for one record, where the `id` is not known beforehand (if the `id` is known, use `findRecord` instead).

This method can be used when it is certain that the server will return a single object for the primary data.

Each time this method is called a new request is made through the adapter.

Let's assume our API provides an endpoint for the currently logged in user via:

    // GET /api/current_user
    {
      user: {
        id: 1234,
        username: 'admin'
      }
    }

Since the specific `id` of the `user` is not known beforehand, we can use `queryRecord` to get the user:

    store.queryRecord('user', {}).then(function(user) {
      let username = user.get('username');
      console.log(Currently logged in as ${username});
    });

The request is made through the adapters' `queryRecord`:

    import $ from 'jquery';
    import DS from 'ember-data';

    export default DS.Adapter.extend({
      queryRecord(modelName, query) {
        return $.getJSON('/api/current_user');
      }
    });

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:

    // GET /users?username=unique
    {
      data: [{
        id: 1234,
        type: 'user',
        attributes: {
          username: "unique"
        }
      }]
    }

    store.query('user', { username: 'unique' }).then(function(users) {
      return users.get('firstObject');
    }).then(function(user) {
      let id = user.get('id');
    });

This method returns a promise, which resolves with the found record.

If the adapter returns no data for the primary data of the payload, then `queryRecord` resolves with `null`:

    // GET /users?username=unique
    {
      data: null
    }

    store.queryRecord('user', { username: 'unique' }).then(function(user) {
      console.log(user); // null
    });

#### Parameters:

- `modelName` String
- `query` Any

  an opaque query to be used by the adapter

- `options` Object

  optional, may include `adapterOptions` hash which will be passed to adapter.queryRecord

#### Returns:

Promise:

promise which resolves with the found record or `null`

### `recordForId`

(

- `modelName`
- `id`

)
DS.Model private

Defined in `addon/-private/system/store.js:1261`

Returns id record for a given type and ID. If one isn't already loaded, it builds a new record and leaves it in the `empty` state.

#### Parameters:

- `modelName` String
- `id` (String | Integer)

#### Returns:

DS.Model:

record

### `recordWasError`

(

- `internalModel`
- `error`

)
private

Defined in `addon/-private/system/store.js:2258`

This method is called once the promise returned by an adapter's `createRecord`, `updateRecord` or `deleteRecord` is rejected (with anything other than a `DS.InvalidError`).

#### Parameters:

- `internalModel` InternalModel
- `error` Error

### `recordWasInvalid`

(

- `internalModel`
- `errors`

)
private

Defined in `addon/-private/system/store.js:2241`

This method is called once the promise returned by an adapter's `createRecord`, `updateRecord` or `deleteRecord` is rejected with a `DS.InvalidError`.

#### Parameters:

- `internalModel` InternalModel
- `errors` Object

### `scheduleSave`

(

- `internalModel`
- `resolver`
- `options`

)
private

Defined in `addon/-private/system/store.js:2152`

This method is called by `record.save`, and gets passed a resolver for the promise that `record.save` returns.

It schedules saving to happen at the end of the run loop.

#### Parameters:

- `internalModel` InternalModel
- `resolver` Resolver
- `options` Object

### `serializerFor`

(

- `modelName`

)
DS.Serializer public

Defined in `addon/-private/system/store.js:3088`

Returns an instance of the serializer for a given type. For example, `serializerFor('person')` will return an instance of `App.PersonSerializer`.

If no `App.PersonSerializer` is found, this method will look for an `App.ApplicationSerializer` (the default serializer for your entire application).

if no `App.ApplicationSerializer` is found, it will attempt to get the `defaultSerializer` from the `PersonAdapter` (`adapterFor('person')`).

If a serializer cannot be found on the adapter, it will fall back to an instance of `DS.JSONSerializer`.

#### Parameters:

- `modelName` String

  the record to serialize

#### Returns:

DS.Serializer:

### `setRecordId`

(

- `modelName`
- `newId`
- `clientId`

)
private

Defined in `addon/-private/system/store.js:2275`

Sets newly received ID from the adapter's `createRecord`, `updateRecord` or `deleteRecord`.

#### Parameters:

- `modelName` String
- `newId` String
- `clientId` Number

### `unloadAll`

(

- `modelName`

)

Defined in `addon/-private/system/store.js:2110`

This method unloads all records in the store. It schedules unloading to happen during the next run loop.

Optionally you can pass a type which unload all records for a given type.

    store.unloadAll();
    store.unloadAll('post');

#### Parameters:

- `modelName` String

### `unloadRecord`

(

- `record`

)

Defined in `addon/-private/system/store.js:452`

For symmetry, a record can be unloaded via the store. This will cause the record to be destroyed and freed up for garbage collection.

Example

    store.findRecord('post', 1).then(function(post) {
      store.unloadRecord(post);
    });

#### Parameters:

- `record` DS.Model

## Properties

### `adapter`

String

Defined in `addon/-private/system/store.js:280`

The default adapter to use to communicate to a backend server or other persistence layer. This will be overridden by an application adapter if present.

If you want to specify `app/adapters/custom.js` as a string, do:

    import DS from 'ember-data';

    export default DS.Store.extend({
      adapter: 'custom',
    });

Default: '-json-api'

### `defaultAdapter`

Unknown private

Defined in `addon/-private/system/store.js:301`

This property returns the adapter, after resolving a possible string key.

If the supplied `adapter` was a class, or a String property path resolved to a class, this property will instantiate the class.

This property is cacheable, so the same instance of a specified adapter class should be used for the lifetime of the store.