Jason Kleinberg
05/17/2022, 2:12 PMRichard Ward
05/17/2022, 9:57 PMprisma.$use(async (params, next) => {
console.log(params.args.data.title)
console.log('1')
const result = await next(params)
console.log('6')
return result
})
//along with:
const create = await prisma.post.create({
data: {
title: 'Welcome to Prisma Day 2020',
},
})
It seems like the prisma.post.create is "triggered" which would start the middleware.
The middleware (as above) would run the first 2 lines (console.logs) and then do
const result = await next(params)
I think that does the actual prisma.post.create and returns the result to result in the middleware.Still in the middleware, console.log('6') would be run
and then the result from the actual query returnedRichard Ward
05/17/2022, 9:58 PMawait next(params) would either call the next piece of middleware (if there is one) or would do the actual query and return the result..Richard Ward
05/17/2022, 10:00 PMconst result = await next(params) in // Middleware 3 is what runs the actual prisma.post.create( query and it returns the result of that back to Middleware 2 which eventually returns it back to Middleware 1 which in turn passes it back to the const create in :
const create = await prisma.post.create({
data: {
title: 'Welcome to Prisma Day 2020',
},
})Richard Ward
05/17/2022, 10:02 PMtry .. catch that you could put anywhere ..
If the actual query (in Middleware 3 ) throws an error then it should bubble up until it gets to the "nearest` try .. catch block ..Richard Ward
05/17/2022, 10:03 PMJason Kleinberg
05/18/2022, 12:27 AMparams in a chain, rather than composed. It’s obvious in retrospect.