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>
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { parse } from 'csv-parse'
|
|
import fs from 'fs'
|
|
|
|
export const readCSV = async (path: string): Promise<any[]> => {
|
|
const buffer = fs.readFileSync(path)
|
|
const data: any[] = []
|
|
const promise = new Promise<void>((resolve) => {
|
|
const parser = parse({ bom: true, columns: true })
|
|
|
|
// Collect data from the CSV
|
|
parser.on('readable', () => {
|
|
let record
|
|
while ((record = parser.read())) {
|
|
data.push(record)
|
|
}
|
|
})
|
|
|
|
// Resolve the promise on 'end'
|
|
parser.on('end', () => {
|
|
resolve()
|
|
})
|
|
|
|
// Handle errors (optional, but good practice)
|
|
parser.on('error', (error) => {
|
|
console.error('Error parsing CSV:', error)
|
|
resolve() // Ensures promise doesn't hang on error
|
|
})
|
|
|
|
// Pipe the buffer into the parser
|
|
parser.write(buffer)
|
|
parser.end()
|
|
})
|
|
|
|
// Await the promise
|
|
await promise
|
|
|
|
return data
|
|
}
|
|
|
|
export const readJSON = async (path: string): Promise<any[]> => {
|
|
const buffer = fs.readFileSync(path)
|
|
const str = buffer.toString()
|
|
|
|
try {
|
|
const json = await JSON.parse(str)
|
|
return json
|
|
} catch (error) {
|
|
console.error('Error reading JSON file:', error)
|
|
throw error
|
|
}
|
|
}
|