2

API with NestJS #76. Working with transactions using raw SQL queries

 1 year ago
source link: https://wanago.io/2022/09/26/api-nestjs-transactions-sql/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

API with NestJS #76. Working with transactions using raw SQL queries

JavaScript NestJS SQL

September 26, 2022

This entry is part 76 of 76 in the API with NestJS

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:

  1. withdrawing money from the first account,
  2. 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:

  1. withdrawing the money reduces the amount of money in the first account,
  2. 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();
      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();
      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,
        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<ListType extends unknown>(
  firstArray: ListType[],
  secondArray: unknown[],
): ListType[] {
  return firstArray.filter((arrayElement) => {
    return !secondArray.includes(arrayElement);
export default getDifferenceBetweenArrays;

Above, we return the elements present in the first array but absent from the second one.

The last part we need to analyze is how we remove and add categories to the post. Removing categories is very straightforward and involves a simple SQL query.

posts.repository.ts
import {
  BadRequestException,
  Injectable,
} from '@nestjs/common';
import { PoolClient } from 'pg';
import PostgresErrorCode from '../database/postgresErrorCode.enum';
import isRecord from '../utils/isRecord';
@Injectable()
class PostsRepository {
  private async removeCategoriesFromPost(
    client: PoolClient,
    postId: number,
    categoryIdsToRemove: number[],
    if (!categoryIdsToRemove.length) {
      return;
    return client.query(
      DELETE FROM categories_posts WHERE post_id = $1 AND category_id = ANY($2::int[])
      [postId, categoryIdsToRemove],
  // ...
export default PostsRepository;

On the other hand, adding categories to a post has a catch. If the user tries to use the id of a category that does not exist, PostgreSQL throws the foreign key violation error. In this example, we catch this error and assume that the user provided the wrong id.

posts.repository.ts
import {
  BadRequestException,
  Injectable,
} from '@nestjs/common';
import { PoolClient } from 'pg';
import PostgresErrorCode from '../database/postgresErrorCode.enum';
import isRecord from '../utils/isRecord';
@Injectable()
class PostsRepository {
  private async addCategoriesToPost(
    client: PoolClient,
    postId: number,
    categoryIdsToAdd: number[],
    if (!categoryIdsToAdd.length) {
      return;
      await client.query(
      INSERT INTO categories_posts (
        post_id, category_id
        SELECT $1 AS post_id, unnest($2::int[]) AS category_id
        [postId, categoryIdsToAdd],
    } catch (error) {
        isRecord(error) &&
        error.code === PostgresErrorCode.ForeignKeyViolation
        throw new BadRequestException('Category not found');
      throw error;
  // ...
export default PostsRepository;

If you want to see the whole PostsRepository, check it out on GitHub.

To catch the above issue elegantly, we’ve added the appropriate error code to our PostgresErrorCode enum.

postgresErrorCode.enum.ts
enum PostgresErrorCode {
  UniqueViolation = '23505',
  ForeignKeyViolation = '23503',
export default PostgresErrorCode;

Thanks to all of the above, we can do the following actions in a single transaction:

  • update the title and content of the post
  • check the categories currently tied to the post,
  • remove and add categories to the post if necessary,
  • return the modified post together with the updated list of categories.
Screenshot-from-2022-09-25-22-35-45.png

Summary

In this article, we’ve gone through the idea of transactions and learned why we might need them. We’ve also learned that we need to use a particular client from the connection pool for all queries in a particular transaction. To practice that, we first implemented a simple example. Then, we went through a more complicated transaction that involved passing the client between multiple methods. The above knowledge is crucial when caring about the integrity of our database.

Series Navigation<< API with NestJS #75. Many-to-many relationships using raw SQL queries


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK