我们如何在不同的文件中添加 prisma 中间件?
How we can add prisma middleware in different file?
我有一个路由的多个中间件。我可以分解代码并将中间件移动到不同的文件中吗?我尝试在服务器文件中导入中间件,但它不起作用
尝试像这样添加
import bcrypt from 'bcryptjs'
import { PrismaClient, Prisma } from '@prisma/client'
const prisma: PrismaClient = new PrismaClient()
prisma.$use(async (params: Prisma.MiddlewareParams, next) => {
if (params.action == 'create' && params.model == 'User') {
let user = params.args.data
let salt = bcrypt.genSaltSync(10)
let hash = bcrypt.hashSync(user.password, salt)
user.password = hash
}
return await next(params)
})
我有一个路由的多个中间件。我可以分解代码并将中间件移动到不同的文件中吗?我尝试在服务器文件中导入中间件,但它不起作用
尝试像这样添加
import bcrypt from 'bcryptjs'
import { PrismaClient, Prisma } from '@prisma/client'
const prisma: PrismaClient = new PrismaClient()
prisma.$use(async (params: Prisma.MiddlewareParams, next) => {
if (params.action == 'create' && params.model == 'User') {
let user = params.args.data
let salt = bcrypt.genSaltSync(10)
let hash = bcrypt.hashSync(user.password, salt)
user.password = hash
}
return await next(params)
})