Node.js

The Node.js interface is based on better-sqlite3



Installation

First follow the instructions to compile and install LiteSync from the source code or use the pre-compiled binaries for your platform. You can start with the free version.


Then you can install the wrapper with this command:


npm install better-sqlite3-litesync



Example code
Primary Node
const options = { verbose: console.log };
const uri = 'file:test.db?node=primary&bind=tcp://0.0.0.0:1234';
const db = require('better-sqlite3-litesync')(uri, options);

db.on('ready', function() {

  db.exec('CREATE TABLE IF NOT EXISTS users (name, email)');

});

db.on('sync', function() {

  console.log('the database received updates');

  const rows = db.prepare('SELECT * FROM users').all();
  for (var row of rows) {
    console.log(row.name, row.email);
  }

});

Secondary Node
const options = { verbose: console.log };
const uri = 'file:test.db?node=secondary&connect=tcp://127.0.0.1:1234';
const db = require('better-sqlite3-litesync')(uri, options);

db.on('ready', function() {

  db.exec("INSERT INTO users (name, email) values ('john',123)");

});

db.on('sync', function() {

  console.log('the database received updates');

  const rows = db.prepare('SELECT * FROM users').all();
  for (var row of rows) {
    console.log(row.name, row.email);
  }

});

You can also check the status with:

let res = db.prepare('PRAGMA sync_status').get();
let status = JSON.parse(res.sync_status);


Troubleshooting

The installation can fail if there is no pre-built wrapper binary for your platform. In this case your computer needs to have the proper build tools.


On Windows

Run this command:

npm install --global --production windows-build-tools

Then try to install the wrapper again.