Skip to content

Commit 450d96a

Browse files
authored
Merge pull request #19 from cemremengu/connection-scope
Feature: Execution scope (transact)
2 parents 8da79ea + 2cd902c commit 450d96a

File tree

6 files changed

+528
-228
lines changed

6 files changed

+528
-228
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,52 @@ fastify.get('/db_data', async function (req, reply) {
131131

132132
If needed `pool` instance can be accessed via `fastify.oracle[.dbname].pool`
133133

134+
## Use of scoped executions
135+
136+
```js
137+
const fastify = require('fastify')
138+
139+
fastify.register(require('fastify-oracle'), {
140+
pool: {
141+
user: 'travis',
142+
password: 'travis',
143+
connectString: 'localhost/xe'
144+
}
145+
})
146+
147+
fastify.post('/user/:username', (req, reply) => {
148+
// will return a promise, fastify will send the result automatically
149+
return fastify.oracle.scope(async conn => {
150+
// will resolve to commit, or reject with an error
151+
return conn.execute(`INSERT INTO USERS (NAME) VALUES('JIMMY')`)
152+
})
153+
})
154+
155+
/* or with a scope callback
156+
157+
fastify.oracle.scope(conn => {
158+
return conn.execute('SELECT * FROM DUAL')
159+
},
160+
function onResult (err, result) {
161+
reply.send(err || result)
162+
}
163+
})
164+
165+
*/
166+
167+
/* or with a commit callback
168+
169+
fastify.oracle.scope((conn, commit) => {
170+
conn.execute('SELECT * FROM DUAL', (err, res) => {
171+
commit(err, res)
172+
});
173+
})
174+
175+
*/
176+
177+
```
178+
179+
134180
## License
135181

136182
[MIT License](http://jsumners.mit-license.org/)

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 transactScope (pool, fn, cb) {
7+
pool.getConnection(function (err, conn) {
8+
if (err) return cb(err)
9+
10+
const commit = (err, res) => {
11+
if (err) return conn.close(() => cb(err))
12+
13+
conn.commit(function (err) {
14+
conn.close(function () {
15+
if (err) {
16+
return cb(err)
17+
}
18+
return cb(null, res)
19+
})
20+
})
21+
}
22+
23+
const promise = fn(conn, commit)
24+
25+
if (promise && typeof promise.then === 'function') {
26+
promise.then(
27+
(res) => commit(null, res),
28+
(e) => commit(e))
29+
}
30+
})
31+
}
32+
33+
function transact (fn, cb) {
34+
if (cb && typeof cb === 'function') {
35+
return transactScope(this, fn, cb)
36+
}
37+
38+
return new Promise((resolve, reject) => {
39+
transactScope(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+
transact: transact.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)