Comparing version 0.8.0 to 0.8.1
@@ -157,3 +157,2 @@ var net = require('net'); | ||
} else { | ||
val = val.toString(); | ||
buffer.addInt32(Buffer.byteLength(val)); | ||
@@ -160,0 +159,0 @@ buffer.addString(val); |
@@ -50,12 +50,5 @@ var EventEmitter = require('events').EventEmitter; | ||
var client = new self.Client(c); | ||
client.connect(); | ||
var connectError = function(err) { | ||
client.removeListener('connect', connectSuccess); | ||
callback(err, null); | ||
}; | ||
var connectSuccess = function() { | ||
client.removeListener('error', connectError); | ||
client.connect(function(err) { | ||
if(err) return callback(err); | ||
//handle connected client background errors by emitting event | ||
@@ -67,7 +60,6 @@ //via the pg object and then removing errored client from the pool | ||
}); | ||
callback(null, client); | ||
}; | ||
}); | ||
client.once('connect', connectSuccess); | ||
client.once('error', connectError); | ||
client.on('drain', function() { | ||
@@ -74,0 +66,0 @@ pool.release(client); |
@@ -33,3 +33,2 @@ //require the c++ bindings & export to javascript | ||
} | ||
nativeConnect.call(self, conString); | ||
if(cb) { | ||
@@ -50,2 +49,3 @@ var errCallback; | ||
} | ||
nativeConnect.call(self, conString); | ||
}) | ||
@@ -52,0 +52,0 @@ } |
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var types = require(__dirname + "/../types"); | ||
var types = require(__dirname + '/../types'); | ||
var utils = require(__dirname + '/../utils'); | ||
@@ -34,17 +35,3 @@ //event emitter proxy | ||
for(var i = 0, len = this.values.length; i < len; i++) { | ||
var item = this.values[i]; | ||
switch(typeof item) { | ||
case 'undefined': | ||
this.values[i] = null; | ||
break; | ||
case 'object': | ||
this.values[i] = item === null ? null : JSON.stringify(item); | ||
break; | ||
case 'string': | ||
//value already string | ||
break; | ||
default: | ||
//numbers | ||
this.values[i] = item.toString(); | ||
} | ||
this.values[i] = utils.prepareValue(this.values[i]); | ||
} | ||
@@ -51,0 +38,0 @@ } |
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var Result = require(__dirname + "/result"); | ||
var Types = require(__dirname + "/types"); | ||
var Result = require(__dirname + '/result'); | ||
var Types = require(__dirname + '/types'); | ||
var utils = require(__dirname + '/utils'); | ||
@@ -132,5 +133,5 @@ var Query = function(config) { | ||
if(self.values) { | ||
self.values = self.values.map(function(val) { | ||
return (val instanceof Date) ? JSON.stringify(val) : val; | ||
}); | ||
for(var i = 0, len = self.values.length; i < len; i++) { | ||
self.values[i] = utils.prepareValue(self.values[i]); | ||
} | ||
} | ||
@@ -137,0 +138,0 @@ |
@@ -94,2 +94,16 @@ var url = require('url'); | ||
//converts values from javascript types | ||
//to their 'raw' counterparts for use as a postgres parameter | ||
//note: you can override this function to provide your own conversion mechanism | ||
//for complex types, etc... | ||
var prepareValue = function(val) { | ||
if(val instanceof Date) { | ||
return JSON.stringify(val); | ||
} | ||
if(typeof val === 'undefined') { | ||
return null; | ||
} | ||
return val === null ? null : val.toString(); | ||
} | ||
module.exports = { | ||
@@ -101,3 +115,4 @@ normalizeConnectionInfo: normalizeConnectionInfo, | ||
buildLibpqConnectionString: getLibpgConString, | ||
parseConnectionString: parseConnectionString | ||
parseConnectionString: parseConnectionString, | ||
prepareValue: prepareValue | ||
} |
{ "name": "pg", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"description": "PostgreSQL client - pure javascript & libpq with the same API", | ||
@@ -4,0 +4,0 @@ "keywords" : ["postgres", "pg", "libpq", "postgre", "database", "rdbms"], |
@@ -13,57 +13,61 @@ #node-postgres | ||
var pg = require('pg'); | ||
//or native libpq bindings | ||
//var pg = require('pg').native | ||
```javascript | ||
var pg = require('pg'); | ||
//or native libpq bindings | ||
//var pg = require('pg').native | ||
var conString = "tcp://postgres:1234@localhost/postgres"; | ||
var conString = "tcp://postgres:1234@localhost/postgres"; | ||
//error handling omitted | ||
pg.connect(conString, function(err, client) { | ||
client.query("SELECT NOW() as when", function(err, result) { | ||
console.log("Row count: %d",result.rows.length); // 1 | ||
console.log("Current year: %d", result.rows[0].when.getYear()); | ||
}); | ||
}); | ||
//error handling omitted | ||
pg.connect(conString, function(err, client) { | ||
client.query("SELECT NOW() as when", function(err, result) { | ||
console.log("Row count: %d",result.rows.length); // 1 | ||
console.log("Current year: %d", result.rows[0].when.getYear()); | ||
}); | ||
}); | ||
``` | ||
### Evented api | ||
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native` | ||
var conString = "tcp://postgres:1234@localhost/postgres"; | ||
var client = new pg.Client(conString); | ||
client.connect(); | ||
```javascript | ||
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native` | ||
var conString = "tcp://postgres:1234@localhost/postgres"; | ||
//queries are queued and executed one after another once the connection becomes available | ||
client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)"); | ||
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]); | ||
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]); | ||
var client = new pg.Client(conString); | ||
client.connect(); | ||
//queries can be executed either via text/parameter values passed as individual arguments | ||
//or by passing an options object containing text, (optional) parameter values, and (optional) query name | ||
client.query({ | ||
name: 'insert beatle', | ||
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", | ||
values: ['George', 70, new Date(1946, 02, 14)] | ||
}); | ||
//queries are queued and executed one after another once the connection becomes available | ||
client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)"); | ||
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]); | ||
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]); | ||
//subsequent queries with the same name will be executed without re-parsing the query plan by postgres | ||
client.query({ | ||
name: 'insert beatle', | ||
values: ['Paul', 63, new Date(1945, 04, 03)] | ||
}); | ||
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']); | ||
//queries can be executed either via text/parameter values passed as individual arguments | ||
//or by passing an options object containing text, (optional) parameter values, and (optional) query name | ||
client.query({ | ||
name: 'insert beatle', | ||
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", | ||
values: ['George', 70, new Date(1946, 02, 14)] | ||
}); | ||
//can stream row results back 1 at a time | ||
query.on('row', function(row) { | ||
console.log(row); | ||
console.log("Beatle name: %s", row.name); //Beatle name: John | ||
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates | ||
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints | ||
}); | ||
//fired after last row is emitted | ||
query.on('end', function() { | ||
client.end(); | ||
}); | ||
//subsequent queries with the same name will be executed without re-parsing the query plan by postgres | ||
client.query({ | ||
name: 'insert beatle', | ||
values: ['Paul', 63, new Date(1945, 04, 03)] | ||
}); | ||
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']); | ||
//can stream row results back 1 at a time | ||
query.on('row', function(row) { | ||
console.log(row); | ||
console.log("Beatle name: %s", row.name); //Beatle name: John | ||
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates | ||
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints | ||
}); | ||
//fired after last row is emitted | ||
query.on('end', function() { | ||
client.end(); | ||
}); | ||
``` | ||
### Example notes | ||
@@ -168,4 +172,1 @@ | ||
THE SOFTWARE. | ||
@@ -83,3 +83,2 @@ var helper = require(__dirname + '/test-helper'); | ||
test('non-query error', function() { | ||
return false; | ||
@@ -94,3 +93,2 @@ var client = new Client({ | ||
test('non-query error with callback', function() { | ||
return false; | ||
var client = new Client({ | ||
@@ -122,3 +120,2 @@ user:'asldkfjsadlfkj' | ||
test('when connecting to invalid host', function() { | ||
return false; | ||
var client = new Client({ | ||
@@ -134,3 +131,2 @@ user: 'brian', | ||
test('when connecting to invalid host with callback', function() { | ||
return false; | ||
var client = new Client({ | ||
@@ -137,0 +133,0 @@ user: 'brian', |
@@ -159,3 +159,5 @@ //make assert a global... | ||
process.on('exit', console.log) | ||
process.on('exit', function() { | ||
console.log('') | ||
}) | ||
@@ -162,0 +164,0 @@ process.on('uncaughtException', function(err) { |
@@ -97,3 +97,3 @@ require(__dirname + "/test-helper"); | ||
statement: 'woo', | ||
values: [1, 'hi', null, 'zing'] | ||
values: ['1', 'hi', null, 'zing'] | ||
}); | ||
@@ -100,0 +100,0 @@ var expectedBuffer = new BufferList() |
@@ -146,2 +146,16 @@ require(__dirname + '/test-helper'); | ||
test('password contains < and/or > characters', function () { | ||
return false; | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello<ther>e', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
} | ||
var connectionString = 'pg://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var config = utils.parseConnectionString(connectionString); | ||
assert.same(config, sourceConfig); | ||
}); | ||
}) |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
5889
171
218894
144