Skip to main content

GraphTransaction

Handle atomic multi-operation updates.

Import

import { GraphTransaction } from 'grafio';

createTransaction()

Returns a new GraphTransaction instance for atomic multi-operation updates.

import { InMemoryGraphFactory } from 'grafio';

const factory = new InMemoryGraphFactory();
const graph = factory.forGraph('default');
const txn = graph.createTransaction();

GraphTransaction Methods

begin()

begin(): Promise<void>

Start the transaction.

await txn.begin();

commit()

commit(): Promise<void>

Apply all changes atomically.

await txn.commit();

rollback()

rollback(): Promise<void>

Discard all changes.

await txn.rollback();

isActive()

isActive(): boolean

Check if transaction is active.

if (txn.isActive()) {
// safe to continue
}

isFailed()

isFailed(): boolean

Check if transaction failed.

if (txn.isFailed()) {
// rollback needed
}

Usage Pattern

const txn = graph.createTransaction();
await txn.begin();

try {
const alice = await graph.addNode('Person', { name: 'Alice' }, txn);
const bob = await graph.addNode('Person', { name: 'Bob' }, txn);
await graph.addEdge(alice.id, bob.id, 'KNOWS', {}, txn);

await txn.commit();
} catch (error) {
if (txn.isActive()) {
await txn.rollback();
}
throw error;
}

Error Handling

On commit() failure:

  • Transaction is automatically marked as failed
  • isFailed() returns true
  • isActive() returns false
  • Explicit rollback not required
try {
await txn.commit();
} catch (error) {
// txn.isFailed() === true
// txn.isActive() === false
// No explicit rollback needed
}

Next Steps