Skip to content

Commit 173e847

Browse files
committed
feat: support transactions on the API
1 parent 3c4a346 commit 173e847

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,48 @@ if (user) {
302302
> catch bugs where a query unexpectedly returns multiple results. Use `fetchAll()` if you
303303
> expect multiple rows.
304304
305+
### Using Transactions
306+
307+
Transactions ensure that multiple operations either all succeed or all fail together,
308+
maintaining data consistency:
309+
310+
```typescript
311+
// Begin a transaction
312+
await db.beginTransaction();
313+
314+
try {
315+
// Execute multiple operations atomically
316+
await db.execute(
317+
'INSERT INTO users (name, email) VALUES ($1, $2)',
318+
['Alice', 'alice@example.com']
319+
);
320+
321+
await db.execute(
322+
'INSERT INTO audit_log (action, user) VALUES ($1, $2)',
323+
['user_created', 'Alice']
324+
);
325+
326+
// Commit if all operations succeed
327+
await db.commitTransaction();
328+
console.log('Transaction completed successfully');
329+
330+
} catch (error) {
331+
// Rollback if any operation fails
332+
await db.rollbackTransaction();
333+
console.error('Transaction failed, rolled back:', error);
334+
throw error;
335+
}
336+
```
337+
338+
**Important Notes:**
339+
340+
* All operations between `beginTransaction()` and
341+
`commitTransaction()`/`rollbackTransaction()` are executed as a single atomic unit
342+
* If an error occurs, call `rollbackTransaction()` to discard all changes
343+
* Nested transactions are not supported
344+
* Always ensure transactions are either committed or rolled back to avoid locking
345+
issues
346+
305347
### Closing Connections
306348

307349
```typescript

api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guest-js/index.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,78 @@ export default class Database {
211211
return result
212212
}
213213

214+
/**
215+
* **beginTransaction**
216+
*
217+
* Begins a new database transaction. All subsequent operations will be
218+
* part of this transaction until `commitTransaction()` or `rollbackTransaction()`
219+
* is called.
220+
*
221+
* Transactions provide atomicity - either all operations succeed or all are rolled back.
222+
*
223+
* @example
224+
* ```ts
225+
* await db.beginTransaction();
226+
* try {
227+
* await db.execute('INSERT INTO users (name) VALUES ($1)', ['Alice']);
228+
* await db.execute('INSERT INTO logs (action) VALUES ($1)', ['user_created']);
229+
* await db.commitTransaction();
230+
* } catch (error) {
231+
* await db.rollbackTransaction();
232+
* throw error;
233+
* }
234+
* ```
235+
*/
236+
async beginTransaction(): Promise<void> {
237+
await invoke('plugin:sqlite|begin_transaction', {
238+
db: this.path
239+
})
240+
}
241+
242+
/**
243+
* **commitTransaction**
244+
*
245+
* Commits the current transaction, making all changes permanent.
246+
*
247+
* @example
248+
* ```ts
249+
* await db.beginTransaction();
250+
* await db.execute('INSERT INTO users (name) VALUES ($1)', ['Alice']);
251+
* await db.execute('INSERT INTO logs (action) VALUES ($1)', ['user_created']);
252+
* await db.commitTransaction();
253+
* ```
254+
*/
255+
async commitTransaction(): Promise<void> {
256+
await invoke('plugin:sqlite|commit_transaction', {
257+
db: this.path
258+
})
259+
}
260+
261+
/**
262+
* **rollbackTransaction**
263+
*
264+
* Rolls back the current transaction, discarding all changes made since
265+
* `beginTransaction()` was called.
266+
*
267+
* @example
268+
* ```ts
269+
* await db.beginTransaction();
270+
* try {
271+
* await db.execute('INSERT INTO users (name) VALUES ($1)', ['Alice']);
272+
* await db.execute('INSERT INTO logs (action) VALUES ($1)', ['user_created']);
273+
* await db.commitTransaction();
274+
* } catch (error) {
275+
* await db.rollbackTransaction();
276+
* throw error;
277+
* }
278+
* ```
279+
*/
280+
async rollbackTransaction(): Promise<void> {
281+
await invoke('plugin:sqlite|rollback_transaction', {
282+
db: this.path
283+
})
284+
}
285+
214286
/**
215287
* **close**
216288
*

0 commit comments

Comments
 (0)