2015-12-04 8 views
5

hasMany関係で関連する2つのモデルがあります。ループバックは、関連する複数のモデルを1回のリクエストで保存します

Customer hasManyのCustomerPhones

新しいCustomerを作成するとき、私は一部として単一の要求を関連CustomerPhonesに合格したいと思います。これは一般的なニーズのように思えますが、私が間違って実装しようとしているアプローチであれば、これを行うための好ましい方法は何ですか?

これは、顧客を作成するためのURLです:

Customer.json:上記URLのPOST /api/Customers

要求がreq.body

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

ループバック・モデルの設定になります

{ 
    "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" 
    } 
    } 
} 

答えて

0

あなたはNoSQLのデータベースコネクタを使用している場合は、別のCustomerPhoneモデルを無視してCustomerモデルにおける配列としてcustomerPhonesプロパティを追加することができます。

SQLデータベースコネクタでは、POST /api/CustomersPOST /api/Customers/id/CustomerPhonesの両方を同時に実行するリモートメソッドを作成することができます。複数の電話番号の場合は、req.bodycustomerPhonesフィールドを繰り返し、毎回POST /api/Customers/id/CustomerPhonesを実行することができます。

+0

私はSQLを使用しています。あなたが提案しているのは、私が基本的にやったことです。私はcreateという名前の新しいRemoteMethodを作った。 – MentalGrinds

+0

'CustomerPhones'は、' belongsTo'関係が動作するための 'Customer'オブジェクトに依存しているので、これを行う方法は他にありません。 –

1

これは、代わりにあなたは、このような単一のステップで番号を挿入することができます反復の任意の助けとなる可能性がある場合:

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" 
+0

私は基本的に同じ顧客の新しい顧客と新しい電話を作成したいと思っています。顧客はまだ存在しないので、私はCustomer /:id/Phoneを使用することはできません。 customerIdはまだ分かっていません。 – MentalGrinds

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メソッドを使うことを望んでいましたが、この解決法はかなりきれいです。

関連する問題