PDA

View Full Version : Getting my head around RESTful CRUD



pixelglow
15 Jun 2009, 8:19 PM
Trying to get my head around how to implement RESTful CRUD on the backend PHP in order to interface well with what ExtJS expects. So I had a look at http://extjs.com/deploy/ext-3.0-rc2/examples/restful/restful.html in Firebug and have some questions.

1. When the DataStore is RESTful, does that mean any batched changes are issued as multiple requests e.g. if I have a batch of 2 deletes and 2 inserts, I will end up with 2 DELETE and 2 POST?

2. Do the CRUD updates happen asynchronously, i.e. when the DataStore.save is called (presumably either manually or in an autosave situation), the call can return before the response is returned from the server?

3. In a batch situation and assuming #1 is true, are all the changes sent asynchronously or does the e.g. second DELETE block until the results of the first DELETE are processed?

4. How can the backend signal to ExtJS that a DELETE, POST or PUT failed? Is it sufficient to send back some HTTP error code and the DataStore and associated machinery then back out of the proposed changes? If I send back an error code, can I still report an error message of some kind from the server?

5. The HttpProxy and Connection machinery appear to encode PUT and POST as x-www-form-url-encoded with (usually) a single "data" field. Where is the name of the field configured? How do I get the machinery send the data as just plain JSON?

6. The POST (or create) returns the sent rows, presumably for updating server-assigned ID's. Does the backend have to return all the fields or only the changed fields and/or id?

7. The PUT (or update) sends also an ID field but this duplicates the ID field in the URL (and in the record itself)?

8. All the CRUD methods in the sample return JSON with a success field and a message field. Besides displaying the message in the app popup, does the framework itself rely on these fields?

Thanks in advance for helping out.

Cheers,
Glen Low
Pixelglow Software

Sgt.Pepper
15 Jun 2009, 11:44 PM
1) Every data record is considered to be a single representation of a resource. Each of these resources has its own specific URL. So if you have a list of e.g. two Users (say user1 and user2) and you delete both, the client sends one DELETE request to http://myapplication/user1 and another one to http://myapplication/user2. So yes, basically every single CRUD operation needs its own request.

3) I don't know how this is handled in EXTJS, but i guess (and hope) that this is a nonblocking operation.

5) See this thread http://www.extjs.com/forum/showthread.php?t=70572, as well as this blog http://www.extjswithrails.com/2009/06/restful-store-example-with-rails_04.html

pixelglow
16 Jun 2009, 4:08 AM
1) Every data record is considered to be a single representation of a resource. Each of these resources has its own specific URL. So if you have a list of e.g. two Users (say user1 and user2) and you delete both, the client sends one DELETE request to http://myapplication/user1 and another one to http://myapplication/user2. So yes, basically every single CRUD operation needs its own request.


I see that it probably can't be avoided semantically for PUT and DELETE since these operations must correspond to a single addressable resource. I think the technical RESTful term is "idempotent". I wonder though if we could or should support a multiple object POST, since POST doesn't correspond to a single response and this would help with multiple objects being inserted and would work well with relational databases in the back end and parameterized INSERT statements.


5) See this thread http://www.extjs.com/forum/showthread.php?t=70572, as well as this blog http://www.extjswithrails.com/2009/06/restful-store-example-with-rails_04.html

Interesting. Your solution looks pretty clean.