Ken Kwok
Ken Kwok

Reputation: 388

Can I insert multiple record with multiple model from Loopback at the same time?

Let's say I have a post object which is divided in several models in Loopback, Post, Tags and RelatedTopic. Post has many Tags and each tag has many RelatedTopic and I want to insert multiple Post with Tags and RelatedTopic using the same create method? I found links about inserting record of multiple model and inserting multiple record but I am not sure if I could use one create method to insert multiple record, each containing multiple models or should I create each record one by one? It has been a year since I last used LoopBack and I was in 2.x and I am not really familiar with the current LoopBack. Hope someone could answer the question.Thank you.

Upvotes: 0

Views: 593

Answers (1)

ChamalPradeep
ChamalPradeep

Reputation: 429

Yes you can achieve that task using "Async" concept.

Step One :

Create your own remote method to achieve your task in one of model.js.(you can import other models also)

Step Two:

require "async" library and use parallel method invoking.

ex:

async.parallel([
    one: function(callback) {
        callback(null, 'abc\n');
    },
    two: function(callback) {
        callback(null, 'xyz\n');
    }
], function(err, results) {
    // results now equals to: [one: 'abc\n', two: 'xyz\n']
});

you can achieve multiple records and multiple models updating functionality easily.

Cheers.

Upvotes: 1

Related Questions