Is possible to add multiple rows at once from array with sequelize.js? This is my code:
var user = User.build({
email: req.body.email,
password: req.body.password,
userlevel: '3',
});
User
.find({ where: { email: req.body.email } })
.then(function(existingUser){
if (existingUser) {
return res.redirect('/staff');
}
user
.save()
.complete(function(err){
if (err) return next(err);
res.redirect('/staff');
});
}).catch(function(err){
return next(err);
});
Thanks for any advise!
Tip: To insert more than one row (or column) at the same time, select as many rows or columns as you want to add before you click the insert control. For example, to insert two rows above a row, first select two rows in your table and then click Insert Above.
Answer. Yes, instead of inserting each row in a separate INSERT statement, you can actually insert multiple rows in a single statement. To do this, you can list the values for each row separated by commas, following the VALUES clause of the statement.
INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.
https://sequelize.readthedocs.io/en/v3/docs/instances/#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once
User.bulkCreate([{ /* record one */ }, { /* record two */ }.. ])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With