Friday, 15 April 2011

node.js - Create/update with Sequelize on an array of items -



node.js - Create/update with Sequelize on an array of items -

i created function to:

take array of 'labels' , whether have record in db already create don't exist, and update exist return json array reporting on each item, whether updated/created, or resulted in error

i managed create work sense made ugly dogs' dinner!

var models = require("../models"); var promise = models.sequelize.promise; module.exports = { addbeans: function (req, callback) { promise.map(req.body.beansarr, function (bean) { homecoming models.portfolio.findorcreate({where: {label: bean}}, {label: bean}); }).then(function (results) { // array of 'instance' , 'created' each bean "findorcreate(where, [defaults], [options]) -> promise<instance>" 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 () { homecoming promise.resolve([result[0].datavalues.label, "updated"]) }) ); } }); homecoming promisesarr; // when it's done create json response }).then(function (results) { var resultobj = {items: []}; // json homecoming @ end promise.settle(results).then(function (promiseinstances) { (var = 0; < 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 response caller }).then(function () { homecoming callback(null, resultobj); }, function (e) { homecoming callback(e, resultobj); }); }); } };

question:

is there easier or more obvious way create/update values sequelize? is utilize of promise.settle() appropriate case? have feeling made more complicated needs be.

i new sequelize , using promises, i'd appreciate if advise on this.

i sense work improve on codereview.se can see few issues.

is there easier or more obvious way create/update values sequelize?

well, 1 thing:

.then(function(array){ var newarr = []; array.foreach(function(elem){ newarr.push(fn(elem); } homecoming newarr; });

is just

.map(fn)

additionally, promises assimilate can return val; .then don't have return promise.resolve(val);.

so:

).then(function (results) { // array of 'instance' , 'created' each bean "findorcreate(where, [defaults], [options]) -> promise<instance>" 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 () { homecoming promise.resolve([result[0].datavalues.label, "updated"]) }) ); } }); homecoming promisesarr; })

is just

.map(function(result){ if(result[1]) homecoming [result[0].datavalues.label, "created"]; homecoming models.portfolio.update({label: result[0].datavalues.label}, {where: {label: result[0].datavalues.label}}). return([result[0].datavalues.label, "updated"]); });

however, since want work regardless of beingness resolved, you'd have do:

.then(function(results){ homecoming results.map(function(result){ if(result[1]) homecoming [result[0].datavalues.label, "created"]; homecoming models.portfolio.update({label: result[0].datavalues.label}, {where: {label: result[0].datavalues.label}}). return([result[0].datavalues.label, "updated"]); }); });

which means it'll resolve regardless, you'd phone call .settle():

.settle().then(function(results){ // settle logic here });

note last:

}).then(function () { homecoming callback(null, resultobj); }, function (e) { homecoming callback(e, resultobj); });

is simply:

.nodeify(callback);

however, recommend sticking promises.

node.js promise bluebird sequelize.js

No comments:

Post a Comment