Comparing version 0.0.2 to 0.1.0
88
index.js
var Libpq = require('libpq'); | ||
var consumeResults = require('./lib/consume-results'); | ||
@@ -28,2 +29,9 @@ var Client = module.exports = function(types) { | ||
var throwIfError = function(pq) { | ||
var err = pq.resultErrorMessage() || pq.errorMessage(); | ||
if(err) { | ||
throw new Error(err); | ||
} | ||
} | ||
var mapResults = function(pq, types) { | ||
@@ -69,39 +77,2 @@ var rows = []; | ||
var consumeResults = function(pq, cb) { | ||
var cleanup = function() { | ||
pq.removeListener('readable', onReadable); | ||
pq.stopReader(); | ||
} | ||
var readError = function(message) { | ||
cleanup(); | ||
return cb(new Error(message || pq.errorMessage)); | ||
}; | ||
var onReadable = function() { | ||
//read waiting data from the socket | ||
//e.g. clear the pending 'select' | ||
if(!pq.consumeInput()) { | ||
return readError(); | ||
} | ||
//check if there is still outstanding data | ||
//if so, wait for it all to come in | ||
if(pq.isBusy()) { | ||
return; | ||
} | ||
//load our result object | ||
pq.getResult(); | ||
//"read until results return null" | ||
//or in our case ensure we only have one result | ||
if(pq.getResult()) { | ||
return readError('Only one result at a time is accepted'); | ||
} | ||
cleanup(); | ||
return cb(null); | ||
}; | ||
pq.on('readable', onReadable); | ||
pq.startReader(); | ||
}; | ||
Client.prototype.query = function(text, values, cb) { | ||
@@ -126,2 +97,29 @@ var queryFn; | ||
Client.prototype.prepare = function(statementName, text, nParams, cb) { | ||
var pq = this.pq; | ||
var fn = pq.sendPrepare.bind(pq, statementName, text, nParams); | ||
dispatchQuery(pq, fn, function(err) { | ||
if(err) return cb(err); | ||
consumeResults(pq, cb); | ||
}); | ||
}; | ||
Client.prototype.execute = function(statementName, parameters, cb) { | ||
var pq = this.pq; | ||
var types = this.types; | ||
var fn = pq.sendQueryPrepared.bind(pq, statementName, parameters); | ||
dispatchQuery(pq, fn, function(err, rows) { | ||
if(err) return cb(err); | ||
consumeResults(pq, function(err) { | ||
return cb(err, err ? null : mapResults(pq, types)); | ||
}); | ||
}); | ||
}; | ||
var CopyFromStream = require('./lib/copy-from-stream'); | ||
Client.prototype.getCopyFromStream = function() { | ||
this.pq.setNonBlocking(true); | ||
return new CopyFromStream(this.pq); | ||
}; | ||
Client.prototype.querySync = function(text, values) { | ||
@@ -131,7 +129,15 @@ var queryFn; | ||
pq[values ? 'execParams' : 'exec'].call(pq, text, values); | ||
var success = !pq.errorMessage(); | ||
if(!success) { | ||
throw new Error(pq.resultErrorMessage()); | ||
} | ||
throwIfError(this.pq); | ||
return mapResults(pq, this.types); | ||
}; | ||
Client.prototype.prepareSync = function(statementName, text, nParams) { | ||
this.pq.prepare(statementName, text, nParams); | ||
throwIfError(this.pq); | ||
}; | ||
Client.prototype.executeSync = function(statementName, parameters) { | ||
this.pq.execPrepared(statementName, parameters); | ||
throwIfError(this.pq); | ||
return mapResults(this.pq, this.types); | ||
}; |
{ | ||
"name": "pg-native", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "A slightly nicer interface to Postgres over node-libpq", | ||
@@ -25,9 +25,10 @@ "main": "index.js", | ||
"dependencies": { | ||
"libpq": "0.2.5", | ||
"libpq": "^0.4.1", | ||
"pg-types": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"async": "^0.9.0", | ||
"mocha": "^1.21.4", | ||
"async": "^0.9.0" | ||
"okay": "^0.3.0" | ||
} | ||
} |
var Client = require('../') | ||
var assert = require('assert') | ||
var async = require('async') | ||
@@ -37,58 +36,1 @@ describe('connection', function() { | ||
}); | ||
describe('async query', function() { | ||
before(function(done) { | ||
this.client = Client(); | ||
this.client.connect(function(err) { | ||
if(err) return done(err); | ||
return done(); | ||
}); | ||
}); | ||
after(function(done) { | ||
this.client.end(done); | ||
}); | ||
it('simple query works', function(done) { | ||
var runQuery = function(n, done) { | ||
this.client.query('SELECT NOW() AS the_time', function(err, rows) { | ||
if(err) return done(err); | ||
assert.equal(rows[0].the_time.getFullYear(), new Date().getFullYear()); | ||
return done(); | ||
}); | ||
}.bind(this); | ||
async.timesSeries(3, runQuery, done) | ||
}); | ||
it('parameters work', function(done) { | ||
var runQuery = function(n, done) { | ||
this.client.query('SELECT $1::text AS name', ['Brian'], done); | ||
}.bind(this); | ||
async.timesSeries(3, runQuery, done) | ||
}); | ||
it('prepared, named statements work'); | ||
}); | ||
describe('query sync', function(done) { | ||
before(function() { | ||
this.client = Client(); | ||
this.client.connectSync(); | ||
}); | ||
after(function(done) { | ||
this.client.end(done); | ||
}); | ||
it('simple query works', function() { | ||
var rows = this.client.querySync('SELECT NOW() AS the_time'); | ||
assert.equal(rows.length, 1); | ||
assert.equal(rows[0].the_time.getFullYear(), new Date().getFullYear()); | ||
}); | ||
it('parameterized query works', function() { | ||
var rows = this.client.querySync('SELECT $1::text AS name', ['Brian']); | ||
assert.equal(rows.length, 1); | ||
assert.equal(rows[0].name, 'Brian'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
15453
15
473
4
3
1
+ Addedlibpq@0.4.1(transitive)
- Removedlibpq@0.2.5(transitive)
Updatedlibpq@^0.4.1