I am looking into hooks api in order to set a value before data is inserted into the database.
For example: I would like to set createdAt to the current date time before a row is inserted. So I looked into beforeChange hook and in the documentation it says
beforeChangeis not used to manipulate data but can be used to preform actions before data is saved.
Am I missing anything or is it not currently possible to manipulate data in the hooks api for now? If not, would it make sense to bring it on the roadmap? Do more people see fit of this use case?
You're after the resolveInput hook: https://www.keystonejs.com/api/hooks#resolveinput
Executed after access control checks and default values are assigned. Used to modify the resolvedData.
The result of resolveInput can be a Promise or an Object. It should have the same structure as the resolvedData.
const resolveInput = ({
resolvedData,
existingItem,
originalInput,
context,
list
}) => resolvedData;
Specifically for adding a createdAt field, we have a List Plugin for that which will handle all the details: https://www.keystonejs.com/keystonejs/list-plugins/at-tracking
const { atTracking } = require('@keystonejs/list-plugins');
keystone.createList('ListWithPlugin', {
fields: {
// ...
},
plugins: [
atTracking({
/* ...config */
}),
],
});