Saturday, January 9, 2016

hapi with oacle node js driver

 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
 
 
const Hapi = require('hapi');
const oracledb = require('oracledb');
 
var dbConPool;
 
//gracefull shut down of node js since we need to terminate con pool
 
if (process.platform === "win32") {
    //since no SIGINT on win, watch for Ctl+C
    console.log('win platform...');
    var rl = require("readline").createInterface({
        input: process.stdin,
        output: process.stdout
    });
 
    rl.on("SIGINT", () => {
        console.log('received SIGINT 0..');
        process.emit("SIGINT");
    });
}
 
process.on("SIGINT", () => {
    console.log('received SIGINT..');
    if (dbConPool) {
        console.log('terminate pool..');
        dbConPool.terminate((e) => {
            if (e) {
                console.log(e);
            }
        });
    }
 
    process.exit();
});
 
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 8000
});
 
 
// Add the route
server.route({
    method: 'GET',
    path: '/hapi/employee',
    handler: (request, reply) => {
 
        dbConPool.getConnection(
            (err, connection) => {
                if (err) {
                    console.error(err.message);
                    return reply('error');
                }
 
                connection.execute(
                    "SELECT * from employees where rownum < 10", [],
                    (err, result) => {
                        releaseDbCon(connection);
                        if (err) {
                            console.error(err.message);
                            return reply('error');
                        }
 
                        var employees = [];
                        result.rows.forEach((r) => {
                            employees.push({
                                id: r[0],
                                firstName: r[1],
                                lastName: r[2],
                                email: r[3],
                                phoneNumber: r[4],
                                hireDate: r[5],
                                jobId: r[6],
                                salary: r[7]
                            });
                        });
 
                        return reply(employees);
                    });
            });
 
 
    }
});
 
function releaseDbCon(connection) {
    connection.release(
        (err) => {
            if (err) {
                console.error(err.message);
            }
        });
}
 
// Start the server
server.start((err) => {
 
    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
 
    //create db con pool
    oracledb.createPool({
            user: "hr",
            password: "password",
            connectString: "localhost:1521/xe"
        },
        (err, pool) => {
            if (!err) {
                dbConPool = pool;
            }
        });
 
});
 
 
'use strict';


const Hapi = require('hapi');
const oracledb = require('oracledb');

var dbConPool;

//gracefull shut down of node js since we need to terminate con pool

if (process.platform === "win32") {
    //since no SIGINT on win, watch for Ctl+C
    console.log('win platform...');
    var rl = require("readline").createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.on("SIGINT", () => {
        console.log('received SIGINT 0..');
        process.emit("SIGINT");
    });
}

process.on("SIGINT", () => {
    console.log('received SIGINT..');
    if (dbConPool) {
        console.log('terminate pool..');
        dbConPool.terminate((e) => {
            if (e) {
                console.log(e);
            }
        });
    }

    process.exit();
});

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 8000
});


// Add the route
server.route({
    method: 'GET',
    path: '/hapi/employee',
    handler: (request, reply) => {

        dbConPool.getConnection(
            (err, connection) => {
                if (err) {
                    console.error(err.message);
                    return reply('error');
                }

                connection.execute(
                    "SELECT * from employees where rownum < 10", [],
                    (err, result) => {
                        releaseDbCon(connection);
                        if (err) {
                            console.error(err.message);
                            return reply('error');
                        }

                        var employees = [];
                        result.rows.forEach((r) => {
                            employees.push({
                                id: r[0],
                                firstName: r[1],
                                lastName: r[2],
                                email: r[3],
                                phoneNumber: r[4],
                                hireDate: r[5],
                                jobId: r[6],
                                salary: r[7]
                            });
                        });

                        return reply(employees);
                    });
            });


    }
});

function releaseDbCon(connection) {
    connection.release(
        (err) => {
            if (err) {
                console.error(err.message);
            }
        });
}

// Start the server
server.start((err) => {

    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);

    //create db con pool
    oracledb.createPool({
            user: "hr",
            password: "password",
            connectString: "localhost:1521/xe"
        },
        (err, pool) => {
            if (!err) {
                dbConPool = pool;
            }
        });

});

No comments: