Transactions

In this schedule, I demonstrate working with transactionality

Transactions are a crucial part of working with databases, as they allow grouping multiple operations into a single unit, ensuring atomicity, consistency, and integrity of data. If one operation in a transaction fails, all other operations are also rolled back.

In my ORM system, transactions can be managed using the transaction method. In this section, we'll demonstrate how to work with transactions using examples of queries on the Users and Tasks tables.

Example of Using Transactions

Let's consider an example where we perform two operations within a single transaction: retrieving users from the Users table and tasks from the Tasks table.

try {
    await databaseManager.transaction(async (trx) => {
        const users = await trx.queryBuilder()
            .select()
            .from('users')
            .execute();

        const tasks = await trx.queryBuilder()
            .select()
            .from('tasks')
            .execute();

        console.log("Users:", users);
        console.log("Tasks:", tasks);
    });
    console.log('Transaction completed successfully.');
} catch (error) {
    console.error('Transaction failed:', error);
}

Transactions in my ORM system allow me to group multiple operations, providing a high level of control over data integrity. This is especially useful in complex operations that must be performed atomically, meaning that either all operations complete successfully or none of them perform.

Last updated