Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Oracle database with Node.js Windows

I am trying to connect to an Oracle database from Node.js in Windows 7. Is this possible? I have not found a plugin for Node.js which will do this for Windows. Are there any recommended work arounds for this? I am guessing there is at least one other person wanting to use Node.js on Windows and needs to connect to Oracle. I'm open to simple workarounds if that is necessary. Thanks for the help.

like image 383
Matthew Crews Avatar asked Apr 04 '12 19:04

Matthew Crews


People also ask

Can we use Oracle with node js?

Your applications can also use Oracle's document storage SODA calls. Node-oracledb can be used with TypeScript or directly with Node. js.

Can JavaScript connect to Oracle database?

Javascript can't talk to a database. It runs on the client. The DB is on the server.


2 Answers

Do you need to connect directly from Node.js to oracle? You could write your database transactions in another language and expose them to Node.js via a web service.

like image 187
Matthew Watson Avatar answered Sep 19 '22 10:09

Matthew Watson


The state of database drivers for node.js on Windows seems somewhat immature compared to the robust and highly performant database drivers we have had available in ADO.NET for years.

I would seriously consider using Edge to call C# or a CLR assembly in-process to access your database. You could write a Repository style data access layer in C# and call it from node.js.

I have proven this works in a development context with C#, PetaPoco (optional), .NET 4.5 and the Oracle ODP driver (Oracle.DataAccess.dll). This should also work with any database that you can talk to in .NET.

Node (server.js) example to call .NET CLR function:

var edge = require('edge');

// define CLR function proxy
var getData = edge.func({
    assemblyFile: '../Repositories/bin/Debug/Repositories.dll',
    typeName: 'Repositories.TestRepository',
    methodName: 'GetData' // This must be Func<object,Task<object>>
});

// call proxy function
getData({ myParam:1 }, function (error, result) {
    if (error) throw error;
    console.log(result);
});

GetData C# looks like this (note you need to put your connection string in node.exe.config in the folder that contains node.exe):

public async Task<object> GetData(object param)
{
    using (var db = new Database("NameOfConnString"))
    {
        return db.Fetch<dynamic>("SELECT * FROM sometable");
    }
}

(Note that Oracle.DataAccess.dll needs to be in the folder that contains node.exe.)

like image 41
saille Avatar answered Sep 21 '22 10:09

saille