Collection Hooks

Collections feature the ability to define the following hooks:

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

1
import { CollectionConfig } from 'payload/types';
2
3
export const ExampleHooks: CollectionConfig = {
4
slug: 'example-hooks',
5
fields: [
6
{ name: 'name', type: 'text'},
7
],
8
hooks: {
9
beforeOperation: [(args) => {...}],
10
beforeValidate: [(args) => {...}],
11
beforeDelete: [(args) => {...}],
12
beforeChange: [(args) => {...}],
13
beforeRead: [(args) => {...}],
14
afterChange: [(args) => {...}],
15
afterRead: [(args) => {...}],
16
afterDelete: [(args) => {...}],
17
afterOperation: [(args) => {...}],
18
19
// Auth-enabled hooks
20
beforeLogin: [(args) => {...}],
21
afterLogin: [(args) => {...}],
22
afterLogout: [(args) => {...}],
23
afterRefresh: [(args) => {...}],
24
afterMe: [(args) => {...}],
25
afterForgotPassword: [(args) => {...}],
26
refresh: [(args) => {...}],
27
me: [(args) => {...}],
28
},
29
}

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.

1
import { CollectionBeforeOperationHook } from 'payload/types'
2
3
const beforeOperationHook: CollectionBeforeOperationHook = async ({
4
args, // original arguments passed into the operation
5
operation, // name of the operation
6
req, // full express request
7
}) => {
8
return args // return modified operation arguments as necessary
9
}

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:

  1. validate runs on the client
  2. if successful, beforeValidate runs on the server
  3. validate runs on the server
1
import { CollectionBeforeValidateHook } from 'payload/types'
2
3
const beforeValidateHook: CollectionBeforeValidateHook = async ({
4
data, // incoming data to update or create with
5
req, // full express request
6
operation, // name of the operation ie. 'create', 'update'
7
originalDoc, // original document
8
}) => {
9
return data // Return data to either create or update a document with
10
}

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.

1
import { CollectionBeforeChangeHook } from 'payload/types'
2
3
const beforeChangeHook: CollectionBeforeChangeHook = async ({
4
data, // incoming data to update or create with
5
req, // full express request
6
operation, // name of the operation ie. 'create', 'update'
7
originalDoc, // original document
8
}) => {
9
return data // Return data to either create or update a document with
10
}

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.

1
import { CollectionAfterChangeHook } from 'payload/types'
2
3
const afterChangeHook: CollectionAfterChangeHook = async ({
4
doc, // full document data
5
req, // full express request
6
previousDoc, // document data before updating the collection
7
operation, // name of the operation ie. 'create', 'update'
8
}) => {
9
return doc
10
}

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.

1
import { CollectionBeforeReadHook } from 'payload/types'
2
3
const beforeReadHook: CollectionBeforeReadHook = async ({
4
doc, // full document data
5
req, // full express request
6
query, // JSON formatted query
7
}) => {
8
return doc
9
}

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.

1
import { CollectionAfterReadHook } from 'payload/types'
2
3
const afterReadHook: CollectionAfterReadHook = async ({
4
doc, // full document data
5
req, // full express request
6
query, // JSON formatted query
7
findMany, // boolean to denote if this hook is running against finding one, or finding many
8
}) => {
9
return doc
10
}

beforeDelete

Runs before the delete operation. Returned values are discarded.

1
import { CollectionBeforeDeleteHook } from 'payload/types';
2
3
const beforeDeleteHook: CollectionBeforeDeleteHook = async ({
4
req, // full express request
5
id, // id of document to delete
6
}) => {...}

afterDelete

Runs immediately after the delete operation removes records from the database. Returned values are discarded.

1
import { CollectionAfterDeleteHook } from 'payload/types';
2
3
const afterDeleteHook: CollectionAfterDeleteHook = async ({
4
req, // full express request
5
id, // id of document to delete
6
doc, // deleted document
7
}) => {...}

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.

1
import { CollectionAfterOperationHook } from 'payload/types'
2
3
const afterOperationHook: CollectionAfterOperationHook = async ({
4
args, // arguments passed into the operation
5
operation, // name of the operation
6
req, // full express request
7
result, // the result of the operation, before modifications
8
}) => {
9
return result // return modified result as necessary
10
}

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.

1
import { CollectionBeforeLoginHook } from 'payload/types'
2
3
const beforeLoginHook: CollectionBeforeLoginHook = async ({
4
req, // full express request
5
user, // user being logged in
6
}) => {
7
return user
8
}

afterLogin

For auth-enabled Collections, this hook runs after successful login operations. You can optionally modify the user that is returned.

1
import { CollectionAfterLoginHook } from 'payload/types';
2
3
const afterLoginHook: CollectionAfterLoginHook = async ({
4
req, // full express request
5
user, // user that was logged in
6
token, // user token
7
}) => {...}

afterLogout

For auth-enabled Collections, this hook runs after logout operations.

1
import { CollectionAfterLogoutHook } from 'payload/types';
2
3
const afterLogoutHook: CollectionAfterLogoutHook = async ({
4
req, // full express request
5
}) => {...}

afterRefresh

For auth-enabled Collections, this hook runs after refresh operations.

1
import { CollectionAfterRefreshHook } from 'payload/types';
2
3
const afterRefreshHook: CollectionAfterRefreshHook = async ({
4
req, // full express request
5
res, // full express response
6
token, // newly refreshed user token
7
}) => {...}

afterMe

For auth-enabled Collections, this hook runs after me operations.

1
import { CollectionAfterMeHook } from 'payload/types';
2
3
const afterMeHook: CollectionAfterMeHook = async ({
4
req, // full express request
5
response, // response to return
6
}) => {...}

afterForgotPassword

For auth-enabled Collections, this hook runs after successful forgotPassword operations. Returned values are discarded.

1
import { CollectionAfterForgotPasswordHook } from 'payload/types'
2
3
const afterForgotPasswordHook: CollectionAfterForgotPasswordHook = async ({
4
args, // arguments passed into the operation
5
context,
6
collection, // The collection which this hook is being run on
7
}) => {...}

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.

1
import type { CollectionRefreshHook } from 'payload/types'
2
3
const myRefreshHook: CollectionRefreshHook = async ({
4
args, // arguments passed into the `refresh` operation
5
user, // the user as queried from the database
6
}) => {...}

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.

1
import type { CollectionMeHook } from 'payload/types'
2
3
const meHook: CollectionMeHook = async ({
4
args, // arguments passed into the `me` operation
5
user, // the user as queried from the database
6
}) => {...}

TypeScript

Payload exports a type for each Collection hook which can be accessed as follows:

1
import type {
2
CollectionBeforeOperationHook,
3
CollectionBeforeValidateHook,
4
CollectionBeforeChangeHook,
5
CollectionAfterChangeHook,
6
CollectionAfterReadHook,
7
CollectionBeforeReadHook,
8
CollectionBeforeDeleteHook,
9
CollectionAfterDeleteHook,
10
CollectionBeforeLoginHook,
11
CollectionAfterLoginHook,
12
CollectionAfterLogoutHook,
13
CollectionAfterRefreshHook,
14
CollectionAfterMeHook,
15
CollectionAfterForgotPasswordHook,
16
CollectionRefreshHook,
17
CollectionMeHook,
18
} from 'payload/types'
Next

Field Hooks