JanRezzonico

payload plugin audit

by JanRezzonico
Logging & monitoring
v3
^3.93.0
MPL-2.0

Overview

Summary generated from the README

Adds an audit-log collection that records create and update operations on selected collections through afterChange hooks. Each log stores the acting user and a jsondiffpatch delta, and read-only virtual fields show the creator and update history in the admin panel. Requires listing audited collections and auth-enabled user collections. Deletes are not recorded, and the plugin is described as early development.

Features

  • Records create and update operations in a dedicated audit-log collection
  • Stores the responsible user via polymorphic relationships to one or more user collections
  • Calculates update deltas with jsondiffpatch and renders them with a DeltaViewer component
  • Adds virtual read-only created-by and update-history fields to audited collections
  • Optional scheduled cleanup of old update logs while keeping create logs
  • Configurable state, collection slug, field names, access rules, labels and date formatting

Use cases

  • Track which editor changed a document and what changed in a content team
  • Keep a retention-limited history of edits using a scheduled cleanup job
  • Show change history inside the admin panel without building custom views

From the author

Audit trail plugin for Payload CMS: records who created or updated documents, stores update deltas, and shows them in the admin panel.

Installation

npm i payload-plugin-audit

1

Stars

0

Forks

2

Open Issues

1 weeks ago

Last Updated

34

Weekly Downloads

v0.4.0

npm Version

155.7 KB

Unpacked Size

2

Dependencies

GitHub stars

1+1 since Nov 8

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20261
Dec 5, 20261
Dec 4, 20261
Dec 3, 20261
Dec 2, 20261
Dec 1, 20261
Nov 30, 20261
Nov 29, 20261
Nov 28, 20261
Nov 27, 20261
Nov 26, 20261
Nov 25, 20261
Nov 24, 20261
Nov 23, 20261
Nov 22, 20261
Nov 21, 20261
Nov 20, 20261
Nov 19, 20261
Nov 18, 20261
Nov 17, 20260
Nov 16, 20260
Nov 15, 20260
Nov 14, 20260
Nov 13, 20260
Nov 12, 20260
Nov 11, 20260
Nov 10, 20260
Nov 9, 20260
Nov 8, 20260
npm weekly downloads

40+40 since Nov 16

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202640
Dec 5, 202635
Dec 4, 202637
Dec 3, 202648
Dec 2, 202659
Dec 1, 202673
Nov 30, 2026238
Nov 29, 2026239
Nov 28, 2026263
Nov 27, 2026431
Nov 26, 2026439
Nov 25, 2026612
Nov 24, 2026862
Nov 23, 2026688
Nov 22, 2026675
Nov 21, 2026650
Nov 20, 2026478
Nov 19, 2026458
Nov 18, 2026270
Nov 17, 20260
Nov 16, 20260

Each point is npm's rolling 7-day download count.

Health Score

61/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1454329568-root.svg)](https://payloaddirectory.dev/plugins/1454329568-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1454329568-root"><img src="https://payloaddirectory.dev/badge/1454329568-root.svg" alt="Payload Directory"></a>
Updated Nov 22, 2026Created Nov 11, 2026Published Nov 21, 2026

README

Audit Plugin for Payload CMS

payload-plugin-audit adds an audit trail to Payload CMS collections. It records who created or updated a document, stores update deltas, and exposes the audit information in the Payload admin panel through read-only virtual fields.

Note: This plugin is in early development and may not be suitable for production use yet. Please report any issues you encounter. Until version 1.x.x, the plugin is not considered stable and may have breaking changes in minor releases. Please pin to a specific version if you want to avoid unexpected changes and manually upgrade after reviewing the release notes and changelog.

Features

  • Records create and update operations in a dedicated audit-log collection.
  • Stores the user responsible for each operation.
  • Calculates update deltas with jsondiffpatch.
  • Adds a virtual created by relationship field to audited collections.
  • Adds a virtual update history field containing the user, date, and delta for each update.
  • Renders update deltas in the admin panel with the included DeltaViewer component.
  • Supports polymorphic relationships to one or more user collections.
  • Allows the log collection, generated field names, access rules, labels, and colors to be customized.
  • Automatically removes old update logs on a configurable schedule while preserving create logs.

Installation

Install the plugin in your Payload application:

npm install payload-plugin-audit
# or
pnpm add payload-plugin-audit
# or
yarn add payload-plugin-audit

Basic Usage

Add the plugin to the plugins array in payload.config.ts:

import { auditPluginPayload } from "payload-plugin-audit";
import { buildConfig } from "payload";

export default buildConfig({
  collections: [Posts, Users],
  plugins: [
    auditPluginPayload({
      collections: ["posts"],
      userCollections: ["users"],
    }),
  ],
});

collections contains the slugs to audit. userCollections contains every auth-enabled collection whose users may create or update an audited document. Both arrays are required and cannot be empty.

After adding or changing the plugin configuration, regenerate Payload types and apply the database schema changes using your normal Payload workflow.

Generated Data

The plugin creates an audit-plugin-logs collection by default. Each log contains:

  • operation: create or update.
  • entity: a polymorphic relationship to the audited document.
  • user: a polymorphic relationship to the user who performed the operation.
  • delta: the jsondiffpatch result for updates.
  • Payload-managed createdAt and updatedAt timestamps.

For every audited collection, the plugin also adds these virtual fields:

  • auditPluginCreatedBy: resolves to the user who created the document.
  • auditPluginUpdateLogs: resolves to the document's update history, including the user, date, and delta for each update.

The generated fields are virtual and read-only.

Limitations

  • Audit logs are generated by afterChange hooks, so deletes are not currently recorded.
  • join fields are not tracked on collections. If you need to track changes to join fields, consider tracking the collection with the relationship field that the join field points to instead.

Configuration

State

Use state to control the plugin lifecycle:

state: 'enabled' // default: hooks, logs, and virtual fields are active
  • enabled: records audit logs and adds the virtual fields.
  • inactive: keeps the audit-log collection and virtual fields but stops creating new logs (disables the afterChange hooks).
  • disabled: leaves the Payload configuration unchanged and disables the plugin entirely.

Database names

The defaults can be overridden when a migration or an existing naming convention requires it:

db: {
  collections: {
    logs: {
      slug: 'my-custom-collection-slug',
      hidden: true,
    },
  },
  fields: {
    createdBy: { name: 'myCustomCreatedBy' },
    updateLogs: { name: 'myCustomUpdateLogs' },
  },
},

Changing collection slug or field names changes the database schema and generated Payload types, so keep it in mind and perform database migrations as needed.

Access control

access.read controls who can read audit logs and the generated virtual fields.

access.delete controls manual delete access on the audit-log collection.

Keep access.delete restrictive as users with it can remove parts of the audit trail.

Log cleanup

Old update logs can be removed by a scheduled Payload job. create logs are never removed by this job.

Cleanup is disabled by default and should be enabled manually with cleanup.enabled: true when a jobs runner is set up to handle it.

cleanup: {
  enabled: false,
  olderThanDays: 90,
  schedule: '0 0 * * *',
  queue: 'payload-plugin-audit',
  silent: false,
},
OptionDefaultDescription
enabledfalseRegisters the cleanup task and its schedule.
olderThanDays90update logs older than this many days are deleted.
schedule'0 0 * * *' (midnight)Cron expression, five or six fields (six adds seconds precision).
queue'payload-plugin-audit'Queue the job is added to.
silentfalseSuppresses cleanup logging.
You need a jobs runner

The plugin only queues the cleanup job. Something has to run the queue, otherwise the job just sits there. Pick one that fits your deployment, and use the same queue name as cleanup.queue:

Dedicated server, autoRun (handles both scheduling and running):

import { AUDIT_PLUGIN_QUEUE } from 'payload-plugin-audit'

jobs: {
  autoRun: [{ cron: '* * * * *', queue: AUDIT_PLUGIN_QUEUE }], // Or your own queue name provided in cleanup.queue
}

Dedicated server, bin script:

# For --queue use the same queue name as cleanup.queue. If not provided, the default queue name is `payload-plugin-audit` as provided via `AUDIT_PLUGIN_QUEUE`.
pnpm payload jobs:run --cron "* * * * *" --queue payload-plugin-audit --handle-schedules

Serverless (e.g. Vercel): use an external cron to call both endpoints:

For ?queue use the same queue name as cleanup.queue. If not provided, the default queue name is payload-plugin-audit as provided via AUDIT_PLUGIN_QUEUE.

GET /api/payload-jobs/handle-schedules?queue=payload-plugin-audit
GET /api/payload-jobs/run?queue=payload-plugin-audit

Don't combine autoRun with the handle-schedules bin script for the same queue, or jobs will be queued twice. Never use autoRun on serverless platforms.

Running the cleanup yourself

If you'd rather not use the built-in schedule, set cleanup.enabled: false and call the cleanup function from your own endpoint, script or task:

import { cleanupAuditLogs } from 'payload-plugin-audit'

await cleanupAuditLogs({
  collectionSlug: 'audit-logs', // your logs collection slug
  olderThanDays: 30,
  payload,
})

If cleanup.enabled is true, you can also trigger the registered task on demand:

import { AUDIT_CLEANUP_TASK_SLUG } from 'payload-plugin-audit'

await payload.jobs.queue({ task: AUDIT_CLEANUP_TASK_SLUG })

The cleanup bypasses collection access control, so it works even though access.delete blocks all manual deletes.

Admin UI

Labels and date formatting can be customized through the ui option.

The client components are also available from the payload-plugin-audit/client export, however, they are primarily intended for Payload admin component configuration and are already wired into the generated fields and audit-log collection.

Development

Prerequisites

  • Node.js 22+
  • Docker & Docker Compose
  • pnpm

Development Setup

pnpm install # install dependencies
pnpm docker:reload # start the database
pnpm dev # start the development server

Useful commands:

pnpm test # run tests
pnpm lint # run linter
pnpm build # build the plugin

License

This project is licensed under the Mozilla Public License 2.0. See the LICENSE file for details.

Similar plugins