home
  • Blog
  • Home
  • Projects
    • Ember
    • EmberData
    • Ember CLI
1.13
  • Packages
    • ember-data
  • Namespaces
    • DS
    • Ember.Date
  • Classes
    • DS.AbortError
    • DS.ActiveModelAdapter
    • DS.ActiveModelSerializer
    • DS.Adapter
    • DS.AdapterError
    • DS.AdapterPopulatedRecordArray
    • DS.BooleanTransform
    • DS.BuildURLMixin
    • DS.DateTransform
    • DS.EmbeddedRecordsMixin
    • DS.Errors
    • DS.FilteredRecordArray
    • DS.FixtureAdapter
    • DS.InternalModel
    • DS.InvalidError
    • DS.JSONAPIAdapter
    • DS.JSONAPISerializer
    • DS.JSONSerializer
    • DS.ManyArray
    • DS.Model
    • DS.NumberTransform
    • DS.PromiseArray
    • DS.PromiseManyArray
    • DS.PromiseObject
    • DS.RESTAdapter
    • DS.RESTSerializer
    • DS.RecordArray
    • DS.RootState
    • DS.Serializer
    • DS.Store
    • DS.StringTransform
    • DS.TimeoutError
    • DS.Transform

Class DS.Adapter


Extends: Ember.Object
Defined in: packages/ember-data/lib/system/adapter.js:7
Module: ember-data

An adapter is an object that receives requests from a store and translates them into the appropriate action to take against your persistence layer. The persistence layer is usually an HTTP API, but may be anything, such as the browser's local storage. Typically the adapter is not invoked directly instead its functionality is accessed through the store.

Creating an Adapter

Create a new subclass of DS.Adapter in the app/adapters folder:

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  // ...your code here
});

Model-specific adapters can be created by putting your adapter class in an app/adapters/ + model-name + .js file of the application.

app/adapters/post.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  // ...Post-specific adapter code goes here
});

DS.Adapter is an abstract base class that you should override in your application to customize it for your backend. The minimum set of methods that you should implement is:

  • findRecord()
  • createRecord()
  • updateRecord()
  • deleteRecord()
  • findAll()
  • query()

To improve the network performance of your application, you can optimize your adapter by overriding these lower-level methods:

  • findMany()

For an example implementation, see DS.RESTAdapter, the included REST adapter.


Methods

convertResourceObject (payload) : Object

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:295

payload
Object
returns
Object

an object formatted the way DS.Store understands

This method converts a JSON-API Resource Object to a format that DS.Store understands.

TODO: This method works as an interim until DS.Store understands JSON-API.

createRecord (store, type, snapshot) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:287

store
DS.Store
type
DS.Model

the DS.Model class of the record

snapshot
DS.Snapshot
returns
Promise

promise

Implement this method in a subclass to handle the creation of new records.

Serializes the record and sends it to the server.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  createRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });
    var url = type;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.ajax({
        type: 'POST',
        url: url,
        dataType: 'json',
        data: data
      }).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

deleteRecord (store, type, snapshot) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:370

store
DS.Store
type
DS.Model

the DS.Model class of the record

snapshot
DS.Snapshot
returns
Promise

promise

Implement this method in a subclass to handle the deletion of a record.

Sends a delete request for the record to the server.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  deleteRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });
    var id = snapshot.id;
    var url = [type, id].join('/');

    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.ajax({
        type: 'DELETE',
        url: url,
        dataType: 'json',
        data: data
      }).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

findAll (store, type, sinceToken, snapshotRecordArray) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:124

store
DS.Store
type
DS.Model
sinceToken
String
snapshotRecordArray
DS.SnapshotRecordArray
returns
Promise

promise

The findAll() method is used to retrieve all records for a given type.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  findAll: function(store, type, sinceToken) {
    var url = type;
    var query = { since: sinceToken };
    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.getJSON(url, query).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

findMany (store, type, ids, snapshots) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:423

store
DS.Store
type
DS.Model

the DS.Model class of the records

ids
Array
snapshots
Array
returns
Promise

promise

Find multiple records at once if coalesceFindRequests is true.

findRecord (store, type, id, snapshot) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:87

store
DS.Store
type
DS.Model
id
String
snapshot
DS.Snapshot
returns
Promise

promise

The findRecord() method is invoked when the store is asked for a record that has not previously been loaded. In response to findRecord() being called, you should query your persistence layer for a record with the given ID. Once found, you can asynchronously call the store's push() method to push the record into the store.

Here is an example findRecord implementation:

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  findRecord: function(store, type, id, snapshot) {
    var url = [type.modelName, id].join('/');

    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.getJSON(url).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

generateIdForRecord (store, type, inputProperties) : (String|Number)

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:230

store
DS.Store
type
DS.Model

the DS.Model class of the record

inputProperties
Object

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

returns
(String|Number)

id

If the globally unique IDs for your records should be generated on the client, implement the generateIdForRecord() method. This method will be invoked each time you create a new record, and the value returned from it will be assigned to the record's primaryKey.

Most traditional REST-like HTTP APIs will not use this method. Instead, the ID of the record will be set by the server, and your adapter will update the store with the new ID when it calls didCreateRecord(). Only implement this method if you intend to generate record IDs on the client-side.

The generateIdForRecord() method will be invoked with the requesting store as the first parameter and the newly created record as the second parameter:

generateIdForRecord: function(store, inputProperties) {
  var uuid = App.generateUUIDWithStatisticallyLowOddsOfCollision();
  return uuid;
}

groupRecordsForFindMany (store, snapshots) : Array

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:434

store
DS.Store
snapshots
Array
returns
Array

an array of arrays of records, each of which is to be loaded separately by findMany.

Organize records into groups, each of which is to be passed to separate calls to findMany.

For example, if your api has nested URLs that depend on the parent, you will want to group records by their parent.

The default implementation returns the records as a single group.

normalizeResponseHelper (serializer, store, modelClass, payload, id, requestType) : Object

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:71

serializer
DS.Serializer
store
DS.Store
modelClass
subclass of DS.Model
payload
Object
id
String|Number
requestType
String
returns
Object

JSON-API Document

This is a helper method that always returns a JSON-API Document.

If the current serializer has isNewSerializerAPI set to true this helper calls normalizeResponse instead of extract.

All the built-in serializers get isNewSerializerAPI set to true automatically if the feature flag is enabled.

pushPayload (store, payload) : DS.Model|Array

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:222

store
DS.Store
payload
Object
returns
DS.Model|Array

one or multiple records from data

Push a JSON-API Document to the store.

This will push both primary data located in data and secondary data located in included (if present).

pushPayloadData (store, payload) : DS.Model|Array

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:239

store
DS.Store
payload
Object
returns
DS.Model|Array

one or multiple records from data

Push the primary data of a JSON-API Document to the store.

This method only pushes the primary data located in data.

pushPayloadIncluded (store, payload) : Array

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:263

store
DS.Store
payload
Object
returns
Array

an array containing zero or more records from included

Push the secondary data of a JSON-API Document to the store.

This method only pushes the secondary data located in included.

queryRecord (store, type, query) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:190

store
DS.Store
type
subclass of DS.Model
query
Object
returns
Promise

promise

The queryRecord() method is invoked when the store is asked for a single record through a query object.

In response to queryRecord() being called, you should always fetch fresh data. Once found, you can asynchronously call the store's push() method to push the record into the store.

Here is an example queryRecord implementation:

Example

app/adapters/application.js
import DS from 'ember-data';
import Ember from 'ember';

export default DS.Adapter.extend(DS.BuildURLMixin, {
  queryRecord: function(store, type, query) {
    var urlForQueryRecord = this.buildURL(type.modelName, null, null, 'queryRecord', query);

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.getJSON(urlForQueryRecord, query).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

serialize (snapshot, options) : Object

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:260

snapshot
DS.Snapshot
options
Object
returns
Object

serialized snapshot

Proxies to the serializer's serialize method.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  createRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });
    var url = type;

    // ...
  }
});

shouldBackgroundReloadAll (store, snapshotRecordArray) : Boolean

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:513

store
DS.Store
snapshotRecordArray
DS.SnapshotRecordArray
returns
Boolean

This method is used by the store to determine if the store should reload a record array after the store.findAll method resolves with a cached record array.

This method is only checked by the store when the store is returning a cached record array.

If this method returns true the store will re-fetch all records from the adapter.

shouldBackgroundReloadRecord (store, snapshot) : Boolean

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:492

store
DS.Store
snapshot
DS.Snapshot
returns
Boolean

This method is used by the store to determine if the store should reload a record after the store.findRecord method resolves a cached record.

This method is only checked by the store when the store is returning a cached record.

If this method returns true the store will re-fetch a record from the adapter.

shouldReloadAll (store, snapshotRecordArray) : Boolean

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:472

store
DS.Store
snapshotRecordArray
DS.SnapshotRecordArray
returns
Boolean

This method is used by the store to determine if the store should reload all records from the adapter when records are requested by store.findAll.

If this method returns true, the store will re-fetch all records from the adapter. If this method returns false, the store will resolve immediately using the cached record.

shouldReloadRecord (store, snapshot) : Boolean

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:454

store
DS.Store
snapshot
DS.Snapshot
returns
Boolean

This method is used by the store to determine if the store should reload a record from the adapter when a record is requested by store.findRecord.

If this method returns true, the store will re-fetch a record from the adapter. If this method returns false, the store will resolve immediately using the cached record.

updateRecord (store, type, snapshot) : Promise

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:328

store
DS.Store
type
DS.Model

the DS.Model class of the record

snapshot
DS.Snapshot
returns
Promise

promise

Implement this method in a subclass to handle the updating of a record.

Serializes the record update and sends it to the server.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  updateRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });
    var id = snapshot.id;
    var url = [type, id].join('/');

    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.ajax({
        type: 'PUT',
        url: url,
        dataType: 'json',
        data: data
      }).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

validateDocumentStructure (doc) : Array

Module: ember-data

Defined in packages/ember-data/lib/system/store/serializer-response.js:12

doc
Object

JSON API document

returns
Array

An array of errors found in the document structure

This is a helper method that validates a JSON API top-level document

The format of a document is described here: http://jsonapi.org/format/#document-top-level

Properties

coalesceFindRequests

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:412

By default the store will try to coalesce all fetchRecord calls within the same runloop into as few requests as possible by calling groupRecordsForFindMany and passing it into a findMany call. You can opt out of this behaviour by either not implementing the findMany hook or by setting coalesceFindRequests to false.

defaultSerializer

Module: ember-data

Defined in packages/ember-data/lib/system/adapter.js:65

If you would like your adapter to use a custom serializer you can set the defaultSerializer property to be the name of the custom serializer.

Note the defaultSerializer serializer has a lower priority than a model specific serializer (i.e. PostSerializer) or the application serializer.

app/adapters/django.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  defaultSerializer: 'django'
});
On this page


Methods

  • convertResourceObject
  • createRecord
  • deleteRecord
  • findAll
  • findMany
  • findRecord
  • generateIdForRecord
  • groupRecordsForFindMany
  • normalizeResponseHelper
  • pushPayload
  • pushPayloadData
  • pushPayloadIncluded
  • queryRecord
  • serialize
  • shouldBackgroundReloadAll
  • shouldBackgroundReloadRecord
  • shouldReloadAll
  • shouldReloadRecord
  • updateRecord
  • validateDocumentStructure

Properties

  • coalesceFindRequests
  • defaultSerializer
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2025 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue Created with Sketch.