我想在json文件之后建立两个模型之间的关系:
{
"name": "Surveyor",
"plural": "surveyors",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "surveyors"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"dateOfBirth": {
"type": "date"
},
"birthPlace": {
"type": "string"
},
"provinceOfBirth": {
"type": "string"
},
"gender": {
"type": "string"
},
"registrationNumber": {
"type": "string",
"required": true,
"unique": true
}
},
"validations": [],
"relations": {}
},
"acls": [],
"methods": {}
}
{
"name": "Dossier",
"plural": "dossiers",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "dossiers"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"delCode": {
"type": "string",
"required": true
},
"client": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"taxCode": {
"type": "string"
}
},
"surveyor": {
"type": "string"
},
"examination": {
"type": "object"
},
"appointment": {
"type": "date"
},
"status": {
"type": "string",
"required": true
},
"notes": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
一个档案可以有一个测量员,作为关键我想在档案模型中使用属性surveyor
,对应于Surveyor模型中的属性registrationNumber
。
我想查询档案模型并在档案文档中获取测量员数据:
[GET] /api/dossiers
或使用过滤器:
[GET] /api/dossiers?filter={"include":"surveyor"}
如果可能,我想在json文件中配置关系,而不是在代码中。
答案 0 :(得分:0)
您可以使用relation generator为您创建。在您的情况下,您需要创建一个从档案到Surveyor的belongsTO relation。如果您想查询相反的方式(来自测量员的档案),您将需要创建从Surveyor到Dossier的hasMany关系。
简而言之,对于你所问的问题,你最终会得到这样的结论:
在Dossier模型中:
...
"relations": {
"surveyor": {
"type": "belongsTo",
"model": "Surveyor",
"foreignKey": "surveyor",
"primaryKey": "registrationNumber"
}
}
...