Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Rollback Interactive Transaction Without throwing Error. #22032

Closed
rojiwon123 opened this issue Nov 20, 2023 · 1 comment
Closed

Comments

@rojiwon123
Copy link

rojiwon123 commented Nov 20, 2023

Problem

now (v5.6.0),
In an interactive transaction, we must throw an error to command a rollback.

This has the following drawback:

  • non type-safe
  • make code less predicatable
  • It requires incidental error handling code.

=> potential risk

Suggested solution

  • rollback when inner function return Prisma.Rollback instance.
const prisma = new PrismaClien();
const response: Prisma.users | "NOT_FOUND_USER" | "UNVERIFIED_USER" = await prisma.$transaction(async (tx) => {
         const user: Prisma.users | null = await tx.users.findFirst({ where: { id: 1 } });
         
         Prisma.Rollback(); // it is just instance, so this line does nothing.
         
         if(user === null) return Prisma.Rollback("NOT_FOUND_USER");
         if(!user.verified) return Prisma.Rollback("UNVERIFIED_USER");
         // ... some transaction code...
         return user;
    });
class Rollback<T = undefined>{
  constructor(public readonly result: T){};
}

Alternatives

  • commit or rollback based on the result of the endHandler.
const prisma = new PrismaClien();
const endHandler = (result: Prisma.users | "NOT_FOUND_USER" | "UNVERIFIED_USER"): boolean => 
        typeof response !== 'string';
    
const response: Prisma.users | "NOT_FOUND_USER" | "UNVERIFIED_USER" = await prisma.$transaction(async (tx) => {
         const user: Prisma.users | null = await tx.users.findFirst({ where: { id: 1 } });        
         if(user === null) return "NOT_FOUND_USER";
         if(!user.verified) return "UNVERIFIED_USER";
         // ... some transaction code...
         return user;
    }, { endHandler });
};
type EndHandler = <T = undefined>(result:T) => boolean;

Additional context

#15211
#8664 (comment)

@rojiwon123 rojiwon123 changed the title Rollback Transaction Without throwing Error. [Feature Request] Rollback Transaction Without throwing Error. Nov 20, 2023
@rojiwon123 rojiwon123 changed the title [Feature Request] Rollback Transaction Without throwing Error. [Feature Request] a new way for rollback Transaction Without throwing Error. Nov 20, 2023
@rojiwon123 rojiwon123 changed the title [Feature Request] a new way for rollback Transaction Without throwing Error. [Feature Request] Rollback Transaction Without throwing Error. Nov 20, 2023
@rojiwon123 rojiwon123 changed the title [Feature Request] Rollback Transaction Without throwing Error. [Feature Request] Rollback Interactive Transaction Without throwing Error. Nov 20, 2023
@SevInf
Copy link
Contributor

SevInf commented Nov 20, 2023

Hi @industriously.
There is already a feature request for this functionality in #15211. I am going to close this as a duplicate, if you have any input, please, comment on the other issue.

@SevInf SevInf closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants