Files
turso/serverless/javascript
Pekka Enberg 45e80cfe26 Return per-statement results from batch()
Make the JavaScript batch() return an array of ResultSet, one per input
statement, matching the libSQL client contract. Each ResultSet carries
that statement's columns, columnTypes, rows (with both positional and
named access), rowsAffected, and a BigInt lastInsertRowid.

Previously the native Database.batch() returned an aggregate
{ rowsAffected, lastInsertRowid } and discarded rows, while the
libSQL-compat client.batch() ignored statement args and collapsed all
statements into a single result. This reworks every layer:

- Native Database.batch() reads each statement's columns, collects rows
  in raw mode, and builds one ResultSet per statement.
- session.batch() requests rows (want_rows) and returns one raw result
  per statement, preserving the atomic Hrana condition chain.
- Connection.batch() shapes each result into a libSQL ResultSet.
- compat client.batch() binds args and runs the batch atomically.

Add conformance tests for both the native Database.batch() and the
libSQL-compat client.batch().
2026-06-18 09:09:38 +03:00
..
2026-06-02 18:57:48 +03:00
2026-06-12 11:23:02 +03:00

Turso Serverless Driver for JavaScript

npm

Chat with other users of Turso on Discord


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')",
]);

// Parameterized batch statements also work
await conn.transaction(async () => {
  await conn.batch([
    { sql: "INSERT INTO users (email) VALUES (?)", args: ["alice@example.com"] },
    { sql: "INSERT INTO users (email) VALUES (?)", args: ["bob@example.com"] },
  ]);
}).concurrent();

Custom Headers

Requests can carry extra HTTP headers, e.g. for routing through a gateway:

const conn = connect({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
  // Extra headers attached to every request. Applied after the standard
  // headers, so they can override e.g. `Authorization`. Setting the `Host`
  // key throws — fetch forbids it.
  requestHeaders: { "x-custom-header": "value" },
});

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.

License

This project is licensed under the MIT license.

Support