Files
payload/packages/plugin-import-export/src/index.ts
Dan Ribbens 4f822a439b feat: plugin-import-export initial work (#10795)
Adds new plugin-import-export initial version.

Allows for direct download and creation of downloadable collection data
stored to a json or csv uses the access control of the user creating the
request to make the file.

config options:
```ts
  /**
   * Collections to include the Import/Export controls in
   * Defaults to all collections
   */
  collections?: string[]
  /**
   * Enable to force the export to run synchronously
   */
  disableJobsQueue?: boolean
  /**
   * This function takes the default export collection configured in the plugin and allows you to override it by modifying and returning it
   * @param collection
   * @returns collection
   */
  overrideExportCollection?: (collection: CollectionOverride) => CollectionOverride

// payload.config.ts:
plugins: [
    importExportPlugin({
      collections: ['pages', 'users'],
      overrideExportCollection: (collection) => {
        collection.admin.group = 'System'
        collection.upload.staticDir = path.resolve(dirname, 'uploads')
        return collection
      },
      disableJobsQueue: true,
    }),
 ],
```

---------

Co-authored-by: Jessica Chowdhury <jessica@trbl.design>
Co-authored-by: Kendell Joseph <kendelljoseph@gmail.com>
2025-03-05 01:06:43 +00:00

73 lines
2.4 KiB
TypeScript

import type { Config, JobsConfig } from 'payload'
import { deepMergeSimple } from 'payload'
import type { ImportExportPluginConfig } from './types.js'
import { getCreateCollectionExportTask } from './export/getCreateExportCollectionTask.js'
import { getExportCollection } from './getExportCollection.js'
import { translations } from './translations/index.js'
export const importExportPlugin =
(pluginConfig: ImportExportPluginConfig) =>
(config: Config): Config => {
const exportCollection = getExportCollection({ config, pluginConfig })
if (config.collections) {
config.collections.push(exportCollection)
} else {
config.collections = [exportCollection]
}
// inject custom import export provider
config.admin = config.admin || {}
config.admin.components = config.admin.components || {}
config.admin.components.providers = config.admin.components.providers || []
config.admin.components.providers.push(
'@payloadcms/plugin-import-export/rsc#ImportExportProvider',
)
// inject the createExport job into the config
config.jobs =
config.jobs ||
({
tasks: [getCreateCollectionExportTask(config)],
} as unknown as JobsConfig) // cannot type jobs config inside of plugins
let collectionsToUpdate = config.collections
const usePluginCollections = pluginConfig.collections && pluginConfig.collections?.length > 0
if (usePluginCollections) {
collectionsToUpdate = config.collections?.filter((collection) => {
return pluginConfig.collections?.includes(collection.slug)
})
}
collectionsToUpdate.forEach((collection) => {
if (!collection.admin) {
collection.admin = { components: { listMenuItems: [] } }
}
const components = collection.admin.components || {}
if (!components.listMenuItems) {
components.listMenuItems = []
}
if (!components.edit) {
components.edit = {}
}
if (!components.edit.SaveButton) {
components.edit.SaveButton = '@payloadcms/plugin-import-export/rsc#ExportSaveButton'
}
components.listMenuItems.push({
clientProps: {
exportCollectionSlug: exportCollection.slug,
},
path: '@payloadcms/plugin-import-export/rsc#ExportListMenuItem',
})
collection.admin.components = components
})
config.i18n = deepMergeSimple(translations, config.i18n?.translations ?? {})
return config
}