Skip to content

Commit 2d9bec6

Browse files
committed
🤘
0 parents  commit 2d9bec6

File tree

8 files changed

+306
-0
lines changed

8 files changed

+306
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 2
10+
trim_trailing_whitespace = true
11+
12+
# [*.md]
13+
# trim_trailing_whitespace = false

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history
34+
35+
# 0x
36+
.__browserify_string_empty.js
37+
profile-*
38+
39+
# tap --cov
40+
.nyc_output/
41+
42+
# JetBrains IntelliJ IDEA
43+
.idea/
44+
*.iml
45+
46+
# VS Code
47+
.vscode/

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
3+
node_js:
4+
- "9"
5+
- "8"
6+
- "6"
7+
8+
script:
9+
- npm run lint-ci
10+
- npm run test-ci
11+
12+
notifications:
13+
email:
14+
on_success: never
15+
on_failure: always

Readme.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# fastify-oracle
2+
3+
This module provides access to an Oracle database connection pool via the
4+
[oracledb](https://npm.im/oracledb) module. It decorates the [Fastify](https://fastify.io)
5+
instance with an `oracle` property that is a connection pool instance.
6+
7+
When the Fastify server is shutdown, this plugin invokes the `.close()` on the
8+
connection pool.
9+
10+
## Example
11+
12+
```js
13+
const fastify = require('fastify')()
14+
15+
fastify.register(require('fastify-oracle'), {
16+
pool: {
17+
user: 'foo',
18+
password: 'bar',
19+
connectString: 'oracle.example.com:1521/foobar'
20+
}
21+
})
22+
23+
fastify.get('/db_data', async function (req, reply) {
24+
const conn = await this.oracle.getConnection()
25+
const results = await conn.execute('select 1 as foo from dual')
26+
await conn.close()
27+
return results
28+
})
29+
```
30+
31+
## Options
32+
33+
`fastify-oracle` requires an options object with at least one of the following
34+
properties:
35+
36+
+ `pool`: an `oracledb` [pool configuration object](https://github.com/oracle/node-oracledb/blob/33331413/doc/api.md#createpool)
37+
+ `poolAlias`: the name of a pool alias that has already been configured. This
38+
takes precedence over the `pool` option.
39+
+ `client`: an instance of an `oracledb` connection pool. This takes precedence
40+
over the `pool` and `poolAlias` options.
41+
42+
## License
43+
44+
[MIT License](http://jsumners.mit-license.org/)

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "fastify-oracle",
3+
"version": "1.0.0",
4+
"description": "A plugin for Fastify to provide Oracle DB connections",
5+
"main": "plugin.js",
6+
"scripts": {
7+
"test": "tap 'test/**/*.test.js'",
8+
"test-ci": "tap --cov 'test/**/*.test.js'",
9+
"lint": "standard | snazzy",
10+
"lint-ci": "standard"
11+
},
12+
"precommit": [
13+
"lint",
14+
"test"
15+
],
16+
"repository": {
17+
"type": "git",
18+
"url": "git+ssh://git@github.com/jsumners/fastify-oracle.git"
19+
},
20+
"keywords": [
21+
"fastify",
22+
"oracle"
23+
],
24+
"author": "James Sumners <james.sumners@gmail.com>",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/jsumners/fastify-oracle/issues"
28+
},
29+
"homepage": "https://github.com/jsumners/fastify-oracle#readme",
30+
"devDependencies": {
31+
"pre-commit": "^1.2.2",
32+
"snazzy": "^7.0.0",
33+
"standard": "^10.0.3",
34+
"tap": "^11.0.1"
35+
},
36+
"dependencies": {
37+
"fastify-plugin": "^0.2.1",
38+
"oracledb": "^2.0.15"
39+
}
40+
}

plugin.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const oracledb = require('oracledb')
5+
6+
function fastifyOracleDB (fastify, options, next) {
7+
const close = function (fastify, done) {
8+
fastify.oracle.close(done)
9+
}
10+
11+
if (options.client) {
12+
if (oracledb.Pool.prototype.isPrototypeOf(options.client) === false) {
13+
return next(Error('supplied client must be an instance of oracledb.pool'))
14+
}
15+
fastify.decorate('oracle', options.client)
16+
fastify.addHook('onClose', close)
17+
return next()
18+
}
19+
20+
if (options.poolAlias) {
21+
const pool = oracledb.getPool(options.poolAlias)
22+
if (!pool) return next('could not get default pool from oracledb instance')
23+
fastify.decorate('oracle', pool)
24+
fastify.addHook('onClose', close)
25+
return next()
26+
}
27+
28+
if (!options.pool) {
29+
return next(Error('must supply options.pool oracledb pool options'))
30+
}
31+
32+
oracledb.createPool(options.pool, (err, pool) => {
33+
if (err) return next(err)
34+
fastify.decorate('oracle', pool)
35+
fastify.addHook('onClose', close)
36+
next()
37+
})
38+
}
39+
40+
module.exports = fp(fastifyOracleDB, {
41+
fastify: '>=0.40.0',
42+
name: 'fastify-oracle'
43+
})

test/client.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const oracledb = require('oracledb')
5+
const plugin = require('../plugin')
6+
7+
test('accepts singleton client', (t) => {
8+
t.plan(4)
9+
oracledb.createPool({
10+
user: 'SYSTEM',
11+
password: 'oracle',
12+
connectString: 'localhost/XE'
13+
}, (err, pool) => {
14+
if (err) t.threw(err)
15+
const fastify = {
16+
decorate (name, obj) {
17+
t.is(name, 'oracle')
18+
t.is(obj, pool)
19+
},
20+
21+
addHook (name, fn) {
22+
t.is(name, 'onClose')
23+
t.match(fn, /fastify\.oracle\.close/)
24+
}
25+
}
26+
27+
plugin(fastify, {client: pool}, (err) => {
28+
if (err) t.threw(err)
29+
pool.close()
30+
})
31+
})
32+
})
33+
34+
test('retrieves a cached pool', (t) => {
35+
t.plan(4)
36+
oracledb.createPool({
37+
user: 'SYSTEM',
38+
password: 'oracle',
39+
connectString: 'localhost/XE',
40+
poolAlias: 'foo'
41+
}, (err, pool) => {
42+
if (err) t.threw(err)
43+
const fastify = {
44+
decorate (name, obj) {
45+
t.is(name, 'oracle')
46+
t.is(obj, pool)
47+
},
48+
49+
addHook (name, fn) {
50+
t.is(name, 'onClose')
51+
t.match(fn, /fastify\.oracle\.close/)
52+
}
53+
}
54+
55+
plugin(fastify, {poolAlias: 'foo'}, (err) => {
56+
if (err) t.threw(err)
57+
pool.close()
58+
})
59+
})
60+
})

test/pool.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
// Unless someone can provide an Oracle Docker image to test against, we'll
4+
// just have to assume this works.
5+
6+
// const test = require('tap').test
7+
// const plugin = require('../plugin')
8+
9+
// test('creates usable pool from config', (t) => {
10+
// t.plan(6)
11+
12+
// const fastify = {
13+
// decorate (name, obj) {
14+
// t.is(name, 'oracle')
15+
// this[name] = obj
16+
// },
17+
18+
// addHook (name, fn) {
19+
// t.is(name, 'onClose')
20+
// t.match(fn, /fastify\.oracle\.close/)
21+
// }
22+
// }
23+
24+
// const opts = {
25+
// user: 'SYSTEM',
26+
// password: 'oracle',
27+
// connectString: 'localhost/xe'
28+
// }
29+
// plugin(fastify, {pool: opts}, (err) => {
30+
// if (err) t.threw(err)
31+
// t.ok(fastify.oracle)
32+
// fastify.oracle.getConnection()
33+
// .then((conn) => {
34+
// conn.execute('select 1 as foo from dual')
35+
// .then((rows) => {
36+
// t.is(rows.length, 1)
37+
// t.is(rows[0].foo, 1)
38+
// })
39+
// .then(() => conn.close())
40+
// .catch(t.threw)
41+
// })
42+
// .catch(t.threw)
43+
// })
44+
// })

0 commit comments

Comments
 (0)