【问题标题】:Loopback.io belongsTo in RESTREST 中的 Loopback.io 属于To
【发布时间】:2015-11-04 22:44:04
【问题描述】:

我对loopback.io的belongsTo关系函数有点困惑

那么我们来看下面的例子:

我有一个名为 Project 的模型,它与 Customer 对象有关系。所以项目属于客户。

这是我的项目模型

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}

所以当我运行应用程序并转到 http://0.0.0.0:3000/explorer 时,我可以看到 API。但是当我去项目时,我只看到

 GET /Projects/{id}/customer     Fetches belongsTo relation customer.

我也期待其他功能,例如

 POST /Projects/{id}/customer 
 DELETE /Projects/{id}/customer 

为什么它们在这里不可用?或者如何通过 REST API 为项目设置客户?

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    我们先了解belongsTo关系,来了解为什么只创建GET rest endpoint:

    GET /Projects/{id}/customer     Fetches belongsTo relation customer.
    

    以及为什么 Loopback 没有创建以下链接:

    POST /Projects/{id}/customer 
    DELETE /Projects/{id}/customer
    

    belongsTo 关系在两个模型实例之间创建一对一的关系。它用于将一个实例的所有权提供给另一个实例。在您的情况下,项目模型实例属于客户模型实例。现在由于项目属于客户,因此客户拥有项目实例,因此对其余端点进行说明。

    GET /Projects/{id}/customer     Fetches belongsTo relation customer.
    

    由于客户可以拥有项目实例,因此上述是有效的,因为可以为项目获取客户。

    POST /Projects/{id}/customer
    DELETE /Projects/{id}/customer
    

    由于客户不属于某个项目,而是拥有项目,因此上述其余端点没有意义,因为项目创建或放弃其所有者(客户)。

    【讨论】:

    • “客户拥有该项目”解释了一切。我将在项目端放置一个 belongsTo,在客户端放置一个 hasMany
    • 正确。这将完成双方的关系。
    【解决方案2】:

    首先,您的 Project 模型缺少您为 belongsTo 关系提到的 customerId 字段。

    这里是项目模型

    {
      "name": "Project",
      "plural": "Projects",
      "base": "PersistedModel",
      "strict": false,
      "idInjection": false,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "title": {
          "type": "string",
          "required": true
        },
        "description": {
          "type": "string"
        },
        "dateCreated": {
          "type": "date"
        },
        "customerId": {
          "type": "string",
          "required":true
      },
      "validations": [],
      "relations": {
        "customer": {
          "type": "belongsTo",
          "model": "Customer",
          "foreignKey": "customerId"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    现在,您的 Customer 模型还应该具有关系 hasMany 的其他部分。没有它,它将无法按预期工作。

    这是客户模型的代码

    {
      "name": "Customer",
      "plural": "Customers",
      "base": "PersistedModel",
      "strict": false,
      "idInjection": false,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "name": {
          "type": "string",
          "required": true
        },
        "email": {
          "type": "string",
          "required": true
        },
        "dateCreated": {
          "type": "date"
        }
      },
      "validations": [],
      "relations": {
        "projects": {
          "type": "hasMany",
          "model": "Project",
          "foreignKey": "customerId"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    现在与客户关系的双方 项目已定义,您的 API 应具有如下端点:

    GET Customers/{id}/Projects
    POST Customers/{id}/Projects
    PUT Customers/{id}/Projects
    

    您提到的端点无效,因为您不能为项目创建客户,而是以其他方式。

    【讨论】:

      【解决方案3】:

      据我所知,它不是那样工作的。为了创建链接,您需要创建客户,然后将该客户的 id 用作项目模型中的 id。

      所以您的 API 调用将是:

      发布/客户

      创建客户,然后

      发布/项目

      创建项目。

      在第二篇文章中,您需要指定 CustomerId 以将项目链接到客户,或者您需要在事后使用 customerId 更新项目。

      然后:

      项目/{id}/客户

      检索属于该项目的客户。

      或者您可以在其中一个模型上编写自己的远程方法,以便在一次调用中执行此操作。

      完整的关系 API 列表可以在这里找到:

      StrongLoop Relations API

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-15
        • 1970-01-01
        • 2018-06-04
        相关资源
        最近更新 更多