Loading...
Loading...
This skill should be used when the user asks to "connect to Turso", "use libSQL", "set up a Turso database", "query Turso with TypeScript", or needs guidance on Turso Cloud, embedded replicas, or vector search with libSQL.
npx skill4agent add the-perfect-developer/the-perfect-opencode turso-libsql| Term | Meaning |
|---|---|
| Database | A single libSQL database instance hosted on Turso Cloud |
| Group | A collection of databases sharing a region and auth tokens |
| Embedded Replica | A local SQLite file that syncs with a remote Turso database |
| Auth Token | JWT used to authenticate SDK connections |
| libsql:// | The native Turso protocol (WebSocket-based, best for persistent connections) |
| https:// | HTTP-based access, better for single-shot serverless queries |
# TypeScript / JavaScript
npm install @libsql/client
# Python
pip install libsql-client
# Rust (Cargo.toml)
# libsql = "0.6"
# Go
# go get github.com/tursodatabase/go-libsqlimport { createClient } from "@libsql/client";
const client = createClient({
url: process.env.TURSO_DATABASE_URL!, // libsql://[DB]-[ORG].turso.io
authToken: process.env.TURSO_AUTH_TOKEN!, // JWT from Turso CLI or Platform API
});libsql://https://file:path/to/db.dbauthToken:memory:// Simple query
const result = await client.execute("SELECT * FROM users");
// Positional placeholders (preferred for brevity)
const user = await client.execute({
sql: "SELECT * FROM users WHERE id = ?",
args: [userId],
});
// Named placeholders
const inserted = await client.execute({
sql: "INSERT INTO users (name, email) VALUES (:name, :email)",
args: { name: "Iku", email: "iku@example.com" },
});rowscolumnsrowsAffectedlastInsertRowidbigint | undefinedawait client.batch(
[
{ sql: "INSERT INTO orders (user_id) VALUES (?)", args: [userId] },
{ sql: "UPDATE inventory SET stock = stock - 1 WHERE id = ?", args: [itemId] },
],
"write",
);const tx = await client.transaction("write");
try {
const { rows } = await tx.execute({
sql: "SELECT balance FROM accounts WHERE id = ?",
args: [accountId],
});
if ((rows[0].balance as number) >= amount) {
await tx.execute({
sql: "UPDATE accounts SET balance = balance - ? WHERE id = ?",
args: [amount, accountId],
});
await tx.commit();
} else {
await tx.rollback();
}
} catch (e) {
await tx.rollback();
throw e;
}| Mode | SQLite Command | Use When |
|---|---|---|
| | Mix of reads and writes |
| | Read-only; can parallelize on replicas |
| | Unknown upfront; may fail if a write is in flight |
// .env.local
TURSO_DATABASE_URL=file:local.db
// No TURSO_AUTH_TOKEN needed for local files
// .env.production
TURSO_DATABASE_URL=libsql://my-db-myorg.turso.io
TURSO_AUTH_TOKEN=eyJ...turso dev --db-file local.db
# Connects at http://127.0.0.1:8080const client = createClient({
url: "file:replica.db", // local file path
syncUrl: process.env.TURSO_DATABASE_URL!,
authToken: process.env.TURSO_AUTH_TOKEN!,
syncInterval: 60, // auto-sync every 60 seconds (optional)
});
// Manually trigger sync
await client.sync();https://references/embedded-replicas.mdF32_BLOB-- Schema
CREATE TABLE documents (
id INTEGER PRIMARY KEY,
text TEXT,
embedding F32_BLOB(1536) -- match your embedding model's dimensions
);
-- Create vector index (DiskANN-based ANN search)
CREATE INDEX documents_idx ON documents (libsql_vector_idx(embedding));
-- Insert with embedding
INSERT INTO documents (text, embedding)
VALUES ('Hello world', vector32('[0.1, 0.2, ...]'));
-- Query top-K nearest neighbors
SELECT d.id, d.text
FROM vector_top_k('documents_idx', vector32('[0.1, 0.2, ...]'), 5)
JOIN documents d ON d.rowid = id;references/vector-search.mdturso db tokens create <db-name>turso group tokens create <group-name>turso db tokens invalidate <db-name>references/connection-and-auth.mdturso auth login # authenticate
turso db create my-db # create database
turso db show my-db # show URL and metadata
turso db tokens create my-db # create auth token
turso db shell my-db # interactive SQL shell
turso db inspect my-db # storage stats and top queries
turso dev --db-file local.db # local libSQL serverreferences/connection-and-auth.mdreferences/vector-search.mdreferences/embedded-replicas.md