Repository navigation
payload-locale-snapshot: Zero-loss safety plugin for localized fields, blocks, & arrays on PostgreSQL #18738
the-muzammilali
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hey everyone! 👋
I'm sharing an open-source plugin I built to address a silent data loss issue when using Payload 3.x with PostgreSQL (
@payloadcms/db-postgres) on multi-lingual collections:payload-locale-snapshot.🔍 The Problem
When saving or updating a document in a specific locale (via Admin UI, local API, REST, or draft publishing) on PostgreSQL, the Drizzle adapter cleans up child tables with parent-scoped deletes:
Because these queries delete child rows without a
_localecondition, any translations not present in the in-memory write buffer are wiped. Depending on the operation path or draft state, this can lead to:duplicate key value violates unique constraint "<collection>_locales_locale_parent_id_unique".🛡️ How
payload-locale-snapshotWorksRather than trying to intercept Payload's internal write transforms, the plugin acts as a transactional safety net:
beforeChange: Inspects PostgreSQL's foreign-key graph (information_schema) to dynamically discover all child tables (_locales, blocks, nested arrays, arbitrary depth) and snapshots non-active locale rows.afterChange: Restores the snapshotted rows using a scoped delete-before-insert andON CONFLICT DO NOTHING.Key Architecture Highlights:
db.sessions[req.transactionID].db). No secondary pool connection is opened inside lifecycle hooks.📦 Quick Start
npm install payload-locale-snapshot # or pnpm add payload-locale-snapshotAdd it to your
payload.config.ts:🔗 Links
I hope this helps teams running multi-locale production setups on PostgreSQL while upstream adapter optimizations continue. Feedback, issues, or ideas are very welcome!
All reactions