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 LiteSync

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 LiteSync

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:/litesync --sqlite_libname=litesync-0.1





Testing Replication

Config for primary node:

{
  "uri": "file:app.db?node=primary&bind=tcp://0.0.0.0:1234",
  "node": "primary"
}

Config for secondary node:

{
  "uri": "file:app.db?node=secondary&connect=tcp://127.0.0.1:1234",
  "node": "secondary"
}

Application:

var config = require('./config.json');
var sqlite3 = require('sqlite3').verbose();

var db = new sqlite3.Database(config.uri);

function start_access() {

  db.serialize(function() {

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

    var stmt = db.prepare("INSERT INTO t1 VALUES (?)");
    for (var i = 1; i <= 3; i++) {
        stmt.run(["row " + i + " from " + config.node + " node"]);
    }
    //stmt.finalize();

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

  });

}

// check if the db is ready
var id = setInterval(function() {

  db.get("PRAGMA sync_status", function(err, res) {
      if (err) return console.log('getting synchronization status: ' + err);
      var status = JSON.parse(res.sync_status);
      if (status.db_is_ready) {
        clearTimeout(id);
        start_access();
      }
  });

}, 1000);

Create 2 folders, save the app and one config file in each folder, open 2 terminals, cd to each folder and run the app on both.

You can do the same on separate devices changing the TCP address in the config.