|
3 | 3 | const fp = require('fastify-plugin') |
4 | 4 | const oracledb = require('oracledb') |
5 | 5 |
|
| 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 | + |
6 | 46 | function decorateFastifyInstance (pool, fastify, options, next) { |
7 | 47 | const oracle = { |
8 | 48 | getConnection: pool.getConnection.bind(pool), |
9 | | - pool |
| 49 | + pool, |
| 50 | + scope: scope.bind(pool) |
10 | 51 | } |
11 | 52 |
|
12 | 53 | if (options.name) { |
|
0 commit comments