【问题标题】:Is it possible to have nested models instead of master detail, loopback?是否有可能有嵌套模型而不是主细节,环回?
【发布时间】:2017-11-20 07:00:19
【问题描述】:

是否可以制作或构建本身包含模型的模型? 我建造了类似休耕的东西,但不是我真正想要的。我知道如何在 SQL 数据库或 noSQL 中详细说明它。

{
  "name": "test",
  "plural": "tests",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "fam": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "tests":{
      "testname":{
      "type": "string"
    },
    "score":{
      "type": "string"
    }
}
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

但在explorer 中显示为POSTGET 数据:

{
  "name": "string",
  "fam": "string",
  "description": "string",
  "tests": {},
  "id": "string"
}

【问题讨论】:

  • 为什么你认为这是个好主意?
  • 我不知道这是好是坏,我想试试。

标签: mongodb nosql loopbackjs loopback


【解决方案1】:

您可以在 json 中定义 hasMany/belongTo 关系(一对多)。确保您定义了外键。例如:我定义了一个人模型,有这样定义的关系:

"person": {
  "type": "embedsMany",
  "model": "person",
  "foreignKey": "boss"
},
"person": {
  "type": "belongsTo",
  "model": "person",
  "foreignKey": "boss"
}

您应该在资源管理器中看到一个额外的 GET /person/{id}/person 方法,该方法返回哪个人属于哪个人(老板)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多