1

向 REST API 请求以下 upsert 的正确方法是什么?

我正在努力构建一个 NoSQL 集合,该集合基于对前端应用程序的最请求返回。

假设您有以下文档:

{
    '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
    } 
}

需要将元素插入到 user.private_comments 数组中。

根据https://www.rfc-editor.org/rfc/rfc6902#appendix-A.5,我可以请求替换指令 PATCHing 以下数据:

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

问题是在这种情况下 '$index' 是未知的。

我想出的一个可能的解决方案是创建类似于以下操作的内容:

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

但是,实现 API 的用户不应在 PATCH 请求中提供 id 值,因为该值已经可以通过接收令牌访问。我应该将操作简化为:

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

并在后端处理它,所以当它和 upsert 操作没有'find'和'find_by'变量时,我假设'find_by':'id'和'find':value_from_token?

此外,我无法在漏洞文档上执行简单的 GET/UPDATE,因为用户没有收到漏洞文档,因此更新会损害数据。

4

0 回答 0