aSt*_*ign 4 api json flux redux
我一直在寻找normalizr来展平以标准JSON API格式格式化的JSON API数据.谁能指点我这样做的一些例子?我特别想知道如何处理资源对象关系的normalizr模式(由JSON API标准定义).在JSON API标准中,在资源对象中定义了"relationships"属性,然后是每组相关对象的属性.以下是JSON API格式的单个产品类别的示例,其中包含两个相关产品:
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"time": "0.006"
},
"data": [
{
"type": "category",
"id": "6",
"attributes": {
"name": "Odwalla"
},
"meta": {
"product_count": "0"
},
"relationships": {
"product": {
"data": [
{
"type": "product",
"id": "4785"
},
{
"type": "product",
"id": "4786"
}
]
}
}
}
],
"included": [
{
"type": "product",
"id": "4786",
"attributes": {
"name": "Strawberry & Banana Odwalla",
"description": null,
"price": "3.19",
"upc": ""
}
},
{
"type": "product",
"id": "4785",
"attributes": {
"name": "Blueberry Odwalla",
"description": null,
"price": "3.19",
"upc": ""
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
此类别中包含的产品列在data.relationships.product.data下,并且这些产品对象包含在包含的数组中.我相信有很多方法可以规范这个; 什么是最有效和友好的方式来平整这个Flux/Redux商店?
据我所知,json:api提供了规范化数据.相关数据不直接与主资源嵌套,但是included
- 请参阅此复合文档和提取包含.normalizr不适用于这种数据结构,但是如果这是你的意图,你可以轻松地构建一些可以给你与normalizr相同结果的东西.由于json:api是一个非常严格的标准,因此非常适合使用.这就是我试图实现这一目标的方法,其中有很多来自normalizr库的"灵感"......
import { reduce, append } from 'ramda'
export default function normalize({data, included}) {
let bag = {}
const result = visit(data, bag)
visit(included, bag)
return {
entities: bag,
result
}
}
function visitIterable(obj, bag) {
return reduce((acc, item) => {
const entityId = visitEntity(item, bag)
return append(entityId, acc)
}, [], obj)
}
function visitEntity(obj, bag) {
const entityKey = obj.type
const entityId = obj.id
if (!bag.hasOwnProperty(entityKey)) {
bag[entityKey] = {}
}
bag[entityKey][entityId] = obj
return entityId
}
function visit(obj, bag) {
if (Array.isArray(obj)) {
return {
list: visitIterable(obj, bag)
}
} else if (typeof obj === 'object') {
return {
item: visitEntity(obj, bag)
}
}
}
Run Code Online (Sandbox Code Playgroud)