30

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!

2 Answers 2

68

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 */ }.. ])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The link is old. A newer one is sequelize.org/master/class/lib/…
Manual has been updated. Here is a link to new version: sequelize.org/v5/manual/…
0

There has been an update in the document. check this link

https://sequelize.org/v5/manual/instances.html#working-in-bulk--creating--updating-and-destroying-multiple-rows-at-once-

User.bulkCreate([
  { username: 'barfooz', isAdmin: true },
  { username: 'foo', isAdmin: true },
  { username: 'bar', isAdmin: false }
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.