Skip to main content

Ruby

This guide will walk you through the process of installing and using Mycelite in a Ruby project.

Download the extension

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

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