title: Create/update with Sequelize on an array of items
body: <p>I created a function to:</p>
  
  <ul>
  <li>take an array of 'labels' and look for whether they have a record in the db already</li>
  <li>create those which don't exist,</li>
  <li>and update those which do exist</li>
  <li>return a json array reporting on each item, whether they were updated/created, or resulted in an error</li>
  </ul>
  
  <p>I managed to make it work but I feel like I just made some ugly dogs' dinner!</p>
  
  <pre><code>var models = require("../models");
  var Promise = models.Sequelize.Promise;
  
  module.exports = {
  
      addBeans: function (req, callback) {
  
          Promise.map(req.body.beansArr, function (bean) {
              return models.Portfolio.findOrCreate({where: {label: bean}}, {label: bean});
  
          }).then(function (results) {   // Array of 'instance' and 'created' for each bean "findOrCreate(where, [defaults], [options]) -&gt; Promise&lt;Instance&gt;"
              var promisesArr = [];
              results.forEach(function (result) {
                  if (result[1]) {   // result[1] = wasCreated
                      promisesArr.push(Promise.resolve([result[0].dataValues.label, "created"]));   
                  } else {
                      promisesArr.push(
                          models.Portfolio.update({label: result[0].dataValues.label},
                              {where: {label: result[0].dataValues.label}}).then(function () {
                                  return Promise.resolve([result[0].dataValues.label, "updated"])
                              })
                      );
                  }
              }); 
              return promisesArr;
  
          // When it's all done create a JSON response
          }).then(function (results) {
              var resultObj = {items: []};  // JSON to return at the end
              Promise.settle(results).then(function (promiseinstances) {
  
                  for (var i = 0; i &lt; promiseInstances.length; i++) {
                      if (promiseInstances[i].isFulfilled()) {
                          resultObj.items.push({
                              item: {
                                  label: promiseInstances[i].value()[0],
                                  result: promiseInstances[i].value()[1],
                                  error: ''
                              }
                          });
                      }
                      else if (promiseInstances[i].isRejected()){
                          resultObj.items.push({
                              label: promiseInstances[i].value()[0],
                              result: 'error',
                              error: promiseInstances[i].reason()
                          });
                      }
                  }
  
              // Send the response back to caller
              }).then(function () {
                  return callback(null, resultObj);
              }, function (e) {
                  return callback(e, resultObj);
              });
  
          });
      }
  
  };
  </code></pre>
  
  <p>Question:</p>
  
  <ul>
  <li>Is there an easier or more obvious way to create/update values with Sequelize? </li>
  <li>Is my use of <code>Promise.settle()</code> appropriate for this case? I have the feeling I made this more complicated than it needs to be.</li>
  </ul>
  
  <p>I am new to Sequelize and using Promises, I'd appreciate if someone could advise on this.</p>