2

Can ember-data send json-patch PATCH on model.save() call? (with media type application/json-patch+json RFC6902)

The documentation says yes but with no details:

https://guides.emberjs.com/release/models/creating-updating-and-deleting-records/#toc_persisting-records

Testing it shows PUT requests with the entire model in the request.

Slim
  • 1,256
  • 1
  • 13
  • 25
  • 2
    Not by default. But you could write an adapter and serializer pair that does this. – Lux Feb 07 '20 at 12:42
  • I would have to clone that for every model? – Slim Feb 08 '20 at 06:37
  • Not at all. The application adapter and serializer will be used if you dont have another adapter/serializer for a model. So put it in your application adapter/serializer. – Lux Feb 08 '20 at 22:03
  • Ok, this kind of adapter maybe available somewhere, isn't dit? I can't believe this job has to be written by framework user. – Slim Feb 09 '20 at 11:06
  • There was a [RFC for adding JSON Patch support to Ember Data](https://github.com/emberjs/rfcs/pull/5) but the decision was to leave this for user space. Not sure if there is a well established addon. [JSON:API](https://jsonapi.org/), GraphQL and some custom REST APIs are far more used in Ember community than JSON Patch. – jelhan Feb 09 '20 at 22:16
  • Json:api doesn't countersays json patch, isn't it? Damn, custom rest API more used than well established standards? – Slim Feb 10 '20 at 13:22

1 Answers1

1

I suspect your application uses the RESTAdapter and not the JSONAPIAdapter.

RESTAdapter was the default adapter before Ember Data 2.0 as stated here

You can take a look a both adapters updateRecord methods:

RestAdapter

/**
    Called by the store when an existing record is saved
    via the `save` method on a model record instance.
    The `updateRecord` method serializes the record and makes an Ajax (HTTP PUT) request
    to a URL computed by `buildURL`.
    See `serialize` for information on how to customize the serialized form
    of a record.
    @method updateRecord
    @param {Store} store
    @param {Model} type
    @param {Snapshot} snapshot
    @return {Promise} promise
  */
  updateRecord(store, type, snapshot) {
    const data = serializeIntoHash(store, type, snapshot, {});

    let id = snapshot.id;
    let url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');

    return this.ajax(url, 'PUT', { data });
  }

JSONAPIAdapter

updateRecord(store, type, snapshot) {
    const data = serializeIntoHash(store, type, snapshot);

    let url = this.buildURL(type.modelName, snapshot.id, snapshot, 'updateRecord');

    return this.ajax(url, 'PATCH', { data: data });
  }

As you can see, one uses PUT, the other PATCH. The JSONAPIAdapter is now the default one, that's why the documentation deals with PATCH requests.

If you want to use PATCH instead of PUT and keep RestAdapter, you should extend from RestAdapter and modify the updateRecord method :D

I hope it will find you well ;)

Guillaume Cisco
  • 2,859
  • 24
  • 25