rgcl / dbh-pg Goto Github PK
View Code? Open in Web Editor NEWNode.js Database Handler for PostgreSQL writer upon pg and bluebird modules
License: MIT License
Node.js Database Handler for PostgreSQL writer upon pg and bluebird modules
License: MIT License
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
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
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
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;
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.