是否可以为一个数据库表建立模型(客户、 vendor )? 我制作了一个合作伙伴模型,它使用合作伙伴表。我的客户和 vendor 模型继承自合作伙伴模型。
http://localhost:3000/api/partners
返回数据库表中的所有数据,但是
http://localhost:3000/api/vendors
没有任何结果。
vendor 和客户几乎相同,只是它们的类型和另外两个属性不同。我想从端点获取类型为“vendor ”的 partner
表中的所有记录
http://localhost:3000/api/vendors
以及来自
的client
类型的所有合作伙伴
http://localhost:3000/api/clients
(http://localhost:3000/api/partners
应该被隐藏)
编辑 models/vendor.js
{
"name": "vendor",
"base": "partner",
"strict": false,
"idInjection": false,
"options": {
"validateUpsert": true,
"postgresql": {
"table": "partner"
}
},
...
}
重启我的服务器后我仍然得到:
{
"links": {
"self": "http://localhost:3000/api/vendors"
},
"data": []
}
在资源管理器中,我可以在 vendor
的 Model Schema 上看到 partner
的属性
最佳答案
是的。您只需要更新 options on the models' config files指向一个特定的表。确保您只识别每个模型中属于该模型(而不是另一个模型)的属性,然后您可以手动 inject the type
before saving :
在 common/models/partner.json
和 common/models/vendor.json
中:
...
"options": {
"mysql": {
"table": "partner"
}
}
...
然后在每个模型的JS文件(common/models/vendor.js
)添加一个before save
钩子(Hook):
module.exports = function(Vendor) {
Vendor.observe('before save', function(ctx, next) {
ctx.instance.type = 'vendor';
next();
});
};
关于javascript - 两个模型的相同数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35103092/