payload 1.0.27 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -4
- package/dist/admin/components/elements/Status/index.js +1 -1
- package/dist/admin/components/elements/ViewDescription/types.d.ts +1 -1
- package/dist/admin/components/elements/WhereBuilder/index.js +3 -1
- package/dist/admin/components/forms/field-types/Array/index.d.ts +3 -2
- package/dist/admin/components/forms/field-types/Array/index.js +201 -6
- package/dist/admin/components/forms/field-types/Blocks/index.d.ts +3 -2
- package/dist/admin/components/forms/field-types/Blocks/index.js +213 -6
- package/dist/admin/components/forms/field-types/Collapsible/index.js +2 -2
- package/dist/admin/components/forms/field-types/Row/index.js +2 -2
- package/dist/admin/components/forms/field-types/Tabs/index.js +2 -2
- package/dist/admin/components/forms/field-types/getFieldPath.d.ts +2 -0
- package/dist/admin/components/forms/field-types/getFieldPath.js +6 -0
- package/dist/admin/components/forms/field-types/index.d.ts +23 -23
- package/dist/admin/components/utilities/RenderCustomComponent/types.d.ts +2 -2
- package/dist/admin/components/views/collections/List/Cell/field-types/Relationship/index.js +1 -1
- package/dist/auth/operations/local/forgotPassword.js +3 -6
- package/dist/auth/operations/local/login.js +6 -9
- package/dist/auth/operations/local/resetPassword.js +4 -7
- package/dist/auth/operations/local/unlock.js +4 -7
- package/dist/collections/buildSchema.js +2 -1
- package/dist/collections/config/types.d.ts +2 -2
- package/dist/collections/dataloader.js +16 -8
- package/dist/collections/operations/local/create.d.ts +2 -2
- package/dist/collections/operations/local/create.js +11 -13
- package/dist/collections/operations/local/find.js +6 -10
- package/dist/collections/operations/local/findByID.js +6 -10
- package/dist/collections/operations/local/findVersionByID.js +6 -9
- package/dist/config/types.d.ts +13 -13
- package/dist/fields/config/sanitize.js +3 -3
- package/dist/fields/config/schema.js +3 -3
- package/dist/fields/config/types.d.ts +9 -9
- package/dist/fields/validations.js +43 -1
- package/dist/globals/config/types.d.ts +1 -1
- package/dist/mongoose/buildSchema.d.ts +1 -0
- package/dist/mongoose/buildSchema.js +3 -3
- package/dist/utilities/getIDType.d.ts +2 -0
- package/dist/utilities/getIDType.js +11 -0
- package/dist/utilities/isValidID.d.ts +1 -0
- package/dist/utilities/isValidID.js +16 -0
- package/docs/access-control/collections.mdx +21 -12
- package/docs/access-control/fields.mdx +5 -3
- package/docs/access-control/globals.mdx +8 -4
- package/docs/access-control/overview.mdx +1 -1
- package/docs/admin/components.mdx +29 -25
- package/docs/admin/customizing-css.mdx +2 -2
- package/docs/admin/overview.mdx +2 -2
- package/docs/admin/webpack.mdx +8 -7
- package/docs/authentication/config.mdx +14 -9
- package/docs/authentication/operations.mdx +22 -22
- package/docs/authentication/overview.mdx +8 -6
- package/docs/authentication/using-middleware.mdx +1 -1
- package/docs/configuration/collections.mdx +11 -7
- package/docs/configuration/globals.mdx +9 -5
- package/docs/configuration/localization.mdx +33 -31
- package/docs/configuration/overview.mdx +4 -7
- package/docs/email/overview.mdx +11 -7
- package/docs/fields/array.mdx +6 -5
- package/docs/fields/blocks.mdx +6 -4
- package/docs/fields/checkbox.mdx +6 -5
- package/docs/fields/code.mdx +6 -5
- package/docs/fields/collapsible.mdx +6 -5
- package/docs/fields/date.mdx +6 -5
- package/docs/fields/email.mdx +6 -5
- package/docs/fields/group.mdx +6 -5
- package/docs/fields/number.mdx +6 -5
- package/docs/fields/overview.mdx +40 -38
- package/docs/fields/point.mdx +6 -4
- package/docs/fields/radio.mdx +5 -3
- package/docs/fields/relationship.mdx +21 -21
- package/docs/fields/rich-text.mdx +9 -7
- package/docs/fields/row.mdx +5 -3
- package/docs/fields/select.mdx +4 -3
- package/docs/fields/tabs.mdx +5 -3
- package/docs/fields/text.mdx +5 -3
- package/docs/fields/textarea.mdx +5 -3
- package/docs/fields/ui.mdx +5 -3
- package/docs/fields/upload.mdx +5 -3
- package/docs/getting-started/concepts.mdx +1 -1
- package/docs/graphql/extending.mdx +1 -1
- package/docs/graphql/overview.mdx +8 -4
- package/docs/hooks/collections.mdx +62 -36
- package/docs/hooks/fields.mdx +5 -3
- package/docs/hooks/globals.mdx +25 -17
- package/docs/local-api/overview.mdx +27 -4
- package/docs/plugins/overview.mdx +3 -4
- package/docs/production/deployment.mdx +1 -1
- package/docs/queries/overview.mdx +4 -2
- package/docs/queries/pagination.mdx +1 -1
- package/docs/rest-api/overview.mdx +4 -2
- package/docs/upload/overview.mdx +35 -29
- package/docs/versions/autosave.mdx +4 -2
- package/docs/versions/drafts.mdx +8 -4
- package/docs/versions/overview.mdx +1 -1
- package/package.json +1 -1
- package/types.d.ts +21 -0
- package/dist/admin/components/forms/field-types/Array/Array.d.ts +0 -5
- package/dist/admin/components/forms/field-types/Array/Array.js +0 -201
- package/dist/admin/components/forms/field-types/Blocks/Blocks.d.ts +0 -5
- package/dist/admin/components/forms/field-types/Blocks/Blocks.js +0 -213
|
@@ -69,26 +69,26 @@ The `filterOptions` property can either be a `Where` query directly, or a functi
|
|
|
69
69
|
|
|
70
70
|
**Example:**
|
|
71
71
|
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
72
|
+
```ts
|
|
73
|
+
const relationshipField = {
|
|
74
|
+
name: 'purchase',
|
|
75
|
+
type: 'relationship',
|
|
76
|
+
relationTo: ['products', 'services'],
|
|
77
|
+
filterOptions: ({ relationTo, siblingData }) => {
|
|
78
|
+
// returns a Where query dynamically by the type of relationship
|
|
79
|
+
if (relationTo === 'products') {
|
|
80
|
+
return {
|
|
81
|
+
'stock': { is_greater_than: siblingData.quantity }
|
|
83
82
|
}
|
|
83
|
+
}
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
85
|
+
if (relationTo === 'services') {
|
|
86
|
+
return {
|
|
87
|
+
'isAvailable': { equals: true }
|
|
89
88
|
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
You can learn more about writing queries [here](/docs/queries/overview).
|
|
@@ -106,7 +106,7 @@ Given the variety of options possible within the `relationship` field type, the
|
|
|
106
106
|
|
|
107
107
|
The most simple pattern of a relationship is to use `hasMany: false` with a `relationTo` that allows for only one type of collection.
|
|
108
108
|
|
|
109
|
-
```
|
|
109
|
+
```ts
|
|
110
110
|
{
|
|
111
111
|
slug: 'example-collection',
|
|
112
112
|
fields: [
|
|
@@ -137,7 +137,7 @@ When querying documents in this collection via REST API, you could query as foll
|
|
|
137
137
|
|
|
138
138
|
Also known as **dynamic references**, in this configuration, the `relationTo` field is an array of Collection slugs that tells Payload which Collections are valid to reference.
|
|
139
139
|
|
|
140
|
-
```
|
|
140
|
+
```ts
|
|
141
141
|
{
|
|
142
142
|
slug: 'example-collection',
|
|
143
143
|
fields: [
|
|
@@ -176,7 +176,7 @@ This query would return only documents that have an owner relationship to organi
|
|
|
176
176
|
|
|
177
177
|
The `hasMany` tells Payload that there may be more than one collection saved to the field.
|
|
178
178
|
|
|
179
|
-
```
|
|
179
|
+
```ts
|
|
180
180
|
{
|
|
181
181
|
slug: 'example-collection',
|
|
182
182
|
fields: [
|
|
@@ -204,7 +204,7 @@ When querying documents, the format does not change for arrays:
|
|
|
204
204
|
|
|
205
205
|
#### Has Many - Polymorphic
|
|
206
206
|
|
|
207
|
-
```
|
|
207
|
+
```ts
|
|
208
208
|
{
|
|
209
209
|
slug: 'example-collection',
|
|
210
210
|
fields: [
|
|
@@ -126,9 +126,11 @@ Custom `Leaf` objects follow a similar pattern but require you to define the `Le
|
|
|
126
126
|
|
|
127
127
|
### Example
|
|
128
128
|
|
|
129
|
-
`collections/ExampleCollection.
|
|
130
|
-
```
|
|
131
|
-
{
|
|
129
|
+
`collections/ExampleCollection.ts`
|
|
130
|
+
```ts
|
|
131
|
+
import { CollectionConfig } from 'payload/types';
|
|
132
|
+
|
|
133
|
+
const ExampleCollection: CollectionConfig = {
|
|
132
134
|
slug: 'example-collection',
|
|
133
135
|
fields: [
|
|
134
136
|
{
|
|
@@ -178,7 +180,7 @@ Custom `Leaf` objects follow a similar pattern but require you to define the `Le
|
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
182
|
]
|
|
181
|
-
}
|
|
183
|
+
};
|
|
182
184
|
```
|
|
183
185
|
|
|
184
186
|
For more examples regarding how to define your own elements and leaves, check out the example [`RichText` field](https://github.com/payloadcms/public-demo/blob/master/src/fields/hero.ts) within the Public Demo source code.
|
|
@@ -187,7 +189,7 @@ For more examples regarding how to define your own elements and leaves, check ou
|
|
|
187
189
|
|
|
188
190
|
As the Rich Text field saves its content in a JSON format, you'll need to render it as HTML yourself. Here is an example for how to generate JSX / HTML from Rich Text content:
|
|
189
191
|
|
|
190
|
-
```
|
|
192
|
+
```ts
|
|
191
193
|
import React, { Fragment } from 'react';
|
|
192
194
|
import escapeHTML from 'escape-html';
|
|
193
195
|
import { Text } from 'slate';
|
|
@@ -308,7 +310,7 @@ If you want to utilize this functionality within your own custom elements, you c
|
|
|
308
310
|
|
|
309
311
|
`customLargeBodyElement.js`:
|
|
310
312
|
|
|
311
|
-
```
|
|
313
|
+
```ts
|
|
312
314
|
import Button from './Button';
|
|
313
315
|
import Element from './Element';
|
|
314
316
|
import withLargeBody from './plugin';
|
|
@@ -338,7 +340,7 @@ The plugin itself extends Payload's built-in `shouldBreakOutOnEnter` Slate funct
|
|
|
338
340
|
|
|
339
341
|
If you are building your own custom Rich Text elements or leaves, you may benefit from importing the following types:
|
|
340
342
|
|
|
341
|
-
```
|
|
343
|
+
```ts
|
|
342
344
|
import type {
|
|
343
345
|
RichTextCustomElement,
|
|
344
346
|
RichTextCustomLeaf,
|
package/docs/fields/row.mdx
CHANGED
|
@@ -21,9 +21,11 @@ keywords: row, fields, config, configuration, documentation, Content Management
|
|
|
21
21
|
|
|
22
22
|
### Example
|
|
23
23
|
|
|
24
|
-
`collections/ExampleCollection.
|
|
25
|
-
```
|
|
26
|
-
{
|
|
24
|
+
`collections/ExampleCollection.ts`
|
|
25
|
+
```ts
|
|
26
|
+
import { CollectionConfig } from 'payload/types';
|
|
27
|
+
|
|
28
|
+
const ExampleCollection: CollectionConfig = {
|
|
27
29
|
slug: 'example-collection',
|
|
28
30
|
fields: [
|
|
29
31
|
{
|
package/docs/fields/select.mdx
CHANGED
|
@@ -57,10 +57,11 @@ Set to `true` if you'd like this field to be sortable within the Admin UI using
|
|
|
57
57
|
|
|
58
58
|
### Example
|
|
59
59
|
|
|
60
|
-
`collections/ExampleCollection.
|
|
60
|
+
`collections/ExampleCollection.ts`
|
|
61
|
+
```ts
|
|
62
|
+
import { CollectionConfig } from 'payload/types';
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
{
|
|
64
|
+
const ExampleCollection: CollectionConfig = {
|
|
64
65
|
slug: 'example-collection',
|
|
65
66
|
fields: [
|
|
66
67
|
{
|
package/docs/fields/tabs.mdx
CHANGED
|
@@ -34,9 +34,11 @@ Each tab has its own required `label` and `fields` array. You can also optionall
|
|
|
34
34
|
|
|
35
35
|
### Example
|
|
36
36
|
|
|
37
|
-
`collections/ExampleCollection.
|
|
38
|
-
```
|
|
39
|
-
{
|
|
37
|
+
`collections/ExampleCollection.ts`
|
|
38
|
+
```ts
|
|
39
|
+
import { CollectionConfig } from 'payload/types';
|
|
40
|
+
|
|
41
|
+
const ExampleCollection: CollectionConfig = {
|
|
40
42
|
slug: 'example-collection',
|
|
41
43
|
fields: [
|
|
42
44
|
{
|
package/docs/fields/text.mdx
CHANGED
|
@@ -46,9 +46,11 @@ Set this property to a string that will be used for browser autocomplete.
|
|
|
46
46
|
|
|
47
47
|
### Example
|
|
48
48
|
|
|
49
|
-
`collections/ExampleCollection.
|
|
50
|
-
```
|
|
51
|
-
{
|
|
49
|
+
`collections/ExampleCollection.ts`
|
|
50
|
+
```ts
|
|
51
|
+
import { CollectionConfig } from 'payload/types';
|
|
52
|
+
|
|
53
|
+
const ExampleCollection: CollectionConfig = {
|
|
52
54
|
slug: 'example-collection',
|
|
53
55
|
fields: [
|
|
54
56
|
{
|
package/docs/fields/textarea.mdx
CHANGED
|
@@ -46,9 +46,11 @@ Set this property to a string that will be used for browser autocomplete.
|
|
|
46
46
|
|
|
47
47
|
### Example
|
|
48
48
|
|
|
49
|
-
`collections/ExampleCollection.
|
|
50
|
-
```
|
|
51
|
-
{
|
|
49
|
+
`collections/ExampleCollection.ts`
|
|
50
|
+
```ts
|
|
51
|
+
import { CollectionConfig } from 'payload/types';
|
|
52
|
+
|
|
53
|
+
const ExampleCollection: CollectionConfig = {
|
|
52
54
|
slug: 'example-collection',
|
|
53
55
|
fields: [
|
|
54
56
|
{
|
package/docs/fields/ui.mdx
CHANGED
|
@@ -34,9 +34,11 @@ With this field, you can also inject custom `Cell` components that appear as add
|
|
|
34
34
|
|
|
35
35
|
### Example
|
|
36
36
|
|
|
37
|
-
`collections/ExampleCollection.
|
|
38
|
-
```
|
|
39
|
-
{
|
|
37
|
+
`collections/ExampleCollection.ts`
|
|
38
|
+
```ts
|
|
39
|
+
import { CollectionConfig } from 'payload/types';
|
|
40
|
+
|
|
41
|
+
const ExampleCollection: CollectionConfig = {
|
|
40
42
|
slug: 'example-collection',
|
|
41
43
|
fields: [
|
|
42
44
|
{
|
package/docs/fields/upload.mdx
CHANGED
|
@@ -46,9 +46,11 @@ keywords: upload, images media, fields, config, configuration, documentation, Co
|
|
|
46
46
|
|
|
47
47
|
### Example
|
|
48
48
|
|
|
49
|
-
`collections/ExampleCollection.
|
|
50
|
-
```
|
|
51
|
-
{
|
|
49
|
+
`collections/ExampleCollection.ts`
|
|
50
|
+
```ts
|
|
51
|
+
import { CollectionConfig } from 'payload/types';
|
|
52
|
+
|
|
53
|
+
const ExampleCollection: CollectionConfig = {
|
|
52
54
|
slug: 'example-collection',
|
|
53
55
|
fields: [
|
|
54
56
|
{
|
|
@@ -22,7 +22,7 @@ By default, the Payload config lives in the root folder of your code and is name
|
|
|
22
22
|
A Collection represents a type of content that Payload will store and can contain many documents.
|
|
23
23
|
</Banner>
|
|
24
24
|
|
|
25
|
-
Collections define the shape of your data as well as all functionalities attached to that data. They will contain or many "documents", all corresponding with the same fields and functionalities that you define.
|
|
25
|
+
Collections define the shape of your data as well as all functionalities attached to that data. They will contain one or many "documents", all corresponding with the same fields and functionalities that you define.
|
|
26
26
|
|
|
27
27
|
They can represent anything you can store in a database - for example - pages, posts, users, people, orders, categories, events, customers, transactions, and anything else your app needs.
|
|
28
28
|
|
|
@@ -33,7 +33,7 @@ Both `graphQL.queries` and `graphQL.mutations` functions should return an object
|
|
|
33
33
|
|
|
34
34
|
`payload.config.js`:
|
|
35
35
|
|
|
36
|
-
```
|
|
36
|
+
```ts
|
|
37
37
|
import { buildConfig } from 'payload/config';
|
|
38
38
|
import myCustomQueryResolver from './graphQL/resolvers/myCustomQueryResolver';
|
|
39
39
|
|
|
@@ -29,8 +29,10 @@ At the top of your Payload config you can define all the options to manage Graph
|
|
|
29
29
|
|
|
30
30
|
Everything that can be done to a Collection via the REST or Local API can be done with GraphQL (outside of uploading files, which is REST-only). If you have a collection as follows:
|
|
31
31
|
|
|
32
|
-
```
|
|
33
|
-
|
|
32
|
+
```ts
|
|
33
|
+
import { CollectionConfig } from 'payload/types';
|
|
34
|
+
|
|
35
|
+
const PublicUser: CollectionConfig = {
|
|
34
36
|
slug: 'public-users',
|
|
35
37
|
auth: true, // Auth is enabled
|
|
36
38
|
labels: {
|
|
@@ -70,8 +72,10 @@ const PublicUser = {
|
|
|
70
72
|
|
|
71
73
|
Globals are also fully supported. For example:
|
|
72
74
|
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
+
```ts
|
|
76
|
+
import { GlobalConfig } from 'payload/types';
|
|
77
|
+
|
|
78
|
+
const Header: GlobalConfig = {
|
|
75
79
|
slug: 'header',
|
|
76
80
|
fields: [
|
|
77
81
|
...
|
|
@@ -30,10 +30,11 @@ Additionally, `auth`-enabled collections feature the following hooks:
|
|
|
30
30
|
|
|
31
31
|
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.
|
|
32
32
|
|
|
33
|
-
`collections/
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
`collections/exampleHooks.js`
|
|
34
|
+
```ts
|
|
35
|
+
import { CollectionConfig } from 'payload/types';
|
|
36
|
+
|
|
37
|
+
const ExampleHooks: CollectionConfig = {
|
|
37
38
|
slug: 'example-hooks',
|
|
38
39
|
fields: [
|
|
39
40
|
{ name: 'name', type: 'text'},
|
|
@@ -65,8 +66,10 @@ The `beforeOperation` Hook type can be used to modify the arguments that operati
|
|
|
65
66
|
|
|
66
67
|
Available Collection operations include `create`, `read`, `update`, `delete`, `login`, `refresh` and `forgotPassword`.
|
|
67
68
|
|
|
68
|
-
```
|
|
69
|
-
|
|
69
|
+
```ts
|
|
70
|
+
import { CollectionBeforeOperationHook } from 'payload/types';
|
|
71
|
+
|
|
72
|
+
const beforeOperationHook: CollectionBeforeOperationHook = async ({
|
|
70
73
|
args, // Original arguments passed into the operation
|
|
71
74
|
operation, // name of the operation
|
|
72
75
|
}) => {
|
|
@@ -78,8 +81,10 @@ const beforeOperationHook = async ({
|
|
|
78
81
|
|
|
79
82
|
Runs before the `create` and `update` operations. This hook allows you to add or format data before the incoming data is validated.
|
|
80
83
|
|
|
81
|
-
```
|
|
82
|
-
|
|
84
|
+
```ts
|
|
85
|
+
import { CollectionBeforeOperationHook } from 'payload/types';
|
|
86
|
+
|
|
87
|
+
const beforeValidateHook: CollectionBeforeValidateHook = async ({
|
|
83
88
|
data, // incoming data to update or create with
|
|
84
89
|
req, // full express request
|
|
85
90
|
operation, // name of the operation ie. 'create', 'update'
|
|
@@ -93,8 +98,10 @@ const beforeValidateHook = async ({
|
|
|
93
98
|
|
|
94
99
|
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.
|
|
95
100
|
|
|
96
|
-
```
|
|
97
|
-
|
|
101
|
+
```ts
|
|
102
|
+
import { CollectionBeforeChangeHook } from 'payload/types';
|
|
103
|
+
|
|
104
|
+
const beforeChangeHook: CollectionBeforeChangeHook = async ({
|
|
98
105
|
data, // incoming data to update or create with
|
|
99
106
|
req, // full express request
|
|
100
107
|
operation, // name of the operation ie. 'create', 'update'
|
|
@@ -108,8 +115,10 @@ const beforeChangeHook = async ({
|
|
|
108
115
|
|
|
109
116
|
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.
|
|
110
117
|
|
|
111
|
-
```
|
|
112
|
-
|
|
118
|
+
```ts
|
|
119
|
+
import { CollectionAfterChangeHook } from 'payload/types';
|
|
120
|
+
|
|
121
|
+
const afterChangeHook: CollectionAfterChangeHook = async ({
|
|
113
122
|
doc, // full document data
|
|
114
123
|
req, // full express request
|
|
115
124
|
operation, // name of the operation ie. 'create', 'update'
|
|
@@ -122,8 +131,10 @@ const afterChangeHook = async ({
|
|
|
122
131
|
|
|
123
132
|
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.
|
|
124
133
|
|
|
125
|
-
```
|
|
126
|
-
|
|
134
|
+
```ts
|
|
135
|
+
import { CollectionBeforeReadHook } from 'payload/types';
|
|
136
|
+
|
|
137
|
+
const beforeReadHook: CollectionBeforeReadHook = async ({
|
|
127
138
|
doc, // full document data
|
|
128
139
|
req, // full express request
|
|
129
140
|
query, // JSON formatted query
|
|
@@ -136,8 +147,10 @@ const beforeReadHook = async ({
|
|
|
136
147
|
|
|
137
148
|
Runs as the last step before documents are returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
|
|
138
149
|
|
|
139
|
-
```
|
|
140
|
-
|
|
150
|
+
```ts
|
|
151
|
+
import { CollectionAfterReadHook } from 'payload/types';
|
|
152
|
+
|
|
153
|
+
const afterReadHook: CollectionAfterReadHook = async ({
|
|
141
154
|
doc, // full document data
|
|
142
155
|
req, // full express request
|
|
143
156
|
query, // JSON formatted query
|
|
@@ -151,8 +164,10 @@ const afterReadHook = async ({
|
|
|
151
164
|
|
|
152
165
|
Runs before the `delete` operation. Returned values are discarded.
|
|
153
166
|
|
|
154
|
-
```
|
|
155
|
-
|
|
167
|
+
```ts
|
|
168
|
+
import { CollectionBeforeDeleteHook } from 'payload/types';
|
|
169
|
+
|
|
170
|
+
const beforeDeleteHook: CollectionBeforeDeleteHook = async ({
|
|
156
171
|
req, // full express request
|
|
157
172
|
id, // id of document to delete
|
|
158
173
|
}) => {...}
|
|
@@ -162,8 +177,10 @@ const beforeDeleteHook = async ({
|
|
|
162
177
|
|
|
163
178
|
Runs immediately after the `delete` operation removes records from the database. Returned values are discarded.
|
|
164
179
|
|
|
165
|
-
```
|
|
166
|
-
|
|
180
|
+
```ts
|
|
181
|
+
import { CollectionAfterDeleteHook } from 'payload/types';
|
|
182
|
+
|
|
183
|
+
const afterDeleteHook: CollectionAfterDeleteHook = async ({
|
|
167
184
|
req, // full express request
|
|
168
185
|
id, // id of document to delete
|
|
169
186
|
doc, // deleted document
|
|
@@ -174,8 +191,10 @@ const afterDeleteHook = async ({
|
|
|
174
191
|
|
|
175
192
|
For auth-enabled Collections, this hook runs after successful `login` operations. You can optionally modify the user that is returned.
|
|
176
193
|
|
|
177
|
-
```
|
|
178
|
-
|
|
194
|
+
```ts
|
|
195
|
+
import { CollectionBeforeLoginHook } from 'payload/types';
|
|
196
|
+
|
|
197
|
+
const beforeLoginHook: CollectionBeforeLoginHook = async ({
|
|
179
198
|
req, // full express request
|
|
180
199
|
user, // user being logged in
|
|
181
200
|
token, // user token
|
|
@@ -188,8 +207,10 @@ const beforeLoginHook = async ({
|
|
|
188
207
|
|
|
189
208
|
For auth-enabled Collections, this hook runs after successful `login` operations. You can optionally modify the user that is returned.
|
|
190
209
|
|
|
191
|
-
```
|
|
192
|
-
|
|
210
|
+
```ts
|
|
211
|
+
import { CollectionAfterLoginHook } from 'payload/types';
|
|
212
|
+
|
|
213
|
+
const afterLoginHook: CollectionAfterLoginHook = async ({
|
|
193
214
|
req, // full express request
|
|
194
215
|
}) => {...}
|
|
195
216
|
```
|
|
@@ -198,8 +219,10 @@ const afterLoginHook = async ({
|
|
|
198
219
|
|
|
199
220
|
For auth-enabled Collections, this hook runs after `logout` operations.
|
|
200
221
|
|
|
201
|
-
```
|
|
202
|
-
|
|
222
|
+
```ts
|
|
223
|
+
import { CollectionAfterLogoutHook } from 'payload/types';
|
|
224
|
+
|
|
225
|
+
const afterLogoutHook: CollectionAfterLogoutHook = async ({
|
|
203
226
|
req, // full express request
|
|
204
227
|
}) => {...}
|
|
205
228
|
```
|
|
@@ -208,8 +231,10 @@ const afterLogoutHook = async ({
|
|
|
208
231
|
|
|
209
232
|
For auth-enabled Collections, this hook runs after `refresh` operations.
|
|
210
233
|
|
|
211
|
-
```
|
|
212
|
-
|
|
234
|
+
```ts
|
|
235
|
+
import { CollectionAfterRefreshHook } from 'payload/types';
|
|
236
|
+
|
|
237
|
+
const afterRefreshHook: CollectionAfterRefreshHook = async ({
|
|
213
238
|
req, // full express request
|
|
214
239
|
res, // full express response
|
|
215
240
|
token, // newly refreshed user token
|
|
@@ -220,8 +245,10 @@ const afterRefreshHook = async ({
|
|
|
220
245
|
|
|
221
246
|
For auth-enabled Collections, this hook runs after `me` operations.
|
|
222
247
|
|
|
223
|
-
```
|
|
224
|
-
|
|
248
|
+
```ts
|
|
249
|
+
import { CollectionAfterMeHook } from 'payload/types';
|
|
250
|
+
|
|
251
|
+
const afterMeHook: CollectionAfterMeHook = async ({
|
|
225
252
|
req, // full express request
|
|
226
253
|
response, // response to return
|
|
227
254
|
}) => {...}
|
|
@@ -231,8 +258,10 @@ const afterMeHook = async ({
|
|
|
231
258
|
|
|
232
259
|
For auth-enabled Collections, this hook runs after successful `forgotPassword` operations. Returned values are discarded.
|
|
233
260
|
|
|
234
|
-
```
|
|
235
|
-
|
|
261
|
+
```ts
|
|
262
|
+
import { CollectionAfterForgotPasswordHook } from 'payload/types';
|
|
263
|
+
|
|
264
|
+
const afterLoginHook: CollectionAfterForgotPasswordHook = async ({
|
|
236
265
|
req, // full express request
|
|
237
266
|
user, // user being logged in
|
|
238
267
|
token, // user token
|
|
@@ -245,7 +274,7 @@ const afterLoginHook = async ({
|
|
|
245
274
|
|
|
246
275
|
Payload exports a type for each Collection hook which can be accessed as follows:
|
|
247
276
|
|
|
248
|
-
```
|
|
277
|
+
```ts
|
|
249
278
|
import type {
|
|
250
279
|
CollectionBeforeOperationHook,
|
|
251
280
|
CollectionBeforeValidateHook,
|
|
@@ -262,7 +291,4 @@ import type {
|
|
|
262
291
|
CollectionAfterMeHook,
|
|
263
292
|
CollectionAfterForgotPasswordHook,
|
|
264
293
|
} from 'payload/types';
|
|
265
|
-
|
|
266
|
-
// Use hook types here...
|
|
267
|
-
}
|
|
268
294
|
```
|
package/docs/hooks/fields.mdx
CHANGED
|
@@ -26,8 +26,10 @@ Field-level hooks offer incredible potential for encapsulating your logic. They
|
|
|
26
26
|
## Config
|
|
27
27
|
|
|
28
28
|
Example field configuration:
|
|
29
|
-
```
|
|
30
|
-
{
|
|
29
|
+
```ts
|
|
30
|
+
import { CollectionConfig } from 'payload/types';
|
|
31
|
+
|
|
32
|
+
const ExampleCollection: CollectionConfig = {
|
|
31
33
|
name: 'name',
|
|
32
34
|
type: 'text',
|
|
33
35
|
// highlight-start
|
|
@@ -77,7 +79,7 @@ All field hooks can optionally modify the return value of the field before the o
|
|
|
77
79
|
|
|
78
80
|
Payload exports a type for field hooks which can be accessed and used as follows:
|
|
79
81
|
|
|
80
|
-
```
|
|
82
|
+
```ts
|
|
81
83
|
import type { FieldHook } from 'payload/types';
|
|
82
84
|
|
|
83
85
|
// Field hook type is a generic that takes three arguments:
|
package/docs/hooks/globals.mdx
CHANGED
|
@@ -19,9 +19,10 @@ Globals feature the ability to define the following hooks:
|
|
|
19
19
|
All Global Hook properties accept arrays of synchronous or asynchronous functions. Each Hook type receives specific arguments and has the ability to modify specific outputs.
|
|
20
20
|
|
|
21
21
|
`globals/example-hooks.js`
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
```ts
|
|
23
|
+
import { GlobalConfig } from 'payload/types';
|
|
24
|
+
|
|
25
|
+
const ExampleHooks: GlobalConfig = {
|
|
25
26
|
slug: 'header',
|
|
26
27
|
fields: [
|
|
27
28
|
{ name: 'title', type: 'text'},
|
|
@@ -40,8 +41,10 @@ module.exports = {
|
|
|
40
41
|
|
|
41
42
|
Runs before the `update` operation. This hook allows you to add or format data before the incoming data is validated.
|
|
42
43
|
|
|
43
|
-
```
|
|
44
|
-
|
|
44
|
+
```ts
|
|
45
|
+
import { GlobalBeforeValidateHook } from 'payload/types'
|
|
46
|
+
|
|
47
|
+
const beforeValidateHook: GlobalBeforeValidateHook = async ({
|
|
45
48
|
data, // incoming data to update or create with
|
|
46
49
|
req, // full express request
|
|
47
50
|
originalDoc, // original document
|
|
@@ -54,8 +57,10 @@ const beforeValidateHook = async ({
|
|
|
54
57
|
|
|
55
58
|
Immediately following validation, `beforeChange` hooks will run within the `update` operation. 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.
|
|
56
59
|
|
|
57
|
-
```
|
|
58
|
-
|
|
60
|
+
```ts
|
|
61
|
+
import { GlobalBeforeChangeHook } from 'payload/types'
|
|
62
|
+
|
|
63
|
+
const beforeChangeHook: GlobalBeforeChangeHook = async ({
|
|
59
64
|
data, // incoming data to update or create with
|
|
60
65
|
req, // full express request
|
|
61
66
|
originalDoc, // original document
|
|
@@ -68,8 +73,10 @@ const beforeChangeHook = async ({
|
|
|
68
73
|
|
|
69
74
|
After a global is updated, the `afterChange` hook runs. Use this hook to purge caches of your applications, sync site data to CRMs, and more.
|
|
70
75
|
|
|
71
|
-
```
|
|
72
|
-
|
|
76
|
+
```ts
|
|
77
|
+
import { GlobalAfterChangeHook } from 'payload/types'
|
|
78
|
+
|
|
79
|
+
const afterChangeHook: GlobalAfterChangeHook = async ({
|
|
73
80
|
doc, // full document data
|
|
74
81
|
req, // full express request
|
|
75
82
|
}) => {
|
|
@@ -81,8 +88,10 @@ const afterChangeHook = async ({
|
|
|
81
88
|
|
|
82
89
|
Runs before `findOne` global operation is 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.
|
|
83
90
|
|
|
84
|
-
```
|
|
85
|
-
|
|
91
|
+
```ts
|
|
92
|
+
import { GlobalBeforeReadHook } from 'payload/types'
|
|
93
|
+
|
|
94
|
+
const beforeReadHook: GlobalBeforeReadHook = async ({
|
|
86
95
|
doc, // full document data
|
|
87
96
|
req, // full express request
|
|
88
97
|
}) => {...}
|
|
@@ -92,8 +101,10 @@ const beforeReadHook = async ({
|
|
|
92
101
|
|
|
93
102
|
Runs as the last step before a global is returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
|
|
94
103
|
|
|
95
|
-
```
|
|
96
|
-
|
|
104
|
+
```ts
|
|
105
|
+
import { GlobalAfterReadHook } from 'payload/types'
|
|
106
|
+
|
|
107
|
+
const afterReadHook: GlobalAfterReadHook = async ({
|
|
97
108
|
doc, // full document data
|
|
98
109
|
req, // full express request
|
|
99
110
|
findMany, // boolean to denote if this hook is running against finding one, or finding many (useful in versions)
|
|
@@ -104,7 +115,7 @@ const afterReadHook = async ({
|
|
|
104
115
|
|
|
105
116
|
Payload exports a type for each Global hook which can be accessed as follows:
|
|
106
117
|
|
|
107
|
-
```
|
|
118
|
+
```ts
|
|
108
119
|
import type {
|
|
109
120
|
GlobalBeforeValidateHook,
|
|
110
121
|
GlobalBeforeChangeHook,
|
|
@@ -112,7 +123,4 @@ import type {
|
|
|
112
123
|
GlobalBeforeReadHook,
|
|
113
124
|
GlobalAfterReadHook,
|
|
114
125
|
} from 'payload/types';
|
|
115
|
-
|
|
116
|
-
// Use hook types here...
|
|
117
|
-
}
|
|
118
126
|
```
|
|
@@ -28,10 +28,11 @@ You can gain access to the currently running `payload` object via two ways:
|
|
|
28
28
|
You can import or require `payload` into your own files after it's been initialized, but you need to make sure that your `import` / `require` statements come **after** you call `payload.init()`—otherwise Payload won't have been initialized yet. That might be obvious. To us, it's usually not.
|
|
29
29
|
|
|
30
30
|
Example:
|
|
31
|
-
```
|
|
31
|
+
```ts
|
|
32
32
|
import payload from 'payload';
|
|
33
|
+
import { CollectionAfterChangeHook } from 'payload/types';
|
|
33
34
|
|
|
34
|
-
const afterChangeHook = async () => {
|
|
35
|
+
const afterChangeHook: CollectionAfterChangeHook = async () => {
|
|
35
36
|
const posts = await payload.find({
|
|
36
37
|
collection: 'posts',
|
|
37
38
|
});
|
|
@@ -43,8 +44,8 @@ const afterChangeHook = async () => {
|
|
|
43
44
|
Payload is available anywhere you have access to the Express `req` - including within your access control and hook functions.
|
|
44
45
|
|
|
45
46
|
Example:
|
|
46
|
-
```
|
|
47
|
-
const afterChangeHook = async ({ req: { payload }}) => {
|
|
47
|
+
```ts
|
|
48
|
+
const afterChangeHook: CollectionAfterChangeHook = async ({ req: { payload }}) => {
|
|
48
49
|
const posts = await payload.find({
|
|
49
50
|
collection: 'posts',
|
|
50
51
|
});
|
|
@@ -319,3 +320,25 @@ const result = await payload.updateGlobal({
|
|
|
319
320
|
showHiddenFields: true,
|
|
320
321
|
})
|
|
321
322
|
```
|
|
323
|
+
|
|
324
|
+
## TypeScript
|
|
325
|
+
|
|
326
|
+
Local API calls also support passing in a generic. This is especially useful if you generate your TS types using a [generate types script](/docs/typescript/generate-types).
|
|
327
|
+
|
|
328
|
+
Here is an example of usage:
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
// Our generated types
|
|
332
|
+
import { Post } from './payload-types'
|
|
333
|
+
|
|
334
|
+
// Add Post types as generic to create function
|
|
335
|
+
const post: Post = await payload.create<Post>({
|
|
336
|
+
collection: 'posts',
|
|
337
|
+
|
|
338
|
+
// Data will now be typed as Post and give you type hints
|
|
339
|
+
data: {
|
|
340
|
+
title: 'my title',
|
|
341
|
+
description: 'my description',
|
|
342
|
+
},
|
|
343
|
+
})
|
|
344
|
+
```
|