您如何处理大型关系数据属性和复合文档?
How do you handle large relationship data attributes and compound documents?
如果一篇文章有多个评论(随着时间的推移想想成千上万)。 data.relationships.comments
return 应该有限制吗?
{
"data": [
{
"type": "articles",
"id": 1,
"attributes": {
"title": "Some title",
},
"relationships": {
"comments": {
"links": {
"related": "https://www.foo.com/api/v1/articles/1/comments"
},
"data": [
{ "type": "comment", "id": "1" }
...
{ "type": "comment", "id": "2000" }
]
}
}
}
],
"included": [
{
"type": "comments",
"id": 1,
"attributes": {
"body": "Lorem ipusm",
}
},
.....
{
"type": "comments",
"id": 2000,
"attributes": {
"body": "Lorem ipusm",
}
},
]
}
当您想到复合文档 (http://jsonapi.org/format/#document-compound-documents) 时,您会开始感到担忧。这意味着,included
部分也将列出所有评论,使 JSON 有效负载非常大。
如果您想限制一次从长列表中获取的记录数,请使用分页 (JSON API spec)。
我会用 store.query
(ember docs) 单独加载 comments
,像这样 -
store.query('comments', { author_id: <author_id>, page: 3 });
这将 return 评论的相关子集。
如果您一开始不想为每个作者提出两个请求,您可以像现在一样在作者请求中包含第一个 'page'。
您可能还想查看像 Ember Infinity(未测试)这样的插件,它将提供无限滚动列表并自动发出分页请求。
如果一篇文章有多个评论(随着时间的推移想想成千上万)。 data.relationships.comments
return 应该有限制吗?
{
"data": [
{
"type": "articles",
"id": 1,
"attributes": {
"title": "Some title",
},
"relationships": {
"comments": {
"links": {
"related": "https://www.foo.com/api/v1/articles/1/comments"
},
"data": [
{ "type": "comment", "id": "1" }
...
{ "type": "comment", "id": "2000" }
]
}
}
}
],
"included": [
{
"type": "comments",
"id": 1,
"attributes": {
"body": "Lorem ipusm",
}
},
.....
{
"type": "comments",
"id": 2000,
"attributes": {
"body": "Lorem ipusm",
}
},
]
}
当您想到复合文档 (http://jsonapi.org/format/#document-compound-documents) 时,您会开始感到担忧。这意味着,included
部分也将列出所有评论,使 JSON 有效负载非常大。
如果您想限制一次从长列表中获取的记录数,请使用分页 (JSON API spec)。
我会用 store.query
(ember docs) 单独加载 comments
,像这样 -
store.query('comments', { author_id: <author_id>, page: 3 });
这将 return 评论的相关子集。
如果您一开始不想为每个作者提出两个请求,您可以像现在一样在作者请求中包含第一个 'page'。
您可能还想查看像 Ember Infinity(未测试)这样的插件,它将提供无限滚动列表并自动发出分页请求。