I'm trying to build a REST API with NodeJS, ExpressJS and Prisma. At the moment, when I want to update a user through the PATH method, I do this:
const data = {}
if (req.body.email != null)
data.email = req.body.email
if (req.body.password != null)
data.password = req.body.password
if (req.body.first_name != null)
data.first_name = req.body.first_name
if (req.body.last_name != null)
data.last_name = req.body.last_name
await prisma.user.update({
where: {
id: parseInt(id)
},
data: data
})
I removed the unnecessary code to show you the essential things.
I think this is not the best option, is there an easier way to do the same thing? I've tried a few solutions but none of them work with Prisma, that's why I'm here.
You could make a helper function to make the code less repetitive
Something like
const copyProps = (src, ...keys) =>
Object.fromEntries(
Object.entries(src)
.filter(([k,v]) => keys.includes(k) && v != null)
);
Then you can use it like
await prisma.user.update({
where: {
id: parseInt(id)
},
data: copyProps(req.body, 'email', 'password', 'first_name', 'last_name');
});