Files
ipal-kit/src/modules/shared/purge.ts
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

30 lines
1.2 KiB
TypeScript

import type { BasePayload, Where } from 'payload'
const DAY_MS = 24 * 60 * 60 * 1000
/**
* Retention purge for a log collection (stats visits, consent records): at
* most once a day per process, and OFF the visitor's request.
*
* It used to be awaited inside the hit / consent request through
* `payload.delete`, which loads every expired document and deletes them one by
* one — the first purge after months of traffic held a visitor's request for
* as long as that took. `db.deleteMany` is a single query (these collections
* have no hooks to run), started and left to finish on its own; a failure is
* logged and retried on the next request.
*/
export function createDailyPurge(collection: string) {
let lastPurge = 0
return (payload: BasePayload, where: Where): void => {
const now = Date.now()
if (now - lastPurge < DAY_MS) {return}
lastPurge = now
void Promise.resolve()
.then(() => payload.db.deleteMany({ collection: collection as never, where }))
.catch((err: unknown) => {
lastPurge = 0
payload.logger.error(`[iapl] ${collection} retention purge failed: ${err instanceof Error ? err.message : String(err)}`)
})
}
}