node.js

The node.js interface is based on node-sqlite3


Installation on Linux and Mac

1. Download and compile nanomsg

wget https://github.com/nanomsg/nanomsg/archive/1.0.0.tar.gz -O nanomsg-1.0.tar.gz

tar -zxvf nanomsg-1.0.tar.gz

cd nanomsg-

./configure

make

sudo make install

sudo ldconfig

cd ..


2. Download and compile binn

git clone https://github.com/liteserver/binn

cd binn

make

sudo make install

cd ..


3. Download the node-sqlite3

wget https://github.com/mapbox/node-sqlite3/archive/v3.1.1.tar.gz

tar -zxvf v3.1.1.tar.gz


4. Download the node-litereplica code and replace some of the node-sqlite3 files

cd node-sqlite3

wget -q -O - http://litereplica.io/node-litereplica.tar | tar -xv --overwrite


5. Compile the node-sqlite3 normally

make



Troubleshooting

if it fails with "/usr/bin/env: node: No such file or directory" run this:

cd /usr/bin

sudo ln -s nodejs node

cd -

make


if it fails with "make: *** [build] Error 1" run this:

make verbose





Installation on Windows

(work in progress)





Testing Point-in-Time Recovery

Note: the node.js wrapper creates the restore points but the restoration must be done with the shell application

var sqlite3 = require('sqlite3').verbose();

var db = new sqlite3.Database('file:test.db?pitr=on&pitr_path=.&pitr_limit=50M');

db.serialize(function() {

  db.run("CREATE TABLE t1 (id,name)");

  var stmt = db.prepare("INSERT INTO t1 VALUES (?,?)");
  for (var i = 0; i < 10; i++) {
      stmt.run([i, "user " + i]);
  }
  stmt.finalize();

  db.each("SELECT * FROM t1", function(err, row) {
      if (err) console.log('db.each: ' + err);
      else console.log(row.id + ": " + row.name);
  });
});

db.close();



Testing Replication

Master:

var sqlite3 = require('sqlite3').verbose();

var db = new sqlite3.Database('file:master.db?replica=master&bind=tcp://0.0.0.0:1234');

// enabling point-in-time recovery
//var db = new sqlite3.Database('file:master.db?replica=master&slave=tcp://127.0.0.1:1234&pitr=on&pitr_path=.&pitr_limit=50M');

db.serialize(function() {

  db.exec("CREATE TABLE t1 (id,name)", function(err) {
    if (err) console.log('db.exec: ' + err);
  });

  var stmt = db.prepare("INSERT INTO t1 VALUES (?,?)");
  for (var i = 0; i < 20; i++) {
      stmt.run([i, "user " + i]);
  }
  stmt.finalize();

  setTimeout(function() {
    db.each("SELECT * FROM t1", function(err, row) {
      if (err) console.log('db.each: ' + err);
      else console.log(row.id + ": " + row.name);
    }, function(err,count) {
      if (err) console.log('db.each complete: ' + err);
      console.log("retrieved rows: " + count + "\nclosing");
      db.close();
    });
  }, 4000);

});

Slave:

var sqlite3 = require('sqlite3').verbose();

var db = new sqlite3.Database('file:slave.db?replica=slave&master=tcp://127.0.0.1:1234');

// enabling point-in-time recovery
//var db = new sqlite3.Database('file:slave.db?replica=slave&master=tcp://127.0.0.1:1234&pitr=on&pitr_path=.&pitr_limit=100M');

setInterval(function() {

  console.log("retrieving data...");

  db.each("SELECT * FROM t1", function(err, row) {
      if (err) console.log('db.each: ' + err);
      else console.log(row.id + ": " + row.name);
  }, function(err,count) {
      if (err) console.log('db.each complete: ' + err);
      console.log("retrieved rows: " + count + "\nclosing");
  });

}, 5000);

//db.close();