Skip to content

Commit cb02ff3

Browse files
committed
Add option to set default outFormat to json
1 parent 1ea9ffa commit cb02ff3

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ properties:
6161
takes precedence over the `pool` option.
6262
+ `client`: an instance of an `oracledb` connection pool. This takes precedence
6363
over the `pool` and `poolAlias` options.
64-
65-
A `name` option can be used in order to connect to multiple oracledb instances.
66-
The first registered instance can be accessed via `fastify.oracle` or `fastify.oracle.<dbname>`. Note that once you register a *named* instance, you will *not* be able to register an unnamed instance.
64+
+ `name`: can be used in order to connect to multiple oracledb instances. The first registered instance can be accessed via `fastify.oracle` or `fastify.oracle.<dbname>`. Note that once you register a *named* instance, you will *not* be able to register an unnamed instance.
65+
+ `jsonOutput`: sets the `outFormat` of oracledb to `OBJECT` (default: `false` i.e: `ARRAY`)
6766

6867
```js
6968
const fastify = require('fastify')()

plugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function decorateFastifyInstance (pool, fastify, options, next) {
2727
}
2828
}
2929

30-
fastify.addHook('onClose', (fastify, done) => pool.close(done))
30+
fastify.addHook('onClose', (_, done) => pool.close(done))
3131

3232
return next()
3333
}
@@ -54,6 +54,10 @@ function fastifyOracleDB (fastify, options, next) {
5454
return next(Error('fastify-oracle: must supply options.pool oracledb pool options'))
5555
}
5656

57+
if (options.jsonOutput) {
58+
oracledb.outFormat = oracledb.OBJECT
59+
}
60+
5761
oracledb.createPool(options.pool, (err, pool) => {
5862
if (err) {
5963
return next(Error('fastify-oracle: failed to create pool' + '-' + err.message))

test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ test('accepts singleton client', (t) => {
9292
})
9393
})
9494

95+
test('sets json as default outFormat', (t) => {
96+
t.plan(9)
97+
98+
const fastify = Fastify()
99+
fastify.register(plugin, { pool: poolOptions, jsonOutput: true })
100+
101+
fastify.ready(err => {
102+
t.error(err)
103+
t.ok(fastify.oracle.pool)
104+
105+
fastify.oracle.getConnection((err, conn) => {
106+
t.error(err)
107+
conn.execute('SELECT 1 AS FOO FROM DUAL', (err, result) => {
108+
t.error(err)
109+
t.is(result.rows.length, 1)
110+
t.is(result.rows[0].FOO, 1)
111+
conn.close(err => {
112+
t.error(err)
113+
114+
fastify.close(err => {
115+
t.error(err)
116+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
117+
})
118+
})
119+
})
120+
})
121+
})
122+
})
123+
95124
test('retrieves a cached pool', (t) => {
96125
t.plan(7)
97126

0 commit comments

Comments
 (0)