Ruby
This guide will walk you through the process of installing and using Mycelite in a Ruby 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 sqlite3
gem install sqlite3
Setup the writer
Require SQLite
require 'sqlite3'
Load the extension
The extension must be loaded before opening the database.
db = SQLite3::Database.new ":memory:"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_reader')")
db.close
Open the database
db = SQLite3::Database.new "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.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/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 = `
sql = <<-SQL
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
SQL
db.execute(sql)
Setup the reader
Require SQLite
require 'sqlite3'
Load the extension
The extension must be loaded before opening the database.
db = SQLite3::Database.new ":memory:"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_reader')")
db.close
Open the database
db = SQLite3::Database.new "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.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/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.
sql = <<-SQL
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
SQL
db.execute(sql)
Observing Synchronization
In the writer instance, create a table and then insert a row.
db.execute('create table if not exists test(number integer);')
db.execute('insert into test values (42);')
# Give Mycelite time to sync before script exits
sleep(5)
db.close
In the reader instance, you will see the new table and can query it.
# Give Mycelite time to sync before querying.
sleep(5)
result = db.execute('select * from test;')
puts result
db.close
Complete Snippets
Complete Writer Snippet
require 'sqlite3'
db = SQLite3::Database.new ":memory:"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_writer')")
db.close
db = SQLite3::Database.new "writer.db"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_config')")
sql = <<-SQL
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
SQL
db.execute(sql)
db.execute('create table if not exists test(number integer);')
db.execute('insert into test values (42);')
# Give Mycelite time to sync before querying.
sleep(5)
db.close
Complete Reader Snippet
require 'sqlite3'
db = SQLite3::Database.new ":memory:"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_reader')")
db.close
db = SQLite3::Database.new "reader.db"
db.enable_load_extension 1
db.execute("select load_extension('#{__dir__}/libmycelite', 'mycelite_config')")
sql = <<-SQL
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
SQL
db.execute(sql)
# Give Mycelite time to sync before script exits
sleep(5)
result = db.execute('select * from test;')
puts result
db.close