📜 ⬆️ ⬇️

REST / CRUD. Am I cooking it wrong?

Introduction


REST is a very interesting method for working with objects on the server. Implementing a CRUD interface using REST is quick and easy! Today I will try to show which of the approaches in REST / CRUD in my opinion are erroneous and detrimental to the project.

Must have for all resources


To continue the narration, you should immediately determine the minimum set of object properties, addressing, and other delights. Let's start:
The important difference between slug and id


For sql-tankers (no offense) I will explain:

For example, users.id is an id, but users.login is quite a slug.

And now we take for an axiom “all objects must have the specified properties,” and, so as not to blur over the object, we stuff them, for example, in the property "__link__":
{ __link__: { url: 'http://localhost/api/users/vasya', urn: '/api/users/vasya', resource: 'users', slug: 'vasya' } } 

By the way, this is a valid object. He could return from some url (not “ localhost / api / users ”). The developer, having received such an object, should request the data by url / urn from __link__, but more on that later.
By the way, "according to the correct" - do not add. property "__lnk__", and use for these purposes the headers of the format X-URI, X-URN, X-URL, etc, but not all weber like to work with headers. Mol took the body and ran.
Hmmm ... and how many implementations of the REST API have similar properties? Maybe I invented the bike?

CRUD / Read or HTTP / GET


It's simple. We take the URL, we request the object, and ... And what about "and"? And sometimes we see the following answer:
Example:
  HTTP / GET http: // localhost / api / users 

 { status: true, count: 100, data: [ { login: 'vasya', name: 'Vasilii', id: 146, __link__: { url: 'http://localhost/api/users/vasya', urn: '/api/users/vasya', resource: 'users', slug: 'vasya' } }, { login: 'petya', name: 'Petr', id: 145, __link__: { url: 'http://localhost/api/users/petya', urn: '/api/users/petya', resource: 'users', slug: 'petya' } } ] } 

What's wrong here:
Conclusion: I do not know if this is correct, but so far all the arguments are not in favor of this approach.
')
The answer should be:
 [ { __link__: { url: 'http://localhost/api/users/vasya', urn: '/api/users/vasya', resource: 'users', slug: 'vasya' } }, { __link__: { url: 'http://localhost/api/users/petya', urn: '/api/users/petya', resource: 'users', slug: 'petya' } } ] 

At the same time count should live in HTTP / HEAD, since does not have a direct relationship to the resource, and describes the state of the collection. Again, you need to display the number of users. How to be? Choose everything and count in javascript? Write a new url to get count of items? What for? Make an HTTP / HEAD request for a collection. There will be no data, only the headings will return.

Skeptics will say: to draw all the elements, you will have to make a lot of requests to the backend,
My answer: HTTP / 1.1 / Keep-alive will save us all. When using javascript frameworks and caching - most of the data will be requested during initialization, one-time, and after - they will only exchange requests with the HTTP / 304 response (the resource has not changed).

Do you know why weber do not like this approach? They have to control the receipt of data from the server for normal display.
For example: If the data arrives immediately with the properties, you can immediately draw several divs in the loop without worrying about synchronization. If you make 4 requests to display 4 lines with users, you must somehow monitor the status of all requests. Those. until ALL requests have worked - do not draw anything. See Promise and Future to solve these problems. They all have code for forced synchronization, although I would draw right away as it is ...

PS At 2:30 o'clock. I really wanted to tell you about the nesting of resources, about the massive DELET / POST / PUT ... but I guess I'll go to sleep. I will leave this opus to the public. I ask you to comment in the comments - is it worth it to continue or the truisms are not in fashion?
PPS I would be very grateful to people for the different rakes that had to occur when working with REST API from third-party developers.
Thanks to all!

Source: https://habr.com/ru/post/242179/


All Articles