Question
· Sep 19, 2016

Updating data using REST / best practices?

Do you have any experience / recommendations / best practice on how to update server-side data residing in a Cache database via a REST service?

A naïve approach would be to send a full JSON representation of the data object via an http POST to a method on a REST handler class: 

<Route Url="/mydata/:id" Method="POST" Call="Update"/>

The problem I see here is, if only one of the data object's properties has changed, how do we know which? Or do we simply overwrite all properties? 

Discussion (3)1
Log in or sign up to continue

If to talk about REST and best practices, then I should say, to update some object or all collection you should use method PUT, while POST should be used only for creating new objects.

What about updating objects, actually it it could be inside objects. And I hope that InterSystems has some optimizations in this case. But I think it is mostly depends on you objects, how difficult they are, and how often you should change this objects. And you of course you can use some profile tools, to understand how many time spends while object is changing and for what.  In may opinion in first you should keep it as is, just update all properties from request, without any checking. Some time it is possible that checking for changes, may take more time, when just save it all.

The problem I see here is, if only one of the data object's properties has changed, how do we know which? Or do we simply overwrite all properties? 

If you use old json classes, you can send only changed properties, for example this json payload would be converted into  Sample.Person object with id=1 and all properties taken from disk except for Name property, which would be set to Bob:

{ _class: "Sample.Person", _id: 1, Name: "Bob" }

With new json you can get dynamic object from json with only modified properties and change persistent object by iterating over defined properties of a modified object.