如何使用normalizr來分配與實體父母相關的ID/slu??Normalizr - 如何生成與父實體相關的slu// ID
例子:
爲用戶呼叫API響應:
{
id: '12345',
firstName: 'John',
images: [
{
url: 'https://www.domain.com/image0',
name: 'image0'
},
{
url: 'https://www.domain.com/image1',
name: 'image1'
}
]
}
我可以通過以下方式定義我的模式:
const image = new Schema('images');
const user = new Schema('users');
user.define({
images: arrayOf(image)
})
的問題是圖像不具備id
屬性,所以normalizr將無法區分它們,除非我們提供id
屬性。當然,我們可以做類似
const image = new Schema('images', { idAttribute: uuid.v4() });
並生成唯一的標識符。
假設我們收到用戶更新並更新了圖像的名稱。由於我們在每個規範化過程中生成唯一標識符,因此我們無法識別和更新現有圖像。
我需要的方式來引用在圖像實體的父實體(用戶)(或者在它的id /蛞蝓等12345-image0
,12345-image1
或作爲單獨的屬性。
什麼是實現這一目標的最佳方式?
'processStrategy'沒有變異的價值,你可以返回一個新的對象與id字段。 – nathancahill
'idAttribute'參數沒有引用返回的對象,它引用了原來的'value'。這就是爲什麼我必須改變原來的價值。 (除非我錯了,但我記得幾次檢查過,我也用保羅·阿姆斯特朗確認過) –
你可能是對的,'getId'函數引用了原來的'input',而不是'processedEntity'在這裏:https: //github.com/paularmstrong/normalizr/blob/a20d535c0ddd21c95cf8498647fa8e060b5ed8a8/src/schemas/Entity.js#L61 – nathancahill