Parameterized batches could produce different effects across embedded and
serverless drivers when a later bind value was invalid. Embedded Rust also
allowed cloned connections to interleave work with a batch, and managed
batches allowed transaction-control statements to escape their wrapper.
Rollback failures were hidden, while serverless Rust could retain a stale
last-insert rowid when COMMIT failed after a successful insert.
Validate every batch input before execution, reject transaction-control SQL
before opening a managed transaction, and hold an operation-wide gate for
embedded Rust batches. Preserve both the primary and rollback errors, and
carry successful statement rowids through serverless batch decoding even
when a synthetic transaction step fails. Reject malformed serverless batch
responses instead of treating incomplete transaction steps as success.
The protocol reports complete per-statement information for a batch —
results, last insert rowid, error codes, and the execution statistics
rows_read, rows_written, and query_duration_ms — but batch() dropped
most of it. Extend the batch API compatibly, in the serverless driver
and in the embedded driver (@tursodatabase/database, whose
DatabasePromise is shared by the wasm and sync drivers), per the driver
API parity requirement:
- Each ResultSet also carries lastInsertRowid, plus rowsRead,
rowsWritten, and queryDurationMs on the serverless driver (the
embedded engine does not report those per statement). The
lastInsertRowid key is present only on entries whose statement
inserted, so `"lastInsertRowid" in resultSet` detects an insert.
- The serverless batch() uses a pipeline batch request instead of the
cursor endpoint, because the cursor stream does not carry the
statistics, and gates each statement on its predecessor succeeding,
so execution stops at the first failure instead of running the
remaining statements against a failed batch — the embedded driver's
sequential semantics.
- The thrown error carries batchIndex (the zero-based index of the
failing statement) and batchResults (one entry per statement: the
completed statement's ResultSet, or null for the failing and skipped
ones; empty when a bind value fails to encode client-side before
anything is sent), so callers can tell exactly which statements took
effect.
- The libsql-compat batch() returns the full libSQL ResultSet shape —
lastInsertRowid and toJSON() included, matching @libsql/client, whose
batch() returns the same ResultSet as execute() — and both of its
batch entry points preserve batchIndex and batchResults on the
LibsqlError they throw.
The JavaScript conformance contract covers the per-statement detail
fields. Also update the serverless integration tests that still
asserted positional access on expanded rows, removed in f52724d45.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The driver caches the server's get_autocommit answer to back the
synchronous Connection.inTransaction property, but only describe and
sequence refreshed it. execute() and batch() went through the cursor
endpoint, which cannot carry a get_autocommit probe, so the cached
flag went stale. After a raw BEGIN IMMEDIATE batch hit a constraint
error, the server-side write transaction stayed open (statement-level
ABORT) while inTransaction still read false, so cleanup code skipped
ROLLBACK and the leaked transaction starved every other writer until
timeout.
Learn the transaction state in-band instead: every cursor batch gets a
trailing SELECT 1 step gated on the is_autocommit condition. The
server executes the step only when the connection is in autocommit,
so its presence in the entry stream is the authoritative answer, with
no extra round trip and no change to the streaming transport. A shared
wrapper filters the probe's entries out of the stream and falls back
to a standalone get_autocommit pipeline request when the stream ends
abnormally (fatal error entry or the consumer stops iterating early),
where the probe answer is unreliable.
Non-atomic batch() now defers its first step error until the stream is
drained instead of throwing immediately, so the probe is still
observed; the server ran the remaining unconditioned steps either way,
so the observable semantics are unchanged.
With the flag accurate, an atomic batch inside a manually-opened
transaction now joins it (mode ignored) instead of failing on a nested
BEGIN, matching batch() inside transaction(); the conformance test
pinning the old failure mode now pins the join contract.
Tests: testing/conformance/javascript async.test.js and compat.test.js
(PROVIDER=turso, PROVIDER=serverless against sqld, including the
queryTimeout cases), serverless/javascript integration-tests, and a
direct check that a 20MB row-producing batch streams successfully.
The batch() method executes an array of SQL statements over a single
connection and returns aggregated `rowsAffected` and `lastInsertRowid`. Each
statement is either a SQL string or a `{ sql, args }` object with positional
or named bind parameters; named-args objects are forwarded to the underlying
engine as named parameters rather than collapsed via Object.values().
By default the batch is non-transactional: each statement runs in its own
autocommit step. Passing a `mode` -- `deferred`, `immediate`, `exclusive`, or
`concurrent` -- makes the batch atomic. You can also use `batch()` with the
transaction API to make it transactional.
The serverless driver implements atomic batch as a a single SQL over HTTP
request. The wire payload chains BEGIN, the user statements, COMMIT, and
ROLLBACK as a single batch using the server-side condition operators.
Connection.prepare() created a throwaway Session for the describe call,
which had no baton and therefore ran on a separate server-side connection.
This meant it could not see uncommitted DDL (e.g. CREATE TABLE) from the
current transaction.
Use this.session instead and serialize through execLock, matching how
exec() and execute() already work.
Fixes#6562
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
decodeValue() returned null for blobs without a base64 field (e.g.
{"type":"blob"} with no base64 key). Now returns Buffer.alloc(0),
matching the turso NAPI driver behavior.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous tests were in async.test.js which runs against the
database binding (PROVIDER=turso), not the serverless driver. They
tested the wrong code path. Replace with direct unit tests of
encodeValue, decodeValue, processCursorEntries, and baton reset
in the serverless package.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>