如何为嵌套对象指定normalizr模式

时间:2017-06-08 16:42:58

标签: ecmascript-6 redux normalizr

输入:

provider: {
                id: 1,
                name: 'abc',
                npi: 1234,
                credentials: [{
                      id: 1,
                      description: 'abc'
                 }],
                specialties: [{
                      id: 1,
                      description: 'abc'
                 }],
                 supervisingPhysician: { 
                          id: 2,
                          name: 'xyz',
                          npi: 56789,
                          credentials: [{
                                id: 1,
                                description: 'abc'
                           }],
                        }
           }

模式

import { schema } from 'normalizr';
import _ from 'lodash';

let specialtySchema = new schema.Entity('specialties');

export const providerSchema = new schema.Entity('provider', {
    specialties: [specialtySchema], primarySpecialty: specialtySchema
});

预期输出:(类似)

entities: { providers: { "1" : {..., supervisingPhysician: 2, specialties: 1},
                         "2" : {..., specialties: 1}
                       }, 
            specialties: {"1" : {...specialty object}
          }

在我的示例中,我的根提供者对象和嵌套的supervisingPhysician对象都是对象的相同类型(提供者)。

如何定义架构,以便我可以将root和supervisingPhysician一起规范化。

感谢您查看此内容。

1 个答案:

答案 0 :(得分:1)

通过命名supervisingPhysician - 实体'providers'的实体密钥,可以非常轻松地实现这一目标。

// define normal schema credentials and specialties
const credentialSchema = new schema.Entity('credentials');
const specialtySchema = new schema.Entity('specialties');

// define the supervisingPhysician schema with 'providers' as key
// normalizr will now put all supervisingPhysicians directly into the providers object
const supervisingPhysicianSchema = new schema.Entity('providers', {
  credentials: [credentialSchema]
});

// define 'root'-provider schema
const providerSchema = new schema.Entity('providers', {
  credentials: [credentialSchema],
  specialties: [specialtySchema],
  supervisingPhysician: supervisingPhysicianSchema
});

// the whole data schema
const dataSchema = { provider: providerSchema };

// ready to normalize
normalize(input, dataSchema);

这会给你以下结果:

{
    entities: {
        credentials: {
            1: {
                id: 1,
                description: "abc"
            }
        },
        specialties: {
            1: {
                id: 1,
                description: "abc"
            }
        },
        providers: {
            1: {
                id: 1,
                name: "abc",
                npi: 1234,
                credentials: [ 1 ],
                specialties: [ 1 ],
                supervisingPhysician: 2
            },
            2: {
                id: 2,
                name: "xyz",
                npi: 56789,
                credentials: [ 1 ]
            }
        }
    },
    result: {
        provider: 1
    }
}

为了澄清和参考,这就是我宣布input

的方式
const input = {
  provider: {
    id: 1,
    name: "abc",
    npi: 1234,
    credentials: [{ id: 1, description: "abc" }],
    specialties: [{ id: 1, description: "abc" }],
    supervisingPhysician: {
      id: 2,
      name: "xyz",
      npi: 56789,
      credentials: [{ id: 1, description: "abc" }]
    }
  }
};
相关问题