the-muzammilali

payload locale snapshot

by the-muzammilali
Databases & data
v3
^3.0.0
MIT

Overview

Summary generated from the README

Guards localized fields, blocks and arrays against silent data loss when saving documents in one locale with the PostgreSQL adapter. It hooks into beforeChange and afterChange, snapshots rows of other locales, and restores them within Payload's own transaction. Requires Payload 3.x, @payloadcms/db-postgres, drizzle-orm and PostgreSQL 14 or newer.

Features

  • Snapshots other-locale rows before save and re-inserts them after Payload's delete and write
  • Discovers child tables through the PostgreSQL foreign-key graph at any nesting depth
  • Runs inside Payload's transaction so the save and restore commit or roll back together
  • Auto-detects collections and globals with localized fields, or accepts explicit slug lists
  • Writes a JSON recovery dump on failure with a function for offline replay
  • Supports integer, text and UUID primary keys and TTL-cached schema lookups

Use cases

  • Protect translations in multilingual Payload sites with localized blocks on Postgres from being wiped by editor saves
  • Keep nested localized arrays and blocks intact during bulk updates and API writes
  • Recover lost locale content from a recovery dump after a failed restore

From the author

Prevents silent data loss of localized blocks and arrays in Payload CMS with the PostgreSQL adapter.

Installation

npm i payload-locale-snapshot

3

Stars

0

Forks

0

Open Issues

2 months ago

Last Updated

59

Weekly Downloads

v0.3.0

npm Version

164.6 KB

Unpacked Size

1

Dependencies

GitHub stars

30 since Sep 6

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

26−6 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202626
Dec 4, 202617
Dec 3, 202616
Dec 2, 202621
Dec 1, 202623
Nov 30, 202623
Nov 29, 202625
Nov 28, 202630
Nov 27, 202628
Nov 26, 202628
Nov 25, 202624
Nov 24, 202622
Nov 23, 202621
Nov 22, 202617
Nov 21, 202612
Nov 20, 202611
Nov 19, 202616
Nov 18, 202618
Nov 17, 202618
Nov 16, 202624
Nov 15, 202626
Nov 14, 202633
Nov 13, 202647
Nov 12, 202648
Nov 11, 202644
Nov 10, 202648
Nov 9, 202643
Nov 8, 202645
Nov 7, 202652
Nov 6, 202639
Nov 5, 202633
Nov 4, 202633
Nov 3, 202630
Nov 2, 202632
Nov 1, 202632
Oct 31, 202626
Oct 30, 202625
Oct 29, 202624
Oct 28, 202622
Oct 27, 202628
Oct 26, 202626
Oct 25, 202627
Oct 24, 202622
Oct 23, 202619
Oct 22, 202627
Oct 21, 202636
Oct 20, 202635
Oct 19, 202644
Oct 18, 202643
Oct 17, 202652
Oct 16, 202659
Oct 15, 202669
Oct 14, 202679
Oct 13, 2026253
Oct 12, 2026247
Oct 11, 2026246
Oct 10, 2026242
Oct 9, 2026242
Oct 8, 2026235
Oct 7, 2026220
Oct 6, 202644
Oct 5, 202644
Oct 4, 202644
Oct 3, 202639
Oct 2, 202639
Oct 1, 202629
Sep 30, 202630
Sep 29, 202628
Sep 28, 202632
Sep 27, 202628
Sep 26, 202629
Sep 25, 202625
Sep 24, 202628
Sep 23, 202626
Sep 22, 202636
Sep 21, 202631
Sep 20, 202634
Sep 19, 202630
Sep 18, 202628
Sep 17, 202625
Sep 16, 202630
Sep 15, 202620
Sep 14, 202619
Sep 13, 202616
Sep 12, 202620
Sep 11, 202622
Sep 10, 202625
Sep 9, 202626
Sep 8, 202628
Sep 7, 202632
Sep 6, 202632

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

Health Score

66/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/1336060174-root.svg)](https://payloaddirectory.dev/plugins/1336060174-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1336060174-root"><img src="https://payloaddirectory.dev/badge/1336060174-root.svg" alt="Payload Directory"></a>
Updated Oct 4, 2026Created Aug 16, 2026Published Oct 4, 2026

README

payload-locale-snapshot

npm version License: MIT

A zero-loss database stability plugin for Payload CMS 3.x using the PostgreSQL adapter (@payloadcms/db-postgres). It guarantees zero data loss of localized fields, scalars, blocks, nested arrays, and sub-items across all languages when saving documents in any locale.


The Problem

When using Payload CMS 3.x with PostgreSQL, collections with localized: true fields (scalars, blocks, or arrays) are vulnerable to silent data loss during routine editorial changes in the admin UI or API.

When saving an update for a document (e.g. in the default English locale), Payload's internal Drizzle adapter deletes child rows without a locale filter:

DELETE FROM "pages_blocks_*" WHERE "_parent_id" = <documentId>;

Depending on your Payload version and operation path, only the active locale is re-inserted — wiping out all other translations.

v2.0 note: Earlier versions of this plugin guarded against this using a separate pool connection inside lifecycle hooks. That caused a PostgreSQL transaction deadlock (infinite "Submitting..." on save). v2 executes entirely inside Payload's own transaction — see How it works.


How It Works (v2.0)

The plugin hooks into Payload's beforeChange and afterChange lifecycles and runs entirely on Payload's active transaction connection (the adapter's drizzle session client). No second database connection is ever opened inside a hook — which is what made v1 deadlock.

  1. beforeChange:
    • Discovers every child table of the collection/global via PostgreSQL's foreign-key graph (information_schema) — arbitrary nesting depth, blocks-in-arrays, arrays-in-blocks, per-block _locales content tables, custom dbName overrides. Version tables (_x_v*) are excluded automatically. Results are TTL-cached.
    • Snapshots all rows of other locales in topological order (parents first) with fully parameterized SQL (sql template literals → bind params; universal ::text casting supports serial / varchar / UUID ids).
  2. Payload performs its destructive delete + active-locale write.
  3. afterChange:
    • Re-inserts snapshotted rows on the same connection, parents before children, ON CONFLICT ("id") DO NOTHING (idempotent if Payload already preserved them).
    • The restore commits atomically together with the save: if anything fails, the whole save rolls back cleanly and a durable JSON recovery dump (.locale-snapshot-recovery/) is written for offline replay.

Guarantees

  • 🚀 Zero external API calls — pure SQL, reusing Payload's connection.
  • 🔑 Universal ID compatibility: integer/serial, text, and UUID primary keys.
  • ⚛️ Atomic: never half-restored; rollback undoes save + restore together.
  • 🔒 Concurrency-safe without locks: same-document saves serialize on the transaction's row locks; racing restores are no-ops thanks to stable block IDs + ON CONFLICT.
  • 🌐 Globals supported (real global row id resolution — no fabricated ids).
  • 📦 Bulk-update safe: snapshots are tracked per document within a request.
  • 💾 Durable fail-safe dump + offline restoreFromRecoveryDump() replay.

Installation

npm install payload-locale-snapshot
# or
pnpm add payload-locale-snapshot
# or
yarn add payload-locale-snapshot

Peer Dependencies

payload 3.x, @payloadcms/db-postgres, and drizzle-orm (installed alongside @payloadcms/db-postgres automatically).


Usage

Register in payload.config.ts

import { buildConfig } from 'payload'
import { localeSnapshot } from 'payload-locale-snapshot'

export default buildConfig({
  // ...other configuration
  plugins: [
    localeSnapshot,
    // Or with custom options:
    // localeSnapshot({
    //   collections: ['pages', 'products'],
    //   globals: ['settings'],
    //   debug: true,
    //   restoreFailureMode: 'throw',
    //   maxDepth: 10,
    //   schemaCacheTTL: 300_000,
    // }),
  ],
})

Configuration Options

OptionTypeDefaultDescription
collectionsstring[]Auto-detectedCollection slugs to guard. If omitted, collections with localized fields (scalars, blocks, arrays) are auto-guarded.
globalsstring[]Auto-detectedGlobal slugs to guard.
debugbooleantrueDetailed snapshot/restore logging.
restoreFailureMode'throw' | 'warn''throw'On snapshot/restore failure: abort the save (recommended — silent locale loss is worse than a failed save) or log and continue unprotected.
failOnErrorboolean—Deprecated. Legacy mapping: true → 'throw', false → 'warn'. Superseded by restoreFailureMode.
maxDepthnumber10Maximum FK-graph traversal depth.
schemaCacheTTLnumber300000 (5 min)Schema cache TTL in ms. 0 disables caching. Call resetSchemaCache() after migrations.
serverlessboolean—Deprecated / no-op. Nothing is deferred anymore; restoration commits inside the save transaction in all runtimes.
lockTimeoutMsnumber—Deprecated / no-op. Advisory locking was removed in v2 (unnecessary and harmful inside a transaction).

Upgrading from v1? Behavior change: failures now abort the save by default (restoreFailureMode: 'throw'). Pass failOnError: false or restoreFailureMode: 'warn' to keep v1's fail-open behavior.


Utility Exports

resetSchemaCache()

Invalidates the schema hierarchy cache (useful after running migrations):

import { resetSchemaCache } from 'payload-locale-snapshot'

resetSchemaCache()

restoreFromRecoveryDump(filePath, pool?)

Replays an offline recovery dump directly into PostgreSQL:

import { restoreFromRecoveryDump } from 'payload-locale-snapshot'

await restoreFromRecoveryDump('.locale-snapshot-recovery/recovery-pages-15-1786884000000.json')

restoreSnapshot(pool, snapshot)

Programmatic restore of a snapshot object on a standalone pool (used by the recovery replay path; runs its own transaction).


Requirements

  • Payload CMS ^3.0.0 with @payloadcms/db-postgres
  • PostgreSQL 14+
  • Node.js >=18.20.0

Development & Testing

docker run -d --name locale-snapshot-test \
  -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -e POSTGRES_DB=snapshot_test \
  -p 55432:5432 postgres:16-alpine

npm run build
node --test --test-concurrency=1 test/*.test.js

The suite mechanically reproduces the v1 deadlock against real PostgreSQL (test/deadlock.repro.test.js) and verifies end-to-end multi-locale preservation, version-table exclusion, UUID ids, injection safety, and standalone restore (test/plugin.behavior.test.js).

See LOCALE_SNAPSHOT_POSTMORTEM_AND_PATCH_GUIDE.md for the full postmortem and architecture rationale.


License

MIT © Muzammil Ali

Similar plugins