Skip to content
module@warp-drive/legacy/adapter @legacy

Class Adapter ​

Defined in: warp-drive-packages/legacy/src/adapter.ts:293

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.

⚠️ CAUTION you likely want the docs for MinimumAdapterInterface as extending this abstract class is unnecessary.

Creating an Adapter ​

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

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';

export default class extends Adapter {
  // ...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
js
import { Adapter } from '@warp-drive/legacy/adapter';

export default class extends Adapter {
  // ...Post-specific adapter code goes here
}

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 of the implementation, see RESTAdapter, the included REST adapter.

Extends ​

  • EmberObject

Implements ​

Constructors ​

Constructor ​

ts
new Adapter(owner?: Owner): Adapter;

Parameters ​

owner? ​

Owner

Returns ​

Adapter

Inherited from ​

ts
EmberObject.constructor

Methods ​

createRecord() ​

ts
createRecord(
   store: Store$1, 
   type: ModelSchema, 
   snapshot: Snapshot
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:546

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
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  createRecord(store, type, snapshot) {
    let data = this.serialize(snapshot, { includeId: true });

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

Parameters ​

store ​

Store$1

type ​

ModelSchema

snapshot ​

Snapshot

Returns ​

Promise<AdapterPayload>

Implementation of ​

MinimumAdapterInterface.createRecord


deleteRecord() ​

ts
deleteRecord(
   store: Store$1, 
   type: ModelSchema, 
   snapshot: Snapshot
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:650

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
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  deleteRecord(store, type, snapshot) {
    let data = this.serialize(snapshot, { includeId: true });
    let id = snapshot.id;

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

Parameters ​

store ​

Store$1

type ​

ModelSchema

the Model class of the record

snapshot ​

Snapshot

Returns ​

Promise<AdapterPayload>

promise

Implementation of ​

MinimumAdapterInterface.deleteRecord


findAll() ​

ts
findAll(
   store: Store$1, 
   type: ModelSchema, 
   neverSet: null, 
   snapshotRecordArray: SnapshotRecordArray
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:366

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

Example

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  findAll(store, type) {
    return new RSVP.Promise(function(resolve, reject) {
      $.getJSON(`/${type.modelName}`).then(function(data) {
        resolve(data);
      }, function(jqXHR) {
        reject(jqXHR);
      });
    });
  }
}

Parameters ​

store ​

Store$1

type ​

ModelSchema

neverSet ​

null

a value is never provided to this argument

snapshotRecordArray ​

SnapshotRecordArray

Returns ​

Promise<AdapterPayload>

Implementation of ​

MinimumAdapterInterface.findAll


findRecord() ​

ts
findRecord(
   store: Store$1, 
   type: ModelSchema, 
   id: string, 
   snapshot: Snapshot
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:334

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. The findRecord method should return a promise that will resolve to a JavaScript object that will be normalized by the serializer.

Here is an example of the findRecord implementation:

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  findRecord(store, type, id, snapshot) {
    return new RSVP.Promise(function(resolve, reject) {
      $.getJSON(`/${type.modelName}/${id}`).then(function(data) {
        resolve(data);
      }, function(jqXHR) {
        reject(jqXHR);
      });
    });
  }
}

Parameters ​

store ​

Store$1

type ​

ModelSchema

id ​

string

snapshot ​

Snapshot

Returns ​

Promise<AdapterPayload>

Implementation of ​

MinimumAdapterInterface.findRecord


groupRecordsForFindMany() ​

ts
groupRecordsForFindMany(store: Store$1, snapshots: Snapshot<unknown>[]): Snapshot<unknown>[][];

Defined in: warp-drive-packages/legacy/src/adapter.ts:730

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.

Parameters ​

store ​

Store$1

snapshots ​

Snapshot<unknown>[]

Returns ​

Snapshot<unknown>[][]

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

Implementation of ​

MinimumAdapterInterface.groupRecordsForFindMany


query() ​

ts
query(
   store: Store$1, 
   type: ModelSchema, 
   query: Record<string, unknown>
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:404

This method is called when you call query on the store.

Example

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  query(store, type, query) {
    return new RSVP.Promise(function(resolve, reject) {
      $.getJSON(`/${type.modelName}`, query).then(function(data) {
        resolve(data);
      }, function(jqXHR) {
        reject(jqXHR);
      });
    });
  }
}

Parameters ​

store ​

Store$1

type ​

ModelSchema

query ​

Record<string, unknown>

Returns ​

Promise<AdapterPayload>

Implementation of ​

MinimumAdapterInterface.query


queryRecord() ​

ts
queryRecord(
   store: Store$1, 
   type: ModelSchema, 
   query: Record<string, unknown>, 
   adapterOptions: object
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:435

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
js
import  { Adapter, BuildURLMixin } from '@warp-drive/legacy/adapter';

export default class ApplicationAdapter extends Adapter.extend(BuildURLMixin) {
  queryRecord(store, type, query) {
    return fetch(`/${type.modelName}`, { body: JSON.stringify(query) })
      .then((response) => response.json());
  }
}

Parameters ​

store ​

Store$1

type ​

ModelSchema

query ​

Record<string, unknown>

adapterOptions ​

object

Returns ​

Promise<AdapterPayload>

Implementation of ​

MinimumAdapterInterface.queryRecord


serialize() ​

ts
serialize(snapshot: Snapshot, options: SerializerOptions): Record<string, unknown>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:500

Proxies to the serializer's serialize method.

Example

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';

export default class ApplicationAdapter extends Adapter {
  createRecord(store, type, snapshot) {
    let data = this.serialize(snapshot, { includeId: true });
    let url = `/${type.modelName}`;

    // ...
  }
}

Parameters ​

snapshot ​

Snapshot

options ​

SerializerOptions

Returns ​

Record<string, unknown>


shouldBackgroundReloadAll() ​

sincev1.13.0
ts
shouldBackgroundReloadAll(store: Store$1, snapshotRecordArray: SnapshotRecordArray): boolean;

Defined in: warp-drive-packages/legacy/src/adapter.ts:907

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.

For example, if you do not want to fetch complex data over a mobile connection, or if the network is down, you can implement shouldBackgroundReloadAll as follows:

javascript
shouldBackgroundReloadAll(store, snapshotArray) {
  let { downlink, effectiveType } = navigator.connection;

  return downlink > 0 && effectiveType === '4g';
}

By default this method returns true, indicating that a background reload should always be triggered.

Parameters ​

store ​

Store$1

snapshotRecordArray ​

SnapshotRecordArray

Returns ​

boolean

Implementation of ​

MinimumAdapterInterface.shouldBackgroundReloadAll


shouldBackgroundReloadRecord() ​

sincev1.13.0
ts
shouldBackgroundReloadRecord(store: Store$1, snapshot: Snapshot): boolean;

Defined in: warp-drive-packages/legacy/src/adapter.ts:871

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.

For example, if you do not want to fetch complex data over a mobile connection, or if the network is down, you can implement shouldBackgroundReloadRecord as follows:

javascript
shouldBackgroundReloadRecord(store, snapshot) {
  let { downlink, effectiveType } = navigator.connection;

  return downlink > 0 && effectiveType === '4g';
}

By default, this hook returns true so the data for the record is updated in the background.

Parameters ​

store ​

Store$1

snapshot ​

Snapshot

Returns ​

boolean

Implementation of ​

MinimumAdapterInterface.shouldBackgroundReloadRecord


shouldReloadAll() ​

sincev1.13.0
ts
shouldReloadAll(store: Store$1, snapshotRecordArray: SnapshotRecordArray): boolean;

Defined in: warp-drive-packages/legacy/src/adapter.ts:835

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

For example, if you are building an events ticketing system, in which users can only reserve tickets for 20 minutes at a time, and want to ensure that in each route you have data that is no more than 20 minutes old you could write:

javascript
shouldReloadAll(store, snapshotArray) {
  let snapshots = snapshotArray.snapshots();

  return snapshots.any((ticketSnapshot) => {
    let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
    let timeDiff = moment().diff(lastAccessedAt, 'minutes');

    if (timeDiff > 20) {
      return true;
    } else {
      return false;
    }
  });
}

This method would ensure that whenever you do store.findAll('ticket') you will always get a list of tickets that are no more than 20 minutes old. In case a cached version is more than 20 minutes old, findAll will not resolve until you fetched the latest versions.

By default, this method returns true if the passed snapshotRecordArray is empty (meaning that there are no records locally available yet), otherwise, it returns false.

Note that, with default settings, shouldBackgroundReloadAll will always re-fetch all the records in the background even if shouldReloadAll returns false. You can override shouldBackgroundReloadAll if this does not suit your use case.

Parameters ​

store ​

Store$1

snapshotRecordArray ​

SnapshotRecordArray

Returns ​

boolean

Implementation of ​

MinimumAdapterInterface.shouldReloadAll


shouldReloadRecord() ​

sincev1.13.0
ts
shouldReloadRecord(store: Store$1, snapshot: Snapshot): boolean;

Defined in: warp-drive-packages/legacy/src/adapter.ts:780

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.

For example, if you are building an events ticketing system, in which users can only reserve tickets for 20 minutes at a time, and want to ensure that in each route you have data that is no more than 20 minutes old you could write:

javascript
shouldReloadRecord(store, ticketSnapshot) {
  let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
  let timeDiff = moment().diff(lastAccessedAt, 'minutes');

  if (timeDiff > 20) {
    return true;
  } else {
    return false;
  }
}

This method would ensure that whenever you do store.findRecord('ticket', id) you will always get a ticket that is no more than 20 minutes old. In case the cached version is more than 20 minutes old, findRecord will not resolve until you fetched the latest version.

By default this hook returns false, as most UIs should not block user interactions while waiting on data update.

Note that, with default settings, shouldBackgroundReloadRecord will always re-fetch the records in the background even if shouldReloadRecord returns false. You can override shouldBackgroundReloadRecord if this does not suit your use case.

Parameters ​

store ​

Store$1

snapshot ​

Snapshot

Returns ​

boolean

Implementation of ​

MinimumAdapterInterface.shouldReloadRecord


updateRecord() ​

ts
updateRecord(
   store: Store$1, 
   type: ModelSchema, 
   snapshot: Snapshot
): Promise<AdapterPayload>;

Defined in: warp-drive-packages/legacy/src/adapter.ts:602

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

Serializes the record update and sends it to the server.

The updateRecord method is expected to return a promise that will resolve with the serialized record. This allows the backend to inform the WarpDrive store the current state of this record after the update. If it is not possible to return a serialized record the updateRecord promise can also resolve with undefined and the WarpDrive store will assume all of the updates were successfully applied on the backend.

Example

app/adapters/application.js
js
import { Adapter } from '@warp-drive/legacy/adapter';
import RSVP from 'RSVP';
import $ from 'jquery';

export default class ApplicationAdapter extends Adapter {
  updateRecord(store, type, snapshot) {
    let data = this.serialize(snapshot, { includeId: true });
    let id = snapshot.id;

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

Parameters ​

store ​

Store$1

type ​

ModelSchema

the Model class of the record

snapshot ​

Snapshot

Returns ​

Promise<AdapterPayload>

promise

Implementation of ​

MinimumAdapterInterface.updateRecord

Properties ​

store ​

ts
store: Store$1;

Defined in: warp-drive-packages/legacy/src/adapter.ts:297

The Store service instance that owns this Adapter.

coalesceFindRequests ​

Get Signature ​

ts
get coalesceFindRequests(): boolean;

Defined in: warp-drive-packages/legacy/src/adapter.ts:666

By default the store will try to coalesce all findRecord 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.

Returns ​

boolean

Set Signature ​

ts
set coalesceFindRequests(value: boolean): void;

Defined in: warp-drive-packages/legacy/src/adapter.ts:674

Optional

If your adapter implements findMany, setting this to true will cause findRecord requests triggered within the same runloop to be coalesced into one or more calls to adapter.findMany. The number of calls made and the records contained in each call can be tuned by your adapter's groupRecordsForHasMany method.

Implementing coalescing using this flag and the associated methods does not always offer the right level of correctness, timing control or granularity. If your application would be better suited coalescing across multiple types, coalescing for longer than a single runloop, or with a more custom request structure, coalescing within your application adapter may prove more effective.

Parameters ​
value ​

boolean

Returns ​

void

Optional

If your adapter implements findMany, setting this to true will cause findRecord requests triggered within the same runloop to be coalesced into one or more calls to adapter.findMany. The number of calls made and the records contained in each call can be tuned by your adapter's groupRecordsForHasMany method.

Implementing coalescing using this flag and the associated methods does not always offer the right level of correctness, timing control or granularity. If your application would be better suited coalescing across multiple types, coalescing for longer than a single runloop, or with a more custom request structure, coalescing within your application adapter may prove more effective.

Implementation of ​

MinimumAdapterInterface.coalesceFindRequests

Released under the MIT License.