Comments (3)
Hi, you're right, this module is under development and not yet documented. I'm working to simplify the process of releasing the connection to poll using the new "using" bluebird module (https://github.com/petkaantonov/bluebird/blob/master/API.md#resource-management).
The problem is that "done" returns void, so then there is no catch or finally.
The correct form should be:
var DBH = require('dbh-pg'),
db = new DBH('postgres://postgres@localhost/db2test');
// var nodemailer = require('nodemailer'), // no included, used for this example only
// transporter = nodemailer.createTransport(),
// sendMail = Promise.promisify(transporter.sendMail);
var Promise = require('bluebird');
db.conn(this /* scope */) // fetch a connection in the pool
.then(DBH.begin) // begin transaction
.then(DBH.fetchAll('SELECT u.email, u.amount FROM users u where u.amount > $1', [1000]))
.then(function(users) {
// create a prepared statement
var prepared = DBH.prepare('UPDATE users SET amount = 1000 where email = $email');
// create a array of promises
var emailPromises = users.map(function(user) {
// single promise: exec the prepared statement and then send the email
return this.exec(prepared(user))
.then(function() {
// sendMail({
// from : '[email protected]',
// to : user.email,
// subject : ':p',
// text : 'You had $ '
// + user.amount
// + ' in your account. Sorry.'
// })
});
}.bind(this));
return Promise.all(emailPromises);
})
.then(DBH.commit)
.done(function () {
this.done();
}, function () {
this.rollback()
.then(DBH.done); // release the connection to the pool
});
I hope to have the new more stable version soon, but for now this module is Esperimental.
from dbh-pg.
Thanks for the comments and explanation! I look forward to the finished product :)
from dbh-pg.
I just updated to v1.0.0. Still lack good documentation and more tests, so is TODO, but the API already is stable.
from dbh-pg.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbh-pg.