Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.turso.tech/llms.txt

Use this file to discover all available pages before exploring further.

This is currently in technical preview. Join us in Discord to report any issues.

Prerequisites

Before you start, make sure you:
1

Install the libsql_activerecord Rubygem

Add the following to your Gemfile:
gem 'libsql_activerecord'
Then run:
bundle install
2

Configure database credentials

Get the database URL:
turso db show --url <database-name>
Get the database authentication token:
turso db tokens create <database-name>
Assign credentials to the environment variables inside .env.
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
Add your Turso credentials to your Rails credentials file or environment variables.
3

Use the libsql adapter

default: &default
  adapter: libsql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  url: <%= ENV['TURSO_DATABASE_URL'] %>
  auth_token: <%= ENV['TURSO_AUTH_TOKEN'] %>
  path: storage/development.sqlite3 # For embedded replica
4

Create and run migrations

Generate a migration:
rails generate migration CreateProducts name:string description:text
Run the migration:
rails db:migrate
5

Define models

Create a model in app/models/product.rb:
class Product < ApplicationRecord
  validates :name, presence: true
end
6

Use in controllers

In your controllers, you can now use ActiveRecord methods to interact with your Turso database:
class ProductsController < ApplicationController
  def index
    @products = Product.all
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      redirect_to @product, notice: 'Product was successfully created.'
    else
      render :new
    end
  end

  private

  def product_params
    params.require(:product).permit(:name, :description)
  end
end
7

Execute raw SQL (if needed)

You can also execute raw SQL queries:
results = ActiveRecord::Base.connection.execute("SELECT * FROM products")

Examples

Ruby on Rails App

See the full source code