Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "q-postgres", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Postgres with promises.", | ||
@@ -22,5 +22,5 @@ "main": "q-postgres.js", | ||
"engines": { | ||
"node": "~4.2.2", | ||
"npm": "~2.14.7" | ||
"node": "~4.3.1", | ||
"npm": "~3.7.3" | ||
} | ||
} |
'use strict'; | ||
var Promise = require('q').Promise; | ||
var q = require('q'); | ||
var Postgres = require('pg'); | ||
var PgWrapper = require('./pg.wrapper'); | ||
function PgPromise(user, pass, host, base) { | ||
function PgPromise(user, pass, host, base, url) { | ||
var pgPromise = this; | ||
user = String(user); | ||
pass = String(pass); | ||
host = String(host); | ||
base = base && String(base).length ? '/'.concat(base) : ''; | ||
var connString = url; | ||
var connString = 'pg://'.concat(user, ':', pass, '@', host, base); | ||
if (!url) { | ||
user = String(user); | ||
pass = String(pass); | ||
host = String(host); | ||
base = base && String(base).length ? '/'.concat(base) : ''; | ||
connString = 'pg://'.concat(user, ':', pass, '@', host, base); | ||
} | ||
pgPromise.connect = function() { | ||
return Promise(function(resolve, reject) { | ||
return q.Promise(function(resolve, reject) { | ||
Postgres.connect(connString, function(err, client, done) { | ||
@@ -19,0 +22,0 @@ if (err) { |
# q-postgres | ||
Postgres with promises. | ||
### Sample | ||
```javascript | ||
var env = process.env.NODE_ENV || 'development'; | ||
var props = require('./server.json')[env]; | ||
var db = props.db; | ||
var poolPostgres = new qPostgres(db.user, db.pass, db.host, db.base, process.env.DATABASE_URL); | ||
//Get connection from pool | ||
poolPostgres.connect().then(function(connection) { | ||
//open transaction | ||
return connection.openTransaction().then(createTableMigrations).then(function(transaction) { | ||
var sql = 'SELECT * FROM table_name'; | ||
return transaction.runScript(sql).then(function(res) { | ||
//commit transaction | ||
returm transaction.commit().then(function() { | ||
//Promise result | ||
return res.rows; | ||
}); | ||
}).catch(transaction.rollback); //rollback transaction | ||
}).then(function() { | ||
connection.end(); //Leave connection from pool | ||
}); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5454
8
63
30