Loopback save hasmany related models in one request

I have two models related to the hasMany relation.

Customer hasMany CustomerPhones

When creating a new Customer I would like to pass the associated CustomerPhones as part of a single request. This seems like a general need, if the approach I was looking to implement incorrectly, what is the preferred way to do this?

This is the URL to create the client: POST /api/Customers

The request for the above url will be req.body

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

Loopback Model Configurations:

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" } } } 
+5
source share
4 answers

If this can be any help, instead of iterating you can insert numbers in one step as follows:

 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" 
+1
source

I'm not sure if this is the best solution, but here is what I ended up doing. I created a new RemoteMethod named createNew on the client. In this new remote method, I use methods added through model relationships.

 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; }); }; 

To make this a little safer, I will need to wrap it in a transaction. I was hoping to use the basic CRUD methods, but this solution if it is clean enough.

+1
source

If you are using a NoSQL database connector, you can ignore another CustomerPhone model and add the customerPhones property as an array in the Customer model.

For another for the SQL database connector, you can create a remote method that executes both POST /api/Customers and POST /api/Customers/id/CustomerPhones together. For multiple phone numbers, you can iterate over the customerPhones field in req.body and do POST /api/Customers/id/CustomerPhones every time.

0
source

Unfortunately, this is not yet implemented loopback. See this question https://github.com/strongloop/loopback-datasource-juggler/issues/846 .

0
source

Source: https://habr.com/ru/post/1237429/


All Articles