normalizr-反规范化方法-填充对象键

时间:2018-07-10 21:49:45

标签: javascript normalizr

normalizr v3

我正在尝试理解 normalizr ,特别是 denormalize 方法。

有一个对象兴趣,键是关系的ID。 您将如何用正确的对象替换键值?

const normalizr = require('normalizr')

const interestsSchema = new normalizr.schema.Entity('interests')

const usersSchema = new normalizr.schema.Entity('users', {
  interests: new normalizr.schema.Object(interestsSchema),
})

const entities = {
  users: {
    idA: { name: 'userA', interests: { a: true } },
    idB: { name: 'userB', interests: { b: true } },
    idC: { name: 'userC', interests: { c: true } },
  },
  interests: {
    a: 'Interest A',
    b: 'Interest B',
    c: 'Interest C',
  },
}


const denormalizedData = normalizr.denormalize(
  entities.users.idA,
  usersSchema,
  entities
)

console.log(denormalizedData)

当前输出

{ name: 'userA', interests: { a: true } }

必需的输出

{ name: 'userA', interests: { a: 'Interest A' } }

0 个答案:

没有答案