Skip to content

Commit 2b52697

Browse files
committed
initial commit
0 parents  commit 2b52697

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//you need to pass in the pg module
2+
//to have this module initialize the type parsers
3+
4+
//technically you can just have this globally initialize
5+
//when its required but I think this is more explicit & cleaner.
6+
7+
var pgParseFloats = module.exports = function(pg) {
8+
//the type & OID match for the float types
9+
var types = {
10+
FLOAT4: 700,
11+
FLOAT8: 701,
12+
NUMERIC: 1700,
13+
FLOAT4_ARRAY: 1021,
14+
FLOAT8_ARRAY: 1022,
15+
NUMERIC_ARRAY: 1231
16+
};
17+
//just use the built-in V8 parseFloat implementation
18+
pg.types.setTypeParser(types.FLOAT4, 'text', parseFloat);
19+
pg.types.setTypeParser(types.FLOAT8, 'text', parseFloat);
20+
pg.types.setTypeParser(types.NUMERIC, 'text', parseFloat);
21+
22+
//TODO array parsers
23+
};
24+
25+
//you can find the OID -> TYPE map like this:
26+
//client.query('select oid, typname from pg_type where typtype = \'b\' order by oid')

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "pg-parse-float",
3+
"version": "0.0.1",
4+
"description": "restore parseFloat behavior to node-postgres (pg) - can be used as a reference for implementing other type parsing addons",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/brianc/node-pg-parse-float"
12+
},
13+
"keywords": [
14+
"postgres",
15+
"float",
16+
"parsing",
17+
"type",
18+
"conversion"
19+
],
20+
"author": "Brian M. Carlson",
21+
"license": "MIT",
22+
"devDependencies": {
23+
"async": "~0.2.7",
24+
"okay": "~0.2.0",
25+
"pg": "~1.0.0",
26+
"mocha": "~1.9.0"
27+
}
28+
}

test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var timeoutSeconds = 5;
2+
var tid = setTimeout(function() {
3+
throw new Error('Tests did not finish within ' + timeoutSeconds + ' seconds');
4+
}, timeoutSeconds * 1000);
5+
6+
var assert = require('assert');
7+
var async = require('async');
8+
var ok = require('okay');
9+
var pg = require('pg');
10+
11+
var setup = function() {
12+
before(function(done) {
13+
this.client = new pg.Client();
14+
this.client.connect();
15+
var client = this.client;
16+
client.query('CREATE TEMP TABLE floatz(small FLOAT4, big FLOAT8, num NUMERIC)', ok(function(result) {
17+
client.query('INSERT INTO floatz(small, big, num) VALUES($1, $2, $3)', [1.1, '2.2', 3.3], ok(function(result) {
18+
done();
19+
}));
20+
}));
21+
});
22+
};
23+
24+
var teardown = function() {
25+
after(function(done) {
26+
this.client.on('end', done);
27+
this.client.end();
28+
});
29+
};
30+
31+
describe('existing (string) functionality', function() {
32+
setup();
33+
34+
it('returns floats as strings', function(done) {
35+
var client = this.client;
36+
client.query('SELECT * FROM floatz', ok(function(result) {
37+
assert(result, 'should have returned result');
38+
assert(result.rows, 'result should have rows');
39+
assert.equal(result.rows.length, 1, 'should have 1 row but returned ' + result.rows.length);
40+
var row = result.rows.pop();
41+
assert.strictEqual(row.small, '1.1');
42+
assert.strictEqual(row.big, '2.2');
43+
assert.strictEqual(row.num, '3.3');
44+
done();
45+
}));
46+
});
47+
48+
teardown();
49+
});
50+
51+
describe('new (parseFloat) functionality', function() {
52+
before(function() {
53+
//load the pg-parse-float module
54+
require(__dirname)(pg);
55+
});
56+
57+
setup();
58+
59+
it('returns floats as floats', function(done) {
60+
var client = this.client;
61+
client.query('SELECT * FROM floatz', ok(function(result) {
62+
assert(result, 'should have returned result');
63+
assert(result.rows, 'result should have rows');
64+
assert.equal(result.rows.length, 1, 'should have 1 row but returned ' + result.rows.length);
65+
var row = result.rows.pop();
66+
assert.strictEqual(row.small, 1.1);
67+
assert.strictEqual(row.big, 2.2);
68+
assert.strictEqual(row.num, 3.3);
69+
done();
70+
}));
71+
});
72+
73+
teardown();
74+
});

0 commit comments

Comments
 (0)