Turso
Reylo on Turso Cloud and Turso's own engine
Turso Cloud runs libSQL, a fork of SQLite, so everything on the SQLite page holds for it.
| Dialect | SQLite |
| The type check reads | A local SQLite file with your schema, never a libsql:// URL |
| Turso Cloud | @libsql/client, with .run() |
| Turso's own engine | @tursodatabase/database, with .run(), and .runSync() on its compat Database |
| Migrations | reylo migrate, on Turso Cloud through its libsql:// URL |
| SQLite versions | Reylo's 3.53, and the SQLite of each driver your package.json lists: 3.45.1 for @libsql/client |
| libSQL's own SQL | Vector columns and indexes, the vector functions, vector_top_k and alter table … alter column (Vectors) |
| Extensions | The functions of the sqlean extensions Turso Cloud loads (Extensions) |
Turso Cloud
Pass an @libsql/client client, made with your database's URL and an auth token, or a transaction taken from it:
import { } from "@libsql/client";
import from "./add_user.sql";
import from "./find_user.sql";
const = ({
: ..!, // libsql://…
: ..,
});
const = await .(, { : 1 });
// both inserts land, or neither
const = await .("write");
await .(, { : "Ada" });
await .(, { : "Grace" });
await .();select id, name, email from where id = :idinsert into (name) values (:name)About the client:
- The client answers through a promise, so use
.run(), not.runSync(). - With the default
intMode, libSQL throws aRangeErroron an integer past 2^53. SetintMode: "bigint"to get it exact. - A Drizzle connection over
@libsql/clientworks too (Drizzle).
Local database
Make a local SQLite file from a dump of your Turso database:
turso db shell app .dump > dump.sql
sqlite3 dev.db < dump.sqlOr run your migrations into a new file:
for file in migrations/*.sql; do sqlite3 dev.db < "$file"; donePoint direct_url at it:
# the path starts at package.json
direct_url: sqlite:./dev.dbWhy a local file:
- A
DATABASE_URLthat holds yourlibsql://URL fails the type check, which reads asqlite:URL for SQLite. Adirect_urlwins over it. - In development, your app can open the same file:
createClient({ url: "file:dev.db" })needs no auth token. - In CI, run your migrations into the file before the type check. The job needs no Turso token.
Migrations
Run Reylo Migrate on Turso Cloud with your database's URL, its auth token in authToken:
npx reylo migrate deploy --url "libsql://app-acme.turso.io?authToken=$TURSO_AUTH_TOKEN"To make a token, run turso db tokens create app. Keep direct_url on your local file, so the type check and reylo migrate dev stay on it. Pass --url to reach Turso Cloud:
steps:
# ...
- run: npx reylo migrate deploy --url "$TURSO_DATABASE_URL?authToken=$TURSO_AUTH_TOKEN"
env:
TURSO_DATABASE_URL: ${{ secrets.TURSO_DATABASE_URL }}
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_AUTH_TOKEN }}Migrations on Turso:
- Every
reylo migrateandreylo dbcommand runs on Turso Cloud and on any libSQL server. It takes the same URLs as@libsql/client. Add?tls=0to alibsql://URL for a server without TLS. - From code, pass the same URL:
migrate({ url })(API). - Shadow databases are in memory, on libSQL's own engine as Turso Cloud runs it. That includes Turso Cloud's extra functions, such as
uuid4andregexp_like. - Migrations can use libSQL's own SQL, such as a vector index or
alter table … alter column. Then every command opens your local file on libSQL's engine too. - Tables named
__turso_internal_…belong to Turso's engine. Reylo leaves them out ofschema.sqland every plan, and keeps them in step when a table changes. - Each migration runs in one transaction with its history row. Two deploys on one machine take turns. Two on different machines can collide, and then one fails and changes nothing.
SQLite version
The SQLite in @libsql/client is 3.45.1, older than Reylo's 3.53. With @libsql/client in your package.json, the type check also runs each query on it:
{
"dependencies": {
"@libsql/client": "^0.18.0"
}
}A query it refuses is a type error, with its own message:
queries/user_settings.sql(1,12): error reylo6: @libsql/client runs SQLite 3.45.1, which refuses this statement: no such function: json_prettyIt refuses newer functions, such as json_pretty and two-argument iif.
Vectors
Reylo types libSQL's vector SQL. Declare a vector column, index it with libsql_vector_idx, and search it with vector_top_k:
import { } from "@libsql/client";
import from "./add_doc.sql";
import from "./nearest_docs.sql";
const = ({ : "file:dev.db" });
await .(, { : "Ada", : "[0.1,0.2,0.3]" });
const = await .(, { : "[0.1,0.2,0.3]" });select .id, .title
from vector_top_k('docs_emb', vector32(:q), 5) as top
join on .id = top.idinsert into (title, emb) values (:title, vector32(:embedding))About vectors:
- A vector parameter takes the vector's text, as
'[0.1,0.2,0.3]'. The count ofvector_top_ktakes an integer. - Each row of
vector_top_kis theidof a row of the indexed table. That's its rowid, or its primary key in aWITHOUT ROWIDtable. - A distance is
number | null. It'sNULLwhere it can't be computed, as for a vector of zeros. - A vector function fails on a
NULLvector, where most SQLite functions returnNULL. Leave rows without one out withwhere emb is not null. - A vector column is typed as a
BLOB. It can't be in aSTRICTtable, since libSQL refuses vector types there. Calling.run()returns a vector as aUint8Array, andvector_extract(emb)returns its text. - A driver whose SQLite has no vectors, such as
better-sqlite3, refuses these queries in the type check. Turso's own engine can't open a schema with a vector index, so the type check skips it then. - Statements with
alter table … alter columntype-check and return nothing.
Extensions
Reylo types the functions of the sqlean extensions Turso Cloud loads: uuid, crypto, fuzzy, math, stats, text and regexp.
import { } from "@libsql/client";
import from "./similar_users.sql";
const = ({
: ..!, // libsql://…
: ..,
});
const = await .(, { : "Ada" });select id, name, levenshtein(name, :name) as distance
from
where regexp_like(name, '^[A-Z]')
order by distance
limit 5Where these functions run:
- They run on Turso Cloud and any libSQL server. The SQLite in
@libsql/clienthas none of them. A query that calls one passes the type check but fails on a localfile:database. - Another driver in your
package.json, such asbetter-sqlite3, refuses them in the type check. - Turso Cloud's
soundexreturnsNULLforNULL. Sosoundex(x)isstring | nullwhereverxmay beNULL.
Turso's own engine
Turso's own engine, @tursodatabase/database, runs in your process on a database file. Pass the connection connect() resolves to, or the transaction .transactionAsync() hands its callback:
import { } from "@tursodatabase/database";
import { } from "@tursodatabase/database/compat";
import from "./add_user.sql";
import from "./find_user.sql";
const = await ("dev.db");
const = await .(, { : 1 });
// both inserts land, or neither
await .(async () => {
await .(, { : "Ada" });
await .(, { : "Grace" });
})();
// synchronous
const = new ("dev.db");
const = .(, { : 1 });About the compat driver:
- The
Databaseof@tursodatabase/database/compatis synchronous, so.runSync()works on it, inside itsdb.transaction(fn)as well. - The type check reads the same file, through
direct_urlas above. - With
@tursodatabase/databasein yourpackage.json, the type check also runs each query on Turso's engine. It skips a schema with an FTS5 table or a vector index, which the engine can't open. - From 0.8 on, its errors carry no code. With
.try(),codeisundefinedand onlymessagenames the failure. - An integer past 2^53 comes back exact.