Skip to content

Commit 992997e

Browse files
committed
Added scoped execution
1 parent 8da79ea commit 992997e

File tree

5 files changed

+456
-228
lines changed

5 files changed

+456
-228
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A plugin for Fastify to provide Oracle DB connections",
55
"main": "plugin.js",
66
"scripts": {
7-
"test": "standard && tap --cov test.js",
7+
"test": "standard && tap --cov test/*.test.js",
88
"lint": "standard | snazzy"
99
},
1010
"precommit": [

plugin.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,51 @@
33
const fp = require('fastify-plugin')
44
const oracledb = require('oracledb')
55

6+
function executionScope (pool, fn, cb) {
7+
pool.getConnection(function (err, conn) {
8+
if (err) return cb(err)
9+
10+
const doRelease = (conn) => {
11+
conn.close(function () { })
12+
}
13+
14+
const done = (err, res) => {
15+
doRelease(conn)
16+
17+
if (err) {
18+
return cb(err)
19+
}
20+
return cb(null, res)
21+
}
22+
23+
const promise = fn(conn, done)
24+
25+
if (promise && typeof promise.then === 'function') {
26+
promise.then(
27+
(res) => done(null, res),
28+
(e) => done(e))
29+
}
30+
})
31+
}
32+
33+
function scope (fn, cb) {
34+
if (cb && typeof cb === 'function') {
35+
return executionScope(this, fn, cb)
36+
}
37+
38+
return new Promise((resolve, reject) => {
39+
executionScope(this, fn, function (err, res) {
40+
if (err) { return reject(err) }
41+
return resolve(res)
42+
})
43+
})
44+
}
45+
646
function decorateFastifyInstance (pool, fastify, options, next) {
747
const oracle = {
848
getConnection: pool.getConnection.bind(pool),
9-
pool
49+
pool,
50+
scope: scope.bind(pool)
1051
}
1152

1253
if (options.name) {

test.js

Lines changed: 0 additions & 226 deletions
This file was deleted.

0 commit comments

Comments
 (0)