Bulk Insert/Create
It'd be good if Loopback supported inserting many records at once. Many data sources would be able to insert records into their datasets far more efficiently. e.g. SQL databases can import all the records at once
Efficiency improvements could also be realized in validations. For example, where a model checks for uniqueness, all the instances could be checked at once rather than individually.
+1
+1
+1
Il giorno mar 19 mag 2015 21:21 Vlad Miller [email protected] ha scritto:
+1
— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/1275#issuecomment-103640495 .
+1
+1
+1
I'm implementing a mixin #1435 that inserts lft and rgt values (a tree table) based on others records in database. When I try insert several records at once, for example, suppose that the database is empty:
Category.create([
{name: 'category 1'},
{name: 'category 2'}], function (err, categories) {
console.log(categories);
});
The expected values for lft and rgt values are
[
{ name: 'category 1', lft: 1, rgt: 2, id: 1 },
{ name: 'category 2', lft: 3, rgt: 4, id: 2 }
]
but the before save event is triggered 2 times. The second time is for {name:'category2'} and in this moment the category 1 is not in database yet and I don't know that the category 1 is going to be inserted. So I can't calculate the right lft and rgt values. So what I got is
[
{ name: 'category 1', lft: 1, rgt: 2, id: 1 },
{ name: 'category 2', lft: 1, rgt: 2, id: 2 }
]
I can see several ways to solve this problems. I'm listing them below:
- The
before savehook to be called only 1 time with a array of instances - When the
before saveis called for the second time, the first record is already in database - Everything remains as it is, but pass more one property in ctx with all instances to be inserted, so I will know what is happening.
OBS: If there is something wrong with my English, please correct me.
+1
+1
+1
+1
+1 I tried inserting 999 records with a medium sized document and looping for 100 times inside to insert related documents. It took more than 30 minutes to accomplish.
+1
+1
+1
+1
+1
+1
+1
+1