Node.js
This guide will walk you through the process of installing and using Mycelite in a Node.js project.
Download the extension
- Mac Arm
- Mac x86
- Linux Arm
- Linux x86 gnu
- Linux x86 musl
- Windows x86 gnu
- Windows x86 msvc
curl -L https://github.com/mycelial/mycelite/releases/latest/download/aarch64-apple-darwin.tgz --output aarch64-apple-darwin.tgz
tar -xvzf aarch64-apple-darwin.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-apple-darwin.tgz --output x86_64-apple-darwin.tgz
tar -xvzf x86_64-apple-darwin.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/arm-unknown-linux-gnueabihf.tgz --output arm-unknown-linux-gnueabihf.tgz
tar -xvzf arm-unknown-linux-gnueabihf.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-unknown-linux-gnu.tgz --output x86_64-unknown-linux-gnu.tgz
tar -xvzf x86_64-unknown-linux-gnu.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-unknown-linux-musl.tgz --output x86_64-unknown-linux-musl.tgz
tar -xvzf x86_64-unknown-linux-musl.tgz
curl.exe -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-pc-windows-gnu.zip --output x86_64-pc-windows-gnu.zip
tar.exe -xvzf x86_64-pc-windows-gnu.zip
curl.exe -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-pc-windows-msvc.zip --output x86_64-pc-windows-msvc.zip
tar.exe -xvzf x86_64-pc-windows-msvc.zip
Install better-sqlite3
npm install better-sqlite3;
Setup the writer
Import better-sqlite
import Database from 'better-sqlite3';
Load the extension
The extension must be loaded before opening the database.
let db = new Database(':memory:');
db.loadExtension('./libmycelite', 'mycelite_writer');
Open the database
db = new Database('writer.db');
Configure Mycelite
The first time you use Mycelite, you'll need to load the configuration extension and configure Mycelite, subsequently, you can skip this step.
db.loadExtension('./libmycelite', 'mycelite_config');
Signup for a free account on the hub and retrieve your client id and secret.
Configure Mycelite by running the following insert statement. The domain is a slug-like unique name of your choosing. The client_id is your user name and the secret is obtained from the hub.
const sql = `
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
`;
db.exec(sql);
Setup the reader
Import better-sqlite
import Database from 'better-sqlite3';
Load the extension
The extension must be loaded before opening the database.
let db = new Database(':memory:');
db.loadExtension('./libmycelite', 'mycelite_reader');
Open the database
db = new Database('reader.db');
Configure Mycelite
The first time you use Mycelite, you'll need to load the configuration extension and configure Mycelite, subsequently, you can skip this step.
db.loadExtension('./libmycelite', 'mycelite_config');
Signup for a free account on the hub and retrieve your client id and secret.
Configure Mycelite by running the following insert statement. The domain is a slug-like unique name of your choosing. The client_id is your user name and the secret is obtained from the hub.
const sql = `
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
`;
db.exec(sql);
Observing Synchronization
In the writer instance, create a table and then insert a row.
db.exec('create table test(number integer);');
db.exec('insert into test(number) values (42);');
// Give Mycelite time to sync before script exits
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(5000);
In the reader instance, you will see the new table and can query it.
// Give Mycelite time to sync before querying.
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(5000);
const result = db.prepare('select * from test;').all();
console.log(result);
Complete Snippets
Complete Writer Snippet
import Database from 'better-sqlite3';
let db = new Database(':memory:');
db.loadExtension('./libmycelite', 'mycelite_writer');
db = new Database('writer.db');
db.loadExtension('./libmycelite', 'mycelite_config');
const sql = `
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
`;
db.exec(sql);
db.exec('create table test(number integer);');
db.exec('insert into test(number) values (42);');
// Give Mycelite time to sync before script exits
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(5000);
Complete Reader Snippet
import Database from 'better-sqlite3';
let db = new Database(':memory:');
db.loadExtension('./libmycelite', 'mycelite_reader');
db = new Database('reader.db');
db.loadExtension('./libmycelite', 'mycelite_config');
const sql = `
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
`;
db.exec(sql);
// Give Mycelite time to sync
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(5000);
const result = db.prepare('select * from test;').all();
console.log(result);