Skip to content

Instantly share code, notes, and snippets.

@Paxa
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paxa/dc366b2c4ce1cc469d90 to your computer and use it in GitHub Desktop.
Save Paxa/dc366b2c4ce1cc469d90 to your computer and use it in GitHub Desktop.
node-postgres and node-pg-native
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