Skip to content

Decorator to add support to Ember Data for the JSON:API Bulk Extension

License

Notifications You must be signed in to change notification settings

movableink/ember-data-json-api-bulk-ext

Repository files navigation

ember-data-json-api-bulk-ext

.github/workflows/verify.yml

Decorator to add support to Ember Data for the JSON:API Bulk Extension.

Compatibility

  • Ember.js v3.24 or above
  • Ember CLI v2.13 or above
  • Node.js v12 or above

Installation

ember install ember-data-json-api-bulk-ext

If you have not yet created a subclass of the Ember Data Store, do so now. You will want to import and apply the decorator to this class.

// app/services/store.js
import Store from '@ember-data/store';
import { withBulkActions } from 'ember-data-json-api-bulk-ext';

@withBulkActions()
class CustomStore extends Store {}

export default CustomStore;

Usage

With the decorator applied to your Store subclass, you'll have new methods on the store available to you for dealing with bulk API actions.

const first = this.store.createRecord('post', { title: 'First Post' });
const second = this.store.createRecord('post', { title: 'Second Post' });

await this.store.bulkCreate([first, second]);

assert.ok(first.id, 'First record was given an ID');
assert.ok(second.id, 'First record was given an ID');

Note the following limitations:

  • The models being operated on must use the JSONAPIAdapter and JSONAPISerializer
  • All records must be of the same type (for now)
  • Records can only be created in bulk (for now)

Using the extension MIME type

The bulk extension for JSON:API describes a custom MIME type for your requests. To override the default JSON:API MIME type and use the one from the extension, pass the following option to the withBulkActions decorator:

@withBulkActions({ useExtensionMimeType: true })
class CustomStore extends Store {

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.