Wikodit / js-data-jsonapi-light

JSData adapter which serialize and deserialize JSONApi response and requests.
MIT License
8 stars 4 forks source link

Detect and use PATCH by default #9

Closed Tronix117 closed 7 years ago

Tronix117 commented 7 years ago

Update using PATCH by default unless usePatch: false is given to the adapter parameter.

Proposed implementation

Disable

If you want to force using PUT instead of PATCH

const jsonApiAdapter = new JsonApiAdapter({
  // By default it is set to true but you can turn it off
  usePatch: false
});

Update with PATCH:

It will send only modified attributes (the name)

User.find(50).then((user) => {
  user.name = "Foo";
  return user.save()
})

or

// If the user 50 doesn't exist in datastore, the whole params object will be sent, otherwise only changed fields
User.update(50, { name: "Foo" });

Force replace

To force the replace and use of PUT method (all attributes and parent relations will be sent):

User.find(50).then((user) => {
  user.name = "Foo";
  return user.save({replace: true});
})

or

User.update(50, { name: "Foo" }, { replace: true });

Requirements

By default, changes needs to be watched to know what to send on the .save() of a record, in the case the Mapper has keepChangeHistory disabled, all attributes and parent relations will be send when using save and the whole params object will be sent when using update.

Référence

http://jsonapi.org/format/#crud-updating