Rails - 如何给 Fastjson 添加分页 api?
Rails - How to add pagination to Fastjson api?
渲染的默认结果FastJsonApi gem serialized_json如下:
render json: FlashcardSerializer.new(flashcards).serialized_json
会是这样的:
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
}
]
}
我宁愿添加一些额外的信息,尤其是对于分页,我希望结果是这样的:
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
},
"count":100,
"page":1,
]
}
我知道在 API 中管理分页的其他可用 gem,并且我知道如何在没有 Fastjson 的情况下进行管理。这里的主要问题是,有没有什么方法可以从 gem 中获得上述结果,而无需对代码进行大量更改。谢谢
所需文档为 invalid according to the JSON API specification。您需要在 link 部分中包含下一个和上一个 link。 current
和 total_count
属于元部分。
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
},
]
"meta": {
"page": { "current": 1, "total": 100 }
},
"links": {
"prev": "/example-data?page[before]=yyy&page[size]=1",
"next": "/example-data?page[after]=yyy&page[size]=1"
},
}
在继续设计 API 之前先看看 JSON API specification。
您可以将这些信息作为选项参数传递给序列化程序
class FlashcardsController < ApplicationController
def index
render json: FlashcardSerializer.new(
flashcards, { links: {}, meta: { page: { current: 1 } }
).serialized_json
end
end
如何生成数据取决于您使用什么进行分页。
如果你设计一个新的 API,我也建议使用基于游标的分页而不是 offset pagination because of it's limitations。
https://github.com/Netflix/fast_jsonapi#compound-document
https://github.com/Netflix/fast_jsonapi/blob/master/spec/lib/object_serializer_spec.rb#L8-L32
渲染的默认结果FastJsonApi gem serialized_json如下:
render json: FlashcardSerializer.new(flashcards).serialized_json
会是这样的:
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
}
]
}
我宁愿添加一些额外的信息,尤其是对于分页,我希望结果是这样的:
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
},
"count":100,
"page":1,
]
}
我知道在 API 中管理分页的其他可用 gem,并且我知道如何在没有 Fastjson 的情况下进行管理。这里的主要问题是,有没有什么方法可以从 gem 中获得上述结果,而无需对代码进行大量更改。谢谢
所需文档为 invalid according to the JSON API specification。您需要在 link 部分中包含下一个和上一个 link。 current
和 total_count
属于元部分。
{
"data": [
{
"id": "1",
"type": "flashcard",
"attributes": {
"question": "why?",
"answer": "pretty good",
"slug": null
}
},
{
"id": "2",
"type": "flashcard",
"attributes": {
"question": "What is 0",
"answer": "it is 0",
"slug": null
}
},
]
"meta": {
"page": { "current": 1, "total": 100 }
},
"links": {
"prev": "/example-data?page[before]=yyy&page[size]=1",
"next": "/example-data?page[after]=yyy&page[size]=1"
},
}
在继续设计 API 之前先看看 JSON API specification。
您可以将这些信息作为选项参数传递给序列化程序
class FlashcardsController < ApplicationController
def index
render json: FlashcardSerializer.new(
flashcards, { links: {}, meta: { page: { current: 1 } }
).serialized_json
end
end
如何生成数据取决于您使用什么进行分页。
如果你设计一个新的 API,我也建议使用基于游标的分页而不是 offset pagination because of it's limitations。
https://github.com/Netflix/fast_jsonapi#compound-document https://github.com/Netflix/fast_jsonapi/blob/master/spec/lib/object_serializer_spec.rb#L8-L32