what-works-global

payload utilities

by what-works-global
payload-packages
Testing & developer experience
v3
>=3.29.0 <4

Overview

Summary generated from the README

A utility library for Payload 3 developers. It provides a JSON schema function that removes unresolved ID variants from relationship types, plus helpers that walk a document against its collection schema, flatten it into rows, or return a transformed clone. Bundled field resolvers handle relationships, Lexical rich text and uploads. Requires Payload 3.29 or later.

Features

  • resolveJsonSchemaRelationships rewrites generated types so relationships are always the full document type
  • traverseDocument calls a callback for each field and value, recursing into arrays and groups
  • flattenDocument returns a schema-ordered flat list of fields and values with optional excluded fields
  • transformDocument returns a deep clone with field resolvers applied
  • Built-in resolvers for relationship titles, Lexical plaintext and upload metadata
  • Custom field resolvers can be defined per field type

Use cases

  • Generate types that assume documents are fetched with full depth
  • Serialize documents for export, indexing or preview generation
  • Convert rich text and relationships into plain values before sending data elsewhere

From the author

A collection of utilities for Payload 3.0

Installation

npm i @whatworks/payload-utilities

16

Stars

1

Forks

2

Open Issues

3 days ago

Last Updated

48

Weekly Downloads

v3.0.1

npm Version

83.4 KB

Unpacked Size

0

Dependencies

Repository stars

16+1 since Sep 7

Show data
Repository stars by day (UTC)
DateStars
Dec 6, 202616
Dec 5, 202616
Dec 4, 202616
Dec 3, 202616
Dec 2, 202616
Dec 1, 202616
Nov 30, 202616
Nov 29, 202616
Nov 28, 202616
Nov 27, 202616
Nov 26, 202616
Nov 25, 202616
Nov 24, 202616
Nov 23, 202616
Nov 22, 202616
Nov 21, 202616
Nov 20, 202616
Nov 19, 202616
Nov 18, 202616
Nov 17, 202615
Nov 16, 202615
Nov 15, 202615
Nov 14, 202615
Nov 13, 202615
Nov 12, 202615
Nov 11, 202615
Nov 10, 202615
Nov 9, 202615
Nov 8, 202615
Nov 7, 202615
Nov 6, 202615
Nov 5, 202615
Nov 4, 202615
Nov 3, 202615
Nov 2, 202615
Nov 1, 202615
Oct 31, 202615
Oct 30, 202615
Oct 29, 202615
Oct 28, 202615
Oct 27, 202615
Oct 26, 202615
Oct 25, 202615
Oct 24, 202615
Oct 23, 202615
Oct 22, 202615
Oct 21, 202615
Oct 20, 202615
Oct 19, 202615
Oct 18, 202615
Oct 17, 202615
Oct 16, 202615
Oct 15, 202615
Oct 14, 202615
Oct 13, 202615
Oct 12, 202615
Oct 11, 202615
Oct 10, 202615
Oct 9, 202615
Oct 8, 202615
Oct 7, 202615
Oct 6, 202615
Oct 5, 202615
Oct 4, 202615
Oct 3, 202615
Oct 2, 202615
Oct 1, 202615
Sep 30, 202615
Sep 29, 202615
Sep 28, 202615
Sep 27, 202615
Sep 26, 202615
Sep 25, 202615
Sep 24, 202615
Sep 23, 202615
Sep 22, 202615
Sep 21, 202615
Sep 20, 202615
Sep 19, 202615
Sep 18, 202615
Sep 17, 202615
Sep 16, 202615
Sep 15, 202615
Sep 14, 202615
Sep 13, 202615
Sep 12, 202615
Sep 11, 202615
Sep 10, 202615
Sep 9, 202615
Sep 8, 202615
Sep 7, 202615

Shared by 12 plugins in what-works-global/payload-packages.

npm weekly downloads

48+39 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202648
Dec 5, 202687
Dec 4, 202690
Dec 3, 2026112
Dec 2, 2026141
Dec 1, 2026172
Nov 30, 2026173
Nov 29, 2026156
Nov 28, 2026115
Nov 27, 2026107
Nov 26, 2026145
Nov 25, 2026154
Nov 24, 2026166
Nov 23, 2026165
Nov 22, 2026190
Nov 21, 2026190
Nov 20, 2026277
Nov 19, 2026245
Nov 18, 2026320
Nov 17, 2026281
Nov 16, 2026290
Nov 15, 2026288
Nov 14, 2026296
Nov 13, 2026241
Nov 12, 2026219
Nov 11, 2026138
Nov 10, 2026139
Nov 9, 2026131
Nov 8, 2026105
Nov 7, 202693
Nov 6, 202658
Nov 5, 202662
Nov 4, 202644
Nov 3, 202638
Nov 2, 202640
Nov 1, 202644
Oct 31, 202638
Oct 30, 202647
Oct 29, 202637
Oct 28, 202617
Oct 27, 202629
Oct 26, 202627
Oct 25, 202628
Oct 24, 202627
Oct 23, 202618
Oct 22, 202621
Oct 21, 202627
Oct 20, 202617
Oct 19, 202618
Oct 18, 202615
Oct 17, 202617
Oct 16, 202620
Oct 15, 202622
Oct 14, 202628
Oct 13, 202628
Oct 12, 202631
Oct 11, 202634
Oct 10, 202644
Oct 9, 2026218
Oct 8, 2026213
Oct 7, 2026201
Oct 6, 2026199
Oct 5, 2026194
Oct 4, 2026189
Oct 3, 2026183
Oct 2, 20266
Oct 1, 202618
Sep 30, 202621
Sep 29, 202621
Sep 28, 202621
Sep 27, 202624
Sep 26, 202621
Sep 25, 202621
Sep 24, 202610
Sep 23, 202615
Sep 22, 202616
Sep 21, 202621
Sep 20, 202621
Sep 19, 202624
Sep 18, 202626
Sep 17, 202626
Sep 16, 202620
Sep 15, 202619
Sep 14, 202614
Sep 13, 202611
Sep 12, 20266
Sep 11, 20265
Sep 10, 20268
Sep 9, 20267
Sep 8, 20268
Sep 7, 20269

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

Health Score

63/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/1064440892-payload-utilities.svg)](https://payloaddirectory.dev/plugins/1064440892-payload-utilities)
HTML
<a href="https://payloaddirectory.dev/plugins/1064440892-payload-utilities"><img src="https://payloaddirectory.dev/badge/1064440892-payload-utilities.svg" alt="Payload Directory"></a>
Updated Dec 2, 2026Created Sep 26, 2025Published Sep 30, 2026

README

Payload Utilities

Payload Utilities

 

A collection of utilities for Payload 3.0.

Contents

Installation

pnpm add @whatworks/payload-utilities

Exports

EntryPath
Root@whatworks/payload-utilities
Document traversal@whatworks/payload-utilities/traverseDocument

resolveJsonSchemaRelationships

A JsonSchemaFunction for config.typescript.schema that rewrites generated types under the assumption that documents are always fetched with full depth. Strips the unresolved string (ID) variant from relationship unions so string | Page becomes Page and (string | Page)[] becomes Page[]. Null is preserved.

import { buildConfig } from 'payload'
import { resolveJsonSchemaRelationships } from '@whatworks/payload-utilities'

export default buildConfig({
  typescript: {
    schema: [resolveJsonSchemaRelationships],
  },
})

traverseDocument

Walks a document against its sanitized collection schema, invoking a callback for each field/value pair. Recurses into array and group fields. Returning a truthy value from the callback short-circuits traversal. Sync callbacks return void; async callbacks return Promise<void>.

import { traverseDocument } from '@whatworks/payload-utilities/traverseDocument'

await traverseDocument({
  collection,
  doc,
  req,
  callback: ({ field, schemaPathSegments, value }) => {
    console.log(schemaPathSegments.map((s) => s.name).join('.'), field.type, value)
  },
})

Callback args:

  • field — the matched Field from the schema map.
  • schemaPathSegments — { name, label }[] describing the path through the schema.
  • indexPathSegments — same as schemaPathSegments but includes array indices.
  • siblingData — the parent object the value lives on.
  • schemaMap — the resolved Payload field schema map.
  • value — the field's current value.

flattenDocument

Returns a flat, schema-ordered array of { field, schemaPathSegments, indexPathSegments, value } for every visited field. Optionally applies fieldResolvers to transform the stored value. Useful for serializing documents (e.g. exporting, indexing, generating previews).

import {
  flattenDocument,
  relationshipTitleResolver,
  richTextPlaintextResolver,
} from '@whatworks/payload-utilities/traverseDocument'

const rows = await flattenDocument({
  collection,
  doc,
  req,
  excludedFields: ['internalNotes'],
  fieldResolvers: {
    relationship: relationshipTitleResolver,
    richText: richTextPlaintextResolver(),
  },
})

transformDocument

Returns a deep-cloned copy of the document with any fieldResolvers applied in place. Resolvers returning undefined leave the value untouched; child paths win over parent paths when both resolve.

import {
  transformDocument,
  uploadMetadataResolver,
} from '@whatworks/payload-utilities/traverseDocument'

const transformed = await transformDocument({
  collection,
  doc,
  req,
  fieldResolvers: {
    upload: uploadMetadataResolver,
  },
})

Field resolvers

A FieldResolver<T> receives the field, its value, sibling data, and the current request, and returns a replacement value (or undefined to keep the original). Three are bundled:

ResolverField typeBehavior
relationshipTitleResolverrelationshipResolves to the referenced document's admin.useAsTitle value. Falls back to the populated value on the doc, then the ID, then the original value. Handles polymorphic and hasMany relationships.
richTextPlaintextResolver({ converters? })richTextConverts a Lexical value to plain text via @payloadcms/richtext-lexical/plaintext. Accepts optional custom PlaintextConverters.
uploadMetadataResolveruploadResolves to { id, filename, filesize, mimeType, url } from the referenced upload document. Uses inline metadata when complete, otherwise fetches it.

Define your own by typing the field key:

import type { FieldResolver } from '@whatworks/payload-utilities/traverseDocument'

const numberResolver: FieldResolver<'number'> = ({ value }) =>
  typeof value === 'number' ? value.toFixed(2) : undefined

Similar plugins