Collection Hooks
Collections feature the ability to define the following hooks:
- beforeOperation
 - beforeValidate
 - beforeChange
 - afterChange
 - beforeRead
 - afterRead
 - beforeDelete
 - afterDelete
 - afterOperation
 
Additionally, auth-enabled collections feature the following hooks:
Config
All collection Hook properties accept arrays of synchronous or asynchronous functions. Each Hook type receives specific arguments and has the ability to modify specific outputs.
collections/exampleHooks.js
beforeOperation
The beforeOperation hook can be used to modify the arguments that operations accept or execute side-effects that run before an operation begins.
Available Collection operations include create, read, update, delete, login, refresh, and forgotPassword.
beforeValidate
Runs before the create and update operations. This hook allows you to add or format data before the incoming data is validated server-side.
Please do note that this does not run before the client-side validation. If you added a validate function, this would be the lifecycle:
validateruns on the client- if successful, 
beforeValidateruns on the server validateruns on the server
beforeChange
Immediately following validation, beforeChange hooks will run within create and update operations. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.
afterChange
After a document is created or updated, the afterChange hook runs. This hook is helpful to recalculate statistics such as total sales within a global, syncing user profile changes to a CRM, and more.
beforeRead
Runs before find and findByID operations are transformed for output by afterRead. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the doc argument.
afterRead
Runs as the last step before documents are returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
beforeDelete
Runs before the delete operation. Returned values are discarded.
afterDelete
Runs immediately after the delete operation removes records from the database. Returned values are discarded.
afterOperation
The afterOperation hook can be used to modify the result of operations or execute side-effects that run after an operation has completed.
Available Collection operations include create, find, findByID, update, updateByID, delete, deleteByID, login, refresh, and forgotPassword.
beforeLogin
For auth-enabled Collections, this hook runs during login operations where a user with the provided credentials exist, but before a token is generated and added to the response. You can optionally modify the user that is returned, or throw an error in order to deny the login operation.
afterLogin
For auth-enabled Collections, this hook runs after successful login operations. You can optionally modify the user that is returned.
afterLogout
For auth-enabled Collections, this hook runs after logout operations.
afterRefresh
For auth-enabled Collections, this hook runs after refresh operations.
afterMe
For auth-enabled Collections, this hook runs after me operations.
afterForgotPassword
For auth-enabled Collections, this hook runs after successful forgotPassword operations. Returned values are discarded.
refresh
For auth-enabled Collections, this hook allows you to optionally replace the default behavior of the refresh operation with your own. If you optionally return a value from your hook, the operation will not perform its own logic and continue.
me
For auth-enabled Collections, this hook allows you to optionally replace the default behavior of the me operation with your own. If you optionally return a value from your hook, the operation will not perform its own logic and continue.
TypeScript
Payload exports a type for each Collection hook which can be accessed as follows: