Friday, November 21, 2014

Node.JS and Firebird driver (node-firebird-libfbclient) installing on Amazon EC2 instance

Here are the notes on Installing node.js module for firebird
I assume Firebird was installed from source or from tar.gz and in a linux instance


wget https://nodejs.org/dist/v4.6.0/node-v4.6.0.tar.gz
tar -zxvf node-v4.6.0.tar.gz
cd  node-v4.6.*
./configure 
sudo make install

Next is time to install the firebird module (Firebird was installed from source in /opt/firebird2.5.x)

git clone git://github.com/xdenser/node-firebird-libfbclient.git
cd node-firebird-lib
export PATH=$PATH:/opt/firebird2.5.x/binfbclient/

npm -g install

create firebird db and test table in /tmp/nodedb.fdb with isql or isql-fb

SQL>CREATE DATABASE "localhost:/tmp/nodedb.fdb" user 'SYSDBA' password '*********';
SQL> CREATE TABLE TEST (ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(20));
SQL> create generator gen_test_id;
SQL> SET GENERATOR gen_test_id TO 10;
SQL> set term !! ;
SQL> CREATE TRIGGER TEST_BI FOR TEST
CON> ACTIVE BEFORE INSERT POSITION 0
CON> AS
CON> BEGIN
CON> if (NEW.ID is NULL) then NEW.ID = GEN_ID(GEN_TEST_ID, 1);
CON> END!!
SQL> set term ; !!

Then write the test.js script to connect to it

var fb = require("firebird");
sys = require("sys");
var con = fb.createConnection();
con.connectSync('/tmp/nodedb.fdb','SYSDBA','masterkey','');
con.querySync("insert into test (name) values ('new one')");
con.commitSync();
var res = con.querySync("select * from test");
var rows = res.fetchSync("all",true);
console.log(sys.inspect(rows));
view raw test.js hosted with ❤ by GitHub


you can observe that the id is auto incremented if you run it multiple time

node test.js 
[ { ID: 5, NAME: 'new one' },
  { ID: 11, NAME: 'new one' },
  { ID: 12, NAME: 'new one' },
  { ID: 13, NAME: 'new one' },
  { ID: 14, NAME: 'new one' } ]

also in the name you can put later the
browser or the ip in one of the columns


We build now the version for the web modifying the hello world version adding the firebird database glue

var http = require('http');
var fb = require("firebird");
sys = require("sys");
var con = fb.createConnection();
con.connectSync('/tmp/nodedb.fdb','SYSDBA','masterkey','');
http.createServer(function (req, res) {
con.querySync("insert into test (name) values ('new one')");
con.commitSync();
var result = con.querySync("select * from test order by id desc rows 1 to 10");
var rows = result.fetchSync("all",true);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Last 10 inserts:'+sys.inspect(rows)+'\n');
}).listen(8080);
console.log('Server running on *:8080');


you can run it with
 node firebird_example.js

No comments: