node.js

The node.js interface is based on node-sqlite3. You can also use sqlite3-wrapper on top of that.



Installation on Linux and Mac

1. Install LiteReplica

You can either compile it or use pre-compiled binaries. You can request them by e-mail for testing purposes.


2. Build and install node-sqlite3

npm install sqlite3 --build-from-source --sqlite=/usr



To Run:

We can either use LD_LIBRARY_PATH, like this:

LD_LIBRARY_PATH=/usr/lib node your-code.js


Or patch the module to include rpath:

patchelf --set-rpath /usr/lib node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node

And then run your code:

node your-code.js





Installation on Windows

1. Install LiteReplica

You can either compile it or use pre-compiled binaries. You can request them by e-mail for testing purposes.


2. Build and install node-sqlite3

npm install sqlite3 --build-from-source --sqlite=C:/litereplica --sqlite_libname=litereplica-0.1





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) return console.log('db.exec: ' + err);

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

  });


  setInterval(function() {

    db.get("PRAGMA replica_status", function(err, row) {
      if (err) console.log('db.get: ' + err);
      else console.log("status: " + row.replica_status);
    });

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

  }, 4000);

  //db.close();

});

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.get("PRAGMA replica_status", function(err, row) {
      if (err) console.log('db.get: ' + err);
      else console.log("status: " + row.replica_status);
  });

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

}, 4000);

//db.close();