如何为嵌套对象指定 normalizr 模式
how to specify normalizr schema for nested object
输入:
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。
感谢您的关注。
这可以很容易地通过命名 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" }]
}
}
};
输入:
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。
感谢您的关注。
这可以很容易地通过命名 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" }]
}
}
};