Durable Object (sql backend) transaction.put not saving

i'm trying to understand how to use transactions to perform batch operations, as running standard sql transactions on the storage sql property throws an error saying to use the javascript api, but i cannot find any documentation for the javascript api or how to use it
✘ [ERROR] queriesAddBatch error: Error: To execute a transaction, please use the state.storage.transaction() or state.storage.transactionSync() APIs instead of the SQL BEGIN TRANSACTION or SAVEPOINT statements. The JavaScript API is safer because it will automatically roll back on exceptions, and because it interacts correctly with Durable Objects' automatic atomic write coalescing.
✘ [ERROR] queriesAddBatch error: Error: To execute a transaction, please use the state.storage.transaction() or state.storage.transactionSync() APIs instead of the SQL BEGIN TRANSACTION or SAVEPOINT statements. The JavaScript API is safer because it will automatically roll back on exceptions, and because it interacts correctly with Durable Objects' automatic atomic write coalescing.
this is the code in question, it does not throw any error and txn.put only returns a Promise<void>
async queriesAddBatch(qArr: QueriesRow[]): Promise<Error | null> {
console.log('received queries to save: ', qArr);

try {
await this.storage.transaction(async (txn) => {
for (const q of qArr) {
await txn.put({
query: q.query,
items: q.items,
});
}
});

return null;
} catch (e) {
console.error('queriesAddBatch error:', e);
return new Error('Error saving queries');
}
}
async queriesAddBatch(qArr: QueriesRow[]): Promise<Error | null> {
console.log('received queries to save: ', qArr);

try {
await this.storage.transaction(async (txn) => {
for (const q of qArr) {
await txn.put({
query: q.query,
items: q.items,
});
}
});

return null;
} catch (e) {
console.error('queriesAddBatch error:', e);
return new Error('Error saving queries');
}
}
i successfully receive the data being sent to save (console.log confirms) but nothing gets actually saved
1 Reply
Patrick M
Patrick M6mo ago
I think the documentation you are looking for is here: https://developers.cloudflare.com/durable-objects/api/storage-api/
Cloudflare Docs
Durable Object Storage
The Durable Object Storage API allows Durable Objects to access transactional and strongly consistent storage. A Durable Object's attached storage is private to its unique instance and cannot be accessed by other objects.

Did you find this page helpful?