两个模型相同的数据库 table

Same db table for two models

是否可以为一个数据库建立模型(客户端、供应商)table? 我制作了一个 partner 模型,它使用了 partner table。我的 clientvendor 模型继承自 partner 模型。

http://localhost:3000/api/partners

返回数据库 table 中的所有数据,但是

http://localhost:3000/api/vendors

没有任何结果。

供应商和客户几乎相同,只是它们的类型和另外两个属性不同。我想从端点

获取tablepartner类型为'vendor'的所有记录
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 模型架构

上看到 partner 的属性

是的。您只需要更新 options on the models' config files to point to a specific table. Make sure you only identify properties in each model that pertain to that model (and not the other one) and then you can manually inject the type before saving:

common/models/partner.jsoncommon/models/vendor.json 中:

...
  "options": {
    "mysql": {
      "table": "partner"
    }
  }
...

然后给每个模型的JS文件(common/models/vendor.js)添加一个before save钩子:

module.exports = function(Vendor) {
  Vendor.observe('before save', function(ctx, next) {
    ctx.instance.type = 'vendor';
    next();
  });
};