Git Product home page Git Product logo

Comments (3)

rgcl avatar rgcl commented on June 11, 2024

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.

flimzy avatar flimzy commented on June 11, 2024

Thanks for the comments and explanation! I look forward to the finished product :)

from dbh-pg.

rgcl avatar rgcl commented on June 11, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.