It's Prisma Time - Transactions

Hello Guys ๐Ÿ‘‹ Today it's time to speak about transactions. Transactions are a pillar of the database...

#javascript #typescript #database #orm
Luca Del Puppo Luca Del Puppo
Article Series ยท Part 12 of 16 It's Prisma Time
View full series (16)
  1. #11 Previous
    Aggregate and GroupBy
  2. #12 Reading now
    Transactions
  3. #13 Up next
    Logging
Browse all 16 parts in this series
  1. #1 Introduction
  2. #2 Setup
  3. #3 Create Table
  4. #4 Create Db
  5. #5 Create Relations
  6. #6 Insert
  7. #7 Delete
  8. #8 Update
  9. #9 Select
  10. #10 Pagination
  11. #11 Aggregate and GroupBy
  12. #12 Transactions Current
  13. #13 Logging
  14. #14 Execute your own queries
  15. #15 Migrations
  16. #16 Seeding

Hello Guys ๐Ÿ‘‹ Today itโ€™s time to speak about transactions.

Transactions are a pillar of the database world, and they help us to make consistency in the data when we work with multiple entities. So, can Prisma not handle them? Obviously the answer is no ๐Ÿ˜ƒ

In this moment, Prisma handles the transactions in two ways, the first one is the official feature and the second one is in preview. Letโ€™s start from the first one. The first method to do a transaction in Prisma is using the $transaction method. This method accepts a list of operations that are performed in a single transaction. If all of these operations are successful the transaction does the commit otherwise it does the rollback. Letโ€™s see this method at work.

const result = await prisma.$transaction([
  prisma.author.create({
    data: {
      firstName: "Author from transaction",
      lastName: "Author from transaction",
      age: getRandomInt(16, 100),
    },
  }),
  prisma.post.create({
    data: {
      title: "Post from transaction",
      content: "Post from transaction",
      published: false,
    },
  }),
]);

The result of this operation is an array of elements, where the elements are the result of each operation in the transaction, in our example the result is composed in this way [Author, Post]. As you can imagine, this type of transaction is a good solution if you need to create a bulk insert or if you need to create a list of entities not relative to each other. Therefore, to solve this problem, the Prisma team is working to improve the transaction method. In this time, we can try this feature in preview, enabling it in the schema.prisma file. First of all, open the schema.prisma file and update the generator client in this way

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["interactiveTransactions"]
}

N.B. since 4.7.0, this feature has been production ready, so you can remove the "interactiveTransactions" flag.

after that itโ€™s necessary to update the prisma definitions, so run in your terminal this command

npx prisma generate

By doing this, we enabled the feature. Now, weโ€™ll see the previous example, rewritten in the way that the author and post are in relation. Letโ€™s see the result

const result = await prisma.$transaction(async prisma => {
  const authorData = {
    firstName: "Author from transaction",
    lastName: "Author from transaction",
    age: getRandomInt(16, 100),
  } as const;
  const author = await prisma.author.create({
    data: authorData,
  });
  const post = await prisma.post.create({
    data: {
      title: "Post from transaction",
      content: "Post from transaction",
      published: false,
      authors: {
        create: [
          {
            authorId: author.id,
          },
        ],
      },
    },
    include: {
      authors: {
        include: {
          author: true,
        },
      },
    },
  });
  return { author, post };
});

As you can see, this feature allows us to handle the transaction like a function, and in this function we can do all the operations that we want to guarantee under transaction. We can also create an entity by using the create method and await its result. After that we can use this result to create or update another entity always under transaction. If you want to roll back the transaction because there are some inconsistent data, you need to throw an exception.

I think for today thatโ€™s all, but before I let you go, I suggest you this reading about transactions in the prisma site. It explains very well how this feature works and how to handle it in the best way.

In the next article, we are going to see how to log the queries executed by Prisma, but now itโ€™s time to say goodbye ๐Ÿ˜ƒ

See you soon Guys Bye bye ๐Ÿ‘‹

You can find the code of this article in the GitHub repository

Luca Del Puppo

Written by Luca Del Puppo

Run, Code & Learn

AI Native Engineer at Nearform based in Povegliano (TV), Italy. Google Developer Expert in Web Technologies, GitKraken Ambassador, and Codemotion Ambassador.