payload 1.0.27 → 1.0.28
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 +18 -0
- 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 +13 -7
- 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/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/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
|
@@ -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
|
+
```
|
|
@@ -83,11 +83,10 @@ After all plugins are executed, the full config with all plugins will be sanitiz
|
|
|
83
83
|
|
|
84
84
|
Here is an example for how to automatically add a `lastModifiedBy` field to all Payload collections using a Plugin written in TypeScript.
|
|
85
85
|
|
|
86
|
-
```
|
|
87
|
-
import { Config } from 'payload/config';
|
|
88
|
-
import { CollectionConfig } from 'payload/dist/collections/config/types';
|
|
86
|
+
```ts
|
|
87
|
+
import { Config, Plugin } from 'payload/config';
|
|
89
88
|
|
|
90
|
-
const addLastModified = (incomingConfig: Config): Config => {
|
|
89
|
+
const addLastModified: Plugin = (incomingConfig: Config): Config => {
|
|
91
90
|
// Find all incoming auth-enabled collections
|
|
92
91
|
// so we can create a lastModifiedBy relationship field
|
|
93
92
|
// to all auth collections
|
|
@@ -37,7 +37,7 @@ Because _**you**_ are in complete control of who can do what with your data, you
|
|
|
37
37
|
Before running in Production, you need to have built a production-ready copy of the Payload Admin panel. To do this, Payload provides the `build` NPM script. You can use it by adding a `script` to your `package.json` file like this:
|
|
38
38
|
|
|
39
39
|
`package.json`:
|
|
40
|
-
```
|
|
40
|
+
```json
|
|
41
41
|
{
|
|
42
42
|
"name": "project-name-here",
|
|
43
43
|
"scripts": {
|
|
@@ -16,8 +16,10 @@ Payload provides an extremely granular querying language through all APIs. Each
|
|
|
16
16
|
|
|
17
17
|
For example, say you have a collection as follows:
|
|
18
18
|
|
|
19
|
-
```
|
|
20
|
-
|
|
19
|
+
```ts
|
|
20
|
+
import { CollectionConfig } from 'payload/types';
|
|
21
|
+
|
|
22
|
+
const Post: CollectionConfig = {
|
|
21
23
|
slug: 'posts',
|
|
22
24
|
fields: [
|
|
23
25
|
{
|
|
@@ -89,9 +89,11 @@ Each endpoint object needs to have:
|
|
|
89
89
|
|
|
90
90
|
Example:
|
|
91
91
|
|
|
92
|
-
```
|
|
92
|
+
```ts
|
|
93
|
+
import { CollectionConfig } from 'payload/types';
|
|
94
|
+
|
|
93
95
|
// a collection of 'orders' with an additional route for tracking details, reachable at /api/orders/:id/tracking
|
|
94
|
-
const Orders = {
|
|
96
|
+
const Orders: CollectionConfig = {
|
|
95
97
|
slug: 'orders',
|
|
96
98
|
fields: [ /* ... */ ],
|
|
97
99
|
// highlight-start
|
package/docs/upload/overview.mdx
CHANGED
|
@@ -55,38 +55,40 @@ _An asterisk denotes that a property above is required._
|
|
|
55
55
|
|
|
56
56
|
**Example Upload collection:**
|
|
57
57
|
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
```ts
|
|
59
|
+
import { CollectionConfig } from 'payload/types';
|
|
60
|
+
|
|
61
|
+
const Media: CollectionConfig = {
|
|
62
|
+
slug: 'media',
|
|
61
63
|
upload: {
|
|
62
|
-
staticURL:
|
|
63
|
-
staticDir:
|
|
64
|
+
staticURL: '/media',
|
|
65
|
+
staticDir: 'media',
|
|
64
66
|
imageSizes: [
|
|
65
67
|
{
|
|
66
|
-
name:
|
|
68
|
+
name: 'thumbnail',
|
|
67
69
|
width: 400,
|
|
68
70
|
height: 300,
|
|
69
|
-
crop:
|
|
71
|
+
crop: 'centre',
|
|
70
72
|
},
|
|
71
73
|
{
|
|
72
|
-
name:
|
|
74
|
+
name: 'card',
|
|
73
75
|
width: 768,
|
|
74
76
|
height: 1024,
|
|
75
|
-
crop:
|
|
77
|
+
crop: 'centre',
|
|
76
78
|
},
|
|
77
79
|
{
|
|
78
|
-
name:
|
|
80
|
+
name: 'tablet',
|
|
79
81
|
width: 1024,
|
|
80
82
|
// By specifying `null` or leaving a height undefined,
|
|
81
83
|
// the image will be sized to a certain width,
|
|
82
84
|
// but it will retain its original aspect ratio
|
|
83
85
|
// and calculate a height automatically.
|
|
84
86
|
height: null,
|
|
85
|
-
crop:
|
|
87
|
+
crop: 'centre',
|
|
86
88
|
},
|
|
87
89
|
],
|
|
88
|
-
adminThumbnail:
|
|
89
|
-
mimeTypes: [
|
|
90
|
+
adminThumbnail: 'thumbnail',
|
|
91
|
+
mimeTypes: ['image/*'],
|
|
90
92
|
},
|
|
91
93
|
};
|
|
92
94
|
```
|
|
@@ -97,17 +99,17 @@ Payload relies on the [`express-fileupload`](https://www.npmjs.com/package/expre
|
|
|
97
99
|
|
|
98
100
|
A common example of what you might want to customize within Payload-wide Upload options would be to increase the allowed `fileSize` of uploads sent to Payload:
|
|
99
101
|
|
|
100
|
-
```
|
|
101
|
-
import { buildConfig } from
|
|
102
|
+
```ts
|
|
103
|
+
import { buildConfig } from 'payload/config';
|
|
102
104
|
|
|
103
105
|
export default buildConfig({
|
|
104
106
|
collections: [
|
|
105
107
|
{
|
|
106
|
-
slug:
|
|
108
|
+
slug: 'media',
|
|
107
109
|
fields: [
|
|
108
110
|
{
|
|
109
|
-
name:
|
|
110
|
-
type:
|
|
111
|
+
name: 'alt',
|
|
112
|
+
type: 'text',
|
|
111
113
|
},
|
|
112
114
|
],
|
|
113
115
|
upload: true,
|
|
@@ -158,12 +160,14 @@ You can specify how Payload retrieves admin thumbnails for your upload-enabled C
|
|
|
158
160
|
|
|
159
161
|
**Example custom Admin thumbnail:**
|
|
160
162
|
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
|
|
163
|
+
```ts
|
|
164
|
+
import { CollectionConfig } from 'payload/types';
|
|
165
|
+
|
|
166
|
+
const Media: CollectionConfig = {
|
|
167
|
+
slug: 'media',
|
|
164
168
|
upload: {
|
|
165
|
-
staticURL:
|
|
166
|
-
staticDir:
|
|
169
|
+
staticURL: '/media',
|
|
170
|
+
staticDir: 'media',
|
|
167
171
|
imageSizes: [
|
|
168
172
|
// ... image sizes here
|
|
169
173
|
],
|
|
@@ -191,13 +195,15 @@ Some example values are: `image/*`, `audio/*`, `video/*`, `image/png`, `applicat
|
|
|
191
195
|
|
|
192
196
|
**Example mimeTypes usage:**
|
|
193
197
|
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
|
|
198
|
+
```ts
|
|
199
|
+
import { CollectionConfig } from 'payload/types';
|
|
200
|
+
|
|
201
|
+
const Media: CollectionConfig = {
|
|
202
|
+
slug: 'media',
|
|
197
203
|
upload: {
|
|
198
|
-
staticURL:
|
|
199
|
-
staticDir:
|
|
200
|
-
mimeTypes: [
|
|
204
|
+
staticURL: '/media',
|
|
205
|
+
staticDir: 'media',
|
|
206
|
+
mimeTypes: ['image/*', 'application/pdf'], // highlight-line
|
|
201
207
|
},
|
|
202
208
|
};
|
|
203
209
|
```
|
|
@@ -25,8 +25,10 @@ Collections and Globals both support the same options for configuring autosave.
|
|
|
25
25
|
|
|
26
26
|
**Example config with versions, drafts, and autosave enabled:**
|
|
27
27
|
|
|
28
|
-
```
|
|
29
|
-
|
|
28
|
+
```ts
|
|
29
|
+
import { CollectionConfig } from 'payload/types';
|
|
30
|
+
|
|
31
|
+
const Pages: CollectionConfig = {
|
|
30
32
|
slug: 'pages',
|
|
31
33
|
access: {
|
|
32
34
|
read: ({ req }) => {
|
package/docs/versions/drafts.mdx
CHANGED
|
@@ -81,8 +81,10 @@ You can use the `read` [Access Control](/docs/access-control/collections#read) m
|
|
|
81
81
|
|
|
82
82
|
Here is an example that utilizes the `_status` field to require a user to be logged in to retrieve drafts:
|
|
83
83
|
|
|
84
|
-
```
|
|
85
|
-
|
|
84
|
+
```ts
|
|
85
|
+
import { CollectionConfig } from 'payload/types';
|
|
86
|
+
|
|
87
|
+
const Pages: CollectionConfig = {
|
|
86
88
|
slug: 'pages',
|
|
87
89
|
access: {
|
|
88
90
|
read: ({ req }) => {
|
|
@@ -114,8 +116,10 @@ const Pages = {
|
|
|
114
116
|
|
|
115
117
|
Here is an example for how to write an access control function that grants access to both documents where `_status` is equal to "published" and where `_status` does not exist:
|
|
116
118
|
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
+
```ts
|
|
120
|
+
import { CollectionConfig } from 'payload/types';
|
|
121
|
+
|
|
122
|
+
const Pages: CollectionConfig = {
|
|
119
123
|
slug: 'pages',
|
|
120
124
|
access: {
|
|
121
125
|
read: ({ req }) => {
|
|
@@ -77,7 +77,7 @@ _slug_versions
|
|
|
77
77
|
|
|
78
78
|
Each document in this new `versions` collection will store a set of meta properties about the version as well as a _full_ copy of the document. For example, a version's data might look like this for a Collection document:
|
|
79
79
|
|
|
80
|
-
```
|
|
80
|
+
```json
|
|
81
81
|
{
|
|
82
82
|
"_id": "61cf752c19cdf1b1af7b61f1", // a unique ID of this version
|
|
83
83
|
"parent": "61ce1354091d5b3ffc20ea6e", // the ID of the parent document
|