0

输入:

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。

谢谢你看这个。

4

1 回答 1

2

这可以通过命名supervisingPhysician-entity的实体键很容易地实现'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" }]
    }
  }
};
于 2017-06-08T20:00:04.433 回答