Try below with use of nullish coalescing
:
shopsModel.Shop.update({
name: req.body.name ?? undefined,
province: req.body.province ?? undefined,
city: req.body.city ?? undefined,
address: req.body.address ?? undefined,
username: req.body.username ?? undefined,
type: req.body.type ?? undefined
})
It provides returning only keys you want to update and as a result changing only those columns in DB.
You can create a helper function for it:
const returnIfNotNil = key => key ?? undefined
and use as above
shopsModel.Shop.update({
name: returnIfNotNil(req.body.name),
province: returnIfNotNil(req.body.province),
city: returnIfNotNil(req.body.city),
address: returnIfNotNil(req.body.address),
username: returnIfNotNil(req.body.username),
type: returnIfNotNil(req.body.type)
})
or make even more consistent (assuming you use all body to update):
// get only defined fields
const getOnlyDefinedFields = (body: Body) = Object.entries(body)
.reduce((acc, [key, value]) => ({
...acc,
[key]: returnIfNotNil(value)
}), {})
const data = getOnlyDefinedFields(req.body)
await shopsModel.Shop.update(data)