Comparing version 1.1.0 to 1.2.0
75
index.js
@@ -5,17 +5,11 @@ var Libpq = require('libpq'); | ||
var assert = require('assert'); | ||
var types = require('pg-types'); | ||
var Client = module.exports = function(types) { | ||
var Client = module.exports = function(resultMapper) { | ||
if(!(this instanceof Client)) { | ||
return new Client(types); | ||
return new Client(resultMapper); | ||
} | ||
EventEmitter.call(this); | ||
if(!types) { | ||
var pgTypes = require('pg-types'); | ||
types = pgTypes.getTypeParser.bind(pgTypes); | ||
} else { | ||
types = types.getTypeParser.bind(types); | ||
} | ||
this.pq = new Libpq(); | ||
this.types = types; | ||
this._reading = false; | ||
@@ -31,3 +25,9 @@ this._read = this._read.bind(this); | ||
self._startReading(); | ||
}) | ||
}); | ||
//allow mapping override for node-postgres | ||
//and custom type/result mapping | ||
if(resultMapper) { | ||
this._mapResults = resultMapper; | ||
} | ||
}; | ||
@@ -45,2 +45,26 @@ | ||
Client.prototype._mapResults = function(pq) { | ||
var rows = []; | ||
var rowCount = pq.ntuples(); | ||
var colCount = pq.nfields(); | ||
for(var i = 0; i < rowCount; i++) { | ||
var row = {}; | ||
rows.push(row); | ||
for(var j = 0; j < colCount; j++) { | ||
var rawValue = pq.getvalue(i, j); | ||
var value = rawValue; | ||
if(rawValue == '') { | ||
if(pq.getisnull()) { | ||
value = null; | ||
} | ||
} else { | ||
value = types.getTypeParser(pq.ftype(j))(rawValue); | ||
} | ||
row[pq.fname(j)] = value; | ||
} | ||
} | ||
return rows; | ||
} | ||
Client.prototype.end = function(cb) { | ||
@@ -122,25 +146,2 @@ this._stopReading(); | ||
var mapResults = function(pq, types) { | ||
var rows = []; | ||
var rowCount = pq.ntuples(); | ||
var colCount = pq.nfields(); | ||
for(var i = 0; i < rowCount; i++) { | ||
var row = {}; | ||
rows.push(row); | ||
for(var j = 0; j < colCount; j++) { | ||
var rawValue = pq.getvalue(i, j); | ||
var value = rawValue; | ||
if(rawValue == '') { | ||
if(pq.getisnull()) { | ||
value = null; | ||
} | ||
} else { | ||
value = types(pq.ftype(j))(rawValue); | ||
} | ||
row[pq.fname(j)] = value; | ||
} | ||
} | ||
return rows; | ||
}; | ||
Client.prototype._awaitResult = function(cb) { | ||
@@ -209,3 +210,3 @@ var self = this; | ||
self._awaitResult(function(err) { | ||
return cb(err, err ? null : mapResults(self.pq, self.types)); | ||
return cb(err, err ? null : self._mapResults(self.pq, self.types)); | ||
}); | ||
@@ -237,3 +238,3 @@ }); | ||
self._awaitResult(function(err) { | ||
return cb(err, err ? null : mapResults(self.pq, self.types)); | ||
return cb(err, err ? null : self._mapResults(self.pq, self.types)); | ||
}); | ||
@@ -265,3 +266,3 @@ }); | ||
throwIfError(this.pq); | ||
return mapResults(pq, this.types); | ||
return this._mapResults(pq, this.types); | ||
}; | ||
@@ -277,3 +278,3 @@ | ||
throwIfError(this.pq); | ||
return mapResults(this.pq, this.types); | ||
return this._mapResults(this.pq, this.types); | ||
}; |
{ | ||
"name": "pg-native", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A slightly nicer interface to Postgres over node-libpq", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,3 +15,5 @@ var Client = require('../'); | ||
it('is used by client', function(done) { | ||
var client = new Client(types); | ||
var client = new Client(function(pq) { | ||
return [{when: 'blah'}] | ||
}); | ||
client.connectSync(); | ||
@@ -18,0 +20,0 @@ var rows = client.querySync('SELECT NOW() AS when'); |
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
40650
1003