Transactions
Transactions allow you to group multiple operations and either commit them all or roll them back as a single unit.
Basic Usage
const tx = users.startTransaction();
try {
tx.insert({ name: 'Alice', email: 'alice@example.com' });
tx.insert({ name: 'Bob', email: 'bob@example.com' });
tx.updateOne({ name: 'Alice' }, { $set: { age: 28 } });
await tx.commit();
console.log('Transaction committed');
} catch (err) {
await tx.rollback();
console.log('Transaction rolled back');
}
How It Works
When you start a transaction the library takes a deep snapshot of the current data and indexes. All operations during the transaction modify the live data. On commit() the changes are saved to disk. On rollback() the snapshot is restored.
Next: Learn about the Async API.