mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
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>
318 lines
11 KiB
JavaScript
318 lines
11 KiB
JavaScript
import test from 'ava';
|
|
import { connect, Transaction } from '../dist/index.js';
|
|
|
|
const client = connect({
|
|
url: process.env.TURSO_DATABASE_URL,
|
|
authToken: process.env.TURSO_AUTH_TOKEN,
|
|
});
|
|
|
|
test.serial('run() method creates table and inserts data', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS test_users');
|
|
|
|
await client.exec('CREATE TABLE test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
|
|
|
|
const insertResult = await client.run(
|
|
'INSERT INTO test_users (name, email) VALUES (?, ?)',
|
|
['John Doe', 'john@example.com']
|
|
);
|
|
|
|
t.is(insertResult.changes, 1);
|
|
t.is(typeof insertResult.lastInsertRowid, 'number');
|
|
});
|
|
|
|
test.serial('all() method queries data correctly', async t => {
|
|
const stmt = await client.prepare('SELECT * FROM test_users WHERE name = ?');
|
|
const columns = stmt.columns().map(col => col.name);
|
|
|
|
t.is(columns.length, 3);
|
|
t.true(columns.includes('id'));
|
|
t.true(columns.includes('name'));
|
|
t.true(columns.includes('email'));
|
|
|
|
const rows = await client.all('SELECT * FROM test_users WHERE name = ?', ['John Doe']);
|
|
t.is(rows.length, 1);
|
|
t.is(rows[0].name, 'John Doe');
|
|
t.is(rows[0].email, 'john@example.com');
|
|
});
|
|
|
|
test.serial('prepare() method creates statement', async t => {
|
|
const stmt = await client.prepare('SELECT * FROM test_users WHERE name = ?');
|
|
|
|
const row = await stmt.get(['John Doe']);
|
|
t.is(row.name, 'John Doe');
|
|
t.is(row.email, 'john@example.com');
|
|
|
|
const rows = await stmt.all(['John Doe']);
|
|
t.is(rows.length, 1);
|
|
t.is(rows[0].name, 'John Doe');
|
|
});
|
|
|
|
test.serial('Statement.run()', async t => {
|
|
const stmt = await client.prepare('INSERT INTO test_users (name, email) VALUES (?, ?)');
|
|
const row = await stmt.run(['Jane Doe', 'jane@example.com']);
|
|
t.is(row.lastInsertRowid, 2);
|
|
});
|
|
|
|
test.serial('statement iterate() method works', async t => {
|
|
// Ensure test data exists
|
|
await client.exec('CREATE TABLE IF NOT EXISTS test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
|
|
await client.run('INSERT OR IGNORE INTO test_users (name, email) VALUES (?, ?)', ['John Doe', 'john@example.com']);
|
|
|
|
const stmt = await client.prepare('SELECT * FROM test_users');
|
|
|
|
const rows = [];
|
|
for await (const row of stmt.iterate()) {
|
|
rows.push(row);
|
|
}
|
|
|
|
t.true(rows.length >= 1);
|
|
t.is(rows[0].name, 'John Doe');
|
|
});
|
|
|
|
test.serial('batch() method executes multiple statements', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS test_products');
|
|
|
|
const batchResult = await client.batch([
|
|
'CREATE TABLE test_products (id INTEGER PRIMARY KEY, name TEXT, price REAL)',
|
|
'INSERT INTO test_products (name, price) VALUES ("Widget", 9.99)',
|
|
'INSERT INTO test_products (name, price) VALUES ("Gadget", 19.99)',
|
|
'INSERT INTO test_products (name, price) VALUES ("Tool", 29.99)'
|
|
]);
|
|
|
|
// batch() returns one ResultSet per statement, in order.
|
|
t.is(batchResult.length, 4);
|
|
const insertedRows = batchResult.slice(1).reduce((sum, rs) => sum + rs.rowsAffected, 0);
|
|
t.is(insertedRows, 3);
|
|
|
|
const countRow = await client.get('SELECT COUNT(*) as count FROM test_products');
|
|
t.is(countRow.count, 3);
|
|
});
|
|
|
|
test.serial('batch() returns per-statement details and statistics', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS test_batch_details');
|
|
|
|
const results = await client.batch([
|
|
'CREATE TABLE test_batch_details (id INTEGER PRIMARY KEY, name TEXT)',
|
|
{ sql: 'INSERT INTO test_batch_details (name) VALUES (?)', args: ['Alice'] },
|
|
{ sql: 'INSERT INTO test_batch_details (name) VALUES (?)', args: ['Bob'] },
|
|
'SELECT name FROM test_batch_details ORDER BY id',
|
|
]);
|
|
|
|
t.is(results.length, 4);
|
|
t.is(results[1].rowsAffected, 1);
|
|
t.is(results[1].lastInsertRowid, 1);
|
|
t.is(results[2].lastInsertRowid, 2);
|
|
t.deepEqual(results[3].rows.map(row => row.name), ['Alice', 'Bob']);
|
|
// Server-side execution statistics are reported per statement.
|
|
for (const result of results) {
|
|
t.is(typeof result.rowsRead, 'number');
|
|
t.is(typeof result.rowsWritten, 'number');
|
|
t.is(typeof result.queryDurationMs, 'number');
|
|
}
|
|
t.true(results[1].rowsWritten >= 1);
|
|
});
|
|
|
|
test.serial('batch() failure reports the failing statement and completed results', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS test_batch_fail');
|
|
await client.exec('CREATE TABLE test_batch_fail (x)');
|
|
|
|
const error = await t.throwsAsync(() =>
|
|
client.batch([
|
|
{ sql: 'INSERT INTO test_batch_fail VALUES (?)', args: [1] },
|
|
{ sql: 'INSERT INTO test_batch_missing VALUES (?)', args: [2] },
|
|
{ sql: 'INSERT INTO test_batch_fail VALUES (?)', args: [3] },
|
|
])
|
|
);
|
|
t.is(error.batchIndex, 1);
|
|
// One entry per statement: the completed first statement's ResultSet,
|
|
// null for the failing statement and the skipped statement after it.
|
|
t.is(error.batchResults.length, 3);
|
|
t.is(error.batchResults[0].rowsAffected, 1);
|
|
t.is(error.batchResults[1], null);
|
|
t.is(error.batchResults[2], null);
|
|
|
|
// Execution stopped at the failure: the first statement committed, the
|
|
// third never ran.
|
|
const countRow = await client.get('SELECT COUNT(*) as count FROM test_batch_fail');
|
|
t.is(countRow.count, 1);
|
|
});
|
|
|
|
test.serial('atomic batch() failure rolls back and reports the failing statement', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS test_batch_atomic');
|
|
await client.exec('CREATE TABLE test_batch_atomic (x)');
|
|
|
|
const error = await t.throwsAsync(() =>
|
|
client.batch(
|
|
[
|
|
{ sql: 'INSERT INTO test_batch_atomic VALUES (?)', args: [1] },
|
|
{ sql: 'INSERT INTO test_batch_missing VALUES (?)', args: [2] },
|
|
],
|
|
'immediate'
|
|
)
|
|
);
|
|
t.is(error.batchIndex, 1);
|
|
t.is(error.batchResults.length, 2);
|
|
|
|
const countRow = await client.get('SELECT COUNT(*) as count FROM test_batch_atomic');
|
|
t.is(countRow.count, 0);
|
|
});
|
|
|
|
test.serial('get() method queries a single value', async t => {
|
|
const row = await client.get('SELECT 42 AS answer');
|
|
|
|
t.is(row.answer, 42);
|
|
});
|
|
|
|
test.serial('get() method queries a single row', async t => {
|
|
const stmt = await client.prepare("SELECT 1 AS one, 'two' AS two, 0.5 AS three");
|
|
t.deepEqual(stmt.columns().map(col => col.name), ["one", "two", "three"]);
|
|
|
|
const rows = await client.all("SELECT 1 AS one, 'two' AS two, 0.5 AS three");
|
|
t.is(rows.length, 1);
|
|
|
|
const r = rows[0];
|
|
t.deepEqual(Object.entries(r), [
|
|
["one", 1],
|
|
["two", "two"],
|
|
["three", 0.5],
|
|
]);
|
|
|
|
});
|
|
|
|
test.serial('error handling works correctly', async t => {
|
|
const error = await t.throwsAsync(
|
|
() => client.all('SELECT * FROM nonexistent_table')
|
|
);
|
|
t.regex(error.message, /SQLite error.*no such table|no such table|HTTP error/);
|
|
});
|
|
|
|
test.serial('transaction.concurrent uses BEGIN CONCURRENT', async t => {
|
|
const localClient = connect({ url: 'http://localhost:0' });
|
|
const calls = [];
|
|
|
|
localClient.exec = async sql => {
|
|
calls.push(sql);
|
|
};
|
|
localClient.session.close = async () => {};
|
|
|
|
try {
|
|
const txn = localClient.transaction(async () => {
|
|
calls.push('body');
|
|
}).concurrent;
|
|
await txn();
|
|
t.deepEqual(calls, ['BEGIN CONCURRENT', 'body', 'COMMIT']);
|
|
} finally {
|
|
await localClient.close();
|
|
}
|
|
});
|
|
|
|
test.serial('transactionAsync.concurrent uses BEGIN CONCURRENT', async t => {
|
|
const localClient = connect({ url: 'http://localhost:0' });
|
|
const calls = [];
|
|
|
|
const originalExec = Transaction.prototype.exec;
|
|
Transaction.prototype.exec = async sql => {
|
|
calls.push(sql);
|
|
};
|
|
localClient.session.close = async () => {};
|
|
|
|
try {
|
|
const txn = localClient.transactionAsync(async (_tx) => {
|
|
calls.push('body');
|
|
}).concurrent;
|
|
await txn();
|
|
t.deepEqual(calls, ['BEGIN CONCURRENT', 'body', 'COMMIT']);
|
|
} finally {
|
|
Transaction.prototype.exec = originalExec;
|
|
await localClient.close();
|
|
}
|
|
});
|
|
|
|
const withTimeout = (promise, timeoutMs, label) => {
|
|
let timer;
|
|
const timeout = new Promise((_, reject) => {
|
|
timer = setTimeout(() => {
|
|
reject(new Error(`${label} timed out after ${timeoutMs}ms`));
|
|
}, timeoutMs);
|
|
});
|
|
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
};
|
|
|
|
// Each transactionAsync runs on its own dedicated stream, so all callbacks
|
|
// can be open at the same time. The barrier resolves only once every
|
|
// callback has been entered — if transactions were serialized on the
|
|
// connection (the pre-dedicated-session behavior), the first callback
|
|
// would wait on the barrier forever and the test would time out.
|
|
test.serial('transactionAsync transactions run in parallel', async t => {
|
|
const N = 3;
|
|
let entered = 0;
|
|
let releaseBarrier;
|
|
const allEntered = new Promise(resolve => { releaseBarrier = resolve; });
|
|
|
|
const txn = client.transactionAsync(async (tx, i) => {
|
|
if (++entered === N) releaseBarrier();
|
|
await withTimeout(allEntered, 5000, 'all transaction callbacks open at once');
|
|
const row = await tx.get('SELECT ? AS i', [i]);
|
|
return row.i;
|
|
});
|
|
|
|
const results = await Promise.all(Array.from({ length: N }, (_, i) => txn(i)));
|
|
t.deepEqual(results.sort(), [0, 1, 2]);
|
|
});
|
|
|
|
// Statements on the connection are not blocked by an open transactionAsync
|
|
// window; they run on the connection's own stream, outside the transaction,
|
|
// so they must not observe its uncommitted writes. Under a connection-level
|
|
// lock this await would deadlock the transaction.
|
|
test.serial('connection statements proceed while transactionAsync is open', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS txn_parallel');
|
|
await client.exec('CREATE TABLE txn_parallel (id INTEGER PRIMARY KEY, v TEXT)');
|
|
|
|
let uncommittedCount;
|
|
await withTimeout(client.transactionAsync(async tx => {
|
|
await tx.run('INSERT INTO txn_parallel (v) VALUES (?)', ['inside']);
|
|
const rows = await client.all('SELECT COUNT(*) AS n FROM txn_parallel');
|
|
uncommittedCount = rows[0].n;
|
|
})(), 5000, 'connection statement inside transactionAsync callback');
|
|
|
|
t.is(uncommittedCount, 0);
|
|
const rows = await client.all('SELECT COUNT(*) AS n FROM txn_parallel');
|
|
t.is(rows[0].n, 1);
|
|
});
|
|
|
|
// Two transactions with overlapping open windows commit and roll back
|
|
// independently. The write sections are kept disjoint on purpose — the
|
|
// overlap under test is the callback windows, not the server's write lock.
|
|
test.serial('rollback of a parallel transactionAsync leaves the other intact', async t => {
|
|
await client.exec('DROP TABLE IF EXISTS txn_writers');
|
|
await client.exec('CREATE TABLE txn_writers (v TEXT)');
|
|
|
|
let entered = 0;
|
|
let releaseBarrier;
|
|
const bothOpen = new Promise(resolve => { releaseBarrier = resolve; });
|
|
let releaseOkDone;
|
|
const okDone = new Promise(resolve => { releaseOkDone = resolve; });
|
|
|
|
const ok = client.transactionAsync(async tx => {
|
|
if (++entered === 2) releaseBarrier();
|
|
await withTimeout(bothOpen, 5000, 'both transaction callbacks open');
|
|
await tx.run("INSERT INTO txn_writers (v) VALUES ('kept')");
|
|
});
|
|
const failing = client.transactionAsync(async tx => {
|
|
if (++entered === 2) releaseBarrier();
|
|
await withTimeout(bothOpen, 5000, 'both transaction callbacks open');
|
|
await withTimeout(okDone, 5000, 'parallel transaction commits first');
|
|
await tx.run("INSERT INTO txn_writers (v) VALUES ('discarded')");
|
|
throw new Error('boom');
|
|
});
|
|
|
|
const [, err] = await Promise.all([
|
|
ok().then(releaseOkDone),
|
|
failing().then(() => null, e => e),
|
|
]);
|
|
t.is(err.message, 'boom');
|
|
|
|
const rows = await client.all('SELECT v FROM txn_writers ORDER BY v');
|
|
t.deepEqual(rows.map(r => r.v), ['kept']);
|
|
});
|