Production Guides & Recipes
Audit options
Section titled “Audit options”| Option | Default | Behavior |
|---|---|---|
collections | "all" | All collections or an array of slugs. |
exclude | [] | Slugs to skip. Unknown requested or excluded slugs throw at config time. |
operations | ["create", "update", "delete", "auth"] | Enabled operations; add "read" to audit reads. |
logger | SDK default logger | Custom logger with an info method. |
The plugin runs at order 1000 and appends hooks after existing hooks. Writes log once per document. Reads are off by default; when enabled, each request produces one list summary or single-read event. Auth auditing is on by default for auth collections; failed logins are not logged. Logging failures do not interrupt operations. Events contain identifiers and counts, never document bodies, emails, tokens, or other credentials.
Events
Section titled “Events”payload.document.createdpayload.document.updatedpayload.document.deletedpayload.documents.listedpayload.document.readpayload.auth.logged_inpayload.auth.password_reset_requestedpayload.auth.password_reset
Write event IDs may be absent. List events include a count rather than document contents or IDs.
Boundaries
Section titled “Boundaries”Payload creates its implicit default users collection after plugins run. Declare the auth collection explicitly to audit its auth and write events.
Audit entries and transactions
Section titled “Audit entries and transactions”With afterCommitPlugin() installed (nexloadPreset() installs it by default), payload.document.created, payload.document.updated and payload.document.deleted are written after the transaction commits. A write that rolls back produces no entry. Without the plugin, write hooks emit events before the commit, so a later rollback can leave an event for a change that did not persist. A request with no transaction writes its entry at once. Read and auth entries are always written at once.
Migrate from logOperation
Section titled “Migrate from logOperation”logOperation is deprecated. Remove per-collection hooks and register the plugin once:
// Before: hooks: { afterChange: [logOperation("afterChange")] }import { auditPlugin } from "@nexload-sdk/payload-hooks";
const plugins = [auditPlugin()];The preset also includes auditPlugin by default; do not register it twice. See Payload Preset.
Run side effects after commit
Section titled “Run side effects after commit”Register afterCommitPlugin() once in your Payload config, then call runAfterCommit(req, key, fn) from any hook or endpoint that holds req:
import { afterCommitPlugin, runAfterCommit } from "@nexload-sdk/payload-hooks";
const plugins = [afterCommitPlugin()];
// Inside a collection hook:afterChange: [ ({ doc, req }) => { runAfterCommit(req, `sms:order:${doc.id}`, () => sms.enqueue(orderShipped(doc)), ); },];Keep callbacks short (enqueue work rather than sending it directly), because commitTransaction awaits each callback sequentially before resolving.
- Plugin required: Calling
runAfterCommitwithoutafterCommitPlugin()installed throwsError("[payload-hooks] runAfterCommit requires afterCommitPlugin()"). - Transaction lifecycle: Callbacks wait for
commitTransaction, are deduplicated bykeywithin a single transaction (last wins, first position kept), and are dropped onrollbackTransaction, commit failure, rejectedtransactionIDpromise, ordb.destroy(). Registration order across mixedPromiseand value transaction IDs follows resolution order. - No transaction: Falsy
req.transactionID(or a promise resolving to a falsy value) runsfnon the next microtask via the safe runner. Because there is no transaction to wait on, registerrunAfterCommitonly after the write succeeds (afterChange/afterDeleteor after the Local API call returns), never inbeforeChange/beforeValidate. - Nested calls during flush: Nested registrations of new keys made with the same
reqjoin the active flush in order (not-yet-started keys are replaced in place) and are awaited beforecommitTransactionresolves; re-registering a key that already started during the same flush is ignored. Use the originalreqinside a callback only to register more callbacks; for new writes create a fresh request (or omitreq), becausereq.transactionIDstill names the committed transaction and a write that reuses it is not rolled back on failure. - Registration during commit: A registration made while the adapter is still committing joins the same queue; it runs if the commit succeeds and is dropped if it fails.
- Destroy and repeated installs:
db.destroy()clears pending callbacks and stops a flush in progress after its current callback. Listing the plugin twice installs it once; the firstloggerwins. - Error isolation: Callback errors are caught and logged at
errorlevel (payload-hooks.after_commit.callback_failedwithkeyanderr) without failing the commit or stopping later callbacks. - Caveats: Callbacks live in memory (a crash between commit and execution loses them; use an outbox for durability), and Payload’s Drizzle adapters (3.86) swallow a failed
COMMITso callbacks still run in that rare case.