Files
ipal-kit/src/modules/shared/versionLimit.ts
T

20 lines
1.1 KiB
TypeScript

/**
* The plugin's own collections and globals turn versions on nowhere. When a project turns them on (globalOverrides,
* collectionOverrides, formOverrides…) without a limit, each keeps at most 20 versions per document (1.18.0;
* docs/performance.md §6) instead of Payload's 100 — a global like Site integrations is saved by the site itself on
* every status change. A limit the project writes wins (`max` for globals, `maxPerDoc` for collections, 0 =
* unlimited). Payload trims the oldest versions of a document on its next save.
*/
export const PLUGIN_VERSION_LIMIT = 20
type Versioned = { slug: string; versions?: unknown }
export function withVersionLimit<T extends Versioned>(entity: T, kind: 'collection' | 'global', limit = PLUGIN_VERSION_LIMIT): T {
const v = entity.versions
if (!v) {return entity}
const key = kind === 'global' ? 'max' : 'maxPerDoc'
if (v === true) {return { ...entity, versions: { [key]: limit } }}
if (typeof v === 'object' && typeof (v as Record<string, unknown>)[key] !== 'number') {return { ...entity, versions: { ...(v), [key]: limit } }}
return entity
}