1

What is the proper way to request the following upsert to a REST API?

I'm struggling to structure a NoSQL collection, which is based on the most requested returns to the front-end application.

Suppose you have the following document:

{
    'user' : {
        'private_comments': [
            //object available only to user 1
            { 
                'id': 1,
                'bar': 'He is very good',
                '...': '...'
            },
            //object available only to user 2
            { 
                'id': 2,
                'bar': 'He is very bad',
                '...': '...'
            }
        ],
        'public_comments': '' //infos available to all users
    } 
}

It is needed to upsert an element to the user.private_comments array.

According to https://www.rfc-editor.org/rfc/rfc6902#appendix-A.5, I could request a replace instruction PATCHing the following data:

{ 'op': 'replace', 'path': '/user/comments/$index', 'value': 'Actually, he is OK'}

The problem is that '$index' is unknown in this case.

A possible solution that I came up with was to create something like the following operation:

{ 'op': 'upsert', 'find_by': 'id', 'find': 1, 'path': '/user/comments', 'value': 'Nope, he really sucks' }

However, the user implementing the API should't provide the id value inside the PATCH request, because this value is already accessible via the receive token. Should I simplify the operation to:

{ 'op': 'upsert', 'path': '/user/comments', 'value': 'Nope, he really sucks' }

and treat it at the backend, so when it's and upsert operation without 'find' and 'find_by' variables I assume 'find_by': 'id' and 'find': value_from_token?

Also, I cannot do a simple GET/UPDATE at the hole document because the user doesn't receive the hole document, so an update would compromise the data.

Community
  • 1
  • 1
vibn
  • 11
  • 3
  • I guess internally you could do whatever you want but once you go public with your API having unstandardized terms like `upsert` or `find_by` might not be such a good idea – Alexandru Marculescu Oct 07 '16 at 09:41
  • Thanks for the response @AlexandruMarculescu. Sorry for this very late reply. I still have kind of the same problem. What would be a standard term for upsert or find_by in this case? – vibn Feb 08 '17 at 20:49

0 Answers0