Home > Back-end >  how can insert array consisting of multiple objects using sequelize and node?
how can insert array consisting of multiple objects using sequelize and node?

Time:10-27

In below block line of code i want to insert an array of multiple object. but it's not working.

let Bookarray = [{author:"john matt", BookName:"Beauty of Nature"},
{author:"Mick Foley", BookName:"Have A Nice Day"},
{author:"Kevin Sullivan.", BookName:"WWE 50"},
{author:"Brian Shields And Dean Miller", BookName:"35 Years Of Wrestlemania"},
{author:"Mick Foley", BookName:"Foley Is Good"},
]
 
let InsertObj = await BooksDetails.create(Bookarray);

What should i do?

CodePudding user response:

For an array containing multiples objects use bulkCreate.

let InsertObj = await BooksDetails.bulkCreate(Bookarray);

CodePudding user response:

i don't know for certain but I imagine

Promise.all( Bookarray.map(BooksDetails.create) )

Will do individual inserts. I bet there is a way to insert multiple records that would be more efficient.

Do what @ijaz recommends

  • Related