Git Product home page Git Product logo

dbh-pg's People

Contributors

rgcl avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

agad

dbh-pg's Issues

Sample code doesn't work

The code provided in README.md doesn't work in my environment. I'm unsure if this is due to the developmental (and therefore incomplete?) nature of the module. If so, I would suggest indicating as much in the documentation. I commented out the nodemailer relevant bits to test, and this is what I came up with:

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);

    })
    .done(DBH.commit)
    .catch(DBH.rollback)
    .finally(DBH.done); // release the connection to the pool

And the result is:

$ nodejs dbh-pg-test.js 

/home/jonhall/git/webui/dbh-pg-test.js:38
    .catch(DBH.rollback)
        ^
TypeError: Cannot call method 'catch' of undefined
    at Object.<anonymous> (/home/jonhall/git/webui/dbh-pg-test.js:38:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

insert works odd with js-false statements

this.insert('table', {hidden:true}); // ok
this.insert('table', {hidden:false}); 
// error: value in column "hidden" violates not-null constraint
this.insert('table', {hidden:'false'}); // ok
var x = 0;
this.insert('table', {value:x});
// error: null value in column "value" violates not-null constraint
var x = 0;
this.insert('table', {value:x+''}); // ok

The connection retains open the transaction

Example

using(db.conn(), function (conn) {
    conn.begin();
});

In this case the connection is release to poll with an open transaction!!

For now you NEED call a commit or rollback before end, example

using(db.conn(), function (conn) {
    conn.begin()
        .then(DBH.update('user', {name: 'pepe'}, { id:3 })) // some stuff
        .done(DBH.commit, DBH.rollback); // if ok commit, else rollback
});

This will be resolved in the next patch version

'the documentation example for `insert` is not valid

from the documentation:

using(dbh.conn(), function (conn) {
  return conn.insert('planet', { name: 'Mart', habitants: 343 }, 'id')
})
.then(function (planet) {
  console.log(planet.id)
  // 435
})

but the function returns an object, not a field:

{ command: 'INSERT',
  rowCount: 1,
  oid: 0,
  rows: [ { id: 24069 } ],
  fields:
   [ { name: 'id',
       tableID: 73862,
       columnID: 1,
       dataTypeID: 23,
       dataTypeSize: 4,
       dataTypeModifier: -1,
       format: 'text' } ],
  _parsers: [ [Function] ],
  RowCtor: [Function],
  rowAsArray: false,
  _getTypeParser: [Function: bound ] }

so, the only way to get the id is:

planet.rows[0].id;

error on 'update' for the table name with schema

using(db.conn(), function (conn) {
    var table_name = 'aggregator.sectors';
    return conn
        .begin()
        .then(function(){
            this.fetchOne('select * from "' + table_name + '" limit 1').then(function(record){
            return {id: record.id};
        })
        .then(function(params){
            console.log(params); //{ id: 250 }
            return this.update( table_name , {name:null}, params);
            // Unhandled rejection error: relation "aggregator.sectors" does not exist
        })
        .then(function(){ this.rollback(); })
    });
});

If I wrap table name with quotas ('"aggregator.sectors"') I get

Unhandled rejection error: zero-length delimited identifier at or near """"

And it only for the update function, the fetch goes ok

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.