Reference
libSQL Rust Reference
The libSQL Rust crate contains everything you need to work with Turso and works flawlessly with popular async runtimes like tokio
.
Installing
Install the crate in your project using the following command:
Conditional compilation
The libsql rust client supports conditionally compiling certain features to reduce compile times depending on what features you would like to use.
The following features are available:
Feature | Description |
---|---|
remote | Enables the HTTP-only client, allowing communication with a remote sqld server using pure Rust. Does not require compiling C code for SQLite. Suitable for projects that only need to interact with a remote database. |
core | Enables the local database only, incorporating the C SQLite3 code into the build. This is the foundation for local database operations but does not include additional features like replication or encryption. |
replication | Combines core with additional code required for replication, enabling the embedded replica features. |
encryption | Enables encryption at rest support, adding the necessary code to compile encryption capabilities and expose functions for configuring it. This is optional and not enabled by default, catering to projects that require enhanced data security. |
Using core
and replication
features require a c compiler. The encryption
feature requires cmake
to be installed on your system.
Initializing
Make sure add the crate to your project at the top of your file:
In-Memory Databases
libSQL supports connecting to in-memory databases for cases where you don’t require persistence:
Local Development
You can work locally using an SQLite file using new_local
:
Embedded Replicas
You can work with embedded replicas using new_remote_replica
that can sync from the remote URL and delegate writes to the remote primary database:
Manual Sync
The sync
function allows you to sync manually the local database with the remote counterpart:
If you require full control over how frames get from your instance of sqld
(libSQL Server), you can do this using new_local_replica
and sync_frames
. Reach out to us on Discord if you want to learn more.
Sync Interval
The sync_interval
function allows you to set an interval for automatic synchronization of the database in the background:
Read Your Own Writes
The read_your_writes
function configures the database connection to ensure that writes are immediately visible to subsequent read operations initiated by the same connection. This is enabled by default, and is particularly important in distributed systems to ensure consistency from the perspective of the writing process.
You can disable this behavior by passing false
to the function:
Encryption
To enable encryption on a SQLite file (new_local
or new_remote_replica
), make sure you have the encryption
feature enabled, and pass the encryption_config
:
Encrypted databases appear as raw data and cannot be read as standard SQLite databases. You must use the libSQL client for any operations — learn more.
Simple query
You can pass a string to execute()
to invoke a SQL statement, as well as optional arguments:
Prepared Statements
You can prepare a cached statement using prepare()
and then execute it with query()
:
Placeholders
libSQL supports the use of positional and named placeholders within SQL statements:
Deserialization
You can use the de::from_row
function to deserialize a row into a struct:
Batch Transactions
A batch consists of multiple SQL statements executed sequentially within an implicit transaction. The backend handles the transaction: success commits all changes, while any failure results in a full rollback with no modifications.
Interactive Transactions
Interactive transactions in SQLite ensure the consistency of a series of read and write operations within a transaction’s scope. These transactions give you control over when to commit or roll back changes, isolating them from other client activity.
transaction()
— with default transaction behavior (DEFFERED
)transaction_with_behavior()
— with custom transaction behavior
Was this page helpful?