假设我有以下架构和数据:
const user = new schema.Entity('users')
const video = new schema.Entity('videos', {
user
})
const comment = new schema.Entity('comments', {
video
})
// Sample Data
const users = [
{ id: 1, object: "User", name: "User 1" },
{ id: 2, object: "User", name: "User 2" },
]
const videos = [
{ id: 1, object: "Video", user: users[0] },
{ id: 2, object: "Video", user: users[1] },
]
const comments = [
{ id: 1, object: "Comment", video: videos[0] },
{ id: 2, object: "Comment", video: videos[0] },
]
我将如何设置一个resource
和resourceList
模式来包装我的所有模式,以便在我的 api 调用中我只需要引用resource
或resourceList
例如:
api.videos.list().then(
// response.data is an array containing multiple videos
response => normalize(response.data, resourceList)
)
// entities: { videos: [...], users: [...] }
或者
api.videos.retrieve(videoId).then(
// response.data is an object containing a single video
response => normalize(response.data, resource)
)
我试过这样做:
const resource = {
user,
video,
}
const resourceList = new schema.Array(resource, inferSchema)
function inferSchema({ object: objectName = '' }) {
return objectName.charAt(0).toLowerCase() + objectName.slice(1)
}
但这似乎不起作用。