Collection Hooks
Collection Hooks are Hooks that run on Documents within a specific Collection. They allow you to execute your own logic during specific events of the Document lifecycle.
To add Hooks to a Collection, use the hooks property in your Collection Config:
Config Options
All Collection Hooks accept an array of synchronous or asynchronous functions. Each Collection Hook receives specific arguments based on its own type, and has the ability to modify specific outputs.
beforeOperation
The beforeOperation hook can be used to modify the arguments that operations accept or execute side-effects that run before an operation begins.
The following arguments are provided to the beforeOperation hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. Available options include: |
| Custom context passed between Hooks. More details. |
| The name of the operation that this hook is running within. |
| The Web Request object. This is mocked for Local API operations. |
beforeValidate
Runs during 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 client-side validation. If you render a custom field component in your front-end and provide it with a validate function, the order that validations will run in is:
validateruns on the client- if successful,
beforeValidateruns on the server validateruns on the server
The following arguments are provided to the beforeValidate hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between Hooks. More details. |
| The incoming data passed through the operation. |
| The name of the operation that this hook is running within. |
| The Document before changes are applied. |
| The Web Request object. This is mocked for Local API operations. |
beforeChange
Immediately before validation, beforeChange hooks will run during create and update operations. At this stage, the data should be treated as unvalidated user input. There is no guarantee that required fields exist or that fields are in the correct format. As such, using this data for side effects requires manual validation. You can optionally modify the shape of the data to be saved.
The following arguments are provided to the beforeChange hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The incoming data passed through the operation. |
| The name of the operation that this hook is running within. |
| The Document before changes are applied. |
| The Web Request object. This is mocked for Local API operations. |
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.
The following arguments are provided to the afterChange hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The incoming data passed through the operation. |
| The resulting Document after changes are applied. |
| The name of the operation that this hook is running within. |
| The Document before changes were applied. |
| The Web Request object. This is mocked for Local API operations. |
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.
The following arguments are provided to the beforeRead hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The resulting Document after changes are applied. |
| The Query of the request. |
| The Web Request object. This is mocked for Local API operations. |
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.
The following arguments are provided to the afterRead hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The resulting Document after changes are applied. |
| The Query of the request. |
| The Web Request object. This is mocked for Local API operations. |
beforeDelete
Runs before the delete operation. Returned values are discarded.
The following arguments are provided to the beforeDelete hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The ID of the Document being deleted. |
| The Web Request object. This is mocked for Local API operations. |
afterDelete
Runs immediately after the delete operation removes records from the database. Returned values are discarded.
The following arguments are provided to the afterDelete hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The resulting Document after changes are applied. |
| The ID of the Document that was deleted. |
| The Web Request object. This is mocked for Local API operations. |
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.
The following arguments are provided to the afterOperation hook:
Option | Description |
|---|---|
| The arguments passed into the operation. |
| The Collection in which this Hook is running against. |
| The Web Request object. This is mocked for Local API operations. |
| The name of the operation that this hook is running within. |
| The result of the operation, before modifications. |
afterError
The afterError Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc. The output can be used to transform the result object / status code.
The following arguments are provided to the afterError Hook:
Argument | Description |
|---|---|
| The error that occurred. |
| Custom context passed between Hooks. More details. |
| The GraphQL result object, available if the hook is executed within a GraphQL context. |
| The |
| The Collection in which this Hook is running against. |
| The formatted error result object, available if the hook is executed from a REST context. |
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.
The following arguments are provided to the beforeLogin hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The Web Request object. This is mocked for Local API operations. |
| The user being logged in. |
afterLogin
For Auth-enabled Collections, this hook runs after successful login operations. You can optionally modify the user that is returned.
The following arguments are provided to the afterLogin hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The Web Request object. This is mocked for Local API operations. |
| The token generated for the user. |
| The user being logged in. |
afterLogout
For Auth-enabled Collections, this hook runs after logout operations.
The following arguments are provided to the afterLogout hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The Web Request object. This is mocked for Local API operations. |
afterMe
For Auth-enabled Collections, this hook runs after me operations.
The following arguments are provided to the afterMe hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The Web Request object. This is mocked for Local API operations. |
| The response to return. |
afterRefresh
For Auth-enabled Collections, this hook runs after refresh operations.
The following arguments are provided to the afterRefresh hook:
Option | Description |
|---|---|
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
| The expiration time of the token. |
| The Web Request object. This is mocked for Local API operations. |
| The newly refreshed user token. |
afterForgotPassword
For Auth-enabled Collections, this hook runs after successful forgotPassword operations. Returned values are discarded.
The following arguments are provided to the afterForgotPassword hook:
Option | Description |
|---|---|
| The arguments passed into the operation. |
| The Collection in which this Hook is running against. |
| Custom context passed between hooks. More details. |
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.
The following arguments are provided to the afterRefresh hook:
Option | Description |
|---|---|
| The arguments passed into the operation. |
| The user being logged in. |
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.
The following arguments are provided to the me hook:
Option | Description |
|---|---|
| The arguments passed into the operation. |
| The user being logged in. |
TypeScript
Payload exports a type for each Collection hook which can be accessed as follows: