Transaction function argument type

Viewed 841

I want to use $transaction but I don't know what type I should pass to Prisma so that it provides me with type checking and autocompletion for models.

export default async function SignUp(data: SIGNUP_INTERFACE) {
  try {
    let user = await prisma.$transaction(async (prisma: any /* <- this */) => {
      let encrypted_password = await bcrypt.hash(data.password, 10);
      // 1. Add user to database
      const user = await prisma.user.create({
        data: {
          username: data.username,
          password: encrypted_password,
        },
      });

      // 2. Add email/phone number to database
      if (data.phoneNumber) {
        await prisma.user_phone.create({
          data: { phone_number: data.phoneNumber },
        });
      }
      if (data.email) {
        await prisma.user_email.create({
          data: { email: data.email },
        });
      }
    });

    return user;
  } catch (err: any) {
    throw new CustomError(err.message);
  }
}

I have tried

PrismaClient<Prisma.PrismaClientOptions, never, Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>

but then it gives

Type '(prisma: PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>) => Promise<...>' is missing the following properties from type 'PrismaPromise[]': pop, push, concat, join, and 28 more

1 Answers

As it turned out from the comments, the interactiveTransactions flag should have been enabled to allow for the new transaction style, which in turn enabled to use PrismaClient as the type for prisma.

Related