我想使用$transaction
,但我不知道我应该传递给Prisma的类型,以便它为我提供模型的类型检查和自动完成。
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);
}
}
我试过了
PrismaClient<Prisma.PrismaClientOptions, never, Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>
然后给出
Type '(prisma: PrismaClient<PrismaClientOptions,>) =>Promise<…>'类型'PrismaPromise[]'缺少以下属性:pop, push, concat, join, and more
从注释中可以看出,interactiveTransactions
标志应该启用以允许新的事务样式,这反过来又启用了使用PrismaClient
作为prisma
的类型。
generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
}
我有这个问题,但在挖掘出事务客户端应该输入为Prisma.TransactionClient
,在您的情况下,prisma: any
变成了prisma: Prisma.TransactionClient
。希望这对将来的人有所帮助。