mirror of
https://github.com/allankoechke/turso.git
synced 2026-12-04 01:45:10 +00:00
Add per-connection defaultQueryTimeout and per-query queryTimeout options to the serverless driver, matching the API from the native JS bindings. Stalled fetch() or stream reads now abort instead of hanging forever, which prevents the AsyncLock from blocking all subsequent queries on the connection. Introduces TimeoutError (subclass of DatabaseError, code "TIMEOUT") so callers can distinguish timeouts from other errors and decide whether to retry or fail gracefully.
Turso Serverless Driver for JavaScript
About
A serverless database driver for Turso Cloud, using only fetch(). Connect to your database from serverless and edge functions, such as Cloudflare Workers and Vercel.
Installation
npm install @tursodatabase/serverless
Getting Started
Basic Usage
import { connect } from "@tursodatabase/serverless";
const conn = connect({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});
// Prepare a statement
const stmt = conn.prepare("SELECT * FROM users WHERE id = ?");
// Get first row
const row = await stmt.get([123]);
console.log(row);
// Get all rows
const rows = await stmt.all([123]);
console.log(rows);
// Iterate through rows (streaming)
for await (const row of stmt.iterate([123])) {
console.log(row);
}
Batch Operations
// Execute multiple statements in a batch
await conn.batch([
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, email TEXT)",
"INSERT INTO users (email) VALUES ('user@example.com')",
"INSERT INTO users (email) VALUES ('admin@example.com')",
]);
libSQL Compatibility Layer
For existing libSQL applications, use the compatibility layer:
import { createClient } from "@tursodatabase/serverless/compat";
const client = createClient({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});
// Execute a single SQL statement
const result = await client.execute("SELECT * FROM users WHERE id = ?", [123]);
console.log(result.rows);
// Execute multiple statements in a batch
await client.batch([
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, email TEXT)",
"INSERT INTO users (email) VALUES ('user@example.com')",
"INSERT INTO users (email) VALUES ('admin@example.com')",
]);
Examples
Check out the examples/ directory for complete usage examples.
API Reference
For complete API documentation, see JavaScript API Reference.
Related Packages
- The @tursodatabase/database package provides the Turso in-memory database, compatible with SQLite.
- The @tursodatabase/sync package provides bidirectional sync between a local Turso database and Turso Cloud.
License
This project is licensed under the MIT license.