Library and CLI that restores a production mongodump archive into a throwaway MongoDB, scrubs personal data by field data type using per-run keyed fakes, runs safety checks, and swaps the result into a target database. Flagged test accounts can stay real, and the run stops on any field the type map does not cover. Requires MongoDB Database Tools and Node 22 or newer; the Payload plugin wrapper is not written yet.
Scrub for Payload: copy production into test with personal data replaced by consistent fake values, flagged test accounts kept as they are, and a copy that stops on any field the map doesn't cover.
Installation
npm i @extantape/payload-plugin-scrub0
Stars
0
Forks
1
Open Issues
Today
Last Updated
0
Weekly Downloads
v
npm Version
Dependencies
Trends appear once the first and last snapshots are 7 days apart. Collecting since Nov 29 — 7 daily snapshots so far.
Trends appear after 7 days of data. Daily snapshots have not started yet.
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1482809375-root)<a href="https://payloaddirectory.dev/plugins/1482809375-root"><img src="https://payloaddirectory.dev/badge/1482809375-root.svg" alt="Payload Directory"></a>README
Scrub for Payload: copy production into test with personal data replaced by consistent fake values, flagged test accounts kept as they are, and a copy that stops on any field the map doesn't cover.
Status: early. The data types, tag helpers, flag field, fake-value generators, the config walker, the in-place scrub engine, the copy command, the swap, carry-over and the ten checks exist. The unit tests use made-up values. The database tests run the whole copy against two throwaway MongoDB 7 servers in Docker, using the real mongodump and mongorestore. Nothing has run against a restored production archive, against a hosted cluster, or inside a running Payload site. The Payload plugin wrapper is not written.
As a library and a command:
Data types and defaults. 17 data types, each with one rule (keep, fake, text, coarse, drop, placeholder or structure), and a default for each Payload field type. resolveField places one field, and returns untyped for anything it can't place, such as an untagged code or json field.
Tags. scrubTag, scrubOverride and scrubCollection write the custom property. An override needs a reason and can't change a secret.
Flag field. flagField() returns a checkbox definition, off by default. The host must supply access, and a way to read the stored record (readRecord or the collection slug). Turning the flag on checks the record as stored and as it will be after the save. With no isEligible, no record can be flagged.
Fakers. Keyed by a random per-run key, so one input gives one fake inside a run and a different one in the next. Email, person name, organization, phone, street address (the city is a real US city by default, or a made-up one if you set cityNames to made-up; a real name can match a real place by chance), personal URL, postal code, coordinates and birth date.
Walker. findUntypedFields(config, options) reads the built Payload config and returns the field paths with no type, the declared tags whose path is gone, the person links missing from subject and staff, and subject or staff entries that point at no person link. Run it in your own test suite. isCovered(result) is true when there is nothing to fix.
Engine. scrubDatabase({ db, config, key, ... }) scrubs a restored MongoDB database in place, collection by collection, by data type. It works on the collections it finds in the database. A collection with no rule is dropped, and so is a version collection whose parent is missing. Globals, version snapshots (version.*) and Payload's own collections follow the rules in the design. A stored key with no field definition is dropped and counted. A value whose shape does not fit its rule is dropped and counted. Secrets are dropped for everyone. It refuses to start when the walker finds anything, and it returns a report of counts with no stored values.
Flagged records. With a flag option the engine reads the latest flag change for every flagged record before it scrubs anything, and keeps a record real only when the flag is exactly true, the latest change says flagged and isEligible agrees. A record that links people keeps real values only when it links at least one and every one is kept. A version snapshot is always scrubbed. The run stops when more records are flagged than maxFlagged.
Staff links. A person link that says who handled a record (reviewedBy, assignedTo) goes under staff, for example staff: { tickets: ['reviewedBy'] }, and the link that says who the record is about stays under subject. A staff link never keeps a record real and never makes one mixed. A record with only staff links is scrubbed. A link listed in neither map stops the run, and so does a path in both. Without staff, every listed link is a subject, as before.
Copy command. payload-scrub copy --config scrub.config.ts reads a host config (see Commands), restores the newest archive into a throwaway MongoDB on this machine, scrubs it, runs the checks, restores the scrubbed copy into the target database under a stage prefix, merges carry-over documents, runs the checks again on the stage copy, and then swaps each collection with renames inside the target database. It stops at the first failed check, and the target is untouched until every check on the temp copy has passed.
Checks. Ten checks, numbered as in docs/design.md: archive age, target allow-list and sentinel, type coverage, document counts, a leak sweep, no secret fields, flagged records, relationship integrity, free space, and the same checks on the stage copy. The report is written as report.json and report.md and holds counts, collection names and field paths. It never holds a stored value or an id.
Swap and clean-up. A manifest lists every path, database and stage collection before it is made, and clean-up removes only what the manifest lists. A swap journal records the state of each collection, and its names must be the stage prefix and prev prefix plus the live name in the target database, or a rollback refuses and touches nothing. A failed swap is rolled back. A run that was killed mid-swap leaves the previous copies in place, and the next run rolls back from them or stops. Previous copies are dropped only after every post-copy command has succeeded.
Limits you should know about:
The copy needs mongodump and mongorestore on the PATH (MongoDB Database Tools). Their output is thrown away, since their errors can print document values, so a failure says only which tool failed and its exit code.
The temp MongoDB must be on this machine (a loopback address). A throwaway container on another address is refused for now. The temp connection string must name one host: mongodb+srv, a proxy option, replicaSet, loadBalanced=true and several hosts without directConnection=true are refused, including option names and values written with percent codes (%70roxyHost), which the driver reads as the plain name. After connecting, every member the server reports in hello (hosts, passives, arbiters, primary, me) must be a loopback address too.
The server's own system collections (system.js, system.views and the rest) are left out of both restores, and the run removes any that is still in the temp database before the dump. Only the collections in the map are copied. Views are not carried over.
The leak sweep looks for the original email, name, organization, phone, street address and postal values of every scrubbed record, found through the plan's field types, as exact whole values. It also finds an email inside longer text, a phone number written with different punctuation, and any email-shaped value that does not end in .invalid. A name inside longer text is not searched for, since text fields are replaced whole. Values under four characters are not searched for. Records kept real and carry-over documents are left out of the sweep.
A target collection that the copy does not cover is left as it was, and the report counts them. Only collections that are in the copy are swapped.
The sentinel document is carried over automatically. The rest of its collection is replaced unless it is listed in carryOver.
A carry-over document that clashes with a scrubbed row on _id or on a unique index replaces it. If any other record in the stage copy links to a row that was replaced, the run stops before the swap, whether or not the link would still resolve. A carry-over document that links to a row missing from the copy also stops the run.
Free space on the target is read from dbStats. Hosted clusters often do not report it. Without targetFreeBytes in the config, check 9 then fails and the run stops. Check 9 passes only on a finite number of bytes. undefined, null, a string, an object, NaN, Infinity and a function that throws all fail it.
A stop signal (SIGINT or SIGTERM) during a run ends the running tools, removes the temp database and the scratch files by manifest, writes the report, and exits with 130 or 143. Once the swap has started, the signal is only noted. The collection being moved finishes both of its renames, then the swap rolls back through the journal, the old collections are back, and the run exits with 130 or 143. The stage collections are not touched while the swap runs. The dump and restore tools run in their own process group, and they are ended when the run ends or is stopped. SIGKILL cannot be caught, so a tool under a killed run stops within about a second (a small watchdog around it checks that the run is still there, on macOS and Linux). The next run cleans up from the manifest, rolls the swap back from the journal, which is written and flushed to disk before each rename, and drops stage collections the stage ledger in the state folder names, since only this tool wrote those names. A collection with the stage prefix that the ledger does not name is left alone and the run refuses.
Clean-up removes a manifest entry only when it matches what the run would have made: a stage collection with the stage prefix in the target database, a temp database named for the run or set in temp.db, and a payload-scrub- folder directly inside the scratch folder, checked by its real path, so a link or a .. that leads elsewhere is refused. Other entries are left alone and listed in the report.
The scrubbed dump and the tool config files in the scratch folder are mode 0600.
The Docker-backed tests are skipped without Docker, and the copy tests are skipped without mongodump on the PATH. CI does not install the database tools, so CI runs only the tests that need no database tools.
A .ts config file needs a Node version that strips types (22.18 or newer) or a loader such as tsx. A .js or .mjs file works everywhere.
The engine changes the database you hand it. If it fails half way, throw that database away.
A kept real email can receive real mail from a test site. Stop outgoing mail there before the first copy.
Rich text and json-structure values keep their shape. A string stays only when it is a known structure word under its own key, such as paragraph under type or h2 under tag. Every other string becomes the fixed text. Every number becomes 0, except in rich text, where structure numbers such as version and indent stay, but only on the Lexical node type that owns the key and inside the range Lexical writes. Number and date types of the database inside Json become 0 and the epoch date. An age, a phone number or a coordinate does not survive. Ids inside Json stay. Inside rich text, the fields of a block node count as content: no number there survives, whatever the key is called. Elsewhere in rich text the engine trusts the type a node claims, so a small number in range, such as a value of 5 or 2017, can stay on a node that only claims to be a list item. A number as long as a phone number cannot.
Links, uploads, relationships and blocks in rich text lose relationTo, blockType and block id, which are scrubbed like other strings, so those nodes stop working in the copy. Nothing leaks. A later change will keep them.
The engine reads the ids of a collection once, then scrubs in batches. If the document count of that collection differs at the end, it stops with an error. The check covers only the collection being scrubbed, so an insert into a finished collection or a new collection made mid-run is not caught. Copy a database nobody is writing to, and throw away any copy where a run failed. A collection holding an _id that is not a number or an ObjectId also stops the run, and every collection is checked before anything is written, since the id is the key and is repeated in every link.
The engine scrubs and drops collections in whatever database it is given. It does not check that the database is a throwaway copy. The copy command checks that the temp host is on this machine, so call the engine directly only on a copy.
A fake street address keeps the real region and country. Only the street lines and the city are replaced. To keep the real city, state, postal code and country of an address stored in separate fields, tag the street field street-line and the others label. A postal code that must stay whole is tagged label too, since postal-code keeps 3 characters. Minors get the same rules as adults.
A collection with a custom id field that is not a number is refused by the walker (custom-text-id). Payload stores that id in _id and repeats it in every link, so the engine cannot scrub it in place. Use the default id or a number id. A number id is left alone.
The engine itself restores, dumps and swaps nothing. The copy command does that around it.
The design for the rest is in docs/design.md.
Not published yet. To build from a clone: npm ci, npm run typecheck, npm test, npm run build. Node 22 or newer. The database tests start a throwaway MongoDB 7 in Docker (one container, with a second server inside it for the target) and remove it when they end. Without Docker they are skipped with a message. The copy tests also need mongodump and mongorestore on the PATH.
payload-scrub copy --config scrub.config.ts
Exit code 0 means the copy finished. 1 means it was refused or stopped, and the report says where. 2 means the command line was wrong. The command never takes a flag that skips a check.
The config file's default export is a ScrubCopyConfig, or a function that returns one. defineScrubCopyConfig() gives it its type.
payloadConfig: the built Payload config, or a promise of it.scrub: options for the engine (flag, subject, personCollections, dropCollections, strictText, postalCodeLength, cityNames). The flag options hold latestFlagChanges, isEligible and maxFlagged.source: archive (a path to a mongodump --archive file, never a URL), db (its database name), host (optional, the host it came from), maxAgeHours, gzip.temp: uri of a MongoDB on this machine, and an optional db name.target: uri, db, and the allowedDbs and allowedHosts lists. A target that is not on both lists, or that is the source, is refused.sentinel: { collection, id }, a document the host puts in each allowed target. It must be on the target and absent from the source archive.carryOver: [{ collection, filter }], documents on the target that survive a copy.postCopy: commands to run after the swap, each { name, run } or { name, command, args }.isMaintenanceOn: the host's gate. The swap refuses to start unless it returns true.stateDir: where the manifest, the swap journal and the reports live. It must outlive one run. reportDir, scratchDir, targetFreeBytes, stagePrefix and prevPrefix are optional.The temp database holds every real value for a while. Run the command on a machine you trust, and use a runner that is thrown away after each run when you can.
MIT. Copyright (c) 2026 EXTANT APE. See LICENSE.
switch env
Switch the admin panel between development and production databases and copy production into development
68239/wk16v3
payload plugin seed
Coverage-first test data seeding for Payload that reads your schema and refuses to run in production
72135/wk0v3
payload plugin switch env
Switch between production and development databases or copy production data to development from the Payload admin
5384/wk5v3
payload upstash
Upstash Redis key-value adapter for Payload's built-in KV store
82333/wk7v3 v4
payload typeid
TypeID identifiers for Payload collections, as native IDs or an extra type_id field, with PostgreSQL integrity guards
802.6k/wk0v3
payload kv
Store counters, feeds and expiring data in Redis or KV stores while querying them as normal Payload fields
771.4k/wk0v3 v4