javascript - Sails.js best practice in using transactions with promises (Postgres) -


i'm using sails 0.9.16 postgres , question is: best way execute transaction using current api promises? may there better than:

    model.query('begin transaction', function (err) {       if (err) {         next(err);       } else {         model           .create(...)           .(function (value) {             return [value, relatedmodel.create(...).then(...)];           })           .fail(function (err) {             model.query('rollback');             next(err);           })           .spread(function (...) {             model.query('commit')             next(...);           })       }     }) 

thanks help!

i'm using exact workflow. executing 1 query promises this:

model  .query(params)  .then(function(result){  //act on result  })  .catch(function(error){  //handle error  })  .done(function(){  //clean  }); 

to execute multiple queries in parallel, this:

var promise = require('q');  promise.all([      user.findone(),     anothermodel.findone(),     anothermodel2.find()  ]) .spread(function(user,anothermodel,anothermodel2){     //use results }) .catch(function(){     //handle errors }) .done(function(){     //clean }); 

if you're trying avoid nesting in code:

model .query(params) .then(function(result){//after query #1     //since you're returning promise here, can use .then after     return model.query(); }) .then(function(results){//after query#2     if(!results){         throw new error("no results found in query #2");     }else{         return model.differentquery(results);     }  }) .then(function(results){ //do results }) .catch(function(err){     console.log(err); }) .done(function(){     //cleanup }); 

note: currently, waterline uses q promises. there pull request switch waterline q bluebird here: waterline/bluebird

when answered question, i'd yet take database class in college, didn't know transaction was. did digging, , bluebird allows transactions promises. problem is, isn't built sails since it's of special use case. here's code bluebird provides situation.

var pg = require('pg'); var promise = require('bluebird'); promise.promisifyall(pg);  function gettransaction(connectionstring) {     var close;     return pg.connectasync(connectionstring).spread(function(client, done) {         close = done;         return client.queryasync('begin').then(function () {             return client;         });     }).disposer(function(client, promise) {         if (promise.isfulfilled()) {             return client.queryasync('commit').then(closeclient);         } else {             return client.queryasync('rollback').then(closeclient);         }         function closeclient() {             if (close) close(client);         }     }); }  exports.gettransaction = gettransaction; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -