Last active
August 29, 2015 14:20
-
-
Save Paxa/dc366b2c4ce1cc469d90 to your computer and use it in GitHub Desktop.
node-postgres and node-pg-native
This file contains 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
if (pg_native) { | |
var async = require('async'); | |
pg = { | |
connect: function (connectString, callback) { | |
var client = new pg_native; | |
client.original_query = client.query; | |
client.sqlQueue = async.queue(function (sql, callback) { | |
//console.log('SQL start ' + sql); | |
var startTime = Date.now(); | |
client.original_query(sql, function (error, result) { | |
if (result) { | |
var newResult = { | |
rows: result, | |
rowCount: result.length, | |
//rowCount: client.pq.ntuples(), | |
//command: client.pq.cmdStatus(), | |
fields: [] | |
}; | |
if (result.length) { | |
newResult.fields = Object.keys(result[0]).map(function (fieldName){ | |
return {name: fieldName}; | |
}); | |
newResult.fieldsCount = newResult.fields.length; | |
} else { | |
newResult.fieldsCount = client.pq.nfields(); | |
for(var i = 0; i < newResult.fieldsCount; i ++) { | |
newResult.fields[i] = {name: client.pq.fname(i)}; | |
//newResult.fields[i] = {columnID: i, name: client.pq.fname(i), format: client.pq.ftype(i)}; | |
} | |
} | |
} | |
newResult.time = Date.now() - startTime; | |
callback(error, newResult); | |
}); | |
}, 1); | |
client.query = function (sql, callback) { | |
client.sqlQueue.push(sql, callback); | |
}; | |
client.connect(connectString, function (error) { | |
callback(error, client); | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment