119 releases (31 breaking)

0.33.4 Jan 25, 2024
0.33.2 Nov 24, 2023
0.31.8 Jul 11, 2023
0.16.2 Mar 31, 2023

#204 in Network programming

Download history 1951/week @ 2023-12-22 1511/week @ 2023-12-29 1427/week @ 2024-01-05 1498/week @ 2024-01-12 1614/week @ 2024-01-19 440/week @ 2024-01-26 92/week @ 2024-02-02 109/week @ 2024-02-09 130/week @ 2024-02-16 223/week @ 2024-02-23 255/week @ 2024-03-01 259/week @ 2024-03-08 174/week @ 2024-03-15 91/week @ 2024-03-22 192/week @ 2024-03-29 163/week @ 2024-04-05

657 downloads per month
Used in 3 crates

Apache-2.0

77KB
1.5K SLoC

Rust libSQL client library

libSQL Rust client library can be used to communicate with sqld natively over HTTP protocol with native Rust interface.

At the moment the library works with the following backends:

  • local
  • reqwest
  • hrana
  • Cloudflare Workers environment (optional)

Quickstart

In order to use the database in your project, just call libsql_client::Client::from_env(), or any of the other constructors:

let db = libsql_client::Client::from_env().await?;

The only thing you need to provide is an env variable with the database URL, e.g.

export LIBSQL_CLIENT_URL="file:////tmp/example.db"

for a local database stored in a file, or

export LIBSQL_CLIENT_URL="https://example.turso.io"

for a remote database connection.

You can also explicitly use a specific backend. Examples of that are covered in the next paragraphs.

Local

In order to connect to the database, set up the URL to point to a local path:

export LIBSQL_CLIENT_URL = "/tmp/example.db"

local_backend feature is enabled by default, so add the dependency like this:

cargo add libsql-client

Example for how to connect to the database and perform a query:

    let db = libsql_client::local::Client::from_env()?;
    let response = db
        .execute("SELECT * FROM table WHERE key = 'key1'")
        .await?;
    (...)

Cloudflare Workers

In order to connect to the database, set up the following variables in .dev.vars, or register them as secrets:

LIBSQL_CLIENT_URL = "https://your-db-url.example.com"
LIBSQL_CLIENT_TOKEN = "<your-jwt>"

Add it as dependency with workers_backend backend enabled. Turn off default features, as they are not guaranteed to compile to wasm32-unknown-unknown, which is required in this environment:

cargo add libsql-client --no-default-features -F workers_backend

Example for how to connect to the database and perform a query from a GET handler:

router.get_async("/", |_, ctx| async move {
    let db = libsql_client::workers::Client::from_ctx(&ctx).await?;
    let response = db
        .execute("SELECT * FROM table WHERE key = 'key1'")
        .await?;
    (...)

Dependencies

~5–22MB
~347K SLoC