Skip to content

Production Guides & Recipes

OptionDefaultBehavior
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.
loggerSDK default loggerCustom 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.

  • payload.document.created
  • payload.document.updated
  • payload.document.deleted
  • payload.documents.listed
  • payload.document.read
  • payload.auth.logged_in
  • payload.auth.password_reset_requested
  • payload.auth.password_reset

Write event IDs may be absent. List events include a count rather than document contents or IDs.

Payload creates its implicit default users collection after plugins run. Declare the auth collection explicitly to audit its auth and write events.

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.

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.

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 runAfterCommit without afterCommitPlugin() installed throws Error("[payload-hooks] runAfterCommit requires afterCommitPlugin()").
  • Transaction lifecycle: Callbacks wait for commitTransaction, are deduplicated by key within a single transaction (last wins, first position kept), and are dropped on rollbackTransaction, commit failure, rejected transactionID promise, or db.destroy(). Registration order across mixed Promise and value transaction IDs follows resolution order.
  • No transaction: Falsy req.transactionID (or a promise resolving to a falsy value) runs fn on the next microtask via the safe runner. Because there is no transaction to wait on, register runAfterCommit only after the write succeeds (afterChange/afterDelete or after the Local API call returns), never in beforeChange/beforeValidate.
  • Nested calls during flush: Nested registrations of new keys made with the same req join the active flush in order (not-yet-started keys are replaced in place) and are awaited before commitTransaction resolves; re-registering a key that already started during the same flush is ignored. Use the original req inside a callback only to register more callbacks; for new writes create a fresh request (or omit req), because req.transactionID still 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 first logger wins.
  • Error isolation: Callback errors are caught and logged at error level (payload-hooks.after_commit.callback_failed with key and err) 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 COMMIT so callbacks still run in that rare case.