Skip to content
On this page

stratadb / Transaction

Type Alias: Transaction

ts
type Transaction = object;

Defined in: src/database-types.ts:155

Transaction interface for atomic multi-operation execution.

Remarks

Transactions ensure that multiple operations either all succeed or all fail. Uses SQLite's transaction support underneath. Implements Symbol.dispose for automatic rollback if commit is not called.

Example

typescript
// Using try/finally pattern
const tx = db.transaction();
try {
  const users = tx.collection('users', userSchema);
  await users.insertOne({ name: 'Alice' });
  await users.insertOne({ name: 'Bob' });
  tx.commit();
} catch (error) {
  tx.rollback();
  throw error;
}

// Using Symbol.dispose (automatic rollback on scope exit)
using tx = db.transaction();
const users = tx.collection('users', userSchema);
await users.insertOne({ name: 'Alice' });
tx.commit(); // Must call commit, otherwise rollback on scope exit

Methods

[dispose]()

ts
dispose: void;

Defined in: src/database-types.ts:182

Disposes the transaction (rolls back if not committed). Enables using tx = db.transaction() syntax.

Returns

void


collection()

ts
collection<T>(name, schema): Collection<T>;

Defined in: src/database-types.ts:163

Gets a collection within this transaction.

Type Parameters

T

T extends Document

Parameters

name

string

Collection name

schema

SchemaDefinition<T>

Schema definition for the collection

Returns

Collection<T>

Collection bound to this transaction


commit()

ts
commit(): void;

Defined in: src/database-types.ts:171

Commits all changes made within this transaction.

Returns

void


rollback()

ts
rollback(): void;

Defined in: src/database-types.ts:176

Rolls back all changes made within this transaction.

Returns

void

Released under the MIT License.