Last active
July 13, 2017 01:25
-
-
Save dmcghan/9f09d931637df16baa634eebef55feed to your computer and use it in GitHub Desktop.
How to get, use, and close a DB connection using callbacks
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'); | |
function getEmployee(empId, getEmployeeCallback) { | |
oracledb.getConnection(function(err, conn) { | |
if (err) { | |
console.log('Error getting connection', err); | |
getEmployeeCallback(err); | |
return; | |
} | |
console.log('Connected to database'); | |
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); | |
getEmployeeCallback(err); | |
conn.close(function(err) { | |
if (err) { | |
console.log('Error closing connection', err); | |
} else { | |
console.log('Connection closed'); | |
} | |
}); | |
return; | |
} | |
console.log('Query executed'); | |
getEmployeeCallback(null, result.rows[0]); | |
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 dbConfig = require('./db-config.js'); | |
const employees = require('./employees.js'); | |
oracledb.createPool(dbConfig, function(err) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
employees.getEmployee(101, function(err, emp) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
console.log(emp); | |
}); | |
}); |
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": "callbacks", | |
"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": { | |
"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 callbacks. See the related blog post and the main post for the series for more details.