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>
89 lines
2.0 KiB
TypeScript
89 lines
2.0 KiB
TypeScript
import type {
|
|
CollectionAfterChangeHook,
|
|
CollectionBeforeChangeHook,
|
|
CollectionBeforeOperationHook,
|
|
CollectionConfig,
|
|
Config,
|
|
} from 'payload'
|
|
|
|
import type { CollectionOverride, ImportExportPluginConfig } from './types.js'
|
|
|
|
import { createExport } from './export/createExport.js'
|
|
import { download } from './export/download.js'
|
|
import { getFields } from './export/getFields.js'
|
|
|
|
export const getExportCollection = ({
|
|
config,
|
|
pluginConfig,
|
|
}: {
|
|
config: Config
|
|
pluginConfig: ImportExportPluginConfig
|
|
}): CollectionConfig => {
|
|
const { overrideExportCollection } = pluginConfig
|
|
|
|
const beforeOperation: CollectionBeforeOperationHook[] = []
|
|
const afterChange: CollectionAfterChangeHook[] = []
|
|
|
|
let collection: CollectionOverride = {
|
|
slug: 'exports',
|
|
access: {
|
|
update: () => false,
|
|
},
|
|
admin: {
|
|
group: false,
|
|
useAsTitle: 'name',
|
|
},
|
|
disableDuplicate: true,
|
|
endpoints: [
|
|
{
|
|
handler: download,
|
|
method: 'post',
|
|
path: '/download',
|
|
},
|
|
],
|
|
fields: getFields(config),
|
|
hooks: {
|
|
afterChange,
|
|
beforeOperation,
|
|
},
|
|
upload: {
|
|
filesRequiredOnCreate: false,
|
|
hideFileInputOnCreate: true,
|
|
hideRemoveFile: true,
|
|
},
|
|
}
|
|
|
|
if (typeof overrideExportCollection === 'function') {
|
|
collection = overrideExportCollection(collection)
|
|
}
|
|
|
|
if (pluginConfig.disableJobsQueue) {
|
|
beforeOperation.push(async ({ args, operation, req }) => {
|
|
if (operation !== 'create') {
|
|
return
|
|
}
|
|
const { user } = req
|
|
await createExport({ input: { ...args.data, user }, req })
|
|
})
|
|
} else {
|
|
afterChange.push(async ({ doc, operation, req }) => {
|
|
if (operation !== 'create') {
|
|
return
|
|
}
|
|
|
|
const input = {
|
|
...doc,
|
|
exportsCollection: collection.slug,
|
|
user: req?.user?.id || req?.user?.user?.id,
|
|
userCollection: 'users',
|
|
}
|
|
await req.payload.jobs.queue({
|
|
input,
|
|
task: 'createCollectionExport',
|
|
})
|
|
})
|
|
}
|
|
|
|
return collection
|
|
}
|