Skip to content

Commit 2c9c9b9

Browse files
committed
parse Edm.Decimal correctly in the postgres driver result
1 parent 02d84c0 commit 2c9c9b9

File tree

4 files changed

+84
-52
lines changed

4 files changed

+84
-52
lines changed

lib/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const Promise = require('bluebird')
22
const pg = require('pg-promise')({promiseLib: Promise})
3+
// how to configure type parsing per instance
4+
// https://github.com/brianc/node-postgres/issues/1838
5+
const TypeOverrides = require('pg/lib/type-overrides')
36
const Store = require('jsreport-sql-store')
47

58
module.exports = function (reporter, definition) {
@@ -8,6 +11,22 @@ module.exports = function (reporter, definition) {
811
return
912
}
1013

14+
const customTypes = new TypeOverrides()
15+
16+
// postgres does not parse numeric(d,d) type by default, we
17+
// need to add the parsing manually, so we register a custom types collection
18+
// to don't alter the global behaviour of the queries,
19+
// we just care about our database driver instance and don't want to change the way the
20+
// driver works by default in all cases/instances.
21+
//
22+
// 1700 is the id for numeric type
23+
// https://github.com/vitaly-t/pg-promise/issues/214
24+
customTypes.setTypeParser(1700, (value) => {
25+
return parseFloat(value)
26+
})
27+
28+
definition.options.types = customTypes
29+
1130
const db = pg(definition.options)
1231

1332
function executeQuery (a, opts = {}) {

package-lock.json

Lines changed: 41 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"devDependencies": {
2424
"eslint-config-standard": "11.0.0",
25-
"jsreport-core": "2.7.0",
25+
"jsreport-core": "2.7.2",
2626
"mocha": "5.2.0",
2727
"should": "13.2.3",
2828
"standard": "11.0.1"

test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ describe('common store tests', () => {
1515
'user': 'postgres',
1616
'password': 'password'
1717
})).use(() => {
18+
instance.documentStore.registerEntityType('InstanceCustomType', {
19+
name: { type: 'Edm.String', publicKey: true },
20+
score: { type: 'Edm.Decimal' }
21+
})
22+
23+
instance.documentStore.registerEntitySet('testingInstances', {
24+
entityType: 'jsreport.InstanceCustomType',
25+
splitIntoDirectories: true
26+
})
27+
1828
jsreport.tests.documentStore().init(() => instance.documentStore)
1929
})
2030

@@ -37,4 +47,17 @@ describe('common store tests', () => {
3747
afterEach(() => reporter.close())
3848

3949
jsreport.tests.documentStore()(() => reporter.documentStore)
50+
51+
it('should correctly parse decimal type from db', async () => {
52+
await reporter.documentStore.collection('testingInstances').insert({
53+
name: 'test',
54+
score: 1.5
55+
})
56+
57+
const entity = await reporter.documentStore.collection('testingInstances').findOne({
58+
name: 'test'
59+
})
60+
61+
entity.score.should.be.eql(1.5)
62+
})
4063
})

0 commit comments

Comments
 (0)