Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a node.js module which uses c libraries for monetdb connection?

I am trying to connect monetdb with node.js. I have a simple (20 line) c program which can query moentdb using mapi libraries.

Can I use those libraries to build something(module/addon) for node.js which uses these libraries and connect to monetdb?

(using odbc is an option but it have its own disadvantages.)

Update1 :
node-ffi is pretty awesome. I was able to create a fetch table program pretty easily. (I have added my working code for example.)

So if I have 3 options
1. ODBC
2. node-ffi
3. a c program to fetch database data and listens to connection from node.js through socket

In terms of performance which is better option to implement, if I have little less time to develop a addon for node.js

var ffi = require("ffi");
var libmylibrary = ffi.Library('/usr/local/lib/libmapi.so', {
    "mapi_connect":["int",["string",'int',"string","string","string","string"]],
    "mapi_query":['int',["int","string"]],
    "mapi_fetch_row":["int",["int"]],
    "mapi_fetch_field":["string",["int","int"]]
});


var res = libmylibrary.mapi_connect("localhost", 50000,"monetdb", "monetdb", "sql", "demo");
console.log(res);
var ret=libmylibrary.mapi_query(res,"select * from table");
while(libmylibrary.mapi_fetch_row(ret)){
    console.log(libmylibrary.mapi_fetch_field(ret,0));
    console.log(libmylibrary.mapi_fetch_field(ret,1));
}

Update 2:
Above code is not recommended for production use...it does not use async functionality of node.js so please use it for baby steps

like image 465
Gaurav Avatar asked Oct 05 '22 15:10

Gaurav


1 Answers

While FFI makes it easy to call native code, you really shouldn't use it to for something you have to do often, like calling a database library. From the docs:

There is non-trivial overhead associated with FFI calls. Comparing a hard-coded binding version of strtoul() to an FFI version of strtoul() shows that the native hard-coded binding is orders of magnitude faster. So don't just use the C version of a function just because it's faster. There's a significant cost in FFI calls, so make them worth it.

In other words, FFI works, but is slow. This is fine if you just need to make a few calls, but very bad news if you need to make frequent calls.

What you need to do is write an addon. Addons are C++ modules that provide glue to C and C++ libraries. (Just because you must write the addon in C++ doesn't mean you can't call pure C code from the addon!)

The node docs provide plenty of examples that should get you started. If you're working with Windows, here's some tips to get VS set up.

If the calls to the C library are blocking, you'll need to make them async. libuv provides a thread pool you can do the work on.

like image 153
josh3736 Avatar answered Oct 18 '22 05:10

josh3736