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;
            }
        });

});

Friday, January 8, 2016

npm install oracledb

npm install oracledb

Trying to install oracle node.js driver requires some effort. Since most of the github conversation on this issues are locked, i will describe it here, may be someone benefit.

My Environment

Windows 10 - 64 bit
Node.js 5.4.0 - 64 bit
Dot net framework 4.x
Visual Studio Community Edition 14
Oracle Xpress 11g 64 bit


Make sure all of your environment is either 64 bit or 32 bit, do not mix.


Prerequisites for oracledb

1- Python 2.7+
2- Dot net framework
3- Visual Studio C/C++ compiler

In win start menu type dev and open vs dev command prompt.
>cd "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
>vcvarsall amd64

--This will switch to vs 64 bit compiler

--set following local env variables. Since i have oracle xpress locally installed:

>set OCI_LIB_DIR=C:\oraclexe\app\oracle\product\11.2.0\server\oci\lib\MSVC
>set OCI_INC_DIR=C:\oraclexe\app\oracle\product\11.2.0\server\oci\include

>cd back-to-node-project-dir
>npm install oracledb

It should work!