Turso is the edge database built on libSQL (a fork of SQLite). It gives you embedded database performance with global replication — and a powerful SDK.
Connect and Query
import { createClient } from "@libsql/client";
const db = createClient({
url: "libsql://my-db-username.turso.io",
authToken: process.env.TURSO_AUTH_TOKEN,
});
// Simple query
const result = await db.execute("SELECT * FROM products WHERE price < 50 ORDER BY created_at DESC LIMIT 20");
console.log(result.rows);
// Parameterized
const product = await db.execute({
sql: "SELECT * FROM products WHERE id = ?",
args: [productId],
});
Batch Operations: Multiple Queries, One Round Trip
const results = await db.batch([
{ sql: "INSERT INTO products (title, price, url) VALUES (?, ?, ?)", args: ["Widget", 29.99, "https://..."] },
{ sql: "INSERT INTO products (title, price, url) VALUES (?, ?, ?)", args: ["Gadget", 49.99, "https://..."] },
{ sql: "UPDATE stats SET total_products = total_products + 2 WHERE id = 1" },
"SELECT COUNT(*) as total FROM products",
], "write");
console.log("Total products:", results[3].rows[0].total);
Transactions
const tx = await db.transaction("write");
try {
await tx.execute({ sql: "INSERT INTO orders (user_id, total) VALUES (?, ?)", args: [userId, total] });
await tx.execute({ sql: "UPDATE inventory SET stock = stock - ? WHERE product_id = ?", args: [qty, productId] });
await tx.commit();
} catch (e) {
await tx.rollback();
throw e;
}
Embedded Replicas: Local + Remote
const db = createClient({
url: "file:local-replica.db", // Local SQLite file
syncUrl: "libsql://my-db.turso.io", // Remote primary
authToken: process.env.TURSO_AUTH_TOKEN,
syncInterval: 60, // Sync every 60 seconds
});
// Reads hit local file (microseconds)
const products = await db.execute("SELECT * FROM products");
// Writes go to remote, then sync
await db.execute({ sql: "INSERT INTO products (title) VALUES (?)", args: ["New Product"] });
// Manual sync
await db.sync();
Vector Search (Extensions)
// Store embeddings
await db.execute({
sql: "INSERT INTO documents (content, embedding) VALUES (?, vector(?))",
args: [text, JSON.stringify(embedding)],
});
// Similarity search
const similar = await db.execute({
sql: "SELECT content, vector_distance_cos(embedding, vector(?)) as distance FROM documents ORDER BY distance LIMIT 5",
args: [JSON.stringify(queryEmbedding)],
});
Drizzle ORM Integration
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
const client = createClient({ url: "libsql://...", authToken: "..." });
const db = drizzle(client);
const products = await db.select().from(productsTable).where(lt(productsTable.price, 50));
Store scraped data at the edge? My Apify tools + Turso = globally distributed scraping results.
Custom edge data solution? Email spinov001@gmail.com
Top comments (0)