Pieter
08/07/2020, 11:45 AMhandler
so it gets re-used, which makes perfect sense. I'm not aware of how to extract handler functions etc inside a function running on vercel.Ryan
08/07/2020, 11:50 AMimport { PrismaClient, PrismaClientRequestError } from '@prisma/client'
const prisma = new PrismaClient()
export default async (req, res) => {
try {
const createdUser = await prisma.user.create({
data: req.body
})
res.status(200).json(createdUser)
} catch (e) {
if (e instanceof PrismaClientRequestError) {
if (e.code === 'P2002') {
return res
.status(409)
.json({ error: 'A user with this email already exists' })
}
}
console.error(e)
return res.status(500)
}
}
Pieter
08/07/2020, 12:11 PMPieter
08/07/2020, 12:11 PMRyan
08/07/2020, 12:17 PMso the default export is run as a handler under the hood on aws lambda?Yes, for AWS Lambda you can configure the export as per your naming and that needs to be specified with the path of the file.
And the parts outside of it will be re-used as long as the lambda is warmed?Yes that’s correct. You can read more about this here. I’m not sure about how Vercel works, but it should be somewhat similar
Pieter
08/07/2020, 2:02 PM