@payloadcms/plugin-import-export 4.0.0-internal.5b1e7cd → 4.0.0-internal.5e065b3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/dist/components/CollectionField/index.d.ts +3 -2
  2. package/dist/components/CollectionField/index.d.ts.map +1 -1
  3. package/dist/components/CollectionField/index.js.map +1 -1
  4. package/dist/components/ExportListMenuItem/index.d.ts.map +1 -1
  5. package/dist/components/ExportListMenuItem/index.js +14 -12
  6. package/dist/components/ExportListMenuItem/index.js.map +1 -1
  7. package/dist/components/ExportPreview/index.css +52 -0
  8. package/dist/components/ExportPreview/index.d.ts +1 -1
  9. package/dist/components/ExportPreview/index.d.ts.map +1 -1
  10. package/dist/components/ExportPreview/index.js +12 -2
  11. package/dist/components/ExportPreview/index.js.map +1 -1
  12. package/dist/components/FieldsToExport/index.d.ts +3 -2
  13. package/dist/components/FieldsToExport/index.d.ts.map +1 -1
  14. package/dist/components/FieldsToExport/index.js +18 -15
  15. package/dist/components/FieldsToExport/index.js.map +1 -1
  16. package/dist/components/FieldsToExport/reduceFields.d.ts +6 -0
  17. package/dist/components/FieldsToExport/reduceFields.d.ts.map +1 -1
  18. package/dist/components/FieldsToExport/reduceFields.js +4 -0
  19. package/dist/components/FieldsToExport/reduceFields.js.map +1 -1
  20. package/dist/components/FormatField/index.d.ts +3 -2
  21. package/dist/components/FormatField/index.d.ts.map +1 -1
  22. package/dist/components/FormatField/index.js +6 -3
  23. package/dist/components/FormatField/index.js.map +1 -1
  24. package/dist/components/ImportListMenuItem/index.d.ts.map +1 -1
  25. package/dist/components/ImportListMenuItem/index.js +14 -12
  26. package/dist/components/ImportListMenuItem/index.js.map +1 -1
  27. package/dist/components/ImportPreview/index.css +45 -0
  28. package/dist/components/ImportPreview/index.d.ts +1 -1
  29. package/dist/components/ImportPreview/index.d.ts.map +1 -1
  30. package/dist/components/ImportPreview/index.js +44 -11
  31. package/dist/components/ImportPreview/index.js.map +1 -1
  32. package/dist/components/LimitField/index.d.ts +3 -2
  33. package/dist/components/LimitField/index.d.ts.map +1 -1
  34. package/dist/components/LimitField/index.js.map +1 -1
  35. package/dist/components/LocaleField/index.d.ts +3 -2
  36. package/dist/components/LocaleField/index.d.ts.map +1 -1
  37. package/dist/components/LocaleField/index.js.map +1 -1
  38. package/dist/components/Page/index.d.ts +4 -3
  39. package/dist/components/Page/index.d.ts.map +1 -1
  40. package/dist/components/Page/index.js +1 -1
  41. package/dist/components/Page/index.js.map +1 -1
  42. package/dist/components/RelationshipCell/getRelationshipGroupKey.d.ts +7 -0
  43. package/dist/components/RelationshipCell/getRelationshipGroupKey.d.ts.map +1 -0
  44. package/dist/components/RelationshipCell/getRelationshipGroupKey.js +3 -0
  45. package/dist/components/RelationshipCell/getRelationshipGroupKey.js.map +1 -0
  46. package/dist/components/RelationshipCell/getRelationshipGroups.d.ts +43 -0
  47. package/dist/components/RelationshipCell/getRelationshipGroups.d.ts.map +1 -0
  48. package/dist/components/RelationshipCell/getRelationshipGroups.js +109 -0
  49. package/dist/components/RelationshipCell/getRelationshipGroups.js.map +1 -0
  50. package/dist/components/RelationshipCell/index.css +30 -0
  51. package/dist/components/RelationshipCell/index.d.ts +15 -0
  52. package/dist/components/RelationshipCell/index.d.ts.map +1 -0
  53. package/dist/components/RelationshipCell/index.js +57 -0
  54. package/dist/components/RelationshipCell/index.js.map +1 -0
  55. package/dist/components/SortBy/index.css +9 -0
  56. package/dist/components/SortBy/index.d.ts +4 -3
  57. package/dist/components/SortBy/index.d.ts.map +1 -1
  58. package/dist/components/SortBy/index.js +1 -2
  59. package/dist/components/SortBy/index.js.map +1 -1
  60. package/dist/components/SortOrder/index.css +9 -0
  61. package/dist/components/SortOrder/index.d.ts +4 -3
  62. package/dist/components/SortOrder/index.d.ts.map +1 -1
  63. package/dist/components/SortOrder/index.js +1 -2
  64. package/dist/components/SortOrder/index.js.map +1 -1
  65. package/dist/export/batchProcessor.d.ts +12 -3
  66. package/dist/export/batchProcessor.d.ts.map +1 -1
  67. package/dist/export/batchProcessor.js +61 -15
  68. package/dist/export/batchProcessor.js.map +1 -1
  69. package/dist/export/createExport.d.ts +4 -2
  70. package/dist/export/createExport.d.ts.map +1 -1
  71. package/dist/export/createExport.js +105 -22
  72. package/dist/export/createExport.js.map +1 -1
  73. package/dist/export/getCreateExportCollectionTask.d.ts.map +1 -1
  74. package/dist/export/getCreateExportCollectionTask.js +11 -0
  75. package/dist/export/getCreateExportCollectionTask.js.map +1 -1
  76. package/dist/export/getExportCollection.d.ts.map +1 -1
  77. package/dist/export/getExportCollection.js +9 -4
  78. package/dist/export/getExportCollection.js.map +1 -1
  79. package/dist/export/handleDownload.d.ts.map +1 -1
  80. package/dist/export/handleDownload.js +7 -3
  81. package/dist/export/handleDownload.js.map +1 -1
  82. package/dist/export/handlePreview.d.ts.map +1 -1
  83. package/dist/export/handlePreview.js +60 -16
  84. package/dist/export/handlePreview.js.map +1 -1
  85. package/dist/exports/types.d.ts +1 -1
  86. package/dist/exports/types.d.ts.map +1 -1
  87. package/dist/exports/types.js.map +1 -1
  88. package/dist/import/batchProcessor.d.ts +17 -4
  89. package/dist/import/batchProcessor.d.ts.map +1 -1
  90. package/dist/import/batchProcessor.js +63 -34
  91. package/dist/import/batchProcessor.js.map +1 -1
  92. package/dist/import/createImport.d.ts +3 -11
  93. package/dist/import/createImport.d.ts.map +1 -1
  94. package/dist/import/createImport.js +42 -54
  95. package/dist/import/createImport.js.map +1 -1
  96. package/dist/import/getCreateImportCollectionTask.d.ts.map +1 -1
  97. package/dist/import/getCreateImportCollectionTask.js +5 -2
  98. package/dist/import/getCreateImportCollectionTask.js.map +1 -1
  99. package/dist/import/getImportCollection.d.ts.map +1 -1
  100. package/dist/import/getImportCollection.js +6 -4
  101. package/dist/import/getImportCollection.js.map +1 -1
  102. package/dist/import/handlePreview.d.ts.map +1 -1
  103. package/dist/import/handlePreview.js +37 -7
  104. package/dist/import/handlePreview.js.map +1 -1
  105. package/dist/index.d.ts +51 -7
  106. package/dist/index.d.ts.map +1 -1
  107. package/dist/index.js +36 -8
  108. package/dist/index.js.map +1 -1
  109. package/dist/translations/languages/en.d.ts.map +1 -1
  110. package/dist/types.d.ts +205 -42
  111. package/dist/types.d.ts.map +1 -1
  112. package/dist/types.js.map +1 -1
  113. package/dist/utilities/applyFieldHooks.d.ts +22 -0
  114. package/dist/utilities/applyFieldHooks.d.ts.map +1 -0
  115. package/dist/utilities/applyFieldHooks.js +117 -0
  116. package/dist/utilities/applyFieldHooks.js.map +1 -0
  117. package/dist/utilities/collectDisabledFieldPaths.d.ts.map +1 -1
  118. package/dist/utilities/collectDisabledFieldPaths.js +1 -1
  119. package/dist/utilities/collectDisabledFieldPaths.js.map +1 -1
  120. package/dist/utilities/flattenObject.d.ts +8 -6
  121. package/dist/utilities/flattenObject.d.ts.map +1 -1
  122. package/dist/utilities/flattenObject.js +85 -75
  123. package/dist/utilities/flattenObject.js.map +1 -1
  124. package/dist/utilities/flattenedFields.d.ts +21 -0
  125. package/dist/utilities/flattenedFields.d.ts.map +1 -0
  126. package/dist/utilities/flattenedFields.js +37 -0
  127. package/dist/utilities/flattenedFields.js.map +1 -0
  128. package/dist/utilities/getExportFieldFunctions.d.ts +5 -5
  129. package/dist/utilities/getExportFieldFunctions.d.ts.map +1 -1
  130. package/dist/utilities/getExportFieldFunctions.js +122 -99
  131. package/dist/utilities/getExportFieldFunctions.js.map +1 -1
  132. package/dist/utilities/getFileFromDoc.js +3 -3
  133. package/dist/utilities/getFileFromDoc.js.map +1 -1
  134. package/dist/utilities/getImportFieldFunctions.d.ts +5 -5
  135. package/dist/utilities/getImportFieldFunctions.d.ts.map +1 -1
  136. package/dist/utilities/getImportFieldFunctions.js +95 -103
  137. package/dist/utilities/getImportFieldFunctions.js.map +1 -1
  138. package/dist/utilities/getSchemaColumns.d.ts +1 -1
  139. package/dist/utilities/getSchemaColumns.js +2 -2
  140. package/dist/utilities/getSchemaColumns.js.map +1 -1
  141. package/dist/utilities/getSelect.d.ts.map +1 -1
  142. package/dist/utilities/getSelect.js +11 -3
  143. package/dist/utilities/getSelect.js.map +1 -1
  144. package/dist/utilities/getSubmittedFormValues.d.ts +12 -0
  145. package/dist/utilities/getSubmittedFormValues.d.ts.map +1 -0
  146. package/dist/utilities/getSubmittedFormValues.js +24 -0
  147. package/dist/utilities/getSubmittedFormValues.js.map +1 -0
  148. package/dist/utilities/isPlainObject.d.ts +2 -0
  149. package/dist/utilities/isPlainObject.d.ts.map +1 -0
  150. package/dist/utilities/isPlainObject.js +3 -0
  151. package/dist/utilities/isPlainObject.js.map +1 -0
  152. package/dist/utilities/polymorphicRel.d.ts +14 -0
  153. package/dist/utilities/polymorphicRel.d.ts.map +1 -0
  154. package/dist/utilities/polymorphicRel.js +17 -0
  155. package/dist/utilities/polymorphicRel.js.map +1 -0
  156. package/dist/utilities/processRichTextField.d.ts.map +1 -1
  157. package/dist/utilities/processRichTextField.js +7 -0
  158. package/dist/utilities/processRichTextField.js.map +1 -1
  159. package/dist/utilities/removeDisabledFields.js.map +1 -1
  160. package/dist/utilities/setNestedValue.d.ts +2 -1
  161. package/dist/utilities/setNestedValue.d.ts.map +1 -1
  162. package/dist/utilities/setNestedValue.js +65 -32
  163. package/dist/utilities/setNestedValue.js.map +1 -1
  164. package/dist/utilities/sortHelpers.d.ts +1 -1
  165. package/dist/utilities/unflattenObject.d.ts +4 -3
  166. package/dist/utilities/unflattenObject.d.ts.map +1 -1
  167. package/dist/utilities/unflattenObject.js +69 -170
  168. package/dist/utilities/unflattenObject.js.map +1 -1
  169. package/dist/utilities/unflattenPostProcess.d.ts +11 -0
  170. package/dist/utilities/unflattenPostProcess.d.ts.map +1 -0
  171. package/dist/utilities/unflattenPostProcess.js +174 -0
  172. package/dist/utilities/unflattenPostProcess.js.map +1 -0
  173. package/license.md +1 -1
  174. package/package.json +9 -9
  175. package/dist/components/ExportPreview/index.scss +0 -48
  176. package/dist/components/FieldsToExport/index.scss +0 -0
  177. package/dist/components/FieldsToExport/reduceFields.spec.js +0 -239
  178. package/dist/components/FieldsToExport/reduceFields.spec.js.map +0 -1
  179. package/dist/components/ImportPreview/index.scss +0 -41
  180. package/dist/components/SortBy/index.scss +0 -3
  181. package/dist/components/SortOrder/index.scss +0 -3
  182. package/dist/utilities/buildDisabledFieldRegex.spec.js +0 -64
  183. package/dist/utilities/buildDisabledFieldRegex.spec.js.map +0 -1
  184. package/dist/utilities/fieldToRegex.spec.js +0 -151
  185. package/dist/utilities/fieldToRegex.spec.js.map +0 -1
  186. package/dist/utilities/filterToSelectedFields.spec.js +0 -204
  187. package/dist/utilities/filterToSelectedFields.spec.js.map +0 -1
  188. package/dist/utilities/getSchemaColumns.spec.js +0 -157
  189. package/dist/utilities/getSchemaColumns.spec.js.map +0 -1
  190. package/dist/utilities/parseCSV.spec.js +0 -195
  191. package/dist/utilities/parseCSV.spec.js.map +0 -1
  192. package/dist/utilities/unflattenObject.spec.js +0 -680
  193. package/dist/utilities/unflattenObject.spec.js.map +0 -1
  194. /package/dist/components/Page/{index.scss → index.css} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/export/batchProcessor.ts"],"sourcesContent":["/**\n * Export-specific batch processor for processing documents in batches during export.\n * Uses the generic batch processing utilities from useBatchProcessor.\n */\nimport type { PayloadRequest, SelectType, Sort, TypedUser, Where } from 'payload'\n\nimport { type BatchProcessorOptions } from '../utilities/useBatchProcessor.js'\n\n/**\n * Export-specific batch processor options\n */\nexport interface ExportBatchProcessorOptions extends BatchProcessorOptions {\n debug?: boolean\n}\n\n/**\n * Find arguments for querying documents during export\n */\nexport interface ExportFindArgs {\n collection: string\n depth: number\n draft: boolean\n limit: number\n locale?: string\n overrideAccess: boolean\n page?: number\n select?: SelectType\n sort?: Sort\n user?: TypedUser\n where?: Where\n}\n\n/**\n * Options for processing an export operation\n */\nexport interface ExportProcessOptions<TDoc = unknown> {\n /**\n * The slug of the collection to export\n */\n collectionSlug: string\n /**\n * Arguments to pass to payload.find()\n */\n findArgs: ExportFindArgs\n /**\n * The export format - affects column tracking for CSV\n */\n format: 'csv' | 'json'\n /**\n * Maximum number of documents to export\n */\n maxDocs: number\n /**\n * The Payload request object\n */\n req: PayloadRequest\n /**\n * Starting page for pagination (default: 1)\n */\n startPage?: number\n /**\n * Transform function to apply to each document\n */\n transformDoc: (doc: TDoc) => Record<string, unknown>\n}\n\n/**\n * Result from processing an export operation\n */\nexport interface ExportResult {\n /**\n * Discovered column names (for CSV exports)\n */\n columns: string[]\n /**\n * Transformed documents ready for output\n */\n docs: Record<string, unknown>[]\n /**\n * Total number of documents fetched\n */\n fetchedCount: number\n}\n\n/**\n * Creates an export batch processor with the specified options.\n *\n * @param options - Configuration options for the batch processor\n * @returns An object containing the processExport method\n *\n * @example\n * ```ts\n * const processor = createExportBatchProcessor({ batchSize: 100, debug: true })\n *\n * const result = await processor.processExport({\n * collectionSlug: 'posts',\n * findArgs: { collection: 'posts', depth: 1, draft: false, limit: 100, overrideAccess: false },\n * format: 'csv',\n * maxDocs: 1000,\n * req,\n * transformDoc: (doc) => flattenObject({ doc }),\n * })\n * ```\n */\nexport function createExportBatchProcessor(options: ExportBatchProcessorOptions = {}) {\n const batchSize = options.batchSize ?? 100\n const debug = options.debug ?? false\n\n /**\n * Process an export operation by fetching and transforming documents in batches.\n *\n * @param processOptions - Options for the export operation\n * @returns The export result containing transformed documents and column info\n */\n const processExport = async <TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): Promise<ExportResult> => {\n const { findArgs, format, maxDocs, req, startPage = 1, transformDoc } = processOptions\n\n const docs: Record<string, unknown>[] = []\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Processing export batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n for (const doc of result.docs) {\n const transformedDoc = transformDoc(doc as TDoc)\n docs.push(transformedDoc)\n\n // Track columns for CSV format\n if (format === 'csv') {\n for (const key of Object.keys(transformedDoc)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n\n return { columns, docs, fetchedCount: fetched }\n }\n\n /**\n * Async generator for streaming export - yields batches instead of collecting all.\n * Useful for streaming exports where you want to process batches as they're fetched.\n *\n * @param processOptions - Options for the export operation\n * @yields Batch results containing transformed documents and discovered columns\n *\n * @example\n * ```ts\n * const processor = createExportBatchProcessor({ batchSize: 100 })\n *\n * for await (const batch of processor.streamExport({ ... })) {\n * // Process each batch as it's yielded\n * console.log(`Got ${batch.docs.length} documents`)\n * }\n * ```\n */\n async function* streamExport<TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): AsyncGenerator<{ columns: string[]; docs: Record<string, unknown>[] }> {\n const { findArgs, format, maxDocs, req, startPage = 1, transformDoc } = processOptions\n\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Streaming export batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n const batchDocs: Record<string, unknown>[] = []\n\n for (const doc of result.docs) {\n const transformedDoc = transformDoc(doc as TDoc)\n batchDocs.push(transformedDoc)\n\n // Track columns for CSV format\n if (format === 'csv') {\n for (const key of Object.keys(transformedDoc)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n }\n\n yield { columns: [...columns], docs: batchDocs }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n }\n\n /**\n * Discover all columns from the dataset by scanning through all batches.\n * Useful for CSV exports where you need to know all columns before streaming.\n *\n * @param processOptions - Options for the export operation\n * @returns Array of discovered column names\n */\n const discoverColumns = async <TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): Promise<string[]> => {\n const { findArgs, maxDocs, req, startPage = 1, transformDoc } = processOptions\n\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Scanning columns from batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n for (const doc of result.docs) {\n const transformedDoc = transformDoc(doc as TDoc)\n\n for (const key of Object.keys(transformedDoc)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n\n if (debug) {\n req.payload.logger.debug(`Discovered ${columns.length} columns`)\n }\n\n return columns\n }\n\n return {\n discoverColumns,\n processExport,\n streamExport,\n }\n}\n"],"names":["createExportBatchProcessor","options","batchSize","debug","processExport","processOptions","findArgs","format","maxDocs","req","startPage","transformDoc","docs","columnsSet","Set","columns","currentPage","fetched","hasNextPage","remaining","Math","max","result","payload","find","limit","min","page","logger","length","doc","transformedDoc","push","key","Object","keys","has","add","fetchedCount","streamExport","batchDocs","discoverColumns"],"mappings":"AAAA;;;CAGC,GAiFD;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,SAASA,2BAA2BC,UAAuC,CAAC,CAAC;IAClF,MAAMC,YAAYD,QAAQC,SAAS,I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ACT;IAEA,OAAO;QACL0B;QACArC;QACAmC;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/export/batchProcessor.ts"],"sourcesContent":["/**\n * Export-specific batch processor for processing documents in batches during export.\n * Uses the generic batch processing utilities from useBatchProcessor.\n */\nimport type { PayloadRequest, SelectType, Sort, User, Where } from 'payload'\n\nimport type { ExportAfterHook, ExportBeforeHook, ExportDoc } from '../types.js'\n\nimport { type BatchProcessorOptions } from '../utilities/useBatchProcessor.js'\n\n/**\n * Export-specific batch processor options\n */\nexport interface ExportBatchProcessorOptions extends BatchProcessorOptions {\n debug?: boolean\n}\n\n/**\n * Find arguments for querying documents during export\n */\nexport interface ExportFindArgs {\n collection: string\n depth: number\n draft: boolean\n limit: number\n locale?: string\n overrideAccess: boolean\n page?: number\n select?: SelectType\n sort?: Sort\n user?: User\n where?: Where\n}\n\n/**\n * Options for processing an export operation\n */\nexport interface ExportProcessOptions<TDoc = unknown> {\n /**\n * The slug of the collection to export\n */\n collectionSlug: string\n exportDoc: ExportDoc\n /**\n * Arguments to pass to payload.find()\n */\n findArgs: ExportFindArgs\n /**\n * The export format - affects column tracking for CSV\n */\n format: 'csv' | 'json'\n /** Lifecycle hooks for this export operation */\n hooks?: {\n after?: ExportAfterHook\n before?: ExportBeforeHook\n }\n /**\n * Maximum number of documents to export\n */\n maxDocs: number\n /**\n * The Payload request object\n */\n req: PayloadRequest\n /**\n * Starting page for pagination (default: 1)\n */\n startPage?: number\n /** Total number of docs available (used to compute totalBatches for hooks) */\n totalDocs?: number\n /**\n * Transform function to apply to each document\n */\n transformDoc: (doc: TDoc) => Record<string, unknown>\n}\n\n/**\n * Result from processing an export operation\n */\nexport interface ExportResult {\n /**\n * Discovered column names (for CSV exports)\n */\n columns: string[]\n /**\n * Transformed documents ready for output\n */\n docs: Record<string, unknown>[]\n /**\n * Total number of documents fetched\n */\n fetchedCount: number\n}\n\n/**\n * Creates an export batch processor with the specified options.\n *\n * @param options - Configuration options for the batch processor\n * @returns An object containing the processExport method\n *\n * @example\n * ```ts\n * const processor = createExportBatchProcessor({ batchSize: 100, debug: true })\n *\n * const result = await processor.processExport({\n * collectionSlug: 'posts',\n * findArgs: { collection: 'posts', depth: 1, draft: false, limit: 100, overrideAccess: false },\n * format: 'csv',\n * maxDocs: 1000,\n * req,\n * transformDoc: (doc) => flattenObject({ data: doc }),\n * })\n * ```\n */\nexport function createExportBatchProcessor(options: ExportBatchProcessorOptions = {}) {\n const batchSize = options.batchSize ?? 100\n const debug = options.debug ?? false\n\n const computeTotalBatches = (totalDocs: number | undefined, maxDocs: number): number => {\n const effectiveDocs =\n totalDocs !== undefined\n ? Math.min(totalDocs, maxDocs === Number.POSITIVE_INFINITY ? totalDocs : maxDocs)\n : 0\n return effectiveDocs > 0 ? Math.ceil(effectiveDocs / batchSize) : 1\n }\n\n /**\n * Process an export operation by fetching and transforming documents in batches.\n *\n * @param processOptions - Options for the export operation\n * @returns The export result containing transformed documents and column info\n */\n const processExport = async <TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): Promise<ExportResult> => {\n const {\n exportDoc,\n findArgs,\n format,\n hooks,\n maxDocs,\n req,\n startPage = 1,\n totalDocs,\n transformDoc,\n } = processOptions\n\n const totalBatches = computeTotalBatches(totalDocs, maxDocs)\n\n const docs: Record<string, unknown>[] = []\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Processing export batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n const batchNumber = currentPage - startPage + 1\n const originalDocs = result.docs as Record<string, unknown>[]\n const batchData = result.docs.map((doc) => transformDoc(doc as TDoc))\n\n const dataToWrite =\n hooks?.before && batchData.length > 0\n ? await hooks.before({\n batchNumber,\n data: batchData,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n : batchData\n\n for (const row of dataToWrite) {\n docs.push(row)\n\n if (format === 'csv') {\n for (const key of Object.keys(row)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n }\n\n if (hooks?.after && dataToWrite.length > 0) {\n await hooks.after({\n batchNumber,\n data: dataToWrite,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n\n return { columns, docs, fetchedCount: fetched }\n }\n\n /**\n * Async generator for streaming export - yields batches instead of collecting all.\n * Useful for streaming exports where you want to process batches as they're fetched.\n *\n * @param processOptions - Options for the export operation\n * @yields Batch results containing transformed documents and discovered columns\n *\n * @example\n * ```ts\n * const processor = createExportBatchProcessor({ batchSize: 100 })\n *\n * for await (const batch of processor.streamExport({ ... })) {\n * // Process each batch as it's yielded\n * console.log(`Got ${batch.docs.length} documents`)\n * }\n * ```\n */\n async function* streamExport<TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): AsyncGenerator<{ columns: string[]; docs: Record<string, unknown>[] }> {\n const {\n exportDoc,\n findArgs,\n format,\n hooks,\n maxDocs,\n req,\n startPage = 1,\n totalDocs,\n transformDoc,\n } = processOptions\n\n const totalBatches = computeTotalBatches(totalDocs, maxDocs)\n\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Streaming export batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n const batchNumber = currentPage - startPage + 1\n const originalDocs = result.docs as Record<string, unknown>[]\n const batchData = result.docs.map((doc) => transformDoc(doc as TDoc))\n\n const dataToWrite =\n hooks?.before && batchData.length > 0\n ? await hooks.before({\n batchNumber,\n data: batchData,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n : batchData\n\n for (const row of dataToWrite) {\n if (format === 'csv') {\n for (const key of Object.keys(row)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n }\n\n yield { columns: [...columns], docs: dataToWrite }\n\n if (hooks?.after && dataToWrite.length > 0) {\n await hooks.after({\n batchNumber,\n data: dataToWrite,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n }\n\n /**\n * Discover all columns from the dataset by scanning through all batches.\n * Useful for CSV exports where you need to know all columns before streaming.\n *\n * @param processOptions - Options for the export operation\n * @returns Array of discovered column names\n */\n const discoverColumns = async <TDoc>(\n processOptions: ExportProcessOptions<TDoc>,\n ): Promise<string[]> => {\n const { findArgs, maxDocs, req, startPage = 1, transformDoc } = processOptions\n\n const columnsSet = new Set<string>()\n const columns: string[] = []\n\n let currentPage = startPage\n let fetched = 0\n let hasNextPage = true\n\n while (hasNextPage) {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n break\n }\n\n const result = await req.payload.find({\n ...findArgs,\n limit: Math.min(batchSize, remaining),\n page: currentPage,\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Scanning columns from batch ${currentPage} with ${result.docs.length} documents`,\n )\n }\n\n for (const doc of result.docs) {\n const transformedDoc = transformDoc(doc as TDoc)\n\n for (const key of Object.keys(transformedDoc)) {\n if (!columnsSet.has(key)) {\n columnsSet.add(key)\n columns.push(key)\n }\n }\n }\n\n fetched += result.docs.length\n hasNextPage = result.hasNextPage && fetched < maxDocs\n currentPage++\n }\n\n if (debug) {\n req.payload.logger.debug(`Discovered ${columns.length} columns`)\n }\n\n return columns\n }\n\n return {\n discoverColumns,\n processExport,\n streamExport,\n }\n}\n"],"names":["createExportBatchProcessor","options","batchSize","debug","computeTotalBatches","totalDocs","maxDocs","effectiveDocs","undefined","Math","min","Number","POSITIVE_INFINITY","ceil","processExport","processOptions","exportDoc","findArgs","format","hooks","req","startPage","transformDoc","totalBatches","docs","columnsSet","Set","columns","currentPage","fetched","hasNextPage","remaining","max","result","payload","find","limit","page","logger","length","batchNumber","originalDocs","batchData","map","doc","dataToWrite","before","data","originalData","row","push","key","Object","keys","has","add","after","fetchedCount","streamExport","discoverColumns","transformedDoc"],"mappings":"AAAA;;;CAGC,GA2FD;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,SAASA,2BAA2BC,UAAuC,CAAC,CAAC;IAClF,MAAMC,YAAYD,QAAQC,SAAS,IAAI;IACvC,MAAMC,QAAQF,QAAQE,KAAK,IAAI;IAE/B,MAAMC,sBAAsB,CAACC,WAA+BC;QAC1D,MAAMC,gBACJF,cAAcG,YACVC,KAAKC,GAAG,CAACL,WAAWC,YAAYK,OAAOC,iBAAiB,GAAGP,YAAYC,WACvE;QACN,OAAOC,gBAAgB,IAAIE,KAAKI,IAAI,CAACN,gBAAgBL,aAAa;IACpE;IAEA;;;;;GAKC,GACD,MAAMY,gBAAgB,OACpBC;QAEA,MAAM,EACJC,SAAS,EACTC,QAAQ,EACRC,MAAM,EACNC,KAAK,EACLb,OAAO,EACPc,GAAG,EACHC,YAAY,CAAC,EACbhB,SAAS,EACTiB,YAAY,EACb,GAAGP;QAEJ,MAAMQ,eAAenB,oBAAoBC,WAAWC;QAEpD,MAAMkB,OAAkC,EAAE;QAC1C,MAAMC,aAAa,IAAIC;QACvB,MAAMC,UAAoB,EAAE;QAE5B,IAAIC,cAAcP;QAClB,IAAIQ,UAAU;QACd,IAAIC,cAAc;QAElB,MAAOA,YAAa;YAClB,MAAMC,YAAYtB,KAAKuB,GAAG,CAAC,GAAG1B,UAAUuB;YAExC,IAAIE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B,MAAMoC,iBAAiBtC,aAAasB;gBAEpC,KAAK,MAAMO,OAAOC,OAAOC,IAAI,CAACO,gBAAiB;oBAC7C,IAAI,CAACnC,WAAW6B,GAAG,CAACH,MAAM;wBACxB1B,WAAW8B,GAAG,CAACJ;wBACfxB,QAAQuB,IAAI,CAACC;oBACf;gBACF;YACF;YAEAtB,WAAWI,OAAOT,IAAI,CAACe,MAAM;YAC7BT,cAAcG,OAAOH,WAAW,IAAID,UAAUvB;YAC9CsB;QACF;QAEA,IAAIzB,OAAO;YACTiB,IAAIc,OAAO,CAACI,MAAM,CAACnC,KAAK,CAAC,CAAC,WAAW,EAAEwB,QAAQY,MAAM,CAAC,QAAQ,CAAC;QACjE;QAEA,OAAOZ;IACT;IAEA,OAAO;QACLgC;QACA7C;QACA4C;IACF;AACF"}
@@ -1,4 +1,5 @@
1
1
  import type { PayloadRequest, Sort, Where } from 'payload';
2
+ import type { ExportDoc } from '../types.js';
2
3
  export type Export = {
3
4
  /**
4
5
  * Number of documents to process in each batch during export
@@ -26,8 +27,8 @@ export type Export = {
26
27
  name: string;
27
28
  page?: number;
28
29
  sort?: Sort;
29
- userCollection: string;
30
- userID: number | string;
30
+ userCollection?: string;
31
+ userID?: number | string;
31
32
  where?: Where;
32
33
  };
33
34
  export type CreateExportArgs = {
@@ -35,6 +36,7 @@ export type CreateExportArgs = {
35
36
  * If true, stream the file instead of saving it
36
37
  */
37
38
  download?: boolean;
39
+ exportDoc: ExportDoc;
38
40
  req: PayloadRequest;
39
41
  } & Export;
40
42
  export declare const createExport: (args: CreateExportArgs) => Promise<Response | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"createExport.d.ts","sourceRoot":"","sources":["../../src/export/createExport.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,IAAI,EAAa,KAAK,EAAE,MAAM,SAAS,CAAA;AAerE,MAAM,MAAM,MAAM,GAAG;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAA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MAAM,CAAC,EAAE,IAAI,GAAG,KAAK,CAAA;IACrB,gBAAgB,EAAE,MAAM,CAAA;IACxB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;IACjB,MAAM,EAAE,KAAK,GAAG,MAAM,CAAA;IACtB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACxB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,SAAS,EAAE,SAAS,CAAA;IACpB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,MAAM,CAAA;AAEV,eAAO,MAAM,YAAY,SAAgB,gBAAgB,kCAsmBxD,CAAA"}
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable perfectionist/sort-objects */ import { stringify } from 'csv-stringify/sync';
2
2
  import { APIError } from 'payload';
3
3
  import { Readable } from 'stream';
4
+ import { applyFieldHooks } from '../utilities/applyFieldHooks.js';
4
5
  import { buildDisabledFieldRegex } from '../utilities/buildDisabledFieldRegex.js';
5
6
  import { flattenObject } from '../utilities/flattenObject.js';
6
7
  import { getExportFieldFunctions } from '../utilities/getExportFieldFunctions.js';
@@ -10,7 +11,7 @@ import { getSelect } from '../utilities/getSelect.js';
10
11
  import { validateLimitValue } from '../utilities/validateLimitValue.js';
11
12
  import { createExportBatchProcessor } from './batchProcessor.js';
12
13
  export const createExport = async (args)=>{
13
- const { id, name: nameArg, batchSize = 100, collectionSlug, debug = false, download, drafts: draftsFromInput, exportCollection, fields, format, limit: incomingLimit, maxLimit, locale: localeFromInput, page, req, sort, userCollection, userID, where: whereFromInput = {} } = args;
14
+ const { id, name: nameArg, batchSize = 100, collectionSlug, debug = false, download, drafts: draftsFromInput, exportCollection, exportDoc, fields, format, limit: incomingLimit, maxLimit, locale: localeFromInput, page, req, sort, userCollection, userID, where: whereFromInput = {} } = args;
14
15
  const { locale: localeFromReq, payload } = req;
15
16
  if (debug) {
16
17
  req.payload.logger.debug({
@@ -38,7 +39,7 @@ export const createExport = async (args)=>{
38
39
  });
39
40
  }
40
41
  if (!user && req.user) {
41
- user = req?.user?.id ? req.user : req?.user?.user;
42
+ user = req.user;
42
43
  }
43
44
  if (!user) {
44
45
  throw new APIError('User authentication is required to create exports.');
@@ -63,10 +64,10 @@ export const createExport = async (args)=>{
63
64
  const select = Array.isArray(fields) && fields.length > 0 ? getSelect(fields) : undefined;
64
65
  if (debug) {
65
66
  req.payload.logger.debug({
66
- message: 'Export configuration:',
67
- name,
68
67
  isCSV,
69
- locale
68
+ locale,
69
+ msg: 'Export configuration:',
70
+ name
70
71
  });
71
72
  }
72
73
  // Determine maximum export documents:
@@ -102,8 +103,8 @@ export const createExport = async (args)=>{
102
103
  accessDenied = true;
103
104
  if (debug) {
104
105
  req.payload.logger.debug({
105
- message: 'Access denied for collection, creating empty export',
106
- collectionSlug
106
+ collectionSlug,
107
+ msg: 'Access denied for collection, creating empty export'
107
108
  });
108
109
  }
109
110
  }
@@ -125,13 +126,14 @@ export const createExport = async (args)=>{
125
126
  };
126
127
  if (debug) {
127
128
  req.payload.logger.debug({
128
- message: 'Find arguments:',
129
- findArgs
129
+ findArgs,
130
+ msg: 'Find arguments:'
130
131
  });
131
132
  }
132
- const toCSVFunctions = getExportFieldFunctions({
133
+ const exportFieldHooks = getExportFieldFunctions({
133
134
  fields: collectionConfig.flattenedFields
134
135
  });
136
+ const exportHooks = collectionConfig.custom?.['plugin-import-export']?.exportHooks;
135
137
  const disabledFields = collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? [];
136
138
  const disabledMatchers = disabledFields.map(buildDisabledFieldRegex);
137
139
  const filterDisabledCSV = (row)=>{
@@ -193,6 +195,9 @@ export const createExport = async (args)=>{
193
195
  let currentBatchPage = adjustedPage;
194
196
  let fetched = 0;
195
197
  const maxDocs = typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY;
198
+ const effectiveStreamDocs = typeof maxExportDocuments === 'number' ? Math.min(totalDocs, maxExportDocuments) : totalDocs;
199
+ const totalBatches = effectiveStreamDocs > 0 ? Math.ceil(effectiveStreamDocs / batchSize) : 1;
200
+ let streamBatchNumber = 0;
196
201
  const stream = new Readable({
197
202
  async read () {
198
203
  const remaining = Math.max(0, maxDocs - fetched);
@@ -221,18 +226,34 @@ export const createExport = async (args)=>{
221
226
  this.push(null);
222
227
  return;
223
228
  }
229
+ streamBatchNumber++;
224
230
  if (isCSV) {
225
231
  // --- CSV Streaming ---
226
232
  const batchRows = result.docs.map((doc)=>filterDisabledCSV(flattenObject({
227
- doc,
233
+ data: doc,
228
234
  fields,
229
- toCSVFunctions
235
+ format,
236
+ exportFieldHooks,
237
+ req
230
238
  })));
239
+ const originalDocs = result.docs;
240
+ let batchRowsToWrite = batchRows;
241
+ if (exportHooks?.before && batchRows.length > 0) {
242
+ batchRowsToWrite = await exportHooks.before({
243
+ batchNumber: streamBatchNumber,
244
+ data: batchRows,
245
+ exportDoc,
246
+ format,
247
+ originalData: originalDocs,
248
+ req,
249
+ totalBatches
250
+ });
251
+ }
231
252
  // On first batch, discover additional columns from data and merge with schema
232
253
  if (!columnsFinalized) {
233
254
  const dataColumns = [];
234
255
  const seenCols = new Set();
235
- for (const row of batchRows){
256
+ for (const row of batchRowsToWrite){
236
257
  for (const key of Object.keys(row)){
237
258
  if (!seenCols.has(key)) {
238
259
  seenCols.add(key);
@@ -240,8 +261,11 @@ export const createExport = async (args)=>{
240
261
  }
241
262
  }
242
263
  }
243
- // Merge schema columns with data-discovered columns
244
- allColumns = mergeColumns(schemaColumns, dataColumns);
264
+ // Merge schema columns with data-discovered columns.
265
+ // When a before hook is active and rows exist, restrict to columns actually present
266
+ // in the data so that the hook can remove fields by not returning them.
267
+ const mergedColumns = mergeColumns(schemaColumns, dataColumns);
268
+ allColumns = Boolean(exportHooks?.before) && batchRowsToWrite.length > 0 ? mergedColumns.filter((col)=>dataColumns.includes(col)) : mergedColumns;
245
269
  columnsFinalized = true;
246
270
  if (debug) {
247
271
  req.payload.logger.debug({
@@ -251,7 +275,7 @@ export const createExport = async (args)=>{
251
275
  });
252
276
  }
253
277
  }
254
- const paddedRows = batchRows.map((row)=>{
278
+ const paddedRows = batchRowsToWrite.map((row)=>{
255
279
  const fullRow = {};
256
280
  for (const col of allColumns){
257
281
  fullRow[col] = row[col] ?? '';
@@ -264,16 +288,59 @@ export const createExport = async (args)=>{
264
288
  columns: allColumns
265
289
  });
266
290
  this.push(encoder.encode(csvString));
291
+ if (exportHooks?.after && batchRowsToWrite.length > 0) {
292
+ await exportHooks.after({
293
+ batchNumber: streamBatchNumber,
294
+ data: batchRowsToWrite,
295
+ exportDoc,
296
+ format,
297
+ originalData: originalDocs,
298
+ req,
299
+ totalBatches
300
+ });
301
+ }
267
302
  } else {
268
303
  // --- JSON Streaming ---
269
- const batchRows = result.docs.map((doc)=>filterDisabledJSON(doc));
304
+ const batchRows = result.docs.map((doc)=>filterDisabledJSON(applyFieldHooks({
305
+ data: doc,
306
+ fieldHooks: exportFieldHooks,
307
+ fields: collectionConfig.flattenedFields,
308
+ format,
309
+ operation: 'export',
310
+ req,
311
+ type: 'beforeExport'
312
+ })));
313
+ const originalDocs = result.docs;
314
+ let batchRowsToWrite = batchRows;
315
+ if (exportHooks?.before && batchRows.length > 0) {
316
+ batchRowsToWrite = await exportHooks.before({
317
+ batchNumber: streamBatchNumber,
318
+ data: batchRows,
319
+ exportDoc,
320
+ format,
321
+ originalData: originalDocs,
322
+ req,
323
+ totalBatches
324
+ });
325
+ }
270
326
  // Convert each filtered/flattened row into JSON string
271
- const batchJSON = batchRows.map((row)=>JSON.stringify(row)).join(',');
327
+ const batchJSON = batchRowsToWrite.map((row)=>JSON.stringify(row)).join(',');
272
328
  if (isFirstBatch) {
273
329
  this.push(encoder.encode('[' + batchJSON));
274
330
  } else {
275
331
  this.push(encoder.encode(',' + batchJSON));
276
332
  }
333
+ if (exportHooks?.after && batchRowsToWrite.length > 0) {
334
+ await exportHooks.after({
335
+ batchNumber: streamBatchNumber,
336
+ data: batchRowsToWrite,
337
+ exportDoc,
338
+ format,
339
+ originalData: originalDocs,
340
+ req,
341
+ totalBatches
342
+ });
343
+ }
277
344
  }
278
345
  fetched += result.docs.length;
279
346
  isFirstBatch = false;
@@ -307,10 +374,20 @@ export const createExport = async (args)=>{
307
374
  });
308
375
  // Transform function based on format
309
376
  const transformDoc = (doc)=>isCSV ? filterDisabledCSV(flattenObject({
310
- doc,
377
+ data: doc,
311
378
  fields,
312
- toCSVFunctions
313
- })) : filterDisabledJSON(doc);
379
+ format,
380
+ exportFieldHooks,
381
+ req
382
+ })) : filterDisabledJSON(applyFieldHooks({
383
+ data: doc,
384
+ fieldHooks: exportFieldHooks,
385
+ fields: collectionConfig.flattenedFields,
386
+ format,
387
+ operation: 'export',
388
+ req,
389
+ type: 'beforeExport'
390
+ }));
314
391
  // Skip fetching if access was denied - we'll create an empty export
315
392
  let exportResult = {
316
393
  columns: [],
@@ -320,11 +397,14 @@ export const createExport = async (args)=>{
320
397
  if (!accessDenied) {
321
398
  exportResult = await processor.processExport({
322
399
  collectionSlug,
400
+ exportDoc,
323
401
  findArgs: findArgs,
324
402
  format,
403
+ hooks: exportHooks,
325
404
  maxDocs: typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY,
326
405
  req,
327
406
  startPage: adjustedPage,
407
+ totalDocs,
328
408
  transformDoc
329
409
  });
330
410
  }
@@ -343,7 +423,10 @@ export const createExport = async (args)=>{
343
423
  });
344
424
  // Merge schema columns with data-discovered columns
345
425
  // Schema provides ordering, data provides additional columns (e.g., array indices > 0)
346
- const finalColumns = mergeColumns(schemaColumns, dataColumns);
426
+ // When a before hook is active and rows exist, restrict to columns actually present in the data
427
+ // so that the hook can remove fields by not returning them
428
+ const mergedColumns = mergeColumns(schemaColumns, dataColumns);
429
+ const finalColumns = Boolean(exportHooks?.before) && rows.length > 0 ? mergedColumns.filter((col)=>dataColumns.includes(col)) : mergedColumns;
347
430
  const paddedRows = rows.map((row)=>{
348
431
  const fullRow = {};
349
432
  for (const col of finalColumns){
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/export/createExport.ts"],"sourcesContent":["/* eslint-disable perfectionist/sort-objects */\nimport type { PayloadRequest, Sort, TypedUser, Where } from 'payload'\n\nimport { stringify } from 'csv-stringify/sync'\nimport { APIError } from 'payload'\nimport { Readable } from 'stream'\n\nimport { buildDisabledFieldRegex } from '../utilities/buildDisabledFieldRegex.js'\nimport { flattenObject } from '../utilities/flattenObject.js'\nimport { getExportFieldFunctions } from '../utilities/getExportFieldFunctions.js'\nimport { getFilename } from '../utilities/getFilename.js'\nimport { getSchemaColumns, mergeColumns } from '../utilities/getSchemaColumns.js'\nimport { getSelect } from '../utilities/getSelect.js'\nimport { validateLimitValue } from '../utilities/validateLimitValue.js'\nimport { createExportBatchProcessor, type ExportFindArgs } from './batchProcessor.js'\n\nexport type Export = {\n /**\n * Number of documents to process in each batch during export\n * @default 100\n */\n batchSize?: number\n collectionSlug: string\n /**\n * If true, enables debug logging\n */\n debug?: boolean\n drafts?: 'no' | 'yes'\n exportCollection: string\n fields?: string[]\n format: 'csv' | 'json'\n globals?: string[]\n id: number | string\n limit?: number\n locale?: string\n /**\n * Maximum number of documents that can be exported in a single operation.\n * This value has already been resolved from the plugin config.\n */\n maxLimit?: number\n name: string\n page?: number\n sort?: Sort\n userCollection: string\n userID: number | string\n where?: Where\n}\n\nexport type CreateExportArgs = {\n /**\n * If true, stream the file instead of saving it\n */\n download?: boolean\n req: PayloadRequest\n} & Export\n\nexport const createExport = async (args: CreateExportArgs) => {\n const {\n id,\n name: nameArg,\n batchSize = 100,\n collectionSlug,\n debug = false,\n download,\n drafts: draftsFromInput,\n exportCollection,\n fields,\n format,\n limit: incomingLimit,\n maxLimit,\n locale: localeFromInput,\n page,\n req,\n sort,\n userCollection,\n userID,\n where: whereFromInput = {},\n } = args\n const { locale: localeFromReq, payload } = req\n\n if (debug) {\n req.payload.logger.debug({\n msg: '[createExport] Starting export process',\n exportDocId: id,\n exportName: nameArg,\n collectionSlug,\n exportCollection,\n draft: draftsFromInput,\n fields,\n format,\n })\n }\n\n const locale = localeFromInput ?? localeFromReq\n const collectionConfig = payload.config.collections.find(({ slug }) => slug === collectionSlug)\n\n if (!collectionConfig) {\n throw new APIError(`Collection with slug ${collectionSlug} not found.`)\n }\n\n let user: TypedUser | undefined\n\n if (userCollection && userID) {\n user = (await req.payload.findByID({\n id: userID,\n collection: userCollection,\n overrideAccess: true,\n })) as TypedUser\n }\n\n if (!user && req.user) {\n user = req?.user?.id ? req.user : req?.user?.user\n }\n\n if (!user) {\n throw new APIError('User authentication is required to create exports.')\n }\n\n const draft = draftsFromInput === 'yes'\n const hasVersions = Boolean(collectionConfig.versions)\n\n // Only filter by _status for versioned collections\n const publishedWhere: Where = hasVersions ? { _status: { equals: 'published' } } : {}\n\n const where: Where = {\n and: [whereFromInput, draft ? {} : publishedWhere],\n }\n\n const baseName = nameArg ?? getFilename()\n const name = `${baseName}-${collectionSlug}.${format}`\n const isCSV = format === 'csv'\n const select = Array.isArray(fields) && fields.length > 0 ? getSelect(fields) : undefined\n\n if (debug) {\n req.payload.logger.debug({ message: 'Export configuration:', name, isCSV, locale })\n }\n\n // Determine maximum export documents:\n // 1. If maxLimit is defined, it sets the absolute ceiling\n // 2. User's limit is applied but clamped to maxLimit if it exceeds it\n let maxExportDocuments: number | undefined\n\n if (typeof maxLimit === 'number' && maxLimit > 0) {\n if (typeof incomingLimit === 'number' && incomingLimit > 0) {\n // User provided a limit - clamp it to maxLimit\n maxExportDocuments = Math.min(incomingLimit, maxLimit)\n } else {\n // No user limit - use maxLimit as the ceiling\n maxExportDocuments = maxLimit\n }\n } else {\n // No maxLimit - use user's limit if provided\n maxExportDocuments =\n typeof incomingLimit === 'number' && incomingLimit > 0 ? incomingLimit : undefined\n }\n\n // Try to count documents - if access is denied, treat as 0 documents\n let totalDocs = 0\n let accessDenied = false\n try {\n const countResult = await payload.count({\n collection: collectionSlug,\n user,\n locale,\n overrideAccess: false,\n })\n totalDocs = countResult.totalDocs\n } catch (error) {\n // Access denied - user can't read from this collection\n // We'll create an empty export file\n accessDenied = true\n if (debug) {\n req.payload.logger.debug({\n message: 'Access denied for collection, creating empty export',\n collectionSlug,\n })\n }\n }\n\n const totalPages = Math.max(1, Math.ceil(totalDocs / batchSize))\n const requestedPage = page || 1\n const adjustedPage = requestedPage > totalPages ? 1 : requestedPage\n\n const findArgs = {\n collection: collectionSlug,\n depth: 1,\n draft,\n limit: batchSize,\n locale,\n overrideAccess: false,\n page: 0,\n select,\n sort,\n user,\n where,\n }\n\n if (debug) {\n req.payload.logger.debug({ message: 'Find arguments:', findArgs })\n }\n\n const toCSVFunctions = getExportFieldFunctions({\n fields: collectionConfig.flattenedFields,\n })\n\n const disabledFields =\n collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? []\n\n const disabledMatchers = disabledFields.map(buildDisabledFieldRegex)\n\n const filterDisabledCSV = (row: Record<string, unknown>): Record<string, unknown> => {\n const filtered: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(row)) {\n const isDisabled = disabledMatchers.some((matcher) => matcher.test(key))\n if (!isDisabled) {\n filtered[key] = value\n }\n }\n\n return filtered\n }\n\n const filterDisabledJSON = (doc: any, parentPath = ''): any => {\n if (Array.isArray(doc)) {\n return doc.map((item) => filterDisabledJSON(item, parentPath))\n }\n\n if (typeof doc !== 'object' || doc === null) {\n return doc\n }\n\n const filtered: Record<string, any> = {}\n for (const [key, value] of Object.entries(doc)) {\n const currentPath = parentPath ? `${parentPath}.${key}` : key\n\n // Only remove if this exact path is disabled\n const isDisabled = disabledFields.includes(currentPath)\n\n if (!isDisabled) {\n filtered[key] = filterDisabledJSON(value, currentPath)\n }\n }\n\n return filtered\n }\n\n if (download) {\n const limitErrorMsg = validateLimitValue(incomingLimit, req.t)\n if (limitErrorMsg) {\n throw new APIError(limitErrorMsg)\n }\n\n // Get schema-based columns first (provides base ordering and handles empty exports)\n let schemaColumns: string[] = []\n if (isCSV) {\n const localeCodes =\n locale === 'all' && payload.config.localization\n ? payload.config.localization.localeCodes\n : undefined\n\n schemaColumns = getSchemaColumns({\n collectionConfig,\n disabledFields,\n fields,\n locale,\n localeCodes,\n })\n\n if (debug) {\n req.payload.logger.debug({\n columnCount: schemaColumns.length,\n msg: 'Schema-based column inference complete',\n })\n }\n }\n\n // allColumns will be finalized after first batch (schema + data columns merged)\n let allColumns: string[] = []\n let columnsFinalized = false\n\n const encoder = new TextEncoder()\n let isFirstBatch = true\n let currentBatchPage = adjustedPage\n let fetched = 0\n const maxDocs =\n typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY\n\n const stream = new Readable({\n async read() {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n if (!isCSV) {\n // If first batch with no remaining, output empty array; otherwise just close\n this.push(encoder.encode(isFirstBatch ? '[]' : ']'))\n }\n this.push(null)\n return\n }\n\n const result = await payload.find({\n ...findArgs,\n page: currentBatchPage,\n limit: Math.min(batchSize, remaining),\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Streaming batch ${currentBatchPage} with ${result.docs.length} docs`,\n )\n }\n\n if (result.docs.length === 0) {\n // Close JSON array properly if JSON\n if (!isCSV) {\n // If first batch with no docs, output empty array; otherwise just close\n this.push(encoder.encode(isFirstBatch ? '[]' : ']'))\n }\n this.push(null)\n return\n }\n\n if (isCSV) {\n // --- CSV Streaming ---\n const batchRows = result.docs.map((doc) =>\n filterDisabledCSV(\n flattenObject({\n doc,\n fields,\n toCSVFunctions,\n }),\n ),\n )\n\n // On first batch, discover additional columns from data and merge with schema\n if (!columnsFinalized) {\n const dataColumns: string[] = []\n const seenCols = new Set<string>()\n for (const row of batchRows) {\n for (const key of Object.keys(row)) {\n if (!seenCols.has(key)) {\n seenCols.add(key)\n dataColumns.push(key)\n }\n }\n }\n // Merge schema columns with data-discovered columns\n allColumns = mergeColumns(schemaColumns, dataColumns)\n columnsFinalized = true\n\n if (debug) {\n req.payload.logger.debug({\n dataColumnsCount: dataColumns.length,\n finalColumnsCount: allColumns.length,\n msg: 'Merged schema and data columns',\n })\n }\n }\n\n const paddedRows = batchRows.map((row) => {\n const fullRow: Record<string, unknown> = {}\n for (const col of allColumns) {\n fullRow[col] = row[col] ?? ''\n }\n return fullRow\n })\n\n const csvString = stringify(paddedRows, {\n bom: isFirstBatch,\n header: isFirstBatch,\n columns: allColumns,\n })\n\n this.push(encoder.encode(csvString))\n } else {\n // --- JSON Streaming ---\n const batchRows = result.docs.map((doc) => filterDisabledJSON(doc))\n\n // Convert each filtered/flattened row into JSON string\n const batchJSON = batchRows.map((row) => JSON.stringify(row)).join(',')\n\n if (isFirstBatch) {\n this.push(encoder.encode('[' + batchJSON))\n } else {\n this.push(encoder.encode(',' + batchJSON))\n }\n }\n\n fetched += result.docs.length\n isFirstBatch = false\n currentBatchPage += 1\n\n if (!result.hasNextPage || fetched >= maxDocs) {\n if (debug) {\n req.payload.logger.debug('Stream complete - no more pages')\n }\n if (!isCSV) {\n this.push(encoder.encode(']'))\n }\n this.push(null) // End the stream\n }\n },\n })\n\n return new Response(Readable.toWeb(stream) as ReadableStream, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${name}\"`,\n 'Content-Type': isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n },\n })\n }\n\n // Non-download path (buffered export)\n if (debug) {\n req.payload.logger.debug('Starting file generation')\n }\n\n // Create export batch processor\n const processor = createExportBatchProcessor({ batchSize, debug })\n\n // Transform function based on format\n const transformDoc = (doc: unknown) =>\n isCSV\n ? filterDisabledCSV(\n flattenObject({\n doc,\n fields,\n toCSVFunctions,\n }),\n )\n : filterDisabledJSON(doc)\n\n // Skip fetching if access was denied - we'll create an empty export\n let exportResult = {\n columns: [] as string[],\n docs: [] as Record<string, unknown>[],\n fetchedCount: 0,\n }\n\n if (!accessDenied) {\n exportResult = await processor.processExport({\n collectionSlug,\n findArgs: findArgs as ExportFindArgs,\n format,\n maxDocs:\n typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY,\n req,\n startPage: adjustedPage,\n transformDoc,\n })\n }\n\n const { columns: dataColumns, docs: rows } = exportResult\n const outputData: string[] = []\n\n // Prepare final output\n if (isCSV) {\n // Get schema-based columns for consistent ordering\n const localeCodes =\n locale === 'all' && payload.config.localization\n ? payload.config.localization.localeCodes\n : undefined\n\n const schemaColumns = getSchemaColumns({\n collectionConfig,\n disabledFields,\n fields,\n locale,\n localeCodes,\n })\n\n // Merge schema columns with data-discovered columns\n // Schema provides ordering, data provides additional columns (e.g., array indices > 0)\n const finalColumns = mergeColumns(schemaColumns, dataColumns)\n\n const paddedRows = rows.map((row) => {\n const fullRow: Record<string, unknown> = {}\n for (const col of finalColumns) {\n fullRow[col] = row[col] ?? ''\n }\n return fullRow\n })\n\n // Always output CSV with header, even if empty\n outputData.push(\n stringify(paddedRows, {\n bom: true,\n header: true,\n columns: finalColumns,\n }),\n )\n } else {\n // JSON format\n outputData.push(rows.map((doc) => JSON.stringify(doc)).join(',\\n'))\n }\n\n // Ensure we always have valid content for the file\n // For JSON, empty exports produce \"[]\"\n // For CSV, if completely empty (no columns, no rows), produce at least a newline to ensure file creation\n const content = format === 'json' ? `[${outputData.join(',')}]` : outputData.join('')\n const buffer = Buffer.from(content.length > 0 ? content : '\\n')\n if (debug) {\n req.payload.logger.debug(`${format} file generation complete`)\n }\n\n if (!id) {\n if (debug) {\n req.payload.logger.debug('Creating new export file')\n }\n req.file = {\n name,\n data: buffer,\n mimetype: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n size: buffer.length,\n }\n } else {\n if (debug) {\n req.payload.logger.debug({\n msg: '[createExport] Updating export document with file',\n exportDocId: id,\n exportCollection,\n fileName: name,\n fileSize: buffer.length,\n mimeType: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n })\n }\n try {\n await req.payload.update({\n id,\n collection: exportCollection,\n data: {},\n file: {\n name,\n data: buffer,\n mimetype: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n size: buffer.length,\n },\n // Override access only here so that we can be sure the export collection itself is updated as expected\n overrideAccess: true,\n req,\n })\n } catch (error) {\n const errorDetails =\n error instanceof Error\n ? {\n message: error.message,\n name: error.name,\n stack: error.stack,\n // @ts-expect-error - data might exist on Payload errors\n data: error.data,\n }\n : error\n req.payload.logger.error({\n msg: '[createExport] Failed to update export document with file',\n err: errorDetails,\n exportDocId: id,\n exportCollection,\n fileName: name,\n })\n throw error\n }\n }\n if (debug) {\n req.payload.logger.debug('Export process completed successfully')\n }\n}\n"],"names":["stringify","APIError","Readable","buildDisabledFieldRegex","flattenObject","getExportFieldFunctions","getFilename","getSchemaColumns","mergeColumns","getSelect","validateLimitValue","createExportBatchProcessor","createExport","args","id","name","nameArg","batchSize","collectionSlug","debug","download","drafts","draftsFromInput","exportCollection","fields","format","limit","incomingLimit","maxLimit","locale","localeFromInput","page","req","sort","userCollection","userID","where","whereFromInput","localeFromReq","payload","logger","msg","exportDocId","exportName","draft","collectionConfig","config","collections","find","slug","user","findByID","collection","overrideAccess","hasVersions","Boolean","versions","publishedWhere","_status","equals","and","baseName","isCSV","select","Array","isArray","length","undefined","message","maxExportDocuments","Math","min","totalDocs","accessDenied","countResult","count","error","totalPages","max","ceil","requestedPage","adjustedPage","findArgs","depth","toCSVFunctions","flattenedFields","disabledFields","admin","custom","disabledMatchers","map","filterDisabledCSV","row","filtered","key","value","Object","entries","isDisabled","some","matcher","test","filterDisabledJSON","doc","parentPath","item","currentPath","includes","limitErrorMsg","t","schemaColumns","localeCodes","localization","columnCount","allColumns","columnsFinalized","encoder","TextEncoder","isFirstBatch","currentBatchPage","fetched","maxDocs","Number","POSITIVE_INFINITY","stream","read","remaining","push","encode","result","docs","batchRows","dataColumns","seenCols","Set","keys","has","add","dataColumnsCount","finalColumnsCount","paddedRows","fullRow","col","csvString","bom","header","columns","batchJSON","JSON","join","hasNextPage","Response","toWeb","headers","processor","transformDoc","exportResult","fetchedCount","processExport","startPage","rows","outputData","finalColumns","content","buffer","Buffer","from","file","data","mimetype","size","fileName","fileSize","mimeType","update","errorDetails","Error","stack","err"],"mappings":"AAAA,6CAA6C,GAG7C,SAASA,SAAS,QAAQ,qBAAoB;AAC9C,SAASC,QAAQ,QAAQ,UAAS;AAClC,SAASC,QAAQ,QAAQ,SAAQ;AAEjC,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,aAAa,QAAQ,gCAA+B;AAC7D,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,gBAAgB,EAAEC,YAAY,QAAQ,mCAAkC;AACjF,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,kBAAkB,QAAQ,qCAAoC;AACvE,SAASC,0BAA0B,QAA6B,sBAAqB;AA0CrF,OAAO,MAAMC,eAAe,OAAOC;IACjC,MAAM,EACJC,EAAE,EACFC,MAAMC,OAAO,EACbC,YAAY,GAAG,EACfC,cAAc,EACdC,QAAQ,KAAK,EACbC,QAAQ,EACRC,QAAQC,eAAe,EACvBC,gBAAgB,EAChBC,MAAM,EACNC,MAAM,EACNC,OAAOC,aAAa,EACpBC,QAAQ,EACRC,QAAQC,eAAe,EACvBC,IAAI,EACJC,GAAG,EACHC,IAAI,EACJC,cAAc,EACdC,MAAM,EACNC,OAAOC,iBAAiB,CAAC,CAAC,E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IACA5F;YACN5C,IAAIO,OAAO,CAACC,MAAM,CAACoC,KAAK,CAAC;gBACvBnC,KAAK;gBACLyI,KAAKH;gBACLrI,aAAa5B;gBACbS;gBACAoJ,UAAU5J;YACZ;YACA,MAAM6D;QACR;IACF;IACA,IAAIzD,OAAO;QACTa,IAAIO,OAAO,CAACC,MAAM,CAACrB,KAAK,CAAC;IAC3B;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/export/createExport.ts"],"sourcesContent":["/* eslint-disable perfectionist/sort-objects */\nimport type { PayloadRequest, Sort, User, Where } from 'payload'\n\nimport { stringify } from 'csv-stringify/sync'\nimport { APIError } from 'payload'\nimport { Readable } from 'stream'\n\nimport type { ExportDoc } from '../types.js'\n\nimport { applyFieldHooks } from '../utilities/applyFieldHooks.js'\nimport { buildDisabledFieldRegex } from '../utilities/buildDisabledFieldRegex.js'\nimport { flattenObject } from '../utilities/flattenObject.js'\nimport { getExportFieldFunctions } from '../utilities/getExportFieldFunctions.js'\nimport { getFilename } from '../utilities/getFilename.js'\nimport { getSchemaColumns, mergeColumns } from '../utilities/getSchemaColumns.js'\nimport { getSelect } from '../utilities/getSelect.js'\nimport { validateLimitValue } from '../utilities/validateLimitValue.js'\nimport { createExportBatchProcessor, type ExportFindArgs } from './batchProcessor.js'\n\nexport type Export = {\n /**\n * Number of documents to process in each batch during export\n * @default 100\n */\n batchSize?: number\n collectionSlug: string\n /**\n * If true, enables debug logging\n */\n debug?: boolean\n drafts?: 'no' | 'yes'\n exportCollection: string\n fields?: string[]\n format: 'csv' | 'json'\n globals?: string[]\n id: number | string\n limit?: number\n locale?: string\n /**\n * Maximum number of documents that can be exported in a single operation.\n * This value has already been resolved from the plugin config.\n */\n maxLimit?: number\n name: string\n page?: number\n sort?: Sort\n userCollection?: string\n userID?: number | string\n where?: Where\n}\n\nexport type CreateExportArgs = {\n /**\n * If true, stream the file instead of saving it\n */\n download?: boolean\n exportDoc: ExportDoc\n req: PayloadRequest\n} & Export\n\nexport const createExport = async (args: CreateExportArgs) => {\n const {\n id,\n name: nameArg,\n batchSize = 100,\n collectionSlug,\n debug = false,\n download,\n drafts: draftsFromInput,\n exportCollection,\n exportDoc,\n fields,\n format,\n limit: incomingLimit,\n maxLimit,\n locale: localeFromInput,\n page,\n req,\n sort,\n userCollection,\n userID,\n where: whereFromInput = {},\n } = args\n const { locale: localeFromReq, payload } = req\n\n if (debug) {\n req.payload.logger.debug({\n msg: '[createExport] Starting export process',\n exportDocId: id,\n exportName: nameArg,\n collectionSlug,\n exportCollection,\n draft: draftsFromInput,\n fields,\n format,\n })\n }\n\n const locale = localeFromInput ?? localeFromReq\n const collectionConfig = payload.config.collections.find(({ slug }) => slug === collectionSlug)\n\n if (!collectionConfig) {\n throw new APIError(`Collection with slug ${collectionSlug} not found.`)\n }\n\n let user: undefined | User\n\n if (userCollection && userID) {\n user = (await req.payload.findByID({\n id: userID,\n collection: userCollection,\n overrideAccess: true,\n })) as User\n }\n\n if (!user && req.user) {\n user = req.user\n }\n\n if (!user) {\n throw new APIError('User authentication is required to create exports.')\n }\n\n const draft = draftsFromInput === 'yes'\n const hasVersions = Boolean(collectionConfig.versions)\n\n // Only filter by _status for versioned collections\n const publishedWhere: Where = hasVersions ? { _status: { equals: 'published' } } : {}\n\n const where: Where = {\n and: [whereFromInput, draft ? {} : publishedWhere],\n }\n\n const baseName = nameArg ?? getFilename()\n const name = `${baseName}-${collectionSlug}.${format}`\n const isCSV = format === 'csv'\n const select = Array.isArray(fields) && fields.length > 0 ? getSelect(fields) : undefined\n\n if (debug) {\n req.payload.logger.debug({ isCSV, locale, msg: 'Export configuration:', name })\n }\n\n // Determine maximum export documents:\n // 1. If maxLimit is defined, it sets the absolute ceiling\n // 2. User's limit is applied but clamped to maxLimit if it exceeds it\n let maxExportDocuments: number | undefined\n\n if (typeof maxLimit === 'number' && maxLimit > 0) {\n if (typeof incomingLimit === 'number' && incomingLimit > 0) {\n // User provided a limit - clamp it to maxLimit\n maxExportDocuments = Math.min(incomingLimit, maxLimit)\n } else {\n // No user limit - use maxLimit as the ceiling\n maxExportDocuments = maxLimit\n }\n } else {\n // No maxLimit - use user's limit if provided\n maxExportDocuments =\n typeof incomingLimit === 'number' && incomingLimit > 0 ? incomingLimit : undefined\n }\n\n // Try to count documents - if access is denied, treat as 0 documents\n let totalDocs = 0\n let accessDenied = false\n try {\n const countResult = await payload.count({\n collection: collectionSlug,\n user,\n locale,\n overrideAccess: false,\n })\n totalDocs = countResult.totalDocs\n } catch (error) {\n // Access denied - user can't read from this collection\n // We'll create an empty export file\n accessDenied = true\n if (debug) {\n req.payload.logger.debug({\n collectionSlug,\n msg: 'Access denied for collection, creating empty export',\n })\n }\n }\n\n const totalPages = Math.max(1, Math.ceil(totalDocs / batchSize))\n const requestedPage = page || 1\n const adjustedPage = requestedPage > totalPages ? 1 : requestedPage\n\n const findArgs = {\n collection: collectionSlug,\n depth: 1,\n draft,\n limit: batchSize,\n locale,\n overrideAccess: false,\n page: 0,\n select,\n sort,\n user,\n where,\n }\n\n if (debug) {\n req.payload.logger.debug({ findArgs, msg: 'Find arguments:' })\n }\n\n const exportFieldHooks = getExportFieldFunctions({\n fields: collectionConfig.flattenedFields,\n })\n\n const exportHooks = collectionConfig.custom?.['plugin-import-export']?.exportHooks\n\n const disabledFields =\n collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? []\n\n const disabledMatchers = disabledFields.map(buildDisabledFieldRegex)\n\n const filterDisabledCSV = (row: Record<string, unknown>): Record<string, unknown> => {\n const filtered: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(row)) {\n const isDisabled = disabledMatchers.some((matcher) => matcher.test(key))\n if (!isDisabled) {\n filtered[key] = value\n }\n }\n\n return filtered\n }\n\n const filterDisabledJSON = (doc: unknown, parentPath = ''): unknown => {\n if (Array.isArray(doc)) {\n return doc.map((item) => filterDisabledJSON(item, parentPath))\n }\n\n if (typeof doc !== 'object' || doc === null) {\n return doc\n }\n\n const filtered: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(doc as Record<string, unknown>)) {\n const currentPath = parentPath ? `${parentPath}.${key}` : key\n\n // Only remove if this exact path is disabled\n const isDisabled = disabledFields.includes(currentPath)\n\n if (!isDisabled) {\n filtered[key] = filterDisabledJSON(value, currentPath)\n }\n }\n\n return filtered\n }\n\n if (download) {\n const limitErrorMsg = validateLimitValue(incomingLimit, req.t)\n if (limitErrorMsg) {\n throw new APIError(limitErrorMsg)\n }\n\n // Get schema-based columns first (provides base ordering and handles empty exports)\n let schemaColumns: string[] = []\n if (isCSV) {\n const localeCodes =\n locale === 'all' && payload.config.localization\n ? payload.config.localization.localeCodes\n : undefined\n\n schemaColumns = getSchemaColumns({\n collectionConfig,\n disabledFields,\n fields,\n locale,\n localeCodes,\n })\n\n if (debug) {\n req.payload.logger.debug({\n columnCount: schemaColumns.length,\n msg: 'Schema-based column inference complete',\n })\n }\n }\n\n // allColumns will be finalized after first batch (schema + data columns merged)\n let allColumns: string[] = []\n let columnsFinalized = false\n\n const encoder = new TextEncoder()\n let isFirstBatch = true\n let currentBatchPage = adjustedPage\n let fetched = 0\n const maxDocs =\n typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY\n\n const effectiveStreamDocs =\n typeof maxExportDocuments === 'number' ? Math.min(totalDocs, maxExportDocuments) : totalDocs\n const totalBatches = effectiveStreamDocs > 0 ? Math.ceil(effectiveStreamDocs / batchSize) : 1\n let streamBatchNumber = 0\n\n const stream = new Readable({\n async read() {\n const remaining = Math.max(0, maxDocs - fetched)\n\n if (remaining === 0) {\n if (!isCSV) {\n // If first batch with no remaining, output empty array; otherwise just close\n this.push(encoder.encode(isFirstBatch ? '[]' : ']'))\n }\n this.push(null)\n return\n }\n\n const result = await payload.find({\n ...findArgs,\n page: currentBatchPage,\n limit: Math.min(batchSize, remaining),\n })\n\n if (debug) {\n req.payload.logger.debug(\n `Streaming batch ${currentBatchPage} with ${result.docs.length} docs`,\n )\n }\n\n if (result.docs.length === 0) {\n // Close JSON array properly if JSON\n if (!isCSV) {\n // If first batch with no docs, output empty array; otherwise just close\n this.push(encoder.encode(isFirstBatch ? '[]' : ']'))\n }\n this.push(null)\n return\n }\n\n streamBatchNumber++\n\n if (isCSV) {\n // --- CSV Streaming ---\n const batchRows = result.docs.map((doc) =>\n filterDisabledCSV(\n flattenObject({\n data: doc as Record<string, unknown>,\n fields,\n format,\n exportFieldHooks,\n req,\n }),\n ),\n )\n\n const originalDocs = result.docs as Record<string, unknown>[]\n let batchRowsToWrite = batchRows\n if (exportHooks?.before && batchRows.length > 0) {\n batchRowsToWrite = await exportHooks.before({\n batchNumber: streamBatchNumber,\n data: batchRows,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n\n // On first batch, discover additional columns from data and merge with schema\n if (!columnsFinalized) {\n const dataColumns: string[] = []\n const seenCols = new Set<string>()\n for (const row of batchRowsToWrite) {\n for (const key of Object.keys(row)) {\n if (!seenCols.has(key)) {\n seenCols.add(key)\n dataColumns.push(key)\n }\n }\n }\n // Merge schema columns with data-discovered columns.\n // When a before hook is active and rows exist, restrict to columns actually present\n // in the data so that the hook can remove fields by not returning them.\n const mergedColumns = mergeColumns(schemaColumns, dataColumns)\n allColumns =\n Boolean(exportHooks?.before) && batchRowsToWrite.length > 0\n ? mergedColumns.filter((col) => dataColumns.includes(col))\n : mergedColumns\n columnsFinalized = true\n\n if (debug) {\n req.payload.logger.debug({\n dataColumnsCount: dataColumns.length,\n finalColumnsCount: allColumns.length,\n msg: 'Merged schema and data columns',\n })\n }\n }\n\n const paddedRows = batchRowsToWrite.map((row) => {\n const fullRow: Record<string, unknown> = {}\n for (const col of allColumns) {\n fullRow[col] = row[col] ?? ''\n }\n return fullRow\n })\n\n const csvString = stringify(paddedRows, {\n bom: isFirstBatch,\n header: isFirstBatch,\n columns: allColumns,\n })\n\n this.push(encoder.encode(csvString))\n\n if (exportHooks?.after && batchRowsToWrite.length > 0) {\n await exportHooks.after({\n batchNumber: streamBatchNumber,\n data: batchRowsToWrite,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n } else {\n // --- JSON Streaming ---\n const batchRows = result.docs.map(\n (doc) =>\n filterDisabledJSON(\n applyFieldHooks({\n data: doc as Record<string, unknown>,\n fieldHooks: exportFieldHooks,\n fields: collectionConfig.flattenedFields,\n format,\n operation: 'export',\n req,\n type: 'beforeExport',\n }),\n ) as Record<string, unknown>,\n )\n\n const originalDocs = result.docs as Record<string, unknown>[]\n let batchRowsToWrite = batchRows\n if (exportHooks?.before && batchRows.length > 0) {\n batchRowsToWrite = await exportHooks.before({\n batchNumber: streamBatchNumber,\n data: batchRows,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n\n // Convert each filtered/flattened row into JSON string\n const batchJSON = batchRowsToWrite.map((row) => JSON.stringify(row)).join(',')\n\n if (isFirstBatch) {\n this.push(encoder.encode('[' + batchJSON))\n } else {\n this.push(encoder.encode(',' + batchJSON))\n }\n\n if (exportHooks?.after && batchRowsToWrite.length > 0) {\n await exportHooks.after({\n batchNumber: streamBatchNumber,\n data: batchRowsToWrite,\n exportDoc,\n format,\n originalData: originalDocs,\n req,\n totalBatches,\n })\n }\n }\n\n fetched += result.docs.length\n isFirstBatch = false\n currentBatchPage += 1\n\n if (!result.hasNextPage || fetched >= maxDocs) {\n if (debug) {\n req.payload.logger.debug('Stream complete - no more pages')\n }\n if (!isCSV) {\n this.push(encoder.encode(']'))\n }\n this.push(null) // End the stream\n }\n },\n })\n\n return new Response(Readable.toWeb(stream) as ReadableStream, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${name}\"`,\n 'Content-Type': isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n },\n })\n }\n\n // Non-download path (buffered export)\n if (debug) {\n req.payload.logger.debug('Starting file generation')\n }\n\n // Create export batch processor\n const processor = createExportBatchProcessor({ batchSize, debug })\n\n // Transform function based on format\n const transformDoc = (doc: unknown) =>\n isCSV\n ? filterDisabledCSV(\n flattenObject({\n data: doc as Record<string, unknown>,\n fields,\n format,\n exportFieldHooks,\n req,\n }),\n )\n : (filterDisabledJSON(\n applyFieldHooks({\n data: doc as Record<string, unknown>,\n fieldHooks: exportFieldHooks,\n fields: collectionConfig.flattenedFields,\n format,\n operation: 'export',\n req,\n type: 'beforeExport',\n }),\n ) as Record<string, unknown>)\n\n // Skip fetching if access was denied - we'll create an empty export\n let exportResult = {\n columns: [] as string[],\n docs: [] as Record<string, unknown>[],\n fetchedCount: 0,\n }\n\n if (!accessDenied) {\n exportResult = await processor.processExport({\n collectionSlug,\n exportDoc,\n findArgs: findArgs as ExportFindArgs,\n format,\n hooks: exportHooks,\n maxDocs:\n typeof maxExportDocuments === 'number' ? maxExportDocuments : Number.POSITIVE_INFINITY,\n req,\n startPage: adjustedPage,\n totalDocs,\n transformDoc,\n })\n }\n\n const { columns: dataColumns, docs: rows } = exportResult\n const outputData: string[] = []\n\n // Prepare final output\n if (isCSV) {\n // Get schema-based columns for consistent ordering\n const localeCodes =\n locale === 'all' && payload.config.localization\n ? payload.config.localization.localeCodes\n : undefined\n\n const schemaColumns = getSchemaColumns({\n collectionConfig,\n disabledFields,\n fields,\n locale,\n localeCodes,\n })\n\n // Merge schema columns with data-discovered columns\n // Schema provides ordering, data provides additional columns (e.g., array indices > 0)\n // When a before hook is active and rows exist, restrict to columns actually present in the data\n // so that the hook can remove fields by not returning them\n const mergedColumns = mergeColumns(schemaColumns, dataColumns)\n const finalColumns =\n Boolean(exportHooks?.before) && rows.length > 0\n ? mergedColumns.filter((col) => dataColumns.includes(col))\n : mergedColumns\n\n const paddedRows = rows.map((row) => {\n const fullRow: Record<string, unknown> = {}\n for (const col of finalColumns) {\n fullRow[col] = row[col] ?? ''\n }\n return fullRow\n })\n\n // Always output CSV with header, even if empty\n outputData.push(\n stringify(paddedRows, {\n bom: true,\n header: true,\n columns: finalColumns,\n }),\n )\n } else {\n // JSON format\n outputData.push(rows.map((doc) => JSON.stringify(doc)).join(',\\n'))\n }\n\n // Ensure we always have valid content for the file\n // For JSON, empty exports produce \"[]\"\n // For CSV, if completely empty (no columns, no rows), produce at least a newline to ensure file creation\n const content = format === 'json' ? `[${outputData.join(',')}]` : outputData.join('')\n const buffer = Buffer.from(content.length > 0 ? content : '\\n')\n if (debug) {\n req.payload.logger.debug(`${format} file generation complete`)\n }\n\n if (!id) {\n if (debug) {\n req.payload.logger.debug('Creating new export file')\n }\n req.file = {\n name,\n data: buffer,\n mimetype: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n size: buffer.length,\n }\n } else {\n if (debug) {\n req.payload.logger.debug({\n msg: '[createExport] Updating export document with file',\n exportDocId: id,\n exportCollection,\n fileName: name,\n fileSize: buffer.length,\n mimeType: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n })\n }\n try {\n await req.payload.update({\n id,\n collection: exportCollection,\n data: {},\n file: {\n name,\n data: buffer,\n mimetype: isCSV ? 'text/csv; charset=utf-8' : 'application/json',\n size: buffer.length,\n },\n // Override access only here so that we can be sure the export collection itself is updated as expected\n overrideAccess: true,\n req,\n })\n } catch (error) {\n const errorDetails =\n error instanceof Error\n ? {\n message: error.message,\n name: error.name,\n stack: error.stack,\n // @ts-expect-error - data might exist on Payload errors\n data: error.data,\n }\n : error\n req.payload.logger.error({\n msg: '[createExport] Failed to update export document with file',\n err: errorDetails,\n exportDocId: id,\n exportCollection,\n fileName: name,\n })\n throw error\n }\n }\n if (debug) {\n req.payload.logger.debug('Export process completed successfully')\n }\n}\n"],"names":["stringify","APIError","Readable","applyFieldHooks","buildDisabledFieldRegex","flattenObject","getExportFieldFunctions","getFilename","getSchemaColumns","mergeColumns","getSelect","validateLimitValue","createExportBatchProcessor","createExport","args","id","name","nameArg","batchSize","collectionSlug","debug","download","drafts","draftsFromInput","exportCollection","exportDoc","fields","format","limit","incomingLimit","maxLimit","locale","localeFromInput","page","req","sort","userCollection","userID","where","whereFromInput","localeFromReq","payload","logger","msg","exportDocId","exportName","draft","collectionConfig","config","collections","find","slug","user","findByID","collection","overrideAccess","hasVersions","Boolean","versions","publishedWhere","_status","equals","and","baseName","isCSV","select","Array","isArray","length","undefined","maxExportDocuments","Math","min","totalDocs","accessDenied","countResult","count","error","totalPages","max","ceil","requestedPage","adjustedPage","findArgs","depth","exportFieldHooks","flattenedFields","exportHooks","custom","disabledFields","admin","disabledMatchers","map","filterDisabledCSV","row","filtered","key","value","Object","entries","isDisabled","some","matcher","test","filterDisabledJSON","doc","parentPath","item","currentPath","includes","limitErrorMsg","t","schemaColumns","localeCodes","localization","columnCount","allColumns","columnsFinalized","encoder","TextEncoder","isFirstBatch","currentBatchPage","fetched","maxDocs","Number","POSITIVE_INFINITY","effectiveStreamDocs","totalBatches","streamBatchNumber","stream","read","remaining","push","encode","result","docs","batchRows","data","originalDocs","batchRowsToWrite","before","batchNumber","originalData","dataColumns","seenCols","Set","keys","has","add","mergedColumns","filter","col","dataColumnsCount","finalColumnsCount","paddedRows","fullRow","csvString","bom","header","columns","after","fieldHooks","operation","type","batchJSON","JSON","join","hasNextPage","Response","toWeb","headers","processor","transformDoc","exportResult","fetchedCount","processExport","hooks","startPage","rows","outputData","finalColumns","content","buffer","Buffer","from","file","mimetype","size","fileName","fileSize","mimeType","update","errorDetails","Error","message","stack","err"],"mappings":"AAAA,6CAA6C,GAG7C,SAASA,SAAS,QAAQ,qBAAoB;AAC9C,SAASC,QAAQ,QAAQ,UAAS;AAClC,SAASC,QAAQ,QAAQ,SAAQ;AAIjC,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,aAAa,QAAQ,gCAA+B;AAC7D,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,gBAAgB,EAAEC,YAAY,QAAQ,mCAAkC;AACjF,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,kBAAkB,QAAQ,qCAAoC;AACvE,SAASC,0BAA0B,QAA6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/D;IAEA,IAAI,CAACZ,IAAI;QACP,IAAIK,OAAO;YACTc,IAAIO,OAAO,CAACC,MAAM,CAACtB,KAAK,CAAC;QAC3B;QACAc,IAAIuJ,IAAI,GAAG;YACTzK;YACAyH,MAAM6C;YACNI,UAAU1H,QAAQ,4BAA4B;YAC9C2H,MAAML,OAAOlH,MAAM;QACrB;IACF,OAAO;QACL,IAAIhD,OAAO;YACTc,IAAIO,OAAO,CAACC,MAAM,CAACtB,KAAK,CAAC;gBACvBuB,KAAK;gBACLC,aAAa7B;gBACbS;gBACAoK,UAAU5K;gBACV6K,UAAUP,OAAOlH,MAAM;gBACvB0H,UAAU9H,QAAQ,4BAA4B;YAChD;QACF;QACA,IAAI;YACF,MAAM9B,IAAIO,OAAO,CAACsJ,MAAM,CAAC;gBACvBhL;gBACAuC,YAAY9B;gBACZiH,MAAM,CAAC;gBACPgD,MAAM;oBACJzK;oBACAyH,MAAM6C;oBACNI,UAAU1H,QAAQ,4BAA4B;oBAC9C2H,MAAML,OAAOlH,MAAM;gBACrB;gBACA,uGAAuG;gBACvGb,gBAAgB;gBAChBrB;YACF;QACF,EAAE,OAAO2C,OAAO;YACd,MAAMmH,eACJnH,iBAAiBoH,QACb;gBACEC,SAASrH,MAAMqH,OAAO;gBACtBlL,MAAM6D,MAAM7D,IAAI;gBAChBmL,OAAOtH,MAAMsH,KAAK;gBAClB,wDAAwD;gBACxD1D,MAAM5D,MAAM4D,IAAI;YAClB,IACA5D;YACN3C,IAAIO,OAAO,CAACC,MAAM,CAACmC,KAAK,CAAC;gBACvBlC,KAAK;gBACLyJ,KAAKJ;gBACLpJ,aAAa7B;gBACbS;gBACAoK,UAAU5K;YACZ;YACA,MAAM6D;QACR;IACF;IACA,IAAIzD,OAAO;QACTc,IAAIO,OAAO,CAACC,MAAM,CAACtB,KAAK,CAAC;IAC3B;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getCreateExportCollectionTask.d.ts","sourceRoot":"","sources":["../../src/export/getCreateExportCollectionTask.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAS,UAAU,EAAE,MAAM,SAAS,CAAA;AAExD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAI/C;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;IACrB,cAAc,EAAE,MAAM,CAAA;CACvB,GAAG,MAAM,CAAA;AAmGV,eAAO,MAAM,6BAA6B,WAChC,MAAM,KACb,UAAU,CAAC;IACZ,KAAK,EAAE,cAAc,CAAA;IACrB,MAAM,EAAE,MAAM,CAAA;CACf,CAuBA,CAAA"}
1
+ {"version":3,"file":"getCreateExportCollectionTask.d.ts","sourceRoot":"","sources":["../../src/export/getCreateExportCollectionTask.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAS,UAAU,EAAE,MAAM,SAAS,CAAA;AAExD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAI/C;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;IACrB,cAAc,EAAE,MAAM,CAAA;CACvB,GAAG,MAAM,CAAA;AAmGV,eAAO,MAAM,6BAA6B,WAChC,MAAM,KACb,UAAU,CAAC;IACZ,KAAK,EAAE,cAAc,CAAA;IACrB,MAAM,EAAE,MAAM,CAAA;CACf,CAoCA,CAAA"}
@@ -115,8 +115,19 @@ export const getCreateCollectionExportTask = (config)=>{
115
115
  output: {}
116
116
  };
117
117
  }
118
+ // The job input omits custom fields, so read the export document once for the hooks.
119
+ const exportDoc = await req.payload.findByID({
120
+ id: input.id,
121
+ collection: input.exportCollection,
122
+ overrideAccess: true,
123
+ req
124
+ });
125
+ if (!exportDoc) {
126
+ throw new Error(`Export document not found: ${input.id}`);
127
+ }
118
128
  await createExport({
119
129
  ...input,
130
+ exportDoc,
120
131
  req
121
132
  });
122
133
  return {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/export/getCreateExportCollectionTask.ts"],"sourcesContent":["import type { Config, Field, TaskConfig } from 'payload'\n\nimport type { Export } from './createExport.js'\n\nimport { createExport } from './createExport.js'\n\n/**\n * Export input type for job queue serialization.\n * When exports are queued as jobs, the user must be serialized as an ID string or number\n * along with the collection name so it can be rehydrated when the job runs.\n */\nexport type ExportJobInput = {\n user: number | string\n userCollection: string\n} & Export\n\n/**\n * Creates a minimal inputSchema for the job queue task.\n */\nconst getJobInputSchema = (config: Config): Field[] => {\n const allCollectionSlugs = config.collections?.map((c) => c.slug) || []\n\n const collectionOptions = allCollectionSlugs.map((slug) => {\n const collectionConfig = config.collections?.find((c) => c.slug === slug)\n return {\n label: collectionConfig?.labels?.singular || slug,\n value: slug,\n }\n })\n\n return [\n {\n name: 'id',\n type: 'text',\n required: true,\n },\n {\n name: 'name',\n type: 'text',\n required: true,\n },\n {\n name: 'batchSize',\n type: 'number',\n },\n {\n name: 'collectionSlug',\n type: 'select',\n options: collectionOptions,\n required: true,\n },\n {\n name: 'drafts',\n type: 'select',\n options: [\n { label: 'Yes', value: 'yes' },\n { label: 'No', value: 'no' },\n ],\n },\n {\n name: 'exportCollection',\n type: 'text',\n required: true,\n },\n {\n name: 'fields',\n type: 'text',\n hasMany: true,\n },\n {\n name: 'format',\n type: 'select',\n options: [\n { label: 'CSV', value: 'csv' },\n { label: 'JSON', value: 'json' },\n ],\n required: true,\n },\n {\n name: 'limit',\n type: 'number',\n },\n {\n name: 'locale',\n type: 'text',\n },\n {\n name: 'maxLimit',\n type: 'number',\n },\n {\n name: 'page',\n type: 'number',\n },\n {\n name: 'sort',\n type: 'text',\n },\n {\n name: 'userCollection',\n type: 'text',\n },\n {\n name: 'userID',\n type: 'text',\n },\n {\n name: 'where',\n type: 'json',\n },\n ]\n}\n\nexport const getCreateCollectionExportTask = (\n config: Config,\n): TaskConfig<{\n input: ExportJobInput\n output: object\n}> => {\n const inputSchema = getJobInputSchema(config)\n\n return {\n slug: 'createCollectionExport',\n handler: async ({ input, req }) => {\n if (!input) {\n req.payload.logger.error('No input provided to createCollectionExport task')\n\n return { output: {} }\n }\n\n await createExport({\n ...input,\n req,\n })\n\n return {\n output: {},\n }\n },\n inputSchema,\n }\n}\n"],"names":["createExport","getJobInputSchema","config","allCollectionSlugs","collections","map","c","slug","collectionOptions","collectionConfig","find","label","labels","singular","value","name","type","required","options","hasMany","getCreateCollectionExportTask","inputSchema","handler","input","req","payload","logger","error","output"],"mappings":"AAIA,SAASA,YAAY,QAAQ,oBAAmB;AAYhD;;CAEC,GACD,MAAMC,oBAAoB,CAACC;IACzB,MAAMC,qBAAqBD,OAAOE,WAAW,EAAEC,IAAI,CAACC,IAAMA,EAAEC,IAAI,KAAK,EAAE;IAEvE,MAAMC,oBAAoBL,mBAAmBE,GAAG,CAAC,CAACE;QAChD,MAAME,mBAAmBP,OAAOE,WAAW,EAAEM,KAAK,CAACJ,IAAMA,EAAEC,IAAI,KAAKA;QACpE,OAAO;YACLI,OAAOF,kBAAkBG,QAAQC,YAAYN;YAC7CO,OAAOP;QACT;IACF;IAEA,OAAO;QACL;YACEQ,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;YACNE,SAASV;YACTS,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNE,SAAS;gBACP;oBAAEP,OAAO;oBAAOG,OAAO;gBAAM;gBAC7B;oBAAEH,OAAO;oBAAMG,OAAO;gBAAK;aAC5B;QACH;QACA;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNG,SAAS;QACX;QACA;YACEJ,MAAM;YACNC,MAAM;YACNE,SAAS;gBACP;oBAAEP,OAAO;oBAAOG,OAAO;gBAAM;gBAC7B;oBAAEH,OAAO;oBAAQG,OAAO;gBAAO;aAChC;YACDG,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;KACD;AACH;AAEA,OAAO,MAAMI,gCAAgC,CAC3ClB;IAKA,MAAMmB,cAAcpB,kBAAkBC;IAEtC,OAAO;QACLK,MAAM;QACNe,SAAS,OAAO,EAAEC,KAAK,EAAEC,GAAG,EAAE;YAC5B,IAAI,CAACD,OAAO;gBACVC,IAAIC,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBAEzB,OAAO;oBAAEC,QAAQ,CAAC;gBAAE;YACtB;YAEA,MAAM5B,aAAa;gBACjB,GAAGuB,KAAK;gBACRC;YACF;YAEA,OAAO;gBACLI,QAAQ,CAAC;YACX;QACF;QACAP;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/export/getCreateExportCollectionTask.ts"],"sourcesContent":["import type { Config, Field, TaskConfig } from 'payload'\n\nimport type { Export } from './createExport.js'\n\nimport { createExport } from './createExport.js'\n\n/**\n * Export input type for job queue serialization.\n * When exports are queued as jobs, the user must be serialized as an ID string or number\n * along with the collection name so it can be rehydrated when the job runs.\n */\nexport type ExportJobInput = {\n user: number | string\n userCollection: string\n} & Export\n\n/**\n * Creates a minimal inputSchema for the job queue task.\n */\nconst getJobInputSchema = (config: Config): Field[] => {\n const allCollectionSlugs = config.collections?.map((c) => c.slug) || []\n\n const collectionOptions = allCollectionSlugs.map((slug) => {\n const collectionConfig = config.collections?.find((c) => c.slug === slug)\n return {\n label: collectionConfig?.labels?.singular || slug,\n value: slug,\n }\n })\n\n return [\n {\n name: 'id',\n type: 'text',\n required: true,\n },\n {\n name: 'name',\n type: 'text',\n required: true,\n },\n {\n name: 'batchSize',\n type: 'number',\n },\n {\n name: 'collectionSlug',\n type: 'select',\n options: collectionOptions,\n required: true,\n },\n {\n name: 'drafts',\n type: 'select',\n options: [\n { label: 'Yes', value: 'yes' },\n { label: 'No', value: 'no' },\n ],\n },\n {\n name: 'exportCollection',\n type: 'text',\n required: true,\n },\n {\n name: 'fields',\n type: 'text',\n hasMany: true,\n },\n {\n name: 'format',\n type: 'select',\n options: [\n { label: 'CSV', value: 'csv' },\n { label: 'JSON', value: 'json' },\n ],\n required: true,\n },\n {\n name: 'limit',\n type: 'number',\n },\n {\n name: 'locale',\n type: 'text',\n },\n {\n name: 'maxLimit',\n type: 'number',\n },\n {\n name: 'page',\n type: 'number',\n },\n {\n name: 'sort',\n type: 'text',\n },\n {\n name: 'userCollection',\n type: 'text',\n },\n {\n name: 'userID',\n type: 'text',\n },\n {\n name: 'where',\n type: 'json',\n },\n ]\n}\n\nexport const getCreateCollectionExportTask = (\n config: Config,\n): TaskConfig<{\n input: ExportJobInput\n output: object\n}> => {\n const inputSchema = getJobInputSchema(config)\n\n return {\n slug: 'createCollectionExport',\n handler: async ({ input, req }) => {\n if (!input) {\n req.payload.logger.error('No input provided to createCollectionExport task')\n\n return { output: {} }\n }\n\n // The job input omits custom fields, so read the export document once for the hooks.\n const exportDoc = await req.payload.findByID({\n id: input.id,\n collection: input.exportCollection,\n overrideAccess: true,\n req,\n })\n\n if (!exportDoc) {\n throw new Error(`Export document not found: ${input.id}`)\n }\n\n await createExport({\n ...input,\n exportDoc,\n req,\n })\n\n return {\n output: {},\n }\n },\n inputSchema,\n }\n}\n"],"names":["createExport","getJobInputSchema","config","allCollectionSlugs","collections","map","c","slug","collectionOptions","collectionConfig","find","label","labels","singular","value","name","type","required","options","hasMany","getCreateCollectionExportTask","inputSchema","handler","input","req","payload","logger","error","output","exportDoc","findByID","id","collection","exportCollection","overrideAccess","Error"],"mappings":"AAIA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,MAAMI,gCAAgC,CAC3ClB;IAKA,MAAMmB,cAAcpB,kBAAkBC;IAEtC,OAAO;QACLK,MAAM;QACNe,SAAS,OAAO,EAAEC,KAAK,EAAEC,GAAG,EAAE;YAC5B,IAAI,CAACD,OAAO;gBACVC,IAAIC,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBAEzB,OAAO;oBAAEC,QAAQ,CAAC;gBAAE;YACtB;YAEA,qFAAqF;YACrF,MAAMC,YAAY,MAAML,IAAIC,OAAO,CAACK,QAAQ,CAAC;gBAC3CC,IAAIR,MAAMQ,EAAE;gBACZC,YAAYT,MAAMU,gBAAgB;gBAClCC,gBAAgB;gBAChBV;YACF;YAEA,IAAI,CAACK,WAAW;gBACd,MAAM,IAAIM,MAAM,CAAC,2BAA2B,EAAEZ,MAAMQ,EAAE,EAAE;YAC1D;YAEA,MAAM/B,aAAa;gBACjB,GAAGuB,KAAK;gBACRM;gBACAL;YACF;YAEA,OAAO;gBACLI,QAAQ,CAAC;YACX;QACF;QACAP;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getExportCollection.d.ts","sourceRoot":"","sources":["../../src/export/getExportCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,gBAAgB,EAChB,MAAM,EACP,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,YAAY,EAAE,wBAAwB,EAAS,MAAM,aAAa,CAAA;AAWhF,eAAO,MAAM,mBAAmB,6DAK7B;IACD;;OAEG;IACH,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,YAAY,EAAE,wBAAwB,CAAA;CACvC,KAAG,gBA8JH,CAAA"}
1
+ {"version":3,"file":"getExportCollection.d.ts","sourceRoot":"","sources":["../../src/export/getExportCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,gBAAgB,EAChB,MAAM,EACP,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,YAAY,EAAE,wBAAwB,EAAS,MAAM,aAAa,CAAA;AAYhF,eAAO,MAAM,mBAAmB,6DAK7B;IACD;;OAEG;IACH,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,YAAY,EAAE,wBAAwB,CAAA;CACvC,KAAG,gBAgKH,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { getSubmittedFormValues } from '../utilities/getSubmittedFormValues.js';
1
2
  import { resolveLimit } from '../utilities/resolveLimit.js';
2
3
  import { createExport } from './createExport.js';
3
4
  import { getFields } from './getFields.js';
@@ -89,10 +90,13 @@ export const getExportCollection = ({ collectionSlugs, config, exportConfig, plu
89
90
  batchSize,
90
91
  debug,
91
92
  exportCollection: collectionConfig.slug,
93
+ exportDoc: getSubmittedFormValues({
94
+ formData: exportData
95
+ }),
92
96
  maxLimit,
93
97
  req,
94
- userCollection: user?.collection || user?.user?.collection,
95
- userID: user?.id || user?.user?.id
98
+ userCollection: user?.collection,
99
+ userID: user?.id
96
100
  });
97
101
  });
98
102
  afterChange.push(async ({ collection: collectionConfig, doc, operation, req })=>{
@@ -129,12 +133,13 @@ export const getExportCollection = ({ collectionSlugs, config, exportConfig, plu
129
133
  maxLimit,
130
134
  page: doc.page,
131
135
  sort: doc.sort,
132
- userCollection: user?.collection || user?.user?.collection,
133
- userID: user?.id || user?.user?.id,
136
+ userCollection: user?.collection,
137
+ userID: user?.id,
134
138
  where: doc.where
135
139
  };
136
140
  await req.payload.jobs.queue({
137
141
  input,
142
+ overrideAccess: true,
138
143
  task: 'createCollectionExport'
139
144
  });
140
145
  });