From 10e6d85266310956b39020e25f35b6dbca258dc9 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:18:17 -0500 Subject: [PATCH] Add support for JSON data type requires >= 9.2 of postgres --- lib/types/textParsers.js | 1 + lib/utils.js | 5 +++- .../client/json-type-parsing-tests.js | 30 +++++++++++++++++++ .../client/query-callback-error-tests.js | 1 - 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/integration/client/json-type-parsing-tests.js diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index 0a49ec6b..558ce73b 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -183,6 +183,7 @@ var init = function(register) { register(1009, parseStringArray); register(1186, parseInterval); register(17, parseByteA); + register(114, JSON.parse.bind(JSON)); }; module.exports = { diff --git a/lib/utils.js b/lib/utils.js index 267f3972..3d12d827 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -55,7 +55,10 @@ var prepareValue = function(val) { if(Array.isArray(val)) { return arrayString(val); } - return val === null ? null : val.toString(); + if(!val || typeof val !== 'object') { + return val === null ? null : val.toString(); + } + return JSON.stringify(val); }; function dateToString(date) { diff --git a/test/integration/client/json-type-parsing-tests.js b/test/integration/client/json-type-parsing-tests.js new file mode 100644 index 00000000..215e2262 --- /dev/null +++ b/test/integration/client/json-type-parsing-tests.js @@ -0,0 +1,30 @@ +var helper = require(__dirname + '/test-helper'); +var assert = require('assert'); +//if you want binary support, pull request me! +if (helper.config.binary) { + return; +} + +test('can read and write json', function() { + helper.pg.connect(helper.config, function(err, client, done) { + assert.ifError(err); + client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)'); + var value ={name: 'Brian', age: 250, alive: true, now: new Date()}; + client.query('INSERT INTO stuff (data) VALUES ($1)', [value]); + client.query('SELECT * FROM stuff', assert.success(function(result) { + assert.equal(result.rows.length, 1); + assert.equal(typeof result.rows[0].data, 'object'); + var row = result.rows[0].data; + assert.strictEqual(row.name, value.name); + assert.strictEqual(row.age, value.age); + assert.strictEqual(row.alive, value.alive); + test('row should have "now" as a date', function() { + return false; + assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now); + }); + assert.equal(JSON.stringify(row.now), JSON.stringify(value.now)); + done(); + helper.pg.end(); + })); + }); +}); diff --git a/test/integration/client/query-callback-error-tests.js b/test/integration/client/query-callback-error-tests.js index 4fba387a..bd80153c 100644 --- a/test/integration/client/query-callback-error-tests.js +++ b/test/integration/client/query-callback-error-tests.js @@ -6,7 +6,6 @@ var withQuery = function(text, resultLength, cb) { var client = new Client(helper.args); process.removeAllListeners('uncaughtException'); assert.emits(process, 'uncaughtException', function() { - console.log('got uncaught exception') assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback'); client.query('SELECT * FROM blah', assert.success(function(result) { assert.equal(result.rows.length, resultLength);