1

I would like to know how to use the repository pattern with Prisma transactions.

I am using OAuth, so when the user authenticates, I need to populate the user, account and session tables. If any of these fail, I will need to rollback - that's why the transaction. Each of these tables are in different repositories with their respective creation methods.

How can I use Transaction in this scenario and still be able to make it work in the in-memory repository without my use case knowing the transaction.

This is the code snippet that should be executed, with each create being in a repository - I put them all together to make it easier to understand the flow I want


await this.prismaService.$transaction(async (prisma) => {
        const newUser = await prisma.user.create({
          data: {
            email,
            username,
            name,
          },
        });

      await prisma.account.create({
        data: {
          user_id: newUser.id,
          provider: provider,
          provider_account_id: oauthUserId,
          access_token: accessToken,
          token_type: tokenType,
          type: 'oauth',
        },
      });

      await prisma.session.create({
        data: {
          user_id: newUser.id,
          expires: sessionExpires,
          session_token: sessionToken,
        },
      });
    });
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.