Skip to content

Commit

Permalink
Merge pull request #19 from cemremengu/connection-scope
Browse files Browse the repository at this point in the history
Feature: Execution scope (transact)
  • Loading branch information
cemremengu authored Nov 28, 2018
2 parents 8da79ea + 2cd902c commit 450d96a
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 228 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,52 @@ fastify.get('/db_data', async function (req, reply) {

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

## Use of scoped executions

```js
const fastify = require('fastify')

fastify.register(require('fastify-oracle'), {
pool: {
user: 'travis',
password: 'travis',
connectString: 'localhost/xe'
}
})

fastify.post('/user/:username', (req, reply) => {
// will return a promise, fastify will send the result automatically
return fastify.oracle.scope(async conn => {
// will resolve to commit, or reject with an error
return conn.execute(`INSERT INTO USERS (NAME) VALUES('JIMMY')`)
})
})

/* or with a scope callback
fastify.oracle.scope(conn => {
return conn.execute('SELECT * FROM DUAL')
},
function onResult (err, result) {
reply.send(err || result)
}
})
*/

/* or with a commit callback
fastify.oracle.scope((conn, commit) => {
conn.execute('SELECT * FROM DUAL', (err, res) => {
commit(err, res)
});
})
*/

```


## License

[MIT License](http://jsumners.mit-license.org/)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A plugin for Fastify to provide Oracle DB connections",
"main": "plugin.js",
"scripts": {
"test": "standard && tap --cov test.js",
"test": "standard && tap --cov test/*.test.js",
"lint": "standard | snazzy"
},
"precommit": [
Expand Down
43 changes: 42 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,51 @@
const fp = require('fastify-plugin')
const oracledb = require('oracledb')

function transactScope (pool, fn, cb) {
pool.getConnection(function (err, conn) {
if (err) return cb(err)

const commit = (err, res) => {
if (err) return conn.close(() => cb(err))

conn.commit(function (err) {
conn.close(function () {
if (err) {
return cb(err)
}
return cb(null, res)
})
})
}

const promise = fn(conn, commit)

if (promise && typeof promise.then === 'function') {
promise.then(
(res) => commit(null, res),
(e) => commit(e))
}
})
}

function transact (fn, cb) {
if (cb && typeof cb === 'function') {
return transactScope(this, fn, cb)
}

return new Promise((resolve, reject) => {
transactScope(this, fn, function (err, res) {
if (err) { return reject(err) }
return resolve(res)
})
})
}

function decorateFastifyInstance (pool, fastify, options, next) {
const oracle = {
getConnection: pool.getConnection.bind(pool),
pool
pool,
transact: transact.bind(pool)
}

if (options.name) {
Expand Down
226 changes: 0 additions & 226 deletions test.js

This file was deleted.

Loading

0 comments on commit 450d96a

Please sign in to comment.