Keystone: manipulating data before save

Created on 22 Nov 2019  路  2Comments  路  Source: keystonejs/keystone

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

beforeChange is 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?

question / idea

All 2 comments

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 */
    }),
  ],
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ra-external picture ra-external  路  10Comments

molomby picture molomby  路  11Comments

thekevinbrown picture thekevinbrown  路  31Comments

bholloway picture bholloway  路  18Comments

amorote picture amorote  路  21Comments