loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Bulk Insert/Create

Open billinghamj opened this issue 11 years ago • 69 comments

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.

billinghamj avatar Apr 03 '15 17:04 billinghamj

+1

mattapperson avatar May 16 '15 12:05 mattapperson

+1

vladmiller avatar May 19 '15 19:05 vladmiller

+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 .

mrbatista avatar May 19 '15 19:05 mrbatista

+1

behrad avatar May 27 '15 12:05 behrad

+1

ainesophaur avatar Jun 03 '15 08:06 ainesophaur

+1

GaryTowers avatar Jun 03 '15 21:06 GaryTowers

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 save hook to be called only 1 time with a array of instances
  • When the before save is 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.

csbenjamin avatar Jun 12 '15 18:06 csbenjamin

+1

Neil-UWA avatar Jul 01 '15 14:07 Neil-UWA

+1

dthib avatar Jul 14 '15 22:07 dthib

+1

gruzilla avatar Aug 05 '15 17:08 gruzilla

+1

stan-ros avatar Aug 09 '15 18:08 stan-ros

+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.

aiampogi avatar Aug 24 '15 13:08 aiampogi

+1

andirotter avatar Sep 17 '15 09:09 andirotter

+1

felipetoffolo1 avatar Sep 23 '15 23:09 felipetoffolo1

+1

diaswrd avatar Sep 26 '15 21:09 diaswrd

+1

djkoell avatar Oct 14 '15 00:10 djkoell

+1

ernie58 avatar Oct 15 '15 08:10 ernie58

+1

zantiu avatar Dec 06 '15 04:12 zantiu

+1

psmarcin avatar Dec 07 '15 15:12 psmarcin

+1

helsont avatar Jan 02 '16 23:01 helsont