Last active
November 8, 2015 23:42
-
-
Save LinZap/d8f196f0484d58963643 to your computer and use it in GitHub Desktop.
PostgreSQL client for node.js (using Promise)
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('./node-pg-promise.js'); | |
// connect db | |
db.connect({ | |
user:'account', | |
password: 'password', | |
host: 'ip or localhost', | |
dbname: 'database name' | |
}) | |
// step 1 | |
.then(function(client){ | |
var successHandler = function(result){ | |
console.log(result.rowCount); // get rowCount | |
console.log(result.rows[0].cid); // get tuple(cid) | |
console.log(result.rows[0].cname); // get tuple(cname) | |
return result; | |
} | |
var failHandler = function(err){ | |
console.log('%s',err); | |
} | |
return db.query("select * from class where cid=?",[6]) | |
.then(successHandler,failHandler); | |
}) | |
// step 2 | |
.then(function(){ | |
// next query | |
return db.query("select * from object where oid in(?,?)",[55261,55242]) | |
.then(function(result){ | |
// print data | |
result.rows.forEach(function(item){ | |
for(var k in item) | |
console.log(k + " :" + item[k]); | |
}) | |
return result; | |
},function(err){ | |
console.log('%s',err); | |
}); | |
}) | |
.then(function(){ | |
// disconnection db | |
db.close(); | |
}) |
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
'use strict'; | |
var pg = require('pg'), | |
util = require('util'); | |
module.exports = { | |
connect: function(opt){ | |
//postgres://user:password@host:port/database | |
var connStr = "postgres://"+opt.user+":"+opt.password+"@"+opt.host; | |
connStr+= ":"+(opt.port||5432)+"/"+opt.dbname; | |
if(this.client) pg.end(); | |
return new Promise(function(resolve,reject){ | |
pg.connect(connStr,function(err, client, done) { | |
// can not handle err, throw err | |
if(err){ console.error("%s",err); return; } | |
this.client = client; | |
this.done = done; | |
resolve(client); | |
}.bind(this)); | |
}.bind(this)); | |
}, | |
query: function(){ | |
var sql = this.transformSQL(arguments[0]), | |
params = arguments[1] || []; | |
return new Promise(function(resolve,reject){ | |
this.client.query(sql, params, function(err,result){ | |
if(err) return reject(err); | |
resolve(result); | |
}) | |
}.bind(this)) | |
}, | |
close: function(){ | |
if(this.done) this.done(); | |
if(this.client) pg.end(); | |
}, | |
transformSQL: function(sql){ | |
var str = "", | |
number = 0; | |
for (var i = 0; i < sql.length; i++) | |
if(sql[i]=='?') str += "$"+(++number); | |
else str += sql[i]; | |
return str; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Node-pg (Promise ver.)
PostgreSQL Client
這是一個基於 node-postgres 的擴充模組。原始的 node-postgres 在撰寫上容易出現 callback hell,因此我將原始的功能全部套上 Promise,讓程式碼更加簡潔。
API
您可參考上面的 index.js ,展示了一個基本的用法
db.connect(option)
連線資料庫,
參數為一個物件,物件結構如下示:
回傳一個 Promise<Client>
db.query(sql [,params])
第一個參數為 SQL 語法,預設使用 Prepared Statements 來防止 SQL injection。需要綁定參數的部分,可以使用
$1
、$2
或是統一使用?
。第二個參數(Array) 為準備榜定的資料,會依序綁定。
回傳一個 Promise<result> 物件
db.close()
關閉與資料庫的連線