ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. Core Features

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.

PreviousRelationshipsNextAdvanced Usage

Last updated 9 months ago