Installation (via npm)
$ npm install hapi-plugin-oracledb --save
Initialize a Oracle database connection pool and either attach a OracleDB connection to every request or enable the user to manually use connection from the server via server.getDb(function (err, connection) {})
.
Initialize the plugin using same options you can pass onto the oracledb
lib for making a connection. See https://www.npmjs.com/package/oracledb for more info on the oracledb
lib itself.
Initialize Plugin
server.register({
register: require('hapi-plugin-oracledb'),
options: {
connectString: "localhost:1521/servicename",
user: "root",
password: ""
}
}, function (err) {
if (err) console.log(err);
...
});
OracleDB pool injection into request.app.db
object will be enabled only if routes or api realms which need the injection pass the following parameter during initialization.
Route Injection
server.route({
method: 'GET',
config: {
plugins:{
oracledb: true
}
},
path: '/api/getName',
handler: function (request, reply) {
}
});
Plugin Realm Injection
server.register(
{
register:require('./server/api/main'),
options:{
oracledb:true
},
routes: {
prefix: '/api'
}
}, (err) =>
{
if (err) {
console.error('Failed to load plugin:', err);
}
}
);
server.route({
method: 'GET',
path: '/teamhierarchy/{employeenumbers}',
config: {
plugins:{
'oracledb': true //No need to pass this here if already passed at Plugin Realm level
}
},
handler: function (request, reply) {
var query = 'select firstname,lastname from users';
request.app.db.execute(query,function (err,result) {
reply(result);
});
}
});
Via request.app.db
. You can also manualy get a connection from the server via server.getDb(function (err, connection) {})
.
server.route({
method: 'GET',
config: {
plugins:{
'oracledb': true
}
},
path: '/api/getName',
handler: function (request, reply) {
var query = 'select firstname,lastname from users';
server.getDb(function(err,connection){
connection.execute(query,function (err,result) {
reply(result);
});
});
}
});