Skip to content

Commit 2afab22

Browse files
add docs
1 parent 3a92200 commit 2afab22

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ console.log(rows)
145145
* <a href="#execute-async-query">Execute Async Query</a>
146146
* <a href="#check-async-query-status">Check Async Query Status</a>
147147
* <a href="#cancel-async-query">Cancel Async Query</a>
148+
* <a href="#transaction-management">Transaction management</a>
149+
* <a href="#transaction-methods">Transaction methods</a>
150+
* <a href="#basic-transaction-usage">Basic transaction usage</a>
151+
* <a href="#transaction-error-handling">Error handling</a>
152+
* <a href="#transaction-isolation">Transaction isolation</a>
148153
* <a href="#engine-management">Engine management</a>
149154
* <a href="#getbyname">getByName</a>
150155
* <a href="#engine">Engine</a>
@@ -472,6 +477,140 @@ const token = statement.asyncQueryToken; // can only be fetched for async query
472477
await connection.cancelAsyncQuery(token);
473478
```
474479

480+
<a id="transaction-management"></a>
481+
## Transaction management
482+
483+
Firebolt's Node.js SDK supports database transactions, allowing you to group multiple operations into atomic units of work. Transactions ensure data consistency and provide the ability to rollback changes if needed.
484+
485+
<a id="transaction-methods"></a>
486+
### Transaction methods
487+
488+
The SDK provides three main methods for transaction management:
489+
490+
```typescript
491+
await connection.begin(); // Start a new transaction
492+
await connection.commit(); // Commit the current transaction
493+
await connection.rollback(); // Rollback the current transaction
494+
```
495+
496+
<a id="basic-transaction-usage"></a>
497+
### Basic transaction usage
498+
499+
The following example demonstrates a basic transaction that inserts data and commits the changes:
500+
501+
```typescript
502+
import { Firebolt } from 'firebolt-sdk'
503+
504+
const firebolt = Firebolt();
505+
const connection = await firebolt.connect({
506+
auth: {
507+
client_id: process.env.FIREBOLT_CLIENT_ID,
508+
client_secret: process.env.FIREBOLT_CLIENT_SECRET,
509+
},
510+
account: process.env.FIREBOLT_ACCOUNT,
511+
database: process.env.FIREBOLT_DATABASE,
512+
engineName: process.env.FIREBOLT_ENGINE_NAME
513+
});
514+
515+
// Start a transaction
516+
await connection.begin();
517+
518+
try {
519+
// Perform multiple operations
520+
await connection.execute(`
521+
INSERT INTO users (id, name, age) VALUES (1, 'Alice', 30)
522+
`);
523+
524+
await connection.execute(`
525+
INSERT INTO users (id, name, age) VALUES (2, 'Bob', 25)
526+
`);
527+
528+
// Commit the transaction
529+
await connection.commit();
530+
console.log('Transaction committed successfully');
531+
} catch (error) {
532+
// Rollback on error
533+
await connection.rollback();
534+
console.error('Transaction rolled back due to error:', error);
535+
}
536+
```
537+
538+
#### Transaction with prepared statements
539+
540+
Transactions work seamlessly with prepared statements:
541+
542+
```typescript
543+
await connection.begin();
544+
545+
try {
546+
// Use prepared statements within transactions
547+
await connection.execute(
548+
'INSERT INTO users (id, name, age) VALUES (?, ?, ?)',
549+
{ parameters: [4, 'Diana', 28] }
550+
);
551+
552+
await connection.execute(
553+
'UPDATE users SET age = ? WHERE id = ?',
554+
{ parameters: [29, 4] }
555+
);
556+
557+
await connection.commit();
558+
} catch (error) {
559+
await connection.rollback();
560+
throw error;
561+
}
562+
```
563+
564+
<a id="transaction-error-handling"></a>
565+
### Error handling
566+
567+
#### Transaction state errors
568+
569+
The SDK will throw errors for invalid transaction operations:
570+
571+
```typescript
572+
try {
573+
// This will throw an error if no transaction is active
574+
await connection.commit();
575+
} catch (error) {
576+
console.error('Cannot commit: no transaction in progress');
577+
}
578+
579+
try {
580+
await connection.begin();
581+
// This will throw an error if a transaction is already active
582+
await connection.begin();
583+
} catch (error) {
584+
console.error('Cannot begin: transaction already in progress');
585+
}
586+
```
587+
588+
<a id="transaction-isolation"></a>
589+
### Transaction isolation
590+
591+
Transactions in Firebolt provide isolation between concurrent operations. Changes made within a transaction are not visible to other connections until the transaction is committed:
592+
593+
```typescript
594+
// Connection 1 - Start transaction and insert data
595+
const connection1 = await firebolt.connect(connectionOptions);
596+
await connection1.begin();
597+
await connection1.execute("INSERT INTO users (id, name) VALUES (5, 'Eve')");
598+
599+
// Connection 2 - Cannot see uncommitted data
600+
const connection2 = await firebolt.connect(connectionOptions);
601+
const statement = await connection2.execute('SELECT COUNT(*) FROM users WHERE id = 5');
602+
const { data } = await statement.fetchResult();
603+
console.log('Count from connection 2:', data[0][0]); // Should show 0
604+
605+
// Connection 1 - Commit transaction
606+
await connection1.commit();
607+
608+
// Connection 2 - Now can see committed data
609+
const statement2 = await connection2.execute('SELECT COUNT(*) FROM users WHERE id = 5');
610+
const { data: data2 } = await statement2.fetchResult();
611+
console.log('Count after commit:', data2[0][0]); // Should show 1
612+
```
613+
475614
<a id="serverSidePreparedStatement"></a>
476615
## Server-side prepared statement
477616

0 commit comments

Comments
 (0)