feat(plugin-import-export): give import/export hooks access to the triggering document - #17797
Draft
nathanlentz wants to merge 4 commits into
Draft
feat(plugin-import-export): give import/export hooks access to the triggering document#17797nathanlentz wants to merge 4 commits into
nathanlentz wants to merge 4 commits into
Conversation
…iggering document The collection-level import/export hooks received no reference to the document that started the run, so a field added to the imports/exports collection via `overrideCollection` could not be read while processing. That also left no way to correlate a running job with its document in jobs mode. Adds `importDoc` to `ImportBeforeHook`/`ImportAfterHook` and `exportDoc` to `ExportBeforeHook`/`ExportAfterHook`, covering all seven hook contexts: - import jobs and sync, which already held the saved document - export jobs, which now reads the document back — the job input schema is a fixed whitelist, so fields added via `overrideCollection` do not survive serialization - export sync, download, and both preview endpoints, which pass the submitted form data `id` is optional on both types because only the export jobs path has a saved document when the hooks run. Export sync runs in `beforeOperation`, and the download and preview paths never persist one. On those, every user-authored field is present but `id`, `createdAt` and `updatedAt` are not, and the values are unvalidated request input — both documented. The document is read once when the run starts and passed unchanged to every batch, so it is a snapshot rather than live state. A long run would otherwise add a database read per batch to observe a change nothing needs. Closes #16961
…w endpoints The preview components built their request bodies from a fixed list of built-in fields, so a field added to an imports/exports collection via `overrideCollection` never reached the collection-level hooks. Both now forward the whole form, and re-fetch when a custom value changes. The endpoints drop `id`, `createdAt` and `updatedAt` from the incoming body, so an `id` on `exportDoc`/`importDoc` means the document really is saved rather than something a caller can put on a request. Adds coverage for the queued-import, synchronous-export and download after hooks, for document identity across batches, and for two cases where a form value changes the rows that get written. The new tests clean up the import and export documents and the job records they create.
…ming Cleanup no longer catches deletion failures, so a delete that does not succeed fails the test rather than passing quietly. Job records are tracked per test: a snapshot of the existing payload-jobs IDs is taken before each test and only the records added on top of it are removed, instead of deleting every job record. Splits the download test so each one checks a single hook, routes the repeated post-and-export setup through helpers, and renames `withBatchRefField` and `generatedKeys` to describe what they actually do.
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Gives the collection-level import/export hooks access to the document that triggered the run, as
exportDocandimportDoc. This lets a hook read a value an editor set on the import/export form — including a field the project added withoverrideCollection— and apply it to every row.How
The document is read once when the run starts and passed unchanged to every batch, so hooks see a snapshot from when the operation was triggered rather than live state. A large export can fire the hook hundreds of times, so re-reading per batch would be both wasteful and inconsistent mid-run.
Where the document comes from depends on the path:
idfindByIDin the task handlerbeforeOperationidis therefore optional, and anidmeans the document is saved. To keep that rule true rather than merely conventional, the paths without a saved document run the incoming body throughgetSubmittedFormValues, which dropsid,createdAtandupdatedAt. A caller cannot put anidon a download or preview request and have a hook treat the run as saved.The two preview components previously built their request bodies from a fixed list of built-in fields, so a custom field never reached the endpoint. Both now forward the whole form. A value-based signature of the form state is added to the debounce dependencies, so editing a custom field refreshes the preview once the editor stops typing rather than on every keystroke.
Tests
idcannot make an unsaved run look saved.A
posts-with-hooks-jobscollection was added so both the synchronous and task-handler paths are exercised; its plugin config leaves the jobs queue enabled.Open item
Import and export previews also render on a saved operation document's edit view, where
useDocumentInfo()has anid. The clients do not send it, so hooks previewing a saved document still receive an unsaved snapshot. Fixing it means sending the route ID and fetching the document server-side, and needs a decision on what happens to the open form's unsaved edits — merging them keeps the preview live but means anidno longer implies every value was validated. Left out of this PR deliberately.