Working with transactions using raw SQL queries
One of the challenges when working with databases is keeping the integrity of the data. In this article, we learn how to deal with it using transactions. A…
One of the challenges when working with databases is keeping the integrity of the data. In this article, we learn how to deal with it using transactions. A transaction can contain multiple different instructions. The crucial thing about a transaction is that it either runs entirely or doesn’t run at all. Let’s revisit the most common example to understand the need for transactions. Transferring money from one bank account to another consist of two steps: withdrawing money from the first account, adding the same amount of money to the second account. The whole operation failing means the integrity of the data is still intact. The amount of money on both of the accounts remains intact. The worst scenario happens when just half of the above steps run successfully. Imagine the following situation: withdrawing the money reduces the amount of money in the first account, adding the money to the second account fails because the account was recently closed. The above scenario causes us to lose the integrity of our data. A specific sum of the money disappeared from the bank and is in neither of the accounts. ACID properties Fortunately, we can deal with the above issue using transactions. It guarantees us the following properties: Atomicity The operations in the transaction form a single unit. Therefore, it either entirely succeeds or fails completely. Consistency A transaction progresses the database from one valid state to another. Isolation More than one transaction could occur in our database at the same time without having an invalid state of the data. For example, another transaction should detect the money in one bank account, but not in neither nor both. Durability When we commit the changes from the transaction, they need to persist permanently. Writing transactions with PostgreSQL Whenever we run a single query, PostgreSQL wraps it in a transaction that ensures all of the ACID properties. Besides that, we can run multiple queries in a transaction. To do that, we can use the BEGIN and COMMIT statements. With the BEGIN statement, we initiate the transaction block. PostgreSQL executes all queries after the BEGIN statement in a single transaction. When we run the COMMIT statement, PostgreSQL stores our changes in the database.BEGIN; --Disconnecting posts from a given category DELETE FROM categories_posts WHERE category_id=1; --Deleting the category from the database DELETE FROM categories WHERE id=1; COMMIT;In the above code, we first disconnect all posts from a given category. Then, we delete the category. If deleting the category fails for any reason, the posts are not removed from the category. When that happens, we should discard the transaction using the ROLLBACK statement. But, of course, we can also do that anytime we want to abort the current transaction. Using transactions with node-postgres We’ve used the node-postgres library in this series of articles to create a connection pool. This means that we have a pool of multiple clients connected to our database. Using the same client instance for all of our queries within a transaction is crucial. To do that, we need to modify our DatabaseService class. database.service.ts import { Inject, Injectable } from '@nestjs/common'; import { Pool } from 'pg'; import { CONNECTION_POOL } from './database.module-definition'; @Injectable() class DatabaseService { constructor(@Inject(CONNECTION_POOL) private readonly pool: Pool) {} async runQuery(query: string, params?: unknown[]) { return this.pool.query(query, params); } async getPoolClient() { return this.pool.connect(); } } export default DatabaseService;When running this.pool.query(), our query runs on any available client in the pool. This is fine if we don’t write transactions. To run a set of operations using a particular client, we need to get it using this.pool.connect() function. Let’s use the above knowledge to delete rows from both categories and posts_categories tables. categories.repository.ts import { Injectable, NotFoundException } from '@nestjs/common'; import DatabaseService from '../database/database.service'; @Injectable() class CategoriesRepository { constructor(private readonly databaseService: DatabaseService) {} async delete(id: number) { const poolClient = await this.databaseService.getPoolClient(); try { await poolClient.query('BEGIN;'); // Disconnecting posts from a given category await poolClient.query( ` DELETE FROM categories_posts WHERE category_id=$1; `, [id], ); // Disconnecting posts from a given category const categoriesResponse = await poolClient.query( ` DELETE FROM categories WHERE id=$1; `, [id], ); if (categoriesResponse.rowCount === 0) { throw new NotFoundException(); } await poolClient.query(` COMMIT; `); } catch (error) { await poolClient.query(` ROLLBACK; `); throw error; } finally { poolClient.release(); } } // ... } export default CategoriesRepository;A significant thing above is that we call the release() method when we don’t need a particular client anymore. Thanks to that, it returns to the pool and becomes available again. Passing the client instance between methods As our logic gets more complex, our transactions might occupy more than one method. To deal with this, we can pass the client instance as an argument. In the previous article, we learned how to work with many-to-many relationships. When creating posts, we sent the following data through the API:{ "title": "My first post", "content": "Hello world!", "categoryIds": [1, 2, 3] }Let’s write a method that allows us to modify the above post. For example, imagine sending the following PUT request:{ "title": "My modified post", "content": "Hello world!", "categoryIds": [2, 4] }After analyzing the above payload, we can notice the following differences: we need to remove the post from the 1 and 3 categories, we have to add the post to the category with id 4. posts.repository.ts import { Injectable, NotFoundException } from '@nestjs/common'; import DatabaseService from '../database/database.service'; import PostDto from './post.dto'; import PostWithCategoryIdsModel from './postWithCategoryIds.model'; @Injectable() class PostsRepository { constructor(private readonly databaseService: DatabaseService) {} async update(id: number, postData: PostDto) { const client = await this.databaseService.getPoolClient(); try { await client.query('BEGIN;'); const databaseResponse = await client.query( ` UPDATE posts SET title = $2, post_content = $3 WHERE id = $1 RETURNING * `, [id, postData.title, postData.content], ); const entity = databaseResponse.rows[0]; if (!entity) { throw new NotFoundException(); } const newCategoryIds = postData.categoryIds || []; const categoryIds = await this.updateCategories( client, id, newCategoryIds, ); return new PostWithCategoryIdsModel({ ...entity, category_ids: categoryIds, }); } catch (error) { await client.query('ROLLBACK;'); throw error; } finally { client.release(); } } // ... } export default PostsRepository;In the above function, we use the this.updateCategories method that modifies the relationship between the post and the categories. posts.repository.ts import { Injectable, } from '@nestjs/common'; import DatabaseService from '../database/database.service'; import { PoolClient } from 'pg'; import getDifferenceBetweenArrays from '../utils/getDifferenceBetweenArrays'; @Injectable() class PostsRepository { constructor(private readonly databaseService: DatabaseService) {} private async getCategoryIdsRelatedToPost( client: PoolClient, postId: number, ): Promise<number[]> { const categoryIdsResponse = await client.query( ` SELECT ARRAY( SELECT category_id FROM categories_posts WHERE post_id = $1 ) AS category_ids `, [postId], ); return categoryIdsResponse.rows[0].category_ids; } private async updateCategories( client: PoolClient, postId: number, newCategoryIds: number[], ) { const existingCategoryIds = await this.getCategoryIdsRelatedToPost( client, postId, ); const categoryIdsToRemove = getDifferenceBetweenArrays( existingCategoryIds, newCategoryIds, ); const categoryIdsToAdd = getDifferenceBetweenArrays( newCategoryIds, existingCategoryIds, ); await this.removeCategoriesFromPost(client, postId, categoryIdsToRemove); await this.addCategoriesToPost(client, postId, categoryIdsToAdd); return this.getCategoryIdsRelatedToPost(client, postId); } // ... } export default PostsRepository;A few important things are happening above: we determine what categories we need to link and unlink from a post using the getDifferenceBetweenArrays method, we remove and add categories associated with the post, we return the list of categories related to the post after the above operations. First, let’s take a look at the getDifferenceBetweenArrays method. getDifferenceBetweenArrays.ts function getDifferenceBetweenArrays