使用JSON API表示非资源聚合数据

Ern*_*sto 5 rest json api-design json-api

我正在研究一个应用程序,我们正在研究使用jsonapi来描述所有API响应中的数据的可能性.它对于类似资源的实体非常有效,但我们在提出一种以类似jsonapi的方式描述报告数据的方法时遇到了一些麻烦.

通过报告数据,我指的是从我们存储在数据库中的基本资源类数据中即时聚合和计算的数据.例如,想象一下我们存储房地产信息,我们有关于房屋,公寓和办公空间的信息,每个都与位置,房屋面积(平方英尺),房产类型(房屋,公寓或办公空间)相关联,以及有关房产的任何其他相关信息.现在想象一下,我们需要一个接收的报告,?group_by[]=location&group_by[]=type我们希望响应能够传达有关这两个group_by参数交集的汇总信息.因此,我们将收到一个对象,其中包含给定位置中所有属性的平均平方英尺面积,也按属性类型分组.

Average Property Sizes (in square feet)
                Houses    Apartments   Offices
Manhattan      1234.56        234.56    123.45
Cape Coral      456.78        654.32    876.54
Portland       4321.00        987.65   2345.67
Run Code Online (Sandbox Code Playgroud)

我们可以从这些数据中想到的最类似资源的是每个单元格,但由于它们是更多基本数据的计算聚合的结果,因此它们没有自然ID.我们一直在思考与计算ID提交他们以及(也许结合通过它们的数据分组的尺寸,即的ID "house,34",其中house代表一种类型的属性,34是位置"曼哈顿"的ID).然后,每个单元将与相应的位置记录具有关系,该记录将包括在included有效载荷的部分中.这是一个示例json文件的示例:

{
  "data": [
    {
      "id": "house,123",
      "type": "report_items",
      "attributes": {
        "property_type": "house",
        "value": 108.75
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 123
          }
        }
      }
    },
    {
      "id": "house,124",
      "type": "report_items",
      "attributes": {
        "property_type": "house",
        "value": 36.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 124
          }
        }
      }
    },
    {
      "id": "house,125",
      "type": "report_items",
      "attributes": {
        "property_type": "house",
        "value": 1.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 125
          }
        }
      }
    },
    {
      "id": "office,123",
      "type": "report_items",
      "attributes": {
        "property_type": "office",
        "value": 4.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 123
          }
        }
      }
    },
    {
      "id": "office,124",
      "type": "report_items",
      "attributes": {
        "property_type": "office",
        "value": 2.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 124
          }
        }
      }
    },
    {
      "id": "apartment,123",
      "type": "report_items",
      "attributes": {
        "property_type": "apartment",
        "value": 2.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 123
          }
        }
      }
    },
    {
      "id": "apartment,125",
      "type": "report_items",
      "attributes": {
        "property_type": "apartment",
        "value": 4.5
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 125
          }
        }
      }
    },
    {
      "id": "apartment,124",
      "type": "report_items",
      "attributes": {
        "property_type": "apartment",
        "value": 2.0
      },
      "relationships": {
        "location": {
          "data": {
            "type": "locations",
            "id": 124
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "locations",
      "id": "123",
      "attributes": {
        "name": "Manhattan"
      }
    },
    {
      "type": "locations",
      "id": "124",
      "attributes": {
        "name": "Cape Coral"
      }
    },
    {
      "type": "locations",
      "id": "125",
      "attributes": {
        "name": "Portland"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:这是在jsonapi中表示这种数据的正确方法吗?jsonapi是否适合和/或推荐用于不直接映射到资源的数据?我会更好地在自定义json中表示这些数据吗?我知道非这些问题可能有一个明确的答案,但也许已经有一些关于如何处理类似场景的经验,试图使这种数据适合jsonapi等的利弊.任何评论和帮助是非常的非常感激.谢谢.

PS:我甚至在论坛和互联网上进行了一些挖掘之后发布了这些内容,这些是我发现的唯一两个类似于我想要找到的内容的链接,我将它们包含在这里以供参考好的:1.- http://discuss.jsonapi.org/t/composite-id-inside-the-resource-object/367/13 2.- http://discuss.jsonapi.org/t/extension-for -chart-图的数据/ 408

Dan*_*rdt 4

一般的答案是考虑哪些数据足够重要以保证 API 双方的身份。我的意思是决定您要稍后引用哪些内容或用关系来表示哪些内容。JSON API 允许您将这些内容定义为资源,并允许您将资源与更通用的 JSON 混合以获取不透明的数据。

例如,也许reports您用来创建它们的选项和过滤器值得跟踪,以便客户可以通过其请求重新查看同一报告id。也许您想轮询您的服务器以查看正在创建哪些报告。

在客户端,您可能希望提供从property_type资源到有关这些属性类型的更多信息的链接。

或者,也许报告中的结果更好地表示为资源中的 JSON blob。attributes并且meta可以包含任何类型的 JSON 值。

在您的特定情况下,您的主要资源可以是 类型reports,或者 的数组,甚至可能是与和关系report_items的数组。property_summariesproperty_typeslocations

如果您选择更通用的资源类型,则可以概括报告过程,但可能无法捕获数据的重要性。

如果您选择非常具体的报告资源,则需要真正自定义每种类型的报告,但您将能够在客户端的资源之间建立有意义的连接。