@payloadcms/plugin-import-export 3.84.1 → 3.85.0
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.
- package/dist/export/batchProcessor.d.ts +9 -1
- package/dist/export/batchProcessor.d.ts.map +1 -1
- package/dist/export/batchProcessor.js +57 -15
- package/dist/export/batchProcessor.js.map +1 -1
- package/dist/export/createExport.d.ts.map +1 -1
- package/dist/export/createExport.js +98 -20
- package/dist/export/createExport.js.map +1 -1
- package/dist/export/handlePreview.d.ts.map +1 -1
- package/dist/export/handlePreview.js +38 -13
- package/dist/export/handlePreview.js.map +1 -1
- package/dist/exports/types.d.ts +1 -1
- package/dist/exports/types.d.ts.map +1 -1
- package/dist/exports/types.js.map +1 -1
- package/dist/import/batchProcessor.d.ts +14 -2
- package/dist/import/batchProcessor.d.ts.map +1 -1
- package/dist/import/batchProcessor.js +49 -27
- package/dist/import/batchProcessor.js.map +1 -1
- package/dist/import/createImport.d.ts +1 -10
- package/dist/import/createImport.d.ts.map +1 -1
- package/dist/import/createImport.js +33 -52
- package/dist/import/createImport.js.map +1 -1
- package/dist/import/handlePreview.d.ts.map +1 -1
- package/dist/import/handlePreview.js +32 -6
- package/dist/import/handlePreview.js.map +1 -1
- package/dist/index.d.ts +58 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +218 -39
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utilities/applyFieldHooks.d.ts +23 -0
- package/dist/utilities/applyFieldHooks.d.ts.map +1 -0
- package/dist/utilities/applyFieldHooks.js +118 -0
- package/dist/utilities/applyFieldHooks.js.map +1 -0
- package/dist/utilities/applyFieldHooks.spec.js +205 -0
- package/dist/utilities/applyFieldHooks.spec.js.map +1 -0
- package/dist/utilities/collectDisabledFieldPaths.d.ts.map +1 -1
- package/dist/utilities/collectDisabledFieldPaths.js +1 -1
- package/dist/utilities/collectDisabledFieldPaths.js.map +1 -1
- package/dist/utilities/flattenObject.d.ts +8 -6
- package/dist/utilities/flattenObject.d.ts.map +1 -1
- package/dist/utilities/flattenObject.js +95 -75
- package/dist/utilities/flattenObject.js.map +1 -1
- package/dist/utilities/flattenObject.spec.js +158 -0
- package/dist/utilities/flattenObject.spec.js.map +1 -0
- package/dist/utilities/flattenedFields.d.ts +21 -0
- package/dist/utilities/flattenedFields.d.ts.map +1 -0
- package/dist/utilities/flattenedFields.js +34 -0
- package/dist/utilities/flattenedFields.js.map +1 -0
- package/dist/utilities/getExportFieldFunctions.d.ts +5 -5
- package/dist/utilities/getExportFieldFunctions.d.ts.map +1 -1
- package/dist/utilities/getExportFieldFunctions.js +92 -98
- package/dist/utilities/getExportFieldFunctions.js.map +1 -1
- package/dist/utilities/getExportFieldFunctions.spec.js +50 -0
- package/dist/utilities/getExportFieldFunctions.spec.js.map +1 -0
- package/dist/utilities/getImportFieldFunctions.d.ts +5 -5
- package/dist/utilities/getImportFieldFunctions.d.ts.map +1 -1
- package/dist/utilities/getImportFieldFunctions.js +103 -103
- package/dist/utilities/getImportFieldFunctions.js.map +1 -1
- package/dist/utilities/getImportFieldFunctions.spec.js +167 -0
- package/dist/utilities/getImportFieldFunctions.spec.js.map +1 -0
- package/dist/utilities/isPlainObject.d.ts +2 -0
- package/dist/utilities/isPlainObject.d.ts.map +1 -0
- package/dist/utilities/isPlainObject.js +3 -0
- package/dist/utilities/isPlainObject.js.map +1 -0
- package/dist/utilities/legacyHookDispatch.spec.js +227 -0
- package/dist/utilities/legacyHookDispatch.spec.js.map +1 -0
- package/dist/utilities/polymorphicRel.d.ts +14 -0
- package/dist/utilities/polymorphicRel.d.ts.map +1 -0
- package/dist/utilities/polymorphicRel.js +17 -0
- package/dist/utilities/polymorphicRel.js.map +1 -0
- package/dist/utilities/processRichTextField.js.map +1 -1
- package/dist/utilities/removeDisabledFields.js.map +1 -1
- package/dist/utilities/setNestedValue.d.ts.map +1 -1
- package/dist/utilities/setNestedValue.js +10 -8
- package/dist/utilities/setNestedValue.js.map +1 -1
- package/dist/utilities/siblingDoc.spec.js +278 -0
- package/dist/utilities/siblingDoc.spec.js.map +1 -0
- package/dist/utilities/unflattenObject.d.ts +4 -3
- package/dist/utilities/unflattenObject.d.ts.map +1 -1
- package/dist/utilities/unflattenObject.js +57 -169
- package/dist/utilities/unflattenObject.js.map +1 -1
- package/dist/utilities/unflattenObject.spec.js +33 -0
- package/dist/utilities/unflattenObject.spec.js.map +1 -1
- package/dist/utilities/unflattenPostProcess.d.ts +11 -0
- package/dist/utilities/unflattenPostProcess.d.ts.map +1 -0
- package/dist/utilities/unflattenPostProcess.js +148 -0
- package/dist/utilities/unflattenPostProcess.js.map +1 -0
- package/package.json +7 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/import/batchProcessor.ts"],"sourcesContent":["import type { PayloadRequest, TypedUser } from 'payload'\n\nimport { isolateObjectProperty } from 'payload'\n\nimport type { ImportMode, ImportResult } from './createImport.js'\n\nimport {\n type BatchError,\n categorizeError,\n createBatches,\n extractErrorMessage,\n} from '../utilities/useBatchProcessor.js'\n\n/**\n * Import-specific batch processor options\n */\nexport interface ImportBatchProcessorOptions {\n batchSize?: number\n defaultVersionStatus?: 'draft' | 'published'\n}\n\n/**\n * Import-specific error type extending the generic BatchError\n */\nexport interface ImportError extends BatchError<Record<string, unknown>> {\n documentData: Record<string, unknown>\n field?: string\n fieldLabel?: string\n rowNumber: number // 1-indexed for user clarity\n}\n\n/**\n * Result from processing a single import batch\n */\nexport interface ImportBatchResult {\n failed: Array<ImportError>\n successful: Array<{\n document: Record<string, unknown>\n index: number\n operation?: 'created' | 'updated'\n result: Record<string, unknown>\n }>\n}\n\n/**\n * Options for processing an import operation\n */\nexport interface ImportProcessOptions {\n collectionSlug: string\n documents: Record<string, unknown>[]\n importMode: ImportMode\n matchField?: string\n req: PayloadRequest\n user?: TypedUser\n}\n\n/**\n * Separates multi-locale data from a document for sequential locale updates.\n *\n * When a field has locale-keyed values (e.g., { title: { en: 'Hello', es: 'Hola' } }),\n * this extracts the default locale's data for initial create/update, and stores\n * remaining locales for subsequent update calls.\n *\n * @returns\n * - flatData: Document with default locale values extracted (for initial operation)\n * - hasMultiLocale: Whether any multi-locale fields were found\n * - localeUpdates: Map of locale -> field data for follow-up updates\n */\nfunction extractMultiLocaleData(\n data: Record<string, unknown>,\n configuredLocales?: string[],\n defaultLocale?: string,\n): {\n flatData: Record<string, unknown>\n hasMultiLocale: boolean\n localeUpdates: Record<string, Record<string, unknown>>\n} {\n const flatData: Record<string, unknown> = {}\n const localeUpdates: Record<string, Record<string, unknown>> = {}\n let hasMultiLocale = false\n\n if (!configuredLocales || configuredLocales.length === 0) {\n return { flatData: { ...data }, hasMultiLocale: false, localeUpdates: {} }\n }\n\n const localeSet = new Set(configuredLocales)\n\n for (const [key, value] of Object.entries(data)) {\n if (value && typeof value === 'object' && !Array.isArray(value)) {\n const valueObj = value as Record<string, unknown>\n const localeKeys = Object.keys(valueObj).filter((k) => localeSet.has(k))\n\n if (localeKeys.length > 0) {\n hasMultiLocale = true\n const baseLocale =\n defaultLocale && localeKeys.includes(defaultLocale) ? defaultLocale : localeKeys[0]\n if (baseLocale) {\n flatData[key] = valueObj[baseLocale]\n for (const locale of localeKeys) {\n if (locale !== baseLocale) {\n if (!localeUpdates[locale]) {\n localeUpdates[locale] = {}\n }\n localeUpdates[locale][key] = valueObj[locale]\n }\n }\n }\n } else {\n flatData[key] = value\n }\n } else {\n flatData[key] = value\n }\n }\n\n return { flatData, hasMultiLocale, localeUpdates }\n}\n\ntype ProcessImportBatchOptions = {\n batch: Record<string, unknown>[]\n batchIndex: number\n collectionSlug: string\n importMode: ImportMode\n matchField: string | undefined\n options: { batchSize: number; defaultVersionStatus: 'draft' | 'published' }\n req: PayloadRequest\n user?: TypedUser\n}\n\n/**\n * Processes a batch of documents for import based on the import mode.\n *\n * For each document in the batch:\n * - create: Creates a new document (removes any existing ID)\n * - update: Finds existing document by matchField and updates it\n * - upsert: Updates if found, creates if not found\n *\n * Handles versioned collections, multi-locale data, and MongoDB ObjectID validation.\n * Continues processing remaining documents even if individual imports fail.\n */\nasync function processImportBatch({\n batch,\n batchIndex,\n collectionSlug,\n importMode,\n matchField,\n options,\n req: reqFromArgs,\n user,\n}: ProcessImportBatchOptions): Promise<ImportBatchResult> {\n const result: ImportBatchResult = {\n failed: [],\n successful: [],\n }\n // Create a request proxy that isolates the transactionID property, then clear it.\n // This is critical because if a nested operation fails (e.g., Forbidden due to access control),\n // Payload's error handling calls killTransaction(req), which would kill the parent's transaction\n // if we shared the same transaction. By isolating and clearing transactionID, each nested\n // operation either uses no transaction or starts its own, independent of the parent.\n const req = isolateObjectProperty(reqFromArgs, 'transactionID')\n req.transactionID = undefined\n\n const collectionEntry = req.payload.collections[collectionSlug]\n\n const collectionConfig = collectionEntry?.config\n const collectionHasVersions = Boolean(collectionConfig?.versions)\n const hasCustomIdField = Boolean(collectionEntry?.customIDType)\n\n const configuredLocales = req.payload.config.localization\n ? req.payload.config.localization.localeCodes\n : undefined\n\n const defaultLocale = req.payload.config.localization\n ? req.payload.config.localization.defaultLocale\n : undefined\n\n const startingRowNumber = batchIndex * options.batchSize\n\n for (let i = 0; i < batch.length; i++) {\n const document = batch[i]\n if (!document) {\n continue\n }\n const rowNumber = startingRowNumber + i + 1\n\n try {\n let savedDocument: Record<string, unknown> | undefined\n let existingDocResult: { docs: Array<Record<string, unknown>> } | undefined\n\n if (importMode === 'create') {\n const createData = { ...document }\n if (!hasCustomIdField) {\n delete createData.id\n }\n\n let draftOption: boolean | undefined\n if (collectionHasVersions) {\n const statusValue = createData._status || options.defaultVersionStatus\n const isPublished = statusValue !== 'draft'\n draftOption = !isPublished\n createData._status = statusValue\n\n if (req.payload.config.debug) {\n req.payload.logger.info({\n _status: createData._status,\n isPublished,\n msg: 'Status handling in create',\n willSetDraft: draftOption,\n })\n }\n }\n\n if (req.payload.config.debug && 'title' in createData) {\n req.payload.logger.info({\n msg: 'Creating document',\n title: createData.title,\n titleIsNull: createData.title === null,\n titleType: typeof createData.title,\n })\n }\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n createData,\n configuredLocales,\n defaultLocale,\n )\n\n if (hasMultiLocale) {\n // Create with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: flatData,\n draft: draftOption,\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n // Update for other locales\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n const localeReq = { ...req, locale }\n await req.payload.update({\n id: savedDocument.id as number | string,\n collection: collectionSlug,\n data: localeData,\n draft: collectionHasVersions ? false : undefined,\n overrideAccess: false,\n req: localeReq,\n user,\n })\n } catch (error) {\n // Log but don't fail the entire import if a locale update fails\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(savedDocument.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, create normally\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: createData,\n draft: draftOption,\n overrideAccess: false,\n req,\n user,\n })\n }\n } else if (importMode === 'update' || importMode === 'upsert') {\n const matchValue = document[matchField || 'id']\n if (!matchValue) {\n throw new Error(`Match field \"${matchField || 'id'}\" not found in document`)\n }\n\n // Special handling for ID field with MongoDB\n // If matching by 'id' and it's not a valid ObjectID format, handle specially\n const isMatchingById = (matchField || 'id') === 'id'\n\n // Check if it's a valid MongoDB ObjectID format (24 hex chars)\n // Note: matchValue could be string, number, or ObjectID object\n let matchValueStr: string\n if (typeof matchValue === 'object' && matchValue !== null) {\n matchValueStr = JSON.stringify(matchValue)\n } else if (typeof matchValue === 'string') {\n matchValueStr = matchValue\n } else if (typeof matchValue === 'number') {\n matchValueStr = matchValue.toString()\n } else {\n // For other types, use JSON.stringify\n matchValueStr = JSON.stringify(matchValue)\n }\n const isValidObjectIdFormat = /^[0-9a-f]{24}$/i.test(matchValueStr)\n\n try {\n existingDocResult = await req.payload.find({\n collection: collectionSlug,\n depth: 0,\n limit: 1,\n overrideAccess: false,\n req,\n user,\n where: {\n [matchField || 'id']: {\n equals: matchValue,\n },\n },\n })\n } catch (error) {\n // MongoDB may throw for invalid ObjectID format - handle gracefully for upsert\n if (isMatchingById && importMode === 'upsert' && !isValidObjectIdFormat) {\n existingDocResult = { docs: [] }\n } else if (isMatchingById && importMode === 'update' && !isValidObjectIdFormat) {\n throw new Error(`Invalid ID format for update: ${matchValueStr}`)\n } else {\n throw error\n }\n }\n\n if (existingDocResult.docs.length > 0) {\n const existingDoc = existingDocResult.docs[0]\n if (!existingDoc) {\n throw new Error(`Document not found`)\n }\n\n // Debug: log what we found\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n existingStatus: existingDoc._status,\n existingTitle: existingDoc.title,\n incomingDocument: document,\n mode: importMode,\n msg: 'Found existing document for update',\n })\n }\n\n const updateData = { ...document }\n // Remove ID and internal fields from update data\n delete updateData.id\n delete updateData._id\n delete updateData.createdAt\n delete updateData.updatedAt\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n updateData,\n configuredLocales,\n defaultLocale,\n )\n\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n hasMultiLocale,\n mode: importMode,\n msg: 'Updating document in upsert/update mode',\n updateData: Object.keys(hasMultiLocale ? flatData : updateData).reduce(\n (acc, key) => {\n const val = (hasMultiLocale ? flatData : updateData)[key]\n acc[key] =\n typeof val === 'string' && val.length > 50 ? val.substring(0, 50) + '...' : val\n return acc\n },\n {} as Record<string, unknown>,\n ),\n })\n }\n\n if (hasMultiLocale) {\n // Update with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: flatData,\n depth: 0,\n // Don't specify draft - this creates a new draft for versioned collections\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n // Update for other locales\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n // Clone the request with the specific locale\n const localeReq = { ...req, locale }\n await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: localeData,\n depth: 0,\n // Don't specify draft - this creates a new draft for versioned collections\n overrideAccess: false,\n req: localeReq,\n user,\n })\n } catch (error) {\n // Log but don't fail the entire import if a locale update fails\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(existingDoc.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, update normally\n try {\n // Extra debug: log before update\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n existingTitle: existingDoc.title,\n msg: 'About to update document',\n newData: updateData,\n })\n }\n\n // Update the document - don't specify draft to let Payload handle versions properly\n // This will create a new draft version for collections with versions enabled\n savedDocument = await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: updateData,\n depth: 0,\n // Don't specify draft - this creates a new draft for versioned collections\n overrideAccess: false,\n req,\n user,\n })\n\n if (req.payload.config.debug && savedDocument) {\n req.payload.logger.info({\n id: savedDocument.id,\n msg: 'Update completed',\n status: savedDocument._status,\n title: savedDocument.title,\n })\n }\n } catch (updateError) {\n req.payload.logger.error({\n id: existingDoc.id,\n err: updateError,\n msg: 'Update failed',\n })\n throw updateError\n }\n }\n } else if (importMode === 'upsert') {\n // Create new in upsert mode\n if (req.payload.config.debug) {\n req.payload.logger.info({\n document,\n matchField: matchField || 'id',\n matchValue: document[matchField || 'id'],\n msg: 'No existing document found, creating new in upsert mode',\n })\n }\n\n const createData = { ...document }\n if (!hasCustomIdField) {\n delete createData.id\n }\n\n // Only handle _status for versioned collections\n let draftOption: boolean | undefined\n if (collectionHasVersions) {\n // Use defaultVersionStatus from config if _status not provided\n const statusValue = createData._status || options.defaultVersionStatus\n const isPublished = statusValue !== 'draft'\n draftOption = !isPublished\n createData._status = statusValue\n }\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n createData,\n configuredLocales,\n defaultLocale,\n )\n\n if (hasMultiLocale) {\n // Create with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: flatData,\n draft: draftOption,\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n // Update for other locales\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n // Clone the request with the specific locale\n const localeReq = { ...req, locale }\n await req.payload.update({\n id: savedDocument.id as number | string,\n collection: collectionSlug,\n data: localeData,\n draft: collectionHasVersions ? false : undefined,\n overrideAccess: false,\n req: localeReq,\n })\n } catch (error) {\n // Log but don't fail the entire import if a locale update fails\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(savedDocument.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, create normally\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: createData,\n draft: draftOption,\n overrideAccess: false,\n req,\n user,\n })\n }\n } else {\n // Update mode but document not found\n let matchValueDisplay: string\n if (typeof matchValue === 'object' && matchValue !== null) {\n matchValueDisplay = JSON.stringify(matchValue)\n } else if (typeof matchValue === 'string') {\n matchValueDisplay = matchValue\n } else if (typeof matchValue === 'number') {\n matchValueDisplay = matchValue.toString()\n } else {\n // For other types, use JSON.stringify to avoid [object Object]\n matchValueDisplay = JSON.stringify(matchValue)\n }\n throw new Error(`Document with ${matchField || 'id'}=\"${matchValueDisplay}\" not found`)\n }\n } else {\n throw new Error(`Unknown import mode: ${String(importMode)}`)\n }\n\n if (savedDocument) {\n // Determine operation type for proper counting\n let operation: 'created' | 'updated' | undefined\n if (importMode === 'create') {\n operation = 'created'\n } else if (importMode === 'update') {\n operation = 'updated'\n } else if (importMode === 'upsert') {\n if (existingDocResult && existingDocResult.docs.length > 0) {\n operation = 'updated'\n } else {\n operation = 'created'\n }\n }\n\n result.successful.push({\n document,\n index: rowNumber - 1, // Store as 0-indexed\n operation,\n result: savedDocument,\n })\n }\n } catch (error) {\n const importError: ImportError = {\n type: categorizeError(error),\n documentData: document || {},\n error: extractErrorMessage(error),\n item: document || {},\n itemIndex: rowNumber - 1,\n rowNumber,\n }\n\n // Try to extract field information from validation errors\n if (error && typeof error === 'object' && 'data' in error) {\n const errorData = error as { data?: { errors?: Array<{ path?: string }> } }\n if (errorData.data?.errors && Array.isArray(errorData.data.errors)) {\n const firstError = errorData.data.errors[0]\n if (firstError?.path) {\n importError.field = firstError.path\n }\n }\n }\n\n result.failed.push(importError)\n // Always continue processing all rows\n }\n }\n\n return result\n}\n\nexport function createImportBatchProcessor(options: ImportBatchProcessorOptions = {}) {\n const processorOptions = {\n batchSize: options.batchSize ?? 100,\n defaultVersionStatus: options.defaultVersionStatus ?? 'published',\n }\n\n const processImport = async (processOptions: ImportProcessOptions): Promise<ImportResult> => {\n const { collectionSlug, documents, importMode, matchField, req, user } = processOptions\n const batches = createBatches(documents, processorOptions.batchSize)\n\n const result: ImportResult = {\n errors: [],\n imported: 0,\n total: documents.length,\n updated: 0,\n }\n\n for (let i = 0; i < batches.length; i++) {\n const currentBatch = batches[i]\n if (!currentBatch) {\n continue\n }\n\n const batchResult = await processImportBatch({\n batch: currentBatch,\n batchIndex: i,\n collectionSlug,\n importMode,\n matchField,\n options: processorOptions,\n req,\n user,\n })\n\n // Update results\n for (const success of batchResult.successful) {\n if (success.operation === 'created') {\n result.imported++\n } else if (success.operation === 'updated') {\n result.updated++\n } else {\n // Fallback\n if (importMode === 'create') {\n result.imported++\n } else {\n result.updated++\n }\n }\n }\n\n for (const error of batchResult.failed) {\n result.errors.push({\n doc: error.documentData,\n error: error.error,\n index: error.rowNumber - 1, // Convert back to 0-indexed\n })\n }\n }\n\n return result\n }\n\n return {\n processImport,\n }\n}\n"],"names":["isolateObjectProperty","categorizeError","createBatches","extractErrorMessage","extractMultiLocaleData","data","configuredLocales","defaultLocale","flatData","localeUpdates","hasMultiLocale","length","localeSet","Set","key","value","Object","entries","Array","isArray","valueObj","localeKeys","keys","filter","k","has","baseLocale","includes","locale","processImportBatch","batch","batchIndex","collectionSlug","importMode","matchField","options","req","reqFromArgs","user","result","failed","successful","transactionID","undefined","collectionEntry","payload","collections","collectionConfig","config","collectionHasVersions","Boolean","versions","hasCustomIdField","customIDType","localization","localeCodes","startingRowNumber","batchSize","i","document","rowNumber","savedDocument","existingDocResult","createData","id","draftOption","statusValue","_status","defaultVersionStatus","isPublished","debug","logger","info","msg","willSetDraft","title","titleIsNull","titleType","defaultLocaleReq","create","collection","draft","overrideAccess","localeData","localeReq","update","error","err","String","matchValue","Error","isMatchingById","matchValueStr","JSON","stringify","toString","isValidObjectIdFormat","test","find","depth","limit","where","equals","docs","existingDoc","existingId","existingStatus","existingTitle","incomingDocument","mode","updateData","_id","createdAt","updatedAt","reduce","acc","val","substring","newData","status","updateError","matchValueDisplay","operation","push","index","importError","type","documentData","item","itemIndex","errorData","errors","firstError","path","field","createImportBatchProcessor","processorOptions","processImport","processOptions","documents","batches","imported","total","updated","currentBatch","batchResult","success","doc"],"mappings":"AAEA,SAASA,qBAAqB,QAAQ,UAAS;AAI/C,SAEEC,eAAe,EACfC,aAAa,EACbC,mBAAmB,QACd,oCAAmC;AA6C1C;;;;;;;;;;;CAWC,GACD,SAASC,uBACPC,IAA6B,EAC7BC,iBAA4B,EAC5BC,aAAsB;IAMtB,MAAMC,WAAoC,CAAC;IAC3C,MAAMC,gBAAyD,CAAC;IAChE,IAAIC,iBAAiB;IAErB,IAAI,CAACJ,qBAAqBA,kBAAkBK,MAAM,KAAK,GAAG;QACxD,OAAO;YAAEH,UAAU;gBAAE,GAAGH,IAAI;YAAC;YAAG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AIrF,IAAI,GAAGA,IAAIkF,QAAQjI,MAAM,EAAE+C,IAAK;YACvC,MAAMsF,eAAeJ,OAAO,CAAClF,EAAE;YAC/B,IAAI,CAACsF,cAAc;gBACjB;YACF;YAEA,MAAMC,cAAc,MAAMpH,mBAAmB;gBAC3CC,OAAOkH;gBACPjH,YAAY2B;gBACZ1B;gBACAC;gBACAC;gBACAC,SAASqG;gBACTpG;gBACAE;YACF;YAEA,iBAAiB;YACjB,KAAK,MAAM4G,WAAWD,YAAYxG,UAAU,CAAE;gBAC5C,IAAIyG,QAAQxB,SAAS,KAAK,WAAW;oBACnCnF,OAAOsG,QAAQ;gBACjB,OAAO,IAAIK,QAAQxB,SAAS,KAAK,WAAW;oBAC1CnF,OAAOwG,OAAO;gBAChB,OAAO;oBACL,WAAW;oBACX,IAAI9G,eAAe,UAAU;wBAC3BM,OAAOsG,QAAQ;oBACjB,OAAO;wBACLtG,OAAOwG,OAAO;oBAChB;gBACF;YACF;YAEA,KAAK,MAAMzD,SAAS2D,YAAYzG,MAAM,CAAE;gBACtCD,OAAO4F,MAAM,CAACR,IAAI,CAAC;oBACjBwB,KAAK7D,MAAMyC,YAAY;oBACvBzC,OAAOA,MAAMA,KAAK;oBAClBsC,OAAOtC,MAAM1B,SAAS,GAAG;gBAC3B;YACF;QACF;QAEA,OAAOrB;IACT;IAEA,OAAO;QACLkG;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../src/import/batchProcessor.ts"],"sourcesContent":["import type { PayloadRequest, TypedUser } from 'payload'\n\nimport { isolateObjectProperty } from 'payload'\n\nimport type { ImportAfterHook, ImportBeforeHook, ImportResult } from '../types.js'\nimport type { ImportMode } from './createImport.js'\n\nimport {\n type BatchError,\n categorizeError,\n createBatches,\n extractErrorMessage,\n} from '../utilities/useBatchProcessor.js'\n\n/**\n * Import-specific batch processor options\n */\nexport interface ImportBatchProcessorOptions {\n batchSize?: number\n defaultVersionStatus?: 'draft' | 'published'\n}\n\n/**\n * Import-specific error type extending the generic BatchError\n */\nexport interface ImportError extends BatchError<Record<string, unknown>> {\n documentData: Record<string, unknown>\n field?: string\n fieldLabel?: string\n rowNumber: number // 1-indexed for user clarity\n}\n\n/**\n * Result from processing a single import batch\n */\nexport interface ImportBatchResult {\n failed: Array<ImportError>\n successful: Array<{\n document: Record<string, unknown>\n index: number\n operation?: 'created' | 'updated'\n result: Record<string, unknown>\n }>\n}\n\n/**\n * Options for processing an import operation\n */\nexport interface ImportProcessOptions {\n collectionSlug: string\n docs: Record<string, unknown>[]\n /** Export format — passed through to hook args */\n format?: 'csv' | 'json'\n /** Lifecycle hooks for this import operation */\n hooks?: {\n after?: ImportAfterHook\n before?: ImportBeforeHook\n }\n importMode: ImportMode\n matchField?: string\n /** Raw parsed rows before unflattening — used as originalData in hooks */\n originalDocs?: Record<string, unknown>[]\n req: PayloadRequest\n /** Total number of batches (pre-computed for hook args) */\n totalBatches?: number\n user?: TypedUser\n}\n\n/**\n * Separates multi-locale data from a document for sequential locale updates.\n *\n * When a field has locale-keyed values (e.g., { title: { en: 'Hello', es: 'Hola' } }),\n * this extracts the default locale's data for initial create/update, and stores\n * remaining locales for subsequent update calls.\n *\n * @returns\n * - flatData: Document with default locale values extracted (for initial operation)\n * - hasMultiLocale: Whether any multi-locale fields were found\n * - localeUpdates: Map of locale -> field data for follow-up updates\n */\nfunction extractMultiLocaleData(\n data: Record<string, unknown>,\n configuredLocales?: string[],\n defaultLocale?: string,\n): {\n flatData: Record<string, unknown>\n hasMultiLocale: boolean\n localeUpdates: Record<string, Record<string, unknown>>\n} {\n const flatData: Record<string, unknown> = {}\n const localeUpdates: Record<string, Record<string, unknown>> = {}\n let hasMultiLocale = false\n\n if (!configuredLocales || configuredLocales.length === 0) {\n return { flatData: { ...data }, hasMultiLocale: false, localeUpdates: {} }\n }\n\n const localeSet = new Set(configuredLocales)\n\n for (const [key, value] of Object.entries(data)) {\n if (value && typeof value === 'object' && !Array.isArray(value)) {\n const valueObj = value as Record<string, unknown>\n const localeKeys = Object.keys(valueObj).filter((k) => localeSet.has(k))\n\n if (localeKeys.length > 0) {\n hasMultiLocale = true\n const baseLocale =\n defaultLocale && localeKeys.includes(defaultLocale) ? defaultLocale : localeKeys[0]\n if (baseLocale) {\n flatData[key] = valueObj[baseLocale]\n for (const locale of localeKeys) {\n if (locale !== baseLocale) {\n if (!localeUpdates[locale]) {\n localeUpdates[locale] = {}\n }\n localeUpdates[locale][key] = valueObj[locale]\n }\n }\n }\n } else {\n flatData[key] = value\n }\n } else {\n flatData[key] = value\n }\n }\n\n return { flatData, hasMultiLocale, localeUpdates }\n}\n\ntype ProcessImportBatchOptions = {\n batch: Record<string, unknown>[]\n batchIndex: number\n collectionSlug: string\n importMode: ImportMode\n matchField: string | undefined\n options: { batchSize: number; defaultVersionStatus: 'draft' | 'published' }\n req: PayloadRequest\n user?: TypedUser\n}\n\n/**\n * Processes a batch of documents for import based on the import mode.\n *\n * For each document in the batch:\n * - create: Creates a new document (removes any existing ID)\n * - update: Finds existing document by matchField and updates it\n * - upsert: Updates if found, creates if not found\n *\n * Handles versioned collections, multi-locale data, and MongoDB ObjectID validation.\n * Continues processing remaining documents even if individual imports fail.\n */\nasync function processImportBatch({\n batch,\n batchIndex,\n collectionSlug,\n importMode,\n matchField,\n options,\n req: reqFromArgs,\n user,\n}: ProcessImportBatchOptions): Promise<ImportBatchResult> {\n const result: ImportBatchResult = {\n failed: [],\n successful: [],\n }\n // Create a request proxy that isolates the transactionID property, then clear it.\n // This is critical because if a nested operation fails (e.g., Forbidden due to access control),\n // Payload's error handling calls killTransaction(req), which would kill the parent's transaction\n // if we shared the same transaction. By isolating and clearing transactionID, each nested\n // operation either uses no transaction or starts its own, independent of the parent.\n const req = isolateObjectProperty(reqFromArgs, 'transactionID')\n req.transactionID = undefined\n\n const collectionEntry = req.payload.collections[collectionSlug]\n\n const collectionConfig = collectionEntry?.config\n const collectionHasVersions = Boolean(collectionConfig?.versions)\n const hasCustomIdField = Boolean(collectionEntry?.customIDType)\n\n const configuredLocales = req.payload.config.localization\n ? req.payload.config.localization.localeCodes\n : undefined\n\n const defaultLocale = req.payload.config.localization\n ? req.payload.config.localization.defaultLocale\n : undefined\n\n const startingRowNumber = batchIndex * options.batchSize\n\n for (let i = 0; i < batch.length; i++) {\n const document = batch[i]\n if (!document) {\n continue\n }\n const rowNumber = startingRowNumber + i + 1\n\n try {\n let savedDocument: Record<string, unknown> | undefined\n let existingDocResult: { docs: Array<Record<string, unknown>> } | undefined\n\n if (importMode === 'create') {\n const createData = { ...document }\n if (!hasCustomIdField) {\n delete createData.id\n }\n\n let draftOption: boolean | undefined\n if (collectionHasVersions) {\n const statusValue = createData._status || options.defaultVersionStatus\n const isPublished = statusValue !== 'draft'\n draftOption = !isPublished\n createData._status = statusValue\n\n if (req.payload.config.debug) {\n req.payload.logger.info({\n _status: createData._status,\n isPublished,\n msg: 'Status handling in create',\n willSetDraft: draftOption,\n })\n }\n }\n\n if (req.payload.config.debug && 'title' in createData) {\n req.payload.logger.info({\n msg: 'Creating document',\n title: createData.title,\n titleIsNull: createData.title === null,\n titleType: typeof createData.title,\n })\n }\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n createData,\n configuredLocales,\n defaultLocale,\n )\n\n if (hasMultiLocale) {\n // Create with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: flatData,\n draft: draftOption,\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n await req.payload.update({\n id: savedDocument.id as number | string,\n collection: collectionSlug,\n data: localeData,\n draft: collectionHasVersions ? false : undefined,\n overrideAccess: false,\n req: { ...req, locale },\n user,\n })\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(savedDocument.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, create normally\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: createData,\n draft: draftOption,\n overrideAccess: false,\n req,\n user,\n })\n }\n } else if (importMode === 'update' || importMode === 'upsert') {\n const matchValue = document[matchField || 'id']\n if (!matchValue) {\n throw new Error(`Match field \"${matchField || 'id'}\" not found in document`)\n }\n\n // Special handling for ID field with MongoDB\n // If matching by 'id' and it's not a valid ObjectID format, handle specially\n const isMatchingById = (matchField || 'id') === 'id'\n\n // Check if it's a valid MongoDB ObjectID format (24 hex chars)\n // Note: matchValue could be string, number, or ObjectID object\n let matchValueStr: string\n if (typeof matchValue === 'object' && matchValue !== null) {\n matchValueStr = JSON.stringify(matchValue)\n } else if (typeof matchValue === 'string') {\n matchValueStr = matchValue\n } else if (typeof matchValue === 'number') {\n matchValueStr = matchValue.toString()\n } else {\n // For other types, use JSON.stringify\n matchValueStr = JSON.stringify(matchValue)\n }\n const isValidObjectIdFormat = /^[0-9a-f]{24}$/i.test(matchValueStr)\n\n try {\n existingDocResult = await req.payload.find({\n collection: collectionSlug,\n depth: 0,\n limit: 1,\n overrideAccess: false,\n req,\n user,\n where: {\n [matchField || 'id']: {\n equals: matchValue,\n },\n },\n })\n } catch (error) {\n // MongoDB may throw for invalid ObjectID format - handle gracefully for upsert\n if (isMatchingById && importMode === 'upsert' && !isValidObjectIdFormat) {\n existingDocResult = { docs: [] }\n } else if (isMatchingById && importMode === 'update' && !isValidObjectIdFormat) {\n throw new Error(`Invalid ID format for update: ${matchValueStr}`)\n } else {\n throw error\n }\n }\n\n if (existingDocResult.docs.length > 0) {\n const existingDoc = existingDocResult.docs[0]\n if (!existingDoc) {\n throw new Error(`Document not found`)\n }\n\n // Debug: log what we found\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n existingStatus: existingDoc._status,\n existingTitle: existingDoc.title,\n incomingDocument: document,\n mode: importMode,\n msg: 'Found existing document for update',\n })\n }\n\n const updateData = { ...document }\n // Remove ID and internal fields from update data\n delete updateData.id\n delete updateData._id\n delete updateData.createdAt\n delete updateData.updatedAt\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n updateData,\n configuredLocales,\n defaultLocale,\n )\n\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n hasMultiLocale,\n mode: importMode,\n msg: 'Updating document in upsert/update mode',\n updateData: Object.keys(hasMultiLocale ? flatData : updateData).reduce(\n (acc, key) => {\n const val = (hasMultiLocale ? flatData : updateData)[key]\n acc[key] =\n typeof val === 'string' && val.length > 50 ? val.substring(0, 50) + '...' : val\n return acc\n },\n {} as Record<string, unknown>,\n ),\n })\n }\n\n if (hasMultiLocale) {\n // Update with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: flatData,\n depth: 0,\n // Don't specify draft - this creates a new draft for versioned collections\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: localeData,\n depth: 0,\n overrideAccess: false,\n req: { ...req, locale },\n user,\n })\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(existingDoc.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, update normally\n try {\n // Extra debug: log before update\n if (req.payload.config.debug) {\n req.payload.logger.info({\n existingId: existingDoc.id,\n existingTitle: existingDoc.title,\n msg: 'About to update document',\n newData: updateData,\n })\n }\n\n // Update the document - don't specify draft to let Payload handle versions properly\n // This will create a new draft version for collections with versions enabled\n savedDocument = await req.payload.update({\n id: existingDoc.id as number | string,\n collection: collectionSlug,\n data: updateData,\n depth: 0,\n // Don't specify draft - this creates a new draft for versioned collections\n overrideAccess: false,\n req,\n user,\n })\n\n if (req.payload.config.debug && savedDocument) {\n req.payload.logger.info({\n id: savedDocument.id,\n msg: 'Update completed',\n status: savedDocument._status,\n title: savedDocument.title,\n })\n }\n } catch (updateError) {\n req.payload.logger.error({\n id: existingDoc.id,\n err: updateError,\n msg: 'Update failed',\n })\n throw updateError\n }\n }\n } else if (importMode === 'upsert') {\n // Create new in upsert mode\n if (req.payload.config.debug) {\n req.payload.logger.info({\n document,\n matchField: matchField || 'id',\n matchValue: document[matchField || 'id'],\n msg: 'No existing document found, creating new in upsert mode',\n })\n }\n\n const createData = { ...document }\n if (!hasCustomIdField) {\n delete createData.id\n }\n\n // Only handle _status for versioned collections\n let draftOption: boolean | undefined\n if (collectionHasVersions) {\n // Use defaultVersionStatus from config if _status not provided\n const statusValue = createData._status || options.defaultVersionStatus\n const isPublished = statusValue !== 'draft'\n draftOption = !isPublished\n createData._status = statusValue\n }\n\n // Check if we have multi-locale data and extract it\n const { flatData, hasMultiLocale, localeUpdates } = extractMultiLocaleData(\n createData,\n configuredLocales,\n defaultLocale,\n )\n\n if (hasMultiLocale) {\n // Create with default locale data\n const defaultLocaleReq = defaultLocale ? { ...req, locale: defaultLocale } : req\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: flatData,\n draft: draftOption,\n overrideAccess: false,\n req: defaultLocaleReq,\n user,\n })\n\n if (savedDocument && Object.keys(localeUpdates).length > 0) {\n for (const [locale, localeData] of Object.entries(localeUpdates)) {\n try {\n await req.payload.update({\n id: savedDocument.id as number | string,\n collection: collectionSlug,\n data: localeData,\n draft: collectionHasVersions ? false : undefined,\n overrideAccess: false,\n req: { ...req, locale },\n user,\n })\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: `Failed to update locale ${locale} for document ${String(savedDocument.id)}`,\n })\n }\n }\n }\n } else {\n // No multi-locale data, create normally\n savedDocument = await req.payload.create({\n collection: collectionSlug,\n data: createData,\n draft: draftOption,\n overrideAccess: false,\n req,\n user,\n })\n }\n } else {\n // Update mode but document not found\n let matchValueDisplay: string\n if (typeof matchValue === 'object' && matchValue !== null) {\n matchValueDisplay = JSON.stringify(matchValue)\n } else if (typeof matchValue === 'string') {\n matchValueDisplay = matchValue\n } else if (typeof matchValue === 'number') {\n matchValueDisplay = matchValue.toString()\n } else {\n // For other types, use JSON.stringify to avoid [object Object]\n matchValueDisplay = JSON.stringify(matchValue)\n }\n throw new Error(`Document with ${matchField || 'id'}=\"${matchValueDisplay}\" not found`)\n }\n } else {\n throw new Error(`Unknown import mode: ${String(importMode)}`)\n }\n\n if (savedDocument) {\n // Determine operation type for proper counting\n let operation: 'created' | 'updated' | undefined\n if (importMode === 'create') {\n operation = 'created'\n } else if (importMode === 'update') {\n operation = 'updated'\n } else if (importMode === 'upsert') {\n if (existingDocResult && existingDocResult.docs.length > 0) {\n operation = 'updated'\n } else {\n operation = 'created'\n }\n }\n\n result.successful.push({\n document,\n index: rowNumber - 1, // Store as 0-indexed\n operation,\n result: savedDocument,\n })\n }\n } catch (error) {\n const importError: ImportError = {\n type: categorizeError(error),\n documentData: document || {},\n error: extractErrorMessage(error),\n item: document || {},\n itemIndex: rowNumber - 1,\n rowNumber,\n }\n\n // Try to extract field information from validation errors\n if (error && typeof error === 'object' && 'data' in error) {\n const errorData = error as { data?: { errors?: Array<{ path?: string }> } }\n if (errorData.data?.errors && Array.isArray(errorData.data.errors)) {\n const firstError = errorData.data.errors[0]\n if (firstError?.path) {\n importError.field = firstError.path\n }\n }\n }\n\n result.failed.push(importError)\n // Always continue processing all rows\n }\n }\n\n return result\n}\n\nexport function createImportBatchProcessor(options: ImportBatchProcessorOptions = {}) {\n const processorOptions = {\n batchSize: options.batchSize ?? 100,\n defaultVersionStatus: options.defaultVersionStatus ?? 'published',\n }\n\n const processImport = async (processOptions: ImportProcessOptions): Promise<ImportResult> => {\n const {\n collectionSlug,\n docs: documents,\n format = 'csv',\n hooks,\n importMode,\n matchField,\n originalDocs: originalDocs,\n req,\n totalBatches: totalBatchesFromOptions,\n user,\n } = processOptions\n const batches = createBatches(documents, processorOptions.batchSize)\n const totalBatches = totalBatchesFromOptions ?? batches.length\n\n const result: ImportResult = {\n errors: [],\n imported: 0,\n total: documents.length,\n updated: 0,\n }\n\n for (let i = 0; i < batches.length; i++) {\n const currentBatch = batches[i]\n if (!currentBatch) {\n continue\n }\n\n const batchNumber = i + 1\n const batchStart = i * processorOptions.batchSize\n const originalBatch = originalDocs\n ? originalDocs.slice(batchStart, batchStart + currentBatch.length)\n : currentBatch\n\n const batchToProcess: Record<string, unknown>[] =\n hooks?.before && currentBatch.length > 0\n ? ((await hooks.before({\n batchNumber,\n data: currentBatch as Parameters<ImportBeforeHook>[0]['data'],\n format,\n originalData: originalBatch,\n req,\n totalBatches,\n })) as Record<string, unknown>[])\n : currentBatch\n\n const batchResult = await processImportBatch({\n batch: batchToProcess,\n batchIndex: i,\n collectionSlug,\n importMode,\n matchField,\n options: processorOptions,\n req,\n user,\n })\n\n let batchImported = 0\n let batchUpdated = 0\n for (const success of batchResult.successful) {\n if (success.operation === 'created') {\n result.imported++\n batchImported++\n } else if (success.operation === 'updated') {\n result.updated++\n batchUpdated++\n } else {\n // Fallback\n if (importMode === 'create') {\n result.imported++\n batchImported++\n } else {\n result.updated++\n batchUpdated++\n }\n }\n }\n\n for (const error of batchResult.failed) {\n result.errors.push({\n doc: error.documentData,\n error: error.error,\n index: error.rowNumber - 1, // Convert back to 0-indexed\n })\n }\n\n if (hooks?.after) {\n const batchHookResult: ImportResult = {\n errors:\n batchResult.failed.length > 0 ? result.errors.slice(-batchResult.failed.length) : [],\n imported: batchImported,\n total: batchToProcess.length,\n updated: batchUpdated,\n }\n await hooks.after({\n batchNumber,\n format,\n originalData: originalBatch,\n req,\n result: batchHookResult,\n totalBatches,\n })\n }\n }\n\n return result\n }\n\n return {\n processImport,\n }\n}\n"],"names":["isolateObjectProperty","categorizeError","createBatches","extractErrorMessage","extractMultiLocaleData","data","configuredLocales","defaultLocale","flatData","localeUpdates","hasMultiLocale","length","localeSet","Set","key","value","Object","entries","Array","isArray","valueObj","localeKeys","keys","filter","k","has","baseLocale","includes","locale","processImportBatch","batch","batchIndex","collectionSlug","importMode","matchField","options","req","reqFromArgs","user","result","failed","successful","transactionID","undefined","collectionEntry","payload","collections","collectionConfig","config","collectionHasVersions","Boolean","versions","hasCustomIdField","customIDType","localization","localeCodes","startingRowNumber","batchSize","i","document","rowNumber","savedDocument","existingDocResult","createData","id","draftOption","statusValue","_status","defaultVersionStatus","isPublished","debug","logger","info","msg","willSetDraft","title","titleIsNull","titleType","defaultLocaleReq","create","collection","draft","overrideAccess","localeData","update","error","err","String","matchValue","Error","isMatchingById","matchValueStr","JSON","stringify","toString","isValidObjectIdFormat","test","find","depth","limit","where","equals","docs","existingDoc","existingId","existingStatus","existingTitle","incomingDocument","mode","updateData","_id","createdAt","updatedAt","reduce","acc","val","substring","newData","status","updateError","matchValueDisplay","operation","push","index","importError","type","documentData","item","itemIndex","errorData","errors","firstError","path","field","createImportBatchProcessor","processorOptions","processImport","processOptions","documents","format","hooks","originalDocs","totalBatches","totalBatchesFromOptions","batches","imported","total","updated","currentBatch","batchNumber","batchStart","originalBatch","slice","batchToProcess","before","originalData","batchResult","batchImported","batchUpdated","success","doc","after","batchHookResult"],"mappings":"AAEA,SAASA,qBAAqB,QAAQ,UAAS;AAK/C,SAEEC,eAAe,EACfC,aAAa,EACbC,mBAAmB,QACd,oCAAmC;AAwD1C;;;;;;;;;;;CAWC,GACD,SAASC,uBACPC,IAA6B,EAC7BC,iBAA4B,EAC5BC,aAAsB;IAMtB,MAAMC,WAAoC,CAAC;IAC3C,MAAMC,gBAAyD,CAAC;IAChE,IAAIC,iBAAiB;IAErB,IAAI,CAACJ,qBAAqBA,kBAAkBK,MAAM,KAAK,GAAG;QACxD,OAAO;YAAEH,UAAU;gBAAE,GAAGH,IAAI;YAAC;YAAGK,gBAAgB;YAAOD,eAAe,CAAC;QAAE;IAC3E;IAEA,MAAMG,YAAY,IAAIC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AE;oBACtFsI,UAAUY;oBACVX,OAAOO,eAAe9I,MAAM;oBAC5BwI,SAASW;gBACX;gBACA,MAAMlB,MAAMqB,KAAK,CAAC;oBAChBZ;oBACAV;oBACAgB,cAAcJ;oBACdnH;oBACAG,QAAQ2H;oBACRpB;gBACF;YACF;QACF;QAEA,OAAOvG;IACT;IAEA,OAAO;QACLiG;IACF;AACF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { ImportResult } from '../types.js';
|
|
2
3
|
export type ImportMode = 'create' | 'update' | 'upsert';
|
|
3
4
|
export type Import = {
|
|
4
5
|
/**
|
|
@@ -36,15 +37,5 @@ export type CreateImportArgs = {
|
|
|
36
37
|
defaultVersionStatus?: 'draft' | 'published';
|
|
37
38
|
req: PayloadRequest;
|
|
38
39
|
} & Import;
|
|
39
|
-
export type ImportResult = {
|
|
40
|
-
errors: Array<{
|
|
41
|
-
doc: Record<string, unknown>;
|
|
42
|
-
error: string;
|
|
43
|
-
index: number;
|
|
44
|
-
}>;
|
|
45
|
-
imported: number;
|
|
46
|
-
total: number;
|
|
47
|
-
updated: number;
|
|
48
|
-
};
|
|
49
40
|
export declare const createImport: ({ batchSize, collectionSlug, debug, defaultVersionStatus, file, format, importMode, matchField, maxLimit, req, userCollection, userID, }: CreateImportArgs) => Promise<ImportResult>;
|
|
50
41
|
//# sourceMappingURL=createImport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createImport.d.ts","sourceRoot":"","sources":["../../src/import/createImport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAa,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"createImport.d.ts","sourceRoot":"","sources":["../../src/import/createImport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAa,MAAM,SAAS,CAAA;AAIxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAU/C,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAEvD,MAAM,MAAM,MAAM,GAAG;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,cAAc,EAAE,MAAM,CAAA;IACtB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,IAAI,CAAC,EAAE;QACL,IAAI,EAAE,MAAM,CAAA;QACZ,QAAQ,EAAE,MAAM,CAAA;QAChB,IAAI,EAAE,MAAM,CAAA;KACb,CAAA;IACD,MAAM,EAAE,KAAK,GAAG,MAAM,CAAA;IACtB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACpB;;OAEG;IACH,UAAU,EAAE,UAAU,CAAA;IACtB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,oBAAoB,CAAC,EAAE,OAAO,GAAG,WAAW,CAAA;IAC5C,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,MAAM,CAAA;AAEV,eAAO,MAAM,YAAY,6IAatB,gBAAgB,KAAG,OAAO,CAAC,YAAY,CAuLzC,CAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { APIError } from 'payload';
|
|
2
|
+
import { applyFieldHooks } from '../utilities/applyFieldHooks.js';
|
|
2
3
|
import { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js';
|
|
3
4
|
import { parseCSV } from '../utilities/parseCSV.js';
|
|
4
5
|
import { parseJSON } from '../utilities/parseJSON.js';
|
|
@@ -23,7 +24,7 @@ export const createImport = async ({ batchSize = 100, collectionSlug, debug = fa
|
|
|
23
24
|
format,
|
|
24
25
|
importMode,
|
|
25
26
|
matchField,
|
|
26
|
-
|
|
27
|
+
msg: 'Starting import process with args:',
|
|
27
28
|
transactionID: req.transactionID
|
|
28
29
|
});
|
|
29
30
|
}
|
|
@@ -37,8 +38,8 @@ export const createImport = async ({ batchSize = 100, collectionSlug, debug = fa
|
|
|
37
38
|
req.payload.logger.debug({
|
|
38
39
|
fileName: file.name,
|
|
39
40
|
fileSize: file.data.length,
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
mimeType: file.mimetype,
|
|
42
|
+
msg: 'File info'
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
45
|
const collectionConfig = req.payload.config.collections.find(({ slug })=>slug === collectionSlug);
|
|
@@ -50,89 +51,64 @@ export const createImport = async ({ batchSize = 100, collectionSlug, debug = fa
|
|
|
50
51
|
}
|
|
51
52
|
// Get disabled fields configuration
|
|
52
53
|
const disabledFields = collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? [];
|
|
54
|
+
const importHooks = collectionConfig.custom?.['plugin-import-export']?.importHooks;
|
|
53
55
|
// Get fromCSV functions for field transformations
|
|
54
|
-
const
|
|
56
|
+
const importFieldHooks = getImportFieldFunctions({
|
|
55
57
|
fields: collectionConfig.flattenedFields || []
|
|
56
58
|
});
|
|
57
59
|
// Parse the file data
|
|
60
|
+
let originalDocs;
|
|
58
61
|
let documents;
|
|
59
62
|
if (format === 'csv') {
|
|
60
63
|
const rawData = await parseCSV({
|
|
61
64
|
data: file.data,
|
|
62
65
|
req
|
|
63
66
|
});
|
|
64
|
-
|
|
65
|
-
if (debug && rawData.length > 0) {
|
|
66
|
-
req.payload.logger.info({
|
|
67
|
-
firstRow: rawData[0],
|
|
68
|
-
msg: 'Parsed CSV data - FULL'
|
|
69
|
-
});
|
|
70
|
-
req.payload.logger.info({
|
|
71
|
-
msg: 'Parsed CSV data',
|
|
72
|
-
rows: rawData.map((row, i)=>({
|
|
73
|
-
excerpt: row.excerpt,
|
|
74
|
-
hasManyNumber: row.hasManyNumber,
|
|
75
|
-
hasOnePolymorphic_id: row.hasOnePolymorphic_id,
|
|
76
|
-
hasOnePolymorphic_relationTo: row.hasOnePolymorphic_relationTo,
|
|
77
|
-
index: i,
|
|
78
|
-
title: row.title
|
|
79
|
-
}))
|
|
80
|
-
});
|
|
81
|
-
}
|
|
67
|
+
originalDocs = rawData;
|
|
82
68
|
documents = rawData;
|
|
83
69
|
// Unflatten CSV data
|
|
84
70
|
documents = documents.map((doc)=>{
|
|
85
71
|
const unflattened = unflattenObject({
|
|
86
72
|
data: doc,
|
|
87
73
|
fields: collectionConfig.flattenedFields ?? [],
|
|
88
|
-
|
|
74
|
+
format,
|
|
75
|
+
importFieldHooks,
|
|
89
76
|
req
|
|
90
77
|
});
|
|
91
78
|
return unflattened ?? {};
|
|
92
79
|
}).filter((doc)=>doc && Object.keys(doc).length > 0);
|
|
93
|
-
// Debug after unflatten
|
|
94
|
-
if (debug && documents.length > 0) {
|
|
95
|
-
req.payload.logger.info({
|
|
96
|
-
msg: 'After unflatten',
|
|
97
|
-
rows: documents.map((row, i)=>({
|
|
98
|
-
hasManyNumber: row.hasManyNumber,
|
|
99
|
-
hasManyPolymorphic: row.hasManyPolymorphic,
|
|
100
|
-
hasOnePolymorphic: row.hasOnePolymorphic,
|
|
101
|
-
hasTitle: 'title' in row,
|
|
102
|
-
index: i,
|
|
103
|
-
title: row.title
|
|
104
|
-
}))
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
80
|
if (debug) {
|
|
108
81
|
req.payload.logger.debug({
|
|
109
82
|
documentCount: documents.length,
|
|
110
|
-
|
|
83
|
+
msg: 'After unflattening CSV',
|
|
111
84
|
rawDataCount: rawData.length
|
|
112
85
|
});
|
|
113
|
-
// Debug: show a sample of raw vs unflattened
|
|
114
|
-
if (rawData.length > 0 && documents.length > 0) {
|
|
115
|
-
req.payload.logger.debug({
|
|
116
|
-
message: 'Sample data transformation',
|
|
117
|
-
raw: Object.keys(rawData[0] || {}).filter((k)=>k.includes('localized')),
|
|
118
|
-
unflattened: JSON.stringify(documents[0], null, 2)
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
86
|
}
|
|
122
87
|
} else {
|
|
123
|
-
|
|
88
|
+
const parsedDocs = parseJSON({
|
|
124
89
|
data: file.data,
|
|
125
90
|
req
|
|
126
91
|
});
|
|
92
|
+
originalDocs = parsedDocs;
|
|
93
|
+
// Apply field-level import hooks for JSON format
|
|
94
|
+
documents = parsedDocs.map((doc)=>applyFieldHooks({
|
|
95
|
+
type: 'beforeImport',
|
|
96
|
+
data: doc,
|
|
97
|
+
fieldHooks: importFieldHooks,
|
|
98
|
+
fields: collectionConfig.flattenedFields ?? [],
|
|
99
|
+
format,
|
|
100
|
+
operation: 'import',
|
|
101
|
+
req
|
|
102
|
+
}));
|
|
127
103
|
}
|
|
128
104
|
if (debug) {
|
|
129
105
|
req.payload.logger.debug({
|
|
130
|
-
|
|
106
|
+
msg: `Parsed ${documents.length} documents from ${format} file`
|
|
131
107
|
});
|
|
132
108
|
if (documents.length > 0) {
|
|
133
109
|
req.payload.logger.debug({
|
|
134
110
|
doc: documents[0],
|
|
135
|
-
|
|
111
|
+
msg: 'First document sample:'
|
|
136
112
|
});
|
|
137
113
|
}
|
|
138
114
|
}
|
|
@@ -148,7 +124,7 @@ export const createImport = async ({ batchSize = 100, collectionSlug, debug = fa
|
|
|
148
124
|
req.payload.logger.debug({
|
|
149
125
|
batchSize,
|
|
150
126
|
documentCount: documents.length,
|
|
151
|
-
|
|
127
|
+
msg: 'Processing import in batches'
|
|
152
128
|
});
|
|
153
129
|
}
|
|
154
130
|
// Create batch processor
|
|
@@ -156,20 +132,25 @@ export const createImport = async ({ batchSize = 100, collectionSlug, debug = fa
|
|
|
156
132
|
batchSize,
|
|
157
133
|
defaultVersionStatus
|
|
158
134
|
});
|
|
135
|
+
const totalBatches = documents.length > 0 ? Math.ceil(documents.length / batchSize) : 1;
|
|
159
136
|
// Process import with batch processor
|
|
160
137
|
const result = await processor.processImport({
|
|
161
138
|
collectionSlug,
|
|
162
|
-
documents,
|
|
139
|
+
docs: documents,
|
|
140
|
+
format,
|
|
141
|
+
hooks: importHooks,
|
|
163
142
|
importMode,
|
|
164
143
|
matchField,
|
|
144
|
+
originalDocs,
|
|
165
145
|
req,
|
|
146
|
+
totalBatches,
|
|
166
147
|
user
|
|
167
148
|
});
|
|
168
149
|
if (debug) {
|
|
169
150
|
req.payload.logger.info({
|
|
170
151
|
errors: result.errors.length,
|
|
171
152
|
imported: result.imported,
|
|
172
|
-
|
|
153
|
+
msg: 'Import completed',
|
|
173
154
|
total: result.total,
|
|
174
155
|
updated: result.updated
|
|
175
156
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/import/createImport.ts"],"sourcesContent":["import type { PayloadRequest, TypedUser } from 'payload'\n\nimport { APIError } from 'payload'\n\nimport { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js'\nimport { parseCSV } from '../utilities/parseCSV.js'\nimport { parseJSON } from '../utilities/parseJSON.js'\nimport { removeDisabledFields } from '../utilities/removeDisabledFields.js'\nimport { unflattenObject } from '../utilities/unflattenObject.js'\nimport { createImportBatchProcessor } from './batchProcessor.js'\n\nexport type ImportMode = 'create' | 'update' | 'upsert'\n\nexport type Import = {\n /**\n * Number of documents to process in each batch during import\n * @default 100\n */\n batchSize?: number\n collectionSlug: string\n /**\n * If true, enabled debug logging\n */\n debug?: boolean\n file?: {\n data: Buffer\n mimetype: string\n name: string\n }\n format: 'csv' | 'json'\n id?: number | string\n /**\n * Import mode: create, update or upset\n */\n importMode: ImportMode\n matchField?: string\n /**\n * Maximum number of documents that can be imported in a single operation.\n * This value has already been resolved from the plugin config.\n */\n maxLimit?: number\n name: string\n userCollection?: string\n userID?: number | string\n}\n\nexport type CreateImportArgs = {\n defaultVersionStatus?: 'draft' | 'published'\n req: PayloadRequest\n} & Import\n\nexport type ImportResult = {\n errors: Array<{\n doc: Record<string, unknown>\n error: string\n index: number\n }>\n imported: number\n total: number\n updated: number\n}\n\nexport const createImport = async ({\n batchSize = 100,\n collectionSlug,\n debug = false,\n defaultVersionStatus = 'published',\n file,\n format,\n importMode = 'create',\n matchField = 'id',\n maxLimit,\n req,\n userCollection,\n userID,\n}: CreateImportArgs): Promise<ImportResult> => {\n let user: TypedUser | undefined\n\n if (userCollection && userID) {\n user = (await req.payload.findByID({\n id: userID,\n collection: userCollection,\n req,\n })) as TypedUser\n }\n\n if (!user) {\n throw new APIError('User is required for import operations', 401, null, true)\n }\n\n if (debug) {\n req.payload.logger.debug({\n collectionSlug,\n format,\n importMode,\n matchField,\n message: 'Starting import process with args:',\n transactionID: req.transactionID, // Log transaction ID to verify we're in same transaction\n })\n }\n\n if (!collectionSlug) {\n throw new APIError('Collection slug is required', 400, null, true)\n }\n\n if (!file || !file?.data) {\n throw new APIError('No file data provided for import', 400, null, true)\n }\n\n if (debug) {\n req.payload.logger.debug({\n fileName: file.name,\n fileSize: file.data.length,\n message: 'File info',\n mimeType: file.mimetype,\n })\n }\n\n const collectionConfig = req.payload.config.collections.find(\n ({ slug }) => slug === collectionSlug,\n )\n\n if (!collectionConfig) {\n if (!collectionSlug) {\n throw new APIError('Collection slug is required', 400, null, true)\n }\n throw new APIError(`Collection with slug ${collectionSlug} not found`, 400, null, true)\n }\n\n // Get disabled fields configuration\n const disabledFields =\n collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? []\n\n // Get fromCSV functions for field transformations\n const fromCSVFunctions = getImportFieldFunctions({\n fields: collectionConfig.flattenedFields || [],\n })\n\n // Parse the file data\n let documents: Record<string, unknown>[]\n if (format === 'csv') {\n const rawData = await parseCSV({\n data: file.data,\n req,\n })\n\n // Debug logging\n if (debug && rawData.length > 0) {\n req.payload.logger.info({\n firstRow: rawData[0], // Show the complete first row\n msg: 'Parsed CSV data - FULL',\n })\n req.payload.logger.info({\n msg: 'Parsed CSV data',\n rows: rawData.map((row, i) => ({\n excerpt: row.excerpt,\n hasManyNumber: row.hasManyNumber, // Add this to see what we get from CSV\n hasOnePolymorphic_id: row.hasOnePolymorphic_id,\n hasOnePolymorphic_relationTo: row.hasOnePolymorphic_relationTo,\n index: i,\n title: row.title,\n })),\n })\n }\n\n documents = rawData\n\n // Unflatten CSV data\n documents = documents\n .map((doc) => {\n const unflattened = unflattenObject({\n data: doc,\n fields: collectionConfig.flattenedFields ?? [],\n fromCSVFunctions,\n req,\n })\n return unflattened ?? {}\n })\n .filter((doc) => doc && Object.keys(doc).length > 0)\n\n // Debug after unflatten\n if (debug && documents.length > 0) {\n req.payload.logger.info({\n msg: 'After unflatten',\n rows: documents.map((row, i) => ({\n hasManyNumber: row.hasManyNumber, // Add this to see the actual value\n hasManyPolymorphic: row.hasManyPolymorphic,\n hasOnePolymorphic: row.hasOnePolymorphic,\n hasTitle: 'title' in row,\n index: i,\n title: row.title,\n })),\n })\n }\n\n if (debug) {\n req.payload.logger.debug({\n documentCount: documents.length,\n message: 'After unflattening CSV',\n rawDataCount: rawData.length,\n })\n\n // Debug: show a sample of raw vs unflattened\n if (rawData.length > 0 && documents.length > 0) {\n req.payload.logger.debug({\n message: 'Sample data transformation',\n raw: Object.keys(rawData[0] || {}).filter((k) => k.includes('localized')),\n unflattened: JSON.stringify(documents[0], null, 2),\n })\n }\n }\n } else {\n documents = parseJSON({ data: file.data, req })\n }\n\n if (debug) {\n req.payload.logger.debug({\n message: `Parsed ${documents.length} documents from ${format} file`,\n })\n if (documents.length > 0) {\n req.payload.logger.debug({\n doc: documents[0],\n message: 'First document sample:',\n })\n }\n }\n\n // Enforce maxLimit before processing to save memory/time\n if (typeof maxLimit === 'number' && maxLimit > 0 && documents.length > maxLimit) {\n throw new APIError(\n `Import file contains ${documents.length} documents but limit is ${maxLimit}`,\n 400,\n null,\n true,\n )\n }\n\n // Remove disabled fields from all documents\n if (disabledFields.length > 0) {\n documents = documents.map((doc) => removeDisabledFields(doc, disabledFields))\n }\n\n if (debug) {\n req.payload.logger.debug({\n batchSize,\n documentCount: documents.length,\n message: 'Processing import in batches',\n })\n }\n\n // Create batch processor\n const processor = createImportBatchProcessor({\n batchSize,\n defaultVersionStatus,\n })\n\n // Process import with batch processor\n const result = await processor.processImport({\n collectionSlug,\n documents,\n importMode,\n matchField,\n req,\n user,\n })\n\n if (debug) {\n req.payload.logger.info({\n errors: result.errors.length,\n imported: result.imported,\n message: 'Import completed',\n total: result.total,\n updated: result.updated,\n })\n }\n\n return result\n}\n"],"names":["APIError","getImportFieldFunctions","parseCSV","parseJSON","removeDisabledFields","unflattenObject","createImportBatchProcessor","createImport","batchSize","collectionSlug","debug","defaultVersionStatus","file","format","importMode","matchField","maxLimit","req","userCollection","userID","user","payload","findByID","id","collection","logger","message","transactionID","data","fileName","name","fileSize","length","mimeType","mimetype","collectionConfig","config","collections","find","slug","disabledFields","admin","custom","fromCSVFunctions","fields","flattenedFields","documents","rawData","info","firstRow","msg","rows","map","row","i","excerpt","hasManyNumber","hasOnePolymorphic_id","hasOnePolymorphic_relationTo","index","title","doc","unflattened","filter","Object","keys","hasManyPolymorphic","hasOnePolymorphic","hasTitle","documentCount","rawDataCount","raw","k","includes","JSON","stringify","processor","result","processImport","errors","imported","total","updated"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAElC,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,oBAAoB,QAAQ,uCAAsC;AAC3E,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,0BAA0B,QAAQ,sBAAqB;AAqDhE,OAAO,MAAMC,eAAe,OAAO,EACjCC,YAAY,GAAG,EACfC,cAAc,EACdC,QAAQ,KAAK,EACbC,uBAAuB,WAAW,EAClCC,IAAI,EACJC,MAAM,EACNC,aAAa,QAAQ,EACrBC,aAAa,IAAI,EACjBC,QAAQ,EACRC,GAAG,EACHC,cAAc,EACdC,MAAM,EACW;IACjB,IAAIC;IAEJ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YA,UAAUM,GAAG,CAAC,CAACS,MAAQzD,qBAAqByD,KAAKrB;IAC/D;IAEA,IAAI9B,OAAO;QACTO,IAAII,OAAO,CAACI,MAAM,CAACf,KAAK,CAAC;YACvBF;YACA6D,eAAevB,UAAUd,MAAM;YAC/BN,SAAS;QACX;IACF;IAEA,yBAAyB;IACzB,MAAMkD,YAAYtE,2BAA2B;QAC3CE;QACAG;IACF;IAEA,sCAAsC;IACtC,MAAMkE,SAAS,MAAMD,UAAUE,aAAa,CAAC;QAC3CrE;QACAqC;QACAhC;QACAC;QACAE;QACAG;IACF;IAEA,IAAIV,OAAO;QACTO,IAAII,OAAO,CAACI,MAAM,CAACuB,IAAI,CAAC;YACtB+B,QAAQF,OAAOE,MAAM,CAAC/C,MAAM;YAC5BgD,UAAUH,OAAOG,QAAQ;YACzBtD,SAAS;YACTuD,OAAOJ,OAAOI,KAAK;YACnBC,SAASL,OAAOK,OAAO;QACzB;IACF;IAEA,OAAOL;AACT,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/import/createImport.ts"],"sourcesContent":["import type { PayloadRequest, TypedUser } from 'payload'\n\nimport { APIError } from 'payload'\n\nimport type { ImportResult } from '../types.js'\n\nimport { applyFieldHooks } from '../utilities/applyFieldHooks.js'\nimport { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js'\nimport { parseCSV } from '../utilities/parseCSV.js'\nimport { parseJSON } from '../utilities/parseJSON.js'\nimport { removeDisabledFields } from '../utilities/removeDisabledFields.js'\nimport { unflattenObject } from '../utilities/unflattenObject.js'\nimport { createImportBatchProcessor } from './batchProcessor.js'\n\nexport type ImportMode = 'create' | 'update' | 'upsert'\n\nexport type Import = {\n /**\n * Number of documents to process in each batch during import\n * @default 100\n */\n batchSize?: number\n collectionSlug: string\n /**\n * If true, enabled debug logging\n */\n debug?: boolean\n file?: {\n data: Buffer\n mimetype: string\n name: string\n }\n format: 'csv' | 'json'\n id?: number | string\n /**\n * Import mode: create, update or upset\n */\n importMode: ImportMode\n matchField?: string\n /**\n * Maximum number of documents that can be imported in a single operation.\n * This value has already been resolved from the plugin config.\n */\n maxLimit?: number\n name: string\n userCollection?: string\n userID?: number | string\n}\n\nexport type CreateImportArgs = {\n defaultVersionStatus?: 'draft' | 'published'\n req: PayloadRequest\n} & Import\n\nexport const createImport = async ({\n batchSize = 100,\n collectionSlug,\n debug = false,\n defaultVersionStatus = 'published',\n file,\n format,\n importMode = 'create',\n matchField = 'id',\n maxLimit,\n req,\n userCollection,\n userID,\n}: CreateImportArgs): Promise<ImportResult> => {\n let user: TypedUser | undefined\n\n if (userCollection && userID) {\n user = (await req.payload.findByID({\n id: userID,\n collection: userCollection,\n req,\n })) as TypedUser\n }\n\n if (!user) {\n throw new APIError('User is required for import operations', 401, null, true)\n }\n\n if (debug) {\n req.payload.logger.debug({\n collectionSlug,\n format,\n importMode,\n matchField,\n msg: 'Starting import process with args:',\n transactionID: req.transactionID, // Log transaction ID to verify we're in same transaction\n })\n }\n\n if (!collectionSlug) {\n throw new APIError('Collection slug is required', 400, null, true)\n }\n\n if (!file || !file?.data) {\n throw new APIError('No file data provided for import', 400, null, true)\n }\n\n if (debug) {\n req.payload.logger.debug({\n fileName: file.name,\n fileSize: file.data.length,\n mimeType: file.mimetype,\n msg: 'File info',\n })\n }\n\n const collectionConfig = req.payload.config.collections.find(\n ({ slug }) => slug === collectionSlug,\n )\n\n if (!collectionConfig) {\n if (!collectionSlug) {\n throw new APIError('Collection slug is required', 400, null, true)\n }\n throw new APIError(`Collection with slug ${collectionSlug} not found`, 400, null, true)\n }\n\n // Get disabled fields configuration\n const disabledFields =\n collectionConfig.admin?.custom?.['plugin-import-export']?.disabledFields ?? []\n\n const importHooks = collectionConfig.custom?.['plugin-import-export']?.importHooks\n\n // Get fromCSV functions for field transformations\n const importFieldHooks = getImportFieldFunctions({\n fields: collectionConfig.flattenedFields || [],\n })\n\n // Parse the file data\n let originalDocs: Record<string, unknown>[] | undefined\n let documents: Record<string, unknown>[]\n if (format === 'csv') {\n const rawData = await parseCSV({\n data: file.data,\n req,\n })\n\n originalDocs = rawData\n documents = rawData\n\n // Unflatten CSV data\n documents = documents\n .map((doc) => {\n const unflattened = unflattenObject({\n data: doc,\n fields: collectionConfig.flattenedFields ?? [],\n format,\n importFieldHooks,\n req,\n })\n return unflattened ?? {}\n })\n .filter((doc) => doc && Object.keys(doc).length > 0)\n\n if (debug) {\n req.payload.logger.debug({\n documentCount: documents.length,\n msg: 'After unflattening CSV',\n rawDataCount: rawData.length,\n })\n }\n } else {\n const parsedDocs = parseJSON({ data: file.data, req })\n originalDocs = parsedDocs\n // Apply field-level import hooks for JSON format\n documents = parsedDocs.map((doc) =>\n applyFieldHooks({\n type: 'beforeImport',\n data: doc,\n fieldHooks: importFieldHooks,\n fields: collectionConfig.flattenedFields ?? [],\n format,\n operation: 'import',\n req,\n }),\n )\n }\n\n if (debug) {\n req.payload.logger.debug({\n msg: `Parsed ${documents.length} documents from ${format} file`,\n })\n if (documents.length > 0) {\n req.payload.logger.debug({\n doc: documents[0],\n msg: 'First document sample:',\n })\n }\n }\n\n // Enforce maxLimit before processing to save memory/time\n if (typeof maxLimit === 'number' && maxLimit > 0 && documents.length > maxLimit) {\n throw new APIError(\n `Import file contains ${documents.length} documents but limit is ${maxLimit}`,\n 400,\n null,\n true,\n )\n }\n\n // Remove disabled fields from all documents\n if (disabledFields.length > 0) {\n documents = documents.map((doc) => removeDisabledFields(doc, disabledFields))\n }\n\n if (debug) {\n req.payload.logger.debug({\n batchSize,\n documentCount: documents.length,\n msg: 'Processing import in batches',\n })\n }\n\n // Create batch processor\n const processor = createImportBatchProcessor({\n batchSize,\n defaultVersionStatus,\n })\n\n const totalBatches = documents.length > 0 ? Math.ceil(documents.length / batchSize) : 1\n\n // Process import with batch processor\n const result = await processor.processImport({\n collectionSlug,\n docs: documents,\n format,\n hooks: importHooks,\n importMode,\n matchField,\n originalDocs,\n req,\n totalBatches,\n user,\n })\n\n if (debug) {\n req.payload.logger.info({\n errors: result.errors.length,\n imported: result.imported,\n msg: 'Import completed',\n total: result.total,\n updated: result.updated,\n })\n }\n\n return result\n}\n"],"names":["APIError","applyFieldHooks","getImportFieldFunctions","parseCSV","parseJSON","removeDisabledFields","unflattenObject","createImportBatchProcessor","createImport","batchSize","collectionSlug","debug","defaultVersionStatus","file","format","importMode","matchField","maxLimit","req","userCollection","userID","user","payload","findByID","id","collection","logger","msg","transactionID","data","fileName","name","fileSize","length","mimeType","mimetype","collectionConfig","config","collections","find","slug","disabledFields","admin","custom","importHooks","importFieldHooks","fields","flattenedFields","originalDocs","documents","rawData","map","doc","unflattened","filter","Object","keys","documentCount","rawDataCount","parsedDocs","type","fieldHooks","operation","processor","totalBatches","Math","ceil","result","processImport","docs","hooks","info","errors","imported","total","updated"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAIlC,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,oBAAoB,QAAQ,uCAAsC;AAC3E,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,0BAA0B,QAAQ,sBAAqB;AA0ChE,OAAO,MAAMC,eAAe,OAAO,EACjCC,YAAY,GAAG,EACfC,cAAc,EACdC,QAAQ,KAAK,EACbC,uBAAuB,WAAW,EAClCC,IAAI,EACJC,MAAM,EACNC,aAAa,QAAQ,EACrBC,aAAa,IAAI,EACjBC,QAAQ,EACRC,GAAG,EACHC,cAAc,EACdC,MAAM,EACW;IACjB,IAAIC;IAEJ,IAAIF,kBAAkBC,QAAQ;QAC5BC,OAAQ,MAAMH,IAAII,OAAO,CAACC,QAAQ,CAAC;YACjCC,IAAIJ;YACJK,YAAYN;YACZD;QACF;IACF;IAEA,IAAI,CAACG,MAAM;QACT,MAAM,IAAIrB,SAAS,0CAA0C,KAAK,MAAM;IAC1E;IAEA,IAAIW,OAAO;QACTO,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AAM,CAAC,wBAAwB,EAAEhB,UAAU,EAC7E,KACA,MACA;IAEJ;IAEA,4CAA4C;IAC5C,IAAIwB,eAAeR,MAAM,GAAG,GAAG;QAC7BgB,YAAYA,UAAUE,GAAG,CAAC,CAACC,MAAQ/C,qBAAqB+C,KAAKX;IAC/D;IAEA,IAAI9B,OAAO;QACTO,IAAII,OAAO,CAACI,MAAM,CAACf,KAAK,CAAC;YACvBF;YACAgD,eAAeR,UAAUhB,MAAM;YAC/BN,KAAK;QACP;IACF;IAEA,yBAAyB;IACzB,MAAMoC,YAAYxD,2BAA2B;QAC3CE;QACAG;IACF;IAEA,MAAMoD,eAAef,UAAUhB,MAAM,GAAG,IAAIgC,KAAKC,IAAI,CAACjB,UAAUhB,MAAM,GAAGxB,aAAa;IAEtF,sCAAsC;IACtC,MAAM0D,SAAS,MAAMJ,UAAUK,aAAa,CAAC;QAC3C1D;QACA2D,MAAMpB;QACNnC;QACAwD,OAAO1B;QACP7B;QACAC;QACAgC;QACA9B;QACA8C;QACA3C;IACF;IAEA,IAAIV,OAAO;QACTO,IAAII,OAAO,CAACI,MAAM,CAAC6C,IAAI,CAAC;YACtBC,QAAQL,OAAOK,MAAM,CAACvC,MAAM;YAC5BwC,UAAUN,OAAOM,QAAQ;YACzB9C,KAAK;YACL+C,OAAOP,OAAOO,KAAK;YACnBC,SAASR,OAAOQ,OAAO;QACzB;IACF;IAEA,OAAOR;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlePreview.d.ts","sourceRoot":"","sources":["../../src/import/handlePreview.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"handlePreview.d.ts","sourceRoot":"","sources":["../../src/import/handlePreview.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAoB7C,eAAO,MAAM,aAAa,QAAe,cAAc,KAAG,OAAO,CAAC,QAAQ,CAuIzE,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { addDataAndFileToRequest } from 'payload';
|
|
2
2
|
import { DEFAULT_PREVIEW_LIMIT, MAX_PREVIEW_LIMIT, MIN_PREVIEW_LIMIT, MIN_PREVIEW_PAGE } from '../constants.js';
|
|
3
|
+
import { applyFieldHooks } from '../utilities/applyFieldHooks.js';
|
|
3
4
|
import { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js';
|
|
4
5
|
import { parseCSV } from '../utilities/parseCSV.js';
|
|
5
6
|
import { parseJSON } from '../utilities/parseJSON.js';
|
|
@@ -36,31 +37,56 @@ export const handlePreview = async (req)=>{
|
|
|
36
37
|
try {
|
|
37
38
|
// Parse the file data
|
|
38
39
|
let parsedData;
|
|
40
|
+
let originalDocs = [];
|
|
39
41
|
const buffer = Buffer.from(fileData, 'base64');
|
|
42
|
+
const importFieldHooks = getImportFieldFunctions({
|
|
43
|
+
fields: targetCollection.config.flattenedFields || []
|
|
44
|
+
});
|
|
40
45
|
if (format === 'csv') {
|
|
41
46
|
const rawData = await parseCSV({
|
|
42
47
|
data: buffer,
|
|
43
48
|
req
|
|
44
49
|
});
|
|
45
|
-
|
|
46
|
-
const fromCSVFunctions = getImportFieldFunctions({
|
|
47
|
-
fields: targetCollection.config.flattenedFields || []
|
|
48
|
-
});
|
|
50
|
+
originalDocs = rawData;
|
|
49
51
|
// Unflatten CSV data
|
|
50
52
|
parsedData = rawData.map((doc)=>{
|
|
51
53
|
const unflattened = unflattenObject({
|
|
52
54
|
data: doc,
|
|
53
55
|
fields: targetCollection.config.flattenedFields ?? [],
|
|
54
|
-
|
|
56
|
+
format: 'csv',
|
|
57
|
+
importFieldHooks,
|
|
55
58
|
req
|
|
56
59
|
});
|
|
57
60
|
return unflattened ?? {};
|
|
58
61
|
}).filter((doc)=>doc && Object.keys(doc).length > 0);
|
|
59
62
|
} else {
|
|
60
|
-
|
|
63
|
+
const parsedDocs = parseJSON({
|
|
61
64
|
data: buffer,
|
|
62
65
|
req
|
|
63
66
|
});
|
|
67
|
+
originalDocs = parsedDocs;
|
|
68
|
+
// Apply field-level import hooks for JSON format
|
|
69
|
+
parsedData = parsedDocs.map((doc)=>applyFieldHooks({
|
|
70
|
+
type: 'beforeImport',
|
|
71
|
+
data: doc,
|
|
72
|
+
fieldHooks: importFieldHooks,
|
|
73
|
+
fields: targetCollection.config.flattenedFields ?? [],
|
|
74
|
+
format: 'json',
|
|
75
|
+
operation: 'import',
|
|
76
|
+
req
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
const importHooks = targetCollection.config.custom?.['plugin-import-export']?.importHooks;
|
|
80
|
+
if (importHooks?.before && parsedData.length > 0) {
|
|
81
|
+
const result = await importHooks.before({
|
|
82
|
+
batchNumber: 1,
|
|
83
|
+
data: parsedData,
|
|
84
|
+
format: format ?? 'csv',
|
|
85
|
+
originalData: originalDocs,
|
|
86
|
+
req,
|
|
87
|
+
totalBatches: 1
|
|
88
|
+
});
|
|
89
|
+
parsedData = result;
|
|
64
90
|
}
|
|
65
91
|
// Remove disabled fields from the documents
|
|
66
92
|
const disabledFields = targetCollection.config.admin?.custom?.['plugin-import-export']?.disabledFields ?? [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/import/handlePreview.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { addDataAndFileToRequest } from 'payload'\n\nimport type { ImportPreviewResponse } from '../types.js'\n\nimport {\n DEFAULT_PREVIEW_LIMIT,\n MAX_PREVIEW_LIMIT,\n MIN_PREVIEW_LIMIT,\n MIN_PREVIEW_PAGE,\n} from '../constants.js'\nimport { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js'\nimport { parseCSV } from '../utilities/parseCSV.js'\nimport { parseJSON } from '../utilities/parseJSON.js'\nimport { removeDisabledFields } from '../utilities/removeDisabledFields.js'\nimport { resolveLimit } from '../utilities/resolveLimit.js'\nimport { unflattenObject } from '../utilities/unflattenObject.js'\n\nexport const handlePreview = async (req: PayloadRequest): Promise<Response> => {\n await addDataAndFileToRequest(req)\n\n const {\n collectionSlug,\n fileData,\n format,\n previewLimit: rawPreviewLimit = DEFAULT_PREVIEW_LIMIT,\n previewPage: rawPreviewPage = 1,\n } = req.data as {\n collectionSlug: string\n fileData?: string\n format?: 'csv' | 'json'\n previewLimit?: number\n previewPage?: number\n }\n\n // Validate and clamp pagination values to safe bounds\n const previewLimit = Math.max(MIN_PREVIEW_LIMIT, Math.min(rawPreviewLimit, MAX_PREVIEW_LIMIT))\n const previewPage = Math.max(MIN_PREVIEW_PAGE, rawPreviewPage)\n\n const targetCollection = req.payload.collections[collectionSlug]\n if (!targetCollection) {\n return Response.json(\n { error: `Collection with slug ${collectionSlug} not found` },\n { status: 400 },\n )\n }\n\n // Resolve max limit from the collection config\n const pluginConfig = targetCollection.config.custom?.['plugin-import-export']\n const maxLimit = await resolveLimit({\n limit: pluginConfig?.importLimit,\n req,\n })\n\n if (!fileData) {\n return Response.json({ error: 'No file data provided' }, { status: 400 })\n }\n\n try {\n // Parse the file data\n let parsedData: Record<string, unknown>[]\n const buffer = Buffer.from(fileData, 'base64')\n\n if (format === 'csv') {\n const rawData = await parseCSV({ data: buffer, req })\n
|
|
1
|
+
{"version":3,"sources":["../../src/import/handlePreview.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { addDataAndFileToRequest } from 'payload'\n\nimport type { ImportPreviewResponse } from '../types.js'\n\nimport {\n DEFAULT_PREVIEW_LIMIT,\n MAX_PREVIEW_LIMIT,\n MIN_PREVIEW_LIMIT,\n MIN_PREVIEW_PAGE,\n} from '../constants.js'\nimport { applyFieldHooks } from '../utilities/applyFieldHooks.js'\nimport { getImportFieldFunctions } from '../utilities/getImportFieldFunctions.js'\nimport { parseCSV } from '../utilities/parseCSV.js'\nimport { parseJSON } from '../utilities/parseJSON.js'\nimport { removeDisabledFields } from '../utilities/removeDisabledFields.js'\nimport { resolveLimit } from '../utilities/resolveLimit.js'\nimport { unflattenObject } from '../utilities/unflattenObject.js'\n\nexport const handlePreview = async (req: PayloadRequest): Promise<Response> => {\n await addDataAndFileToRequest(req)\n\n const {\n collectionSlug,\n fileData,\n format,\n previewLimit: rawPreviewLimit = DEFAULT_PREVIEW_LIMIT,\n previewPage: rawPreviewPage = 1,\n } = req.data as {\n collectionSlug: string\n fileData?: string\n format?: 'csv' | 'json'\n previewLimit?: number\n previewPage?: number\n }\n\n // Validate and clamp pagination values to safe bounds\n const previewLimit = Math.max(MIN_PREVIEW_LIMIT, Math.min(rawPreviewLimit, MAX_PREVIEW_LIMIT))\n const previewPage = Math.max(MIN_PREVIEW_PAGE, rawPreviewPage)\n\n const targetCollection = req.payload.collections[collectionSlug]\n if (!targetCollection) {\n return Response.json(\n { error: `Collection with slug ${collectionSlug} not found` },\n { status: 400 },\n )\n }\n\n // Resolve max limit from the collection config\n const pluginConfig = targetCollection.config.custom?.['plugin-import-export']\n const maxLimit = await resolveLimit({\n limit: pluginConfig?.importLimit,\n req,\n })\n\n if (!fileData) {\n return Response.json({ error: 'No file data provided' }, { status: 400 })\n }\n\n try {\n // Parse the file data\n let parsedData: Record<string, unknown>[]\n let originalDocs: Record<string, unknown>[] = []\n const buffer = Buffer.from(fileData, 'base64')\n\n const importFieldHooks = getImportFieldFunctions({\n fields: targetCollection.config.flattenedFields || [],\n })\n\n if (format === 'csv') {\n const rawData = await parseCSV({ data: buffer, req })\n originalDocs = rawData\n\n // Unflatten CSV data\n parsedData = rawData\n .map((doc) => {\n const unflattened = unflattenObject({\n data: doc,\n fields: targetCollection.config.flattenedFields ?? [],\n format: 'csv',\n importFieldHooks,\n req,\n })\n return unflattened ?? {}\n })\n .filter((doc) => doc && Object.keys(doc).length > 0)\n } else {\n const parsedDocs = parseJSON({ data: buffer, req })\n originalDocs = parsedDocs\n // Apply field-level import hooks for JSON format\n parsedData = parsedDocs.map((doc) =>\n applyFieldHooks({\n type: 'beforeImport',\n data: doc,\n fieldHooks: importFieldHooks,\n fields: targetCollection.config.flattenedFields ?? [],\n format: 'json',\n operation: 'import',\n req,\n }),\n )\n }\n\n const importHooks = targetCollection.config.custom?.['plugin-import-export']?.importHooks\n if (importHooks?.before && parsedData.length > 0) {\n const result = await importHooks.before({\n batchNumber: 1,\n data: parsedData as unknown as Parameters<typeof importHooks.before>[0]['data'],\n format: format ?? 'csv',\n originalData: originalDocs,\n req,\n totalBatches: 1,\n })\n parsedData = result as unknown as Record<string, unknown>[]\n }\n\n // Remove disabled fields from the documents\n const disabledFields =\n targetCollection.config.admin?.custom?.['plugin-import-export']?.disabledFields ?? []\n\n if (disabledFields.length > 0) {\n parsedData = parsedData.map((doc) => removeDisabledFields(doc, disabledFields))\n }\n\n // Calculate pagination\n const totalDocs = parsedData.length\n const totalPages = totalDocs === 0 ? 0 : Math.ceil(totalDocs / previewLimit)\n const startIndex = (previewPage - 1) * previewLimit\n const endIndex = startIndex + previewLimit\n const paginatedDocs = parsedData.slice(startIndex, endIndex)\n\n const hasNextPage = previewPage < totalPages\n const hasPrevPage = previewPage > 1\n\n // Check if the file exceeds the max limit\n const limitExceeded = typeof maxLimit === 'number' && maxLimit > 0 && totalDocs > maxLimit\n\n const response: ImportPreviewResponse = {\n docs: paginatedDocs,\n hasNextPage,\n hasPrevPage,\n limit: previewLimit,\n limitExceeded,\n maxLimit,\n page: previewPage,\n totalDocs,\n totalPages,\n }\n\n return Response.json(response)\n } catch (error) {\n req.payload.logger.error({ err: error, msg: 'Error parsing import preview data' })\n return Response.json({ error: 'Failed to parse file data' }, { status: 500 })\n }\n}\n"],"names":["addDataAndFileToRequest","DEFAULT_PREVIEW_LIMIT","MAX_PREVIEW_LIMIT","MIN_PREVIEW_LIMIT","MIN_PREVIEW_PAGE","applyFieldHooks","getImportFieldFunctions","parseCSV","parseJSON","removeDisabledFields","resolveLimit","unflattenObject","handlePreview","req","collectionSlug","fileData","format","previewLimit","rawPreviewLimit","previewPage","rawPreviewPage","data","Math","max","min","targetCollection","payload","collections","Response","json","error","status","pluginConfig","config","custom","maxLimit","limit","importLimit","parsedData","originalDocs","buffer","Buffer","from","importFieldHooks","fields","flattenedFields","rawData","map","doc","unflattened","filter","Object","keys","length","parsedDocs","type","fieldHooks","operation","importHooks","before","result","batchNumber","originalData","totalBatches","disabledFields","admin","totalDocs","totalPages","ceil","startIndex","endIndex","paginatedDocs","slice","hasNextPage","hasPrevPage","limitExceeded","response","docs","page","logger","err","msg"],"mappings":"AAEA,SAASA,uBAAuB,QAAQ,UAAS;AAIjD,SACEC,qBAAqB,EACrBC,iBAAiB,EACjBC,iBAAiB,EACjBC,gBAAgB,QACX,kBAAiB;AACxB,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,uBAAuB,QAAQ,0CAAyC;AACjF,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,oBAAoB,QAAQ,uCAAsC;AAC3E,SAASC,YAAY,QAAQ,+BAA8B;AAC3D,SAASC,eAAe,QAAQ,kCAAiC;AAEjE,OAAO,MAAMC,gBAAgB,OAAOC;IAClC,MAAMb,wBAAwBa;IAE9B,MAAM,EACJC,cAAc,EACdC,QAAQ,EACRC,MAAM,EACNC,cAAcC,kBAAkBjB,qBAAqB,EACrDkB,aAAaC,iBAAiB,CAAC,EAChC,GAAGP,IAAIQ,IAAI;IAQZ,sDAAsD;IACtD,MAAMJ,eAAeK,KAAKC,GAAG,CAACpB,mBAAmBmB,KAAKE,GAAG,CAACN,iBAAiBhB;IAC3E,MAAMiB,cAAcG,KAAKC,GAAG,CAACnB,kBAAkBgB;IAE/C,MAAMK,mBAAmBZ,IAAIa,OAAO,CAACC,WAAW,CAACb,eAAe;IAChE,IAAI,CAACW,kBAAkB;QACrB,OAAOG,SAASC,IAAI,CAClB;YAAEC,OAAO,CAAC,qBAAqB,EAAEhB,eAAe,UAAU,CAAC;QAAC,GAC5D;YAAEiB,QAAQ;QAAI;IAElB;IAEA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