Using Adonis js
I am facing an issue when trying to convert an ISO string to Datetime while saving data (the opposite of serializing DateTime fields to ISO string). I cannot find a way to do this in the model, like I would with a mutator in Laravel. Whenever I try to use the beforeSave() hook to achieve this, I encounter a type error because the model is expecting DateTime and not a string type. Any suggestions?
Controller
public async update({ request, params, response }) {
let data = request.all()
//Hopefully move this logic to model for date fields
for (const [key, value] of Object.entries(data)) {
if (['recievedAt', 'dueAt'].includes(key) && typeof value == 'string') {
data[key] = DateTime.fromISO(value)
}
}
const task = await Task.findOrFail(params.id)
task.merge(data)
await task.save()
return response.status(200).send({ request, params: params.id, data })
}
Model
@column.dateTime({
serialize: (value: DateTime | null) => {
return value ? value.setZone('utc').toISODate() : value
},
})
public recievedAt: DateTime