父母关系中的强环性
我不知道我是否只是盲目或什么,但我怎么能做到以下几点:
我有一个与UserData
模型hasOne
关系的User
模型。 我只想在User
的结果中直接使用UserData
一个属性。
User
的关系如下所示:
"relations": { "userData": { "type": "hasOne", "model": "UserData" } }
而User
的默认范围:
"scope": { "include": "userData" }
所以一个User
的结果是:
[ { "id": 5, "email": "example@example.com", "name": "Example", "userData": { "id": 5, "birthdate": "1971-09-06T00:00:00.000Z" } } ]
但是我想要的是这样的:
[ { "id": 5, "email": "example@example.com", "name": "Example", "birthdate": "1971-09-06T00:00:00.000Z" } ]
我怎样才能做到这一点?
编辑:
这两个模型定义:
ChiliUser
:
{ "name": "ChiliUser", "base": "ChiliUserData", "idInjection": true, "options": { "validateUpsert": true, "mysql": { "table": "person" } }, "properties": { "id": { "type": "number" }, "email": { "type": "string" }, "password": { "type": "string" }, "vorname": { "type": "string" }, "name": { "type": "string" }, "spitzname": { "type": "string" }, "strasse": { "type": "string" }, "plz": { "type": "number" }, "ort": { "type": "string" }, "geolat": { "type": "string" }, "geolng": { "type": "string" } }, "validations": [], "relations": {}, "acls": [], "methods": [] }
ChiliUserData
:
{ "name": "ChiliUserData", "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true, "mysql": { "table": "person_data" } }, "properties": { "id": { "type": "number" }, "person_id": { "type": "number" }, "birthdate": { "type": "date" } }, "validations": [], "relations": {}, "acls": [], "methods": [] }
你在使用内置的User
模式吗? 如果你是这样,你可以简单地使用你的UserData
模型来扩展它,使用base: "User"
,而底层模式将是你想要的:
{ "name": "UserData", "base": "User", "properties": {} ... }
但是这个设置意味着你会在你的代码中使用UserData
不是User
。 由于内置的User
模型妨碍了我的build议,所以我build议使用不同的单词,如Person
或Customer
,在这种情况下,它看起来像:
{ "name": "Person", "base": "User", "properties": { ...extra user data properties only... } ... }
这样,只有Person
将拥有所有额外的“用户数据”字段,但也会包含在node_modules/loopback/common/models/User.json
为User
定义的所有字段/方法。 如果您使用的是MySQL, Person
表将使用User
和Person
的字段的组合来创build。