Last active
July 13, 2017 01:25
-
-
Save dmcghan/9e507fbd5383d9f768364f8d4d3fdecf to your computer and use it in GitHub Desktop.
How to get, use, and close a DB connection using the Async module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
user: 'hr', | |
password: 'oracle', | |
connectString: 'localhost:1521/orcl', | |
poolMax: 20, | |
poolMin: 20, | |
poolIncrement: 0 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const oracledb = require('oracledb'); | |
const async = require('async'); | |
function getEmployee(empId, getEmployeeCallback) { | |
async.waterfall( | |
[ | |
function(callback) { | |
oracledb.getConnection(function(err, conn) { | |
if (err) { | |
console.log('Error getting connection', err); | |
} else { | |
console.log('Connected to database'); | |
} | |
callback(err, conn); | |
}); | |
}, | |
function(conn, callback) { | |
conn.execute( | |
`select * | |
from employees | |
where employee_id = :emp_id`, | |
[empId], | |
{ | |
outFormat: oracledb.OBJECT | |
}, | |
function(err, result) { | |
if (err) { | |
console.log('Error executing query', err); | |
} else { | |
console.log('Query executed'); | |
} | |
callback(err, conn, result); | |
} | |
); | |
} | |
], | |
function(err, conn, result) { | |
if (err) { | |
getEmployeeCallback(err); | |
} else { | |
getEmployeeCallback(null, result.rows[0]); | |
} | |
// If error getting conn, no need to close. | |
if (conn) { | |
conn.close(function(err) { | |
if (err) { | |
console.log('Error closing connection', err); | |
} else { | |
console.log('Connection closed'); | |
} | |
}); | |
} | |
} | |
); | |
} | |
module.exports.getEmployee = getEmployee; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const oracledb = require('oracledb'); | |
const async = require('async'); | |
const dbConfig = require('./db-config.js'); | |
const employees = require('./employees.js'); | |
async.series( | |
[ | |
function(callback) { | |
oracledb.createPool(dbConfig, function(err) { | |
callback(err); | |
}); | |
}, | |
function(callback) { | |
employees.getEmployee(101, function(err, emp) { | |
if (err) { | |
callback(err); | |
return; | |
} | |
console.log(emp); | |
}); | |
} | |
], | |
function(err) { | |
if (err) { | |
console.log(err); | |
} | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "async-module", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Dan McGhan <dan.mcghan@oracle.com> (https://jsao.io/)", | |
"license": "ISC", | |
"dependencies": { | |
"async": "^2.4.1", | |
"oracledb": "^1.13.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a demo app that shows how to get, use, and close a DB connection using the Async module. See the related blog post and the main post for the series for more details.