【问题标题】:loopback save related hasmany models in single request环回在单个请求中保存相关的模型
【发布时间】:2016-03-09 22:16:51
【问题描述】:

我有两个通过 hasMany 关系关联的模型。

CustomerhasMany CustomerPhones

创建新的Customer 时,我想将相关的CustomerPhones 作为单个请求的一部分传递。这似乎是一种常见的需求,如果我希望实施的方法有误,那么首选的方法是什么?

这是创建客户的网址:POST /api/Customers

对上述 url 的请求将是 req.body

{
  "name": "Foo",
  "customerPhones": [
    { "phoneNumber": "8085551234" },
    { "phoneNumber": "8085554567" }
  ]
}

环回模型配置:

Customer.json

{
  "name": "Customer",
  "base": "User",
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "relations": {
    "customerPhones": {
      "type": "hasMany",
      "model": "CustomerPhone",
      "foreignKey": ""
    }
  }
}

CustomerPhone.json

{
  "name": "CustomerPhone",
  "base": "PersistedModel",
  "properties": {
    "phoneNumber": {
      "type": "string",
      "required": true
    },
    "customerId": {
      "type": "number",
      "required": true
    }
  },
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  }
}

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    我不确定这是否是最好的解决方案,但这就是我最终要做的。我在 Customer 上创建了一个名为 createNew 的新 RemoteMethod。在这个新的远程方法中,我使用通过模型关系添加的方法。

    Customer.createNew = function (data) {
      var newCustomerId;
      var customerPhones = null;
    
      if (data.customerPhones && data.customerPhones.length) {
        customerPhones = data.customerPhones;
      }
    
      return Customer
        .create(data)
        .then(function createCustomerPhones (customer) {
          newCustomerId = customer.id;
          if (customerPhones) {
            customer.customerPhones.create(customerPhones);
          }
        })
        .then(function fetchNewCustomerIncludeRelated () {
          return Customer
            .findById(newCustomerId, {
              include: [ 'customerPhones' ]
            });
        })
        .catch(function (err) {
          return err;
        });
    };
    

    为了使这更安全,我需要将其包装在事务中。我希望使用基本的 CRUD 方法,但如果这个解决方案相当干净。

    【讨论】:

      【解决方案2】:

      如果您使用的是 NoSQL 数据库连接器,则可以忽略另一个 CustomerPhone 模型并将 customerPhones 属性作为数组添加到 Customer 模型中。

      对于 SQL 数据库连接器,您可以创建一个同时执行 POST /api/CustomersPOST /api/Customers/id/CustomerPhones 的远程方法。对于多个电话号码,您可以迭代 req.body 中的 customerPhones 字段并每次都执行 POST /api/Customers/id/CustomerPhones

      【讨论】:

      • 我正在使用 SQL。你的建议是我基本上最终做的。我创建了一个名为 create 的新 RemoteMethod。
      • 我认为没有其他方法可以做到这一点,因为CustomerPhones 依赖于Customer 对象,以使belongsTo 关系起作用。
      【解决方案3】:

      如果这可能有任何帮助,您可以像这样在一个步骤中插入数字,而不是迭代:

      curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "[{
              \"number\": \"1000\",
              \"type\": \"mobile\",
              \"customerId\": 1
          }, {
              \"number\": \"1004\",
              \"type\": \"home\",
              \"customerId\": 1
          }, {
              \"number\": \"1400\",
              \"type\": \"work\",
              \"customerId\": 1
          }]" "http://127.0.0.1:3000/api/customers/1/phones"
      

      【讨论】:

      • 我想基本上在同一个通话中创建一个新客户和新电话。因为客户还不存在,所以我不能使用 Customer/:id/Phones。 customerId 还不知道。
      • 投了反对票,因为它说它想在一个请求中创建客户和电话
      • @Anoop.PA 唤醒到期并再次阅读问题。它说它想在 1 个请求中输入 2 个数据。在您的回答中,客户数据输入已经完成,您正在更新它第二个请求。所以你的答案是错误的
      【解决方案4】:

      不幸的是,这仍然没有通过环回实现。请参考本期https://github.com/strongloop/loopback-datasource-juggler/issues/846

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-24
        • 2022-08-04
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2014-09-12
        • 2015-04-14
        • 1970-01-01
        相关资源
        最近更新 更多