packages/turso-serverless/AGENT.md
This document provides guidance for LLMs working on the @tursodatabase/serverless TypeScript driver.
This is a fetch() API-compatible serverless database driver for Turso Cloud that implements the SQL over HTTP protocol (internally called "hrana"). It's designed for serverless and edge compute environments like Cloudflare Workers and Vercel Edge Functions.
fetch() APIsrc/
├── connection.ts # Connection class and connect() function
├── statement.ts # Statement class with get()/all()/iterate() methods
├── protocol.ts # Low-level SQL over HTTP protocol implementation
├── compat.ts # LibSQL API compatibility layer
├── compat/index.ts # Compatibility layer exports
└── index.ts # Main package exports
@tursodatabase/serverless - Native streaming API@tursodatabase/serverless/compat - LibSQL-compatible APIimport { connect } from "@tursodatabase/serverless";
const client = connect({ url, authToken });
const stmt = client.prepare("SELECT * FROM users WHERE id = ?", [123]);
// Three execution modes:
const row = await stmt.get(); // First row or null
const rows = await stmt.all(); // All rows as array
for await (const row of stmt.iterate()) { ... } // Streaming iterator
prepare(), execute(), batch(), executeRaw()get(), all(), iterate()iterate() provides row-by-row streaming via AsyncGeneratorexecuteCursor() returns streaming cursor entries/v3/cursor) with newline-delimited JSONProvides drop-in compatibility with the standard libSQL client API for existing applications.
createClient() instead of connect()@tursodatabase/serverless/compaturl and authToken, validates against unsupported options// ✅ Supported
const client = createClient({ url, authToken });
await client.execute(sql, args);
await client.batch(statements);
// ✅ Supported: remote encryption key for encrypted Turso Cloud databases
createClient({ url, authToken, remoteEncryptionKey: "base64-encoded-key" });
// ❌ Unsupported (throws LibsqlError)
createClient({ url, authToken, encryptionKey: "..." }); // Validation error
await client.transaction(); // Not implemented
await client.sync(); // Not supported for remote
POST /v3/cursorstep_begin, row, step_end, step_error, errorlibsql:// to https:// automaticallyintegration-tests/
├── serverless.test.mjs # Native API tests
└── compat.test.mjs # Compatibility layer tests
TURSO_DATABASE_URL, TURSO_AUTH_TOKENtest.serial() to avoid conflictsnpm test # Runs all integration tests
npm run build # TypeScript compilation
LibsqlError for compat)protocol.tsiterate() for large result setsprotocol.ts if it requires HTTP changesconnection.ts for connection-level featuresstatement.ts for statement-level featurescompat.ts if LibSQL compatibility neededexecuteRaw() to inspect cursor entriesall() vs iterate() behaviorLibsqlError vs generic errorsresources/libsql-client-ts for API patternsThis guide should help future contributors understand the architecture and maintain consistency across the codebase.