Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
autumn-plugin-turso
A Turso database for Autumn 0.10. Turso is an embedded, SQLite-compatible engine. It needs no server. Turso Cloud sync is optional.
Install
[]
= "0.3"
# For Turso Cloud sync:
# autumn-plugin-turso = { version = "0.3", features = ["sync"] }
Quick start
use ;
use *;
const NOTES: Migration = new;
async
async
At startup the plugin:
- Reads
[turso]. - Opens the database. It makes missing parent directories.
- Applies pending migrations.
- Puts a
Tursohandle inAppState.
Get the handle in a handler with db: Turso, or with
state.extension::<Turso>(). If the plugin is not installed or is disabled,
the extractor returns 500.
An app has one Turso database. A second TursoPlugin is skipped with a
warning.
Example
examples/notes is a JSON notes API. It shows
migrations, the Turso extractor, bound parameters and HTTP status codes.
- The app stores the note text as sent.
- An empty note, or a note longer than 1000 Unicode code points, gives
422. A missingbodyfield also gives422. Bad JSON or a bad id gives400. - Run the example from the repository root. Git ignores
/data. - The example has no authentication. Do not deploy it as is. In the
devprofile, Autumn allows CORS from all origins. tests/example.rsruns the same code.
Handle
| Call | Result |
|---|---|
connect() |
A new turso::Connection. Use one per task. |
execute(sql, params) |
The number of changed rows. |
execute_batch(sql) |
Runs one or more statements with no parameters. |
query(sql, params) |
All rows as Vec<turso::Row>. |
ping() |
Runs SELECT 1. |
migrate(&[Migration]) |
The number of applied migrations. |
pending(&[Migration]) |
The ids that are not applied. |
push(), pull() |
Sync with Turso Cloud (feature sync). |
close() |
Closes the handle and all its clones. |
All calls return Result<_, TursoError>. After close, calls return
TursoError::Closed. Close does not stop work that started before it. A
connection from connect() stays open until you drop it. The crate re-exports turso, so you can use
autumn_plugin_turso::turso::Value.
Configuration
[]
= true # Open the database at startup.
= "data/turso.db" # A file path. ":memory:" gives an in-memory database.
= false
= 5000 # Lock wait time.
= true # When false, the plugin only logs pending ids.
[] # Needs the `sync` feature.
= true # Set false to turn off sync from a higher layer.
= "libsql://my-db.turso.io"
= "..." # Prefer AUTUMN_TURSO__SYNC__AUTH_TOKEN.
= true
= true
= 0 # Push and pull every N seconds. 0 disables.
Layers (lowest first):
- Defaults.
- Builder setters, for example
TursoPlugin::new().path("app.db"). [turso]inautumn.toml.[profile.<p>.turso]inautumn.toml.[turso]inautumn-<p>.toml.- Environment variables:
AUTUMN_TURSO__PATH,AUTUMN_TURSO__SYNC__AUTH_TOKEN, and so on. Use__between levels. The plugin also reads them from.envfiles, with the same rules as Autumn core. A real environment variable wins.
The plugin finds the profile as Autumn core does: AUTUMN_ENV, then
AUTUMN_PROFILE, then --profile.
An unknown key, an unknown AUTUMN_TURSO__* variable or a bad value stops
startup. The message names the key. A file: URI as path also stops
startup.
These sync settings also stop startup:
read_only = truetogether with[turso.sync].- An empty
auth_token, or one with control characters. The plugin removes white space at the ends of the token. - An
auth_tokenwith anhttp://URL to a host that is not loopback. Usehttps://orlibsql://. This keeps the token encrypted on the network.
Migrations
use Migration;
const MIGRATIONS: = ;
- Migrations run in the order you add them.
- Each migration runs in one transaction. The plugin rolls back a failed migration. Earlier migrations stay applied.
- Do not use
BEGIN,COMMITorROLLBACKin a migration. A migration that ends the transaction fails. - Two processes can migrate one file at the same time. Each migration runs once.
- The checksum reads
\r\nas\n. A checkout with other line endings does not change it. - The table
_autumn_turso_migrationsrecords each id and checksum. - Do not edit an applied migration. A changed checksum stops startup. Add a new migration.
- A read-only database with pending migrations stops startup.
Health and metrics
/actuator/healthhas atursocomponent. It isUPwhenSELECT 1works. It isDOWNwhen the handle is closed or the query fails. Details showpath,modeandread_only. They never show the sync URL or token. Autumn shows details only whenhealth.detailedis on (thedevprofile default)./actuator/prometheushasturso_upand these counters:turso_statements_total,turso_errors_total,turso_connections_opened_total,turso_migrations_applied_total,turso_sync_pushes_total,turso_sync_pulls_total,turso_sync_errors_total. The counters do not count statements on a connection fromconnect(), or the healthping().
Shutdown
The plugin stops the sync loop, pushes (when push_on_shutdown is set) and
closes the handle.
The push is best-effort. It has a 10-second limit. Autumn also gives each
shutdown hook a time limit (server.shutdown_timeout_secs, 1 second in the
dev profile). When the push does not complete, the data stays in the local
file. The next push or start sends it.
Features
| Feature | Use |
|---|---|
sync |
Turso Cloud sync. |
fts |
Full-text search in the engine. |
The crate disables all Turso default features (mimalloc and fts). Thus it
does not set a global allocator. Enable the fts feature of this crate to get
full-text search.
Use the re-exported autumn_plugin_turso::turso. If you also add turso to
your own Cargo.toml, set default-features = false. If you do not, Cargo
enables mimalloc and Turso sets the global allocator.
Limits
- Use one process per database file. The plugin does not turn on the experimental Turso multi-process mode.
- The Turso engine is new. Read the Turso compatibility notes before you use it in production.
- This plugin does not give a Diesel pool, a session store or a cache.
- The public API uses
tursotypes. A newtursominor version (0.x) is a breaking change of this crate. - The plugin makes the database directory with the process umask. On a shared host, set a strict umask or make the directory before startup.
Tests
# Sync tests against a local server:
TURSODB_BIN=
Install tursodb with
cargo install --git https://github.com/tursodatabase/turso --rev 607bff9fd0e547a25a97b3bf06e83bcc5d99d53c turso_cli --locked
(the v0.8.3 tag).
License
Apache-2.0