You can use Embedded Replicas to work with a local SQLite file that periodically synchronizes with a remote Turso database. This allows for offline capabilities and low-latency local reads.
To set this up, use createClient with a url pointing to a local file, a syncUrl for the remote database, and an authToken. The syncInterval determines how often the local database syncs with the remote server (in milliseconds).
import { createClient } from "@libsql/client";
export const turso = createClient({
url: "file:local.db",
syncUrl: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
syncInterval: 60000,
});
// Execute a batch of operations
await turso.batch(
[
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)",
{
sql: "INSERT INTO users(name) VALUES (?)",
args: ["Iku"],
},
],
"write",
);
// Execute a single query
await turso.execute({
sql: "SELECT * FROM users WHERE id = ?",
args: [1],
});