Created
April 29, 2015 17:49
-
-
Save vastus/02e27b9b954d08d4d1e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pg = require('pg'); | |
| var c = require('./config'); | |
| var db = (function db() { | |
| 'use strict'; | |
| var cnxStr = `postgres://${c.db.user}:${c.db.password}@${c.db.host}/${c.db.database}` | |
| function query(sql, params) { | |
| params = params || []; | |
| return new Promise(function (resolve, reject) { | |
| pg.connect(cnxStr, function(err, client, done) { | |
| if (err) { | |
| return reject(err); | |
| } | |
| client.query(sql, params, function (err, result) { | |
| // Release client back to pool. | |
| done(client); | |
| if (err) { | |
| return reject(err); | |
| } | |
| return resolve(result); | |
| }); | |
| }); | |
| }); | |
| } | |
| return { | |
| query: query, | |
| }; | |
| })(); | |
| module.exports = db; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var db = require('./db'); | |
| db.query('select * from users').then(function (d){ | |
| console.log('hi', d); | |
| }, function (data) { | |
| console.log('a', data); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment