docs: misc improvements to high-level docs (#7177)

This commit is contained in:
Jacob Fletcher
2024-07-16 15:47:56 -04:00
committed by GitHub
parent 2f0aa83a93
commit 2d96a1f0b6
18 changed files with 150 additions and 131 deletions

View File

@@ -89,9 +89,9 @@ Available Collection operations include `create`, `read`, `update`, `delete`, `l
import type { CollectionBeforeOperationHook } from 'payload'
const beforeOperationHook: CollectionBeforeOperationHook = async ({
args, // original arguments passed into the operation
operation, // name of the operation
req, // full Request object
args,
operation,
req,
}) => {
return args // return modified operation arguments as necessary
}

View File

@@ -8,7 +8,7 @@ keywords: hooks, context, payload context, payloadcontext, data, extra data, sha
The `context` object is used to share data across different Hooks. This persists throughout the entire lifecycle of a request and is available within every Hook. By setting properties to `req.context`, you can effectively logic across multiple Hooks.
## When to use Context
## When To Use Context
Context gives you a way forward on otherwise difficult problems such as:
@@ -17,11 +17,11 @@ Context gives you a way forward on otherwise difficult problems such as:
3. **Passing data to local API**: Setting values on the `req.context` and pass it to `payload.create()` you can provide additional data to hooks without adding extraneous fields.
4. **Passing data between hooks and middleware or custom endpoints**: Hooks could set context across multiple collections and then be used in a final `postMiddleware`.
## How to Use Context
## How To Use Context
Let's see examples on how context can be used in the first two scenarios mentioned above:
### Passing data between hooks
### Passing Data Between Hooks
To pass data between hooks, you can assign values to context in an earlier hook in the lifecycle of a request and expect it the context in a later hook.
@@ -58,7 +58,7 @@ const Customer: CollectionConfig = {
}
```
### Preventing infinite loops
### Preventing Infinite Loops
Let's say you have an `afterChange` hook, and you want to do a calculation inside the hook (as the document ID needed for the calculation is available in the `afterChange` hook, but not in the `beforeChange` hook). Once that's done, you want to update the document with the result of the calculation.

View File

@@ -58,11 +58,11 @@ Runs before the `update` operation. This hook allows you to add or format data b
import type { GlobalBeforeValidateHook } from 'payload'
const beforeValidateHook: GlobalBeforeValidateHook = async ({
data, // incoming data to update or create with
req, // full Request object
originalDoc, // original document
data,
req,
originalDoc,
}) => {
return data // Return data to update the document with
return data
}
```
@@ -84,11 +84,11 @@ Immediately following validation, `beforeChange` hooks will run within the `upda
import type { GlobalBeforeChangeHook } from 'payload'
const beforeChangeHook: GlobalBeforeChangeHook = async ({
data, // incoming data to update or create with
req, // full Request object
originalDoc, // original document
data,
req,
originalDoc,
}) => {
return data // Return data to update the document with
return data
}
```
@@ -110,9 +110,9 @@ After a global is updated, the `afterChange` hook runs. Use this hook to purge c
import type { GlobalAfterChangeHook } from 'payload'
const afterChangeHook: GlobalAfterChangeHook = async ({
doc, // full document data
previousDoc, // document data before updating the collection
req, // full Request object
doc,
previousDoc,
req,
}) => {
return data
}
@@ -136,8 +136,8 @@ Runs before `findOne` global operation is transformed for output by `afterRead`.
import type { GlobalBeforeReadHook } from 'payload'
const beforeReadHook: GlobalBeforeReadHook = async ({
doc, // full document data
req, // full Request object
doc,
req,
}) => {...}
```
@@ -158,9 +158,9 @@ Runs as the last step before a global is returned. Flattens locales, hides prote
import type { GlobalAfterReadHook } from 'payload'
const afterReadHook: GlobalAfterReadHook = async ({
doc, // full document data
req, // full Request object
findMany, // boolean to denote if this hook is running against finding one, or finding many (useful in versions)
doc,
req,
findMany,
}) => {...}
```

View File

@@ -2,13 +2,13 @@
title: Hooks Overview
label: Overview
order: 10
desc: Hooks allow you to add your own logic to Payload, including integrating with third-party APIs, adding auto-generated data, or modifing Payload's base functionality.
desc: Hooks allow you to add your own logic to Payload, including integrating with third-party APIs, adding auto-generated data, or modifying Payload's base functionality.
keywords: hooks, overview, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
---
Hooks allow you to execute your own side effects during specific events of the Document lifecycle. With Hooks, you can transform Payload from a traditional CMS into a fully-fledged application framework. They allow you to perform business logic, third-party integrations, etc. during precise moments within the data lifecycle.
Hooks allow you to execute your own side effects during specific events of the Document lifecycle. They allow you to do things like mutate data, perform business logic, integrate with third-parties, or anything else, all during precise moments within your application.
There are many use cases for Hooks, including:
With Hooks, you can transform Payload from a traditional CMS into a fully-fledged application framework. There are many use cases for Hooks, including:
- Modify data before it is read or updated
- Encrypt and decrypt sensitive data