Model 模型关系定义的约定
是在模型JSON文件中定义EmbeddesOne等关系的正确环回约定,如下所示:Model 模型关系定义的约定,model,loopbackjs,strongloop,Model,Loopbackjs,Strongloop,是在模型JSON文件中定义EmbeddesOne等关系的正确环回约定,如下所示: { "name": "Customer", "base": "PersistedModel", "idInjection": true, "properties": { "name": { "type": "string" } }, "relations": { "address": { "type": "embedsOne", "
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"address": {
"type": "embedsOne",
"model": "Address",
"property": "billingAddress",
}
}
}
Customer.embedsOne(Address, {
as: 'address', // default to the relation name - address
property: 'billingAddress' // default to addressItem
});
或者模型的JS文件,如下所示:
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"address": {
"type": "embedsOne",
"model": "Address",
"property": "billingAddress",
}
}
}
Customer.embedsOne(Address, {
as: 'address', // default to the relation name - address
property: 'billingAddress' // default to addressItem
});
如果不更改模型结构,则首选第一个 第二个是对称的。在动态结构中,您还需要使用第二个免责声明:我是环回项目的技术负责人 推荐的方法是通过模型JSON文件定义模型关系,如第一个示例所示 主要原因是我们的工具(从
yo loopback
到apic edit
)可以读取、理解甚至编辑JSON文件中指定的元数据,但它不能解析/编辑(任意)javascript源代码
在后台,从模型JSON文件解释关系元数据的代码调用API,如Customer.embedsOne
,因此最终结果是相同的