Skip to content

Commit 431d745

Browse files
committed
Fix committing, update readme
1 parent 992997e commit 431d745

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
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/)

plugin.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,33 @@ function executionScope (pool, fn, cb) {
77
pool.getConnection(function (err, conn) {
88
if (err) return cb(err)
99

10-
const doRelease = (conn) => {
11-
conn.close(function () { })
10+
const release = (conn) => {
11+
conn.close(function (_) {
12+
})
1213
}
1314

14-
const done = (err, res) => {
15-
doRelease(conn)
16-
15+
const commit = (err, res) => {
1716
if (err) {
17+
release(conn)
1818
return cb(err)
1919
}
20-
return cb(null, res)
20+
21+
conn.commit(function (err) {
22+
if (err) {
23+
release(conn)
24+
return cb(err)
25+
}
26+
release(conn)
27+
return cb(null, res)
28+
})
2129
}
2230

23-
const promise = fn(conn, done)
31+
const promise = fn(conn, commit)
2432

2533
if (promise && typeof promise.then === 'function') {
2634
promise.then(
27-
(res) => done(null, res),
28-
(e) => done(e))
35+
(res) => commit(null, res),
36+
(e) => commit(e))
2937
}
3038
})
3139
}

test/lib.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ test('execution scope with execute callback', (t) => {
175175
t.error(err)
176176
t.ok(fastify.oracle.pool)
177177

178-
fastify.oracle.scope((conn, done) => {
178+
fastify.oracle.scope((conn, commit) => {
179179
conn.execute('SELECT * FROM DUAL', function (err, result) {
180-
done(err, result)
180+
commit(err, result)
181181
})
182182
}, function (err, res) {
183183
t.error(err)
@@ -247,9 +247,9 @@ test('execution scope with execute callback (error)', (t) => {
247247
t.error(err)
248248
t.ok(fastify.oracle.pool)
249249

250-
fastify.oracle.scope((conn, done) => {
250+
fastify.oracle.scope((conn, commit) => {
251251
conn.execute('SELECT * FROM ??', function (err, result) {
252-
done(err, result)
252+
commit(err, result)
253253
})
254254
}, function (err, res) {
255255
t.is(res, undefined)

0 commit comments

Comments
 (0)