Skip to content

Commit 10e6d85

Browse files
committed
Add support for JSON data type
requires >= 9.2 of postgres
1 parent 3997b38 commit 10e6d85

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

lib/types/textParsers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ var init = function(register) {
183183
register(1009, parseStringArray);
184184
register(1186, parseInterval);
185185
register(17, parseByteA);
186+
register(114, JSON.parse.bind(JSON));
186187
};
187188

188189
module.exports = {

lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ var prepareValue = function(val) {
5555
if(Array.isArray(val)) {
5656
return arrayString(val);
5757
}
58-
return val === null ? null : val.toString();
58+
if(!val || typeof val !== 'object') {
59+
return val === null ? null : val.toString();
60+
}
61+
return JSON.stringify(val);
5962
};
6063

6164
function dateToString(date) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var helper = require(__dirname + '/test-helper');
2+
var assert = require('assert');
3+
//if you want binary support, pull request me!
4+
if (helper.config.binary) {
5+
return;
6+
}
7+
8+
test('can read and write json', function() {
9+
helper.pg.connect(helper.config, function(err, client, done) {
10+
assert.ifError(err);
11+
client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)');
12+
var value ={name: 'Brian', age: 250, alive: true, now: new Date()};
13+
client.query('INSERT INTO stuff (data) VALUES ($1)', [value]);
14+
client.query('SELECT * FROM stuff', assert.success(function(result) {
15+
assert.equal(result.rows.length, 1);
16+
assert.equal(typeof result.rows[0].data, 'object');
17+
var row = result.rows[0].data;
18+
assert.strictEqual(row.name, value.name);
19+
assert.strictEqual(row.age, value.age);
20+
assert.strictEqual(row.alive, value.alive);
21+
test('row should have "now" as a date', function() {
22+
return false;
23+
assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now);
24+
});
25+
assert.equal(JSON.stringify(row.now), JSON.stringify(value.now));
26+
done();
27+
helper.pg.end();
28+
}));
29+
});
30+
});

test/integration/client/query-callback-error-tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var withQuery = function(text, resultLength, cb) {
66
var client = new Client(helper.args);
77
process.removeAllListeners('uncaughtException');
88
assert.emits(process, 'uncaughtException', function() {
9-
console.log('got uncaught exception')
109
assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback');
1110
client.query('SELECT * FROM blah', assert.success(function(result) {
1211
assert.equal(result.rows.length, resultLength);

0 commit comments

Comments
 (0)