-
Notifications
You must be signed in to change notification settings - Fork 12
feat: export delimited zip #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f7bb4ae
feat: export delimited zip
3c3b674
rollup -> tsup
20d73b0
exclude browser build
ee4aeb9
guardrail for empty sheets
168d43a
simplify
9e608f0
Merge branch 'main' into feat/export-delimited-zip
bangarang fb0ab32
add server-side note
d8c4b4c
update rollup config to be UMD-first
86fd03c
Add changeset
b4c5b7e
Update export/delimited-zip/README.MD
f835bd3
add name sanitation
6db7ee0
Merge branch 'feat/export-delimited-zip' of https://github.com/FlatFi…
0a48ce4
update orchestration url
0b85535
Update lemon-bears-drive.md
7c3604e
Update export.delimited.zip.plugin.ts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@flatfile/plugin-delimiter-extractor': minor | ||
| '@flatfile/plugin-export-workbook': minor | ||
| '@flatfile/bundler-config-rollup': minor | ||
| '@flatfile/plugin-export-delimited-zip': minor | ||
| '@flatfile/plugin-record-hook': minor | ||
| '@flatfile/plugin-export-pivot-table': minor | ||
| '@flatfile/plugin-rollout': minor | ||
| '@flatfile/plugin-dedupe': minor | ||
| --- | ||
|
|
||
| This release introduces the @flatfile/plugin-export-delimited-zip plugin. This plugin runs on a Workbook-level action to export the Workbook's Sheets as CSV files in a Zip file and uploaded back to Flatfile. | ||
|
|
||
| Additionally, this PR updates some documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <!-- START_INFOCARD --> | ||
|
|
||
| The `@flatfile/plugin-export-delimited-zip` plugin exports data from Flatfile sheets to delimited files (e.g., CSV) and compresses them into a ZIP file. This plugin provides an efficient way to download and package your data from Flatfile workbooks. | ||
|
|
||
| **Event Type:** | ||
| `job:ready` | ||
|
|
||
| <!-- END_INFOCARD --> | ||
|
|
||
|
|
||
| > When embedding Flatfile, this plugin should be deployed in a server-side listener. [Learn more](/docs/orchestration/listeners#listener-types) | ||
|
|
||
|
|
||
|
|
||
| ## Parameters | ||
|
|
||
| #### `job` - `string` | ||
| The job name to trigger the export. Default: 'downloadDelimited' | ||
|
|
||
| #### `delimiter` - `string` | ||
| The delimiter to use in the exported files. Default: ',' | ||
|
|
||
| #### `fileExtension` - `string` | ||
| The file extension for the exported files. Default: 'csv' | ||
|
|
||
| #### `debug` - `boolean` - (optional) | ||
| Enable debug logging. Default: false | ||
|
|
||
|
|
||
|
|
||
| ## API Calls | ||
|
|
||
| - `api.jobs.ack` | ||
| - `api.jobs.complete` | ||
| - `api.jobs.fail` | ||
| - `api.sheets.list` | ||
| - `api.records.get` | ||
| - `api.files.upload` | ||
|
|
||
|
|
||
|
|
||
| ## Usage | ||
|
|
||
| **install** | ||
| ```bash | ||
| npm install @flatfile/plugin-export-delimited-zip | ||
| ``` | ||
|
|
||
| **listener.ts** | ||
| ```typescript | ||
| import type { FlatfileListener } from '@flatfile/listener' | ||
| import { exportDelimitedZip } from '@flatfile/plugin-export-delimited-zip' | ||
|
|
||
| export default function (listener: FlatfileListener) { | ||
| listener.use(exportDelimitedZip({ | ||
| job: 'export-delimited-zip', | ||
| delimiter: ',', | ||
| fileExtension: 'csv', | ||
| })) | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module.exports = { | ||
| testEnvironment: 'node', | ||
|
|
||
| transform: { | ||
| '^.+\\.tsx?$': 'ts-jest', | ||
| }, | ||
| setupFiles: ['../../test/dotenv-config.js'], | ||
| setupFilesAfterEnv: [ | ||
| '../../test/betterConsoleLog.js', | ||
| '../../test/unit.cleanup.js', | ||
| ], | ||
| testTimeout: 60_000, | ||
| globalSetup: '../../test/setup-global.js', | ||
| forceExit: true, | ||
| passWithNoTests: true, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| { | ||
| "name": "@flatfile/plugin-export-delimited-zip", | ||
| "version": "0.0.0", | ||
| "url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/export/delimited-zip", | ||
| "description": "A Flatfile plugin for exporting Workbooks to delimited files and zipping them together", | ||
| "registryMetadata": { | ||
| "category": "export" | ||
| }, | ||
| "type": "module", | ||
| "engines": { | ||
| "node": ">= 16" | ||
| }, | ||
| "browserslist": [ | ||
| "> 0.5%", | ||
| "last 2 versions", | ||
| "not dead" | ||
| ], | ||
| "exports": { | ||
| ".": { | ||
| "node": { | ||
| "types": { | ||
| "import": "./dist/index.d.ts", | ||
| "require": "./dist/index.d.cts" | ||
| }, | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| }, | ||
| "browser": { | ||
| "types": { | ||
| "import": "./dist/index.d.ts", | ||
| "require": "./dist/index.d.cts" | ||
| }, | ||
| "import": "./dist/index.browser.js", | ||
| "require": "./dist/index.browser.cjs" | ||
| }, | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "main": "./dist/index.cjs", | ||
| "module": "./dist/index.js", | ||
| "source": "./src/index.ts", | ||
| "types": "./dist/index.d.ts", | ||
| "files": [ | ||
| "dist/**" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsup", | ||
| "build:watch": "tsup --watch", | ||
| "build:prod": "NODE_ENV=production tsup", | ||
| "checks": "tsc --noEmit && attw --pack . && publint .", | ||
| "lint": "tsc --noEmit", | ||
| "test": "jest src/*.spec.ts --detectOpenHandles", | ||
| "test:unit": "jest src/*.spec.ts --testPathIgnorePatterns=.*\\.e2e\\.spec\\.ts$ --detectOpenHandles", | ||
| "test:e2e": "jest src/*.e2e.spec.ts --detectOpenHandles" | ||
| }, | ||
| "keywords": [ | ||
| "flatfile-plugins", | ||
| "category-export" | ||
| ], | ||
| "author": "Flatfile, Inc.", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/FlatFilers/flatfile-plugins.git", | ||
| "directory": "export/delimited-zip" | ||
| }, | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@flatfile/plugin-job-handler": "^0.7.0", | ||
| "@flatfile/util-common": "^1.5.0", | ||
| "adm-zip": "^0.5.16", | ||
| "csv-stringify": "^6.5.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "@flatfile/api": "^1.9.19", | ||
| "@flatfile/listener": "^1.1.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@flatfile/bundler-config-tsup": "^0.1.0" | ||
| } | ||
| } |
199 changes: 199 additions & 0 deletions
199
export/delimited-zip/src/export.delimited.zip.plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { FlatfileClient } from '@flatfile/api' | ||
| import { jobHandler } from '@flatfile/plugin-job-handler' | ||
| import { processRecords } from '@flatfile/util-common' | ||
| import AdmZip from 'adm-zip' | ||
| import { stringify } from 'csv-stringify/sync' | ||
| import fs from 'fs' | ||
| import { tmpdir } from 'os' | ||
| import path from 'path' | ||
|
|
||
| const api = new FlatfileClient() | ||
|
|
||
| export interface PluginOptions { | ||
| job: string | ||
| delimiter: string | ||
| fileExtension: string | ||
| debug?: boolean | ||
| } | ||
|
|
||
| export function exportDelimitedZip(options: PluginOptions) { | ||
| return jobHandler(`workbook:${options.job}`, async (event, tick) => { | ||
| const { workbookId, spaceId, environmentId } = event.context | ||
|
|
||
| try { | ||
| const { data: workbook } = await api.workbooks.get(workbookId) | ||
|
|
||
| await tick(1, `Starting ${workbook.name} export`) | ||
|
|
||
| const { data: sheets } = await api.sheets.list({ workbookId }) | ||
| if (options.debug) { | ||
| console.log('Sheets retrieved:', sheets) | ||
| } | ||
|
|
||
| // Get current date-time | ||
| const dateTime = new Date().toISOString().replace(/[:.]/g, '-') | ||
|
|
||
| // Get the path to the system's temporary directory | ||
| const tempDir = tmpdir() | ||
|
|
||
| const sanitizeFileName = (name: string) => | ||
| path.basename(name.replace(/[<>:"/\\|?*]/g, '').replace(/\s+/g, '_')) | ||
| const sanitizedWorkbookName = sanitizeFileName(workbook.name) | ||
|
|
||
| // Create a new directory in the system's temporary directory for the delimited files | ||
| const dir = path.join(tempDir, `${sanitizedWorkbookName}_${dateTime}`) | ||
| if (!fs.existsSync(dir)) { | ||
|
carlbrugger marked this conversation as resolved.
|
||
| fs.mkdirSync(dir) | ||
|
carlbrugger marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (options.debug) { | ||
| console.log(`Creating zip file in ${dir}`) | ||
| } | ||
|
|
||
| const zipFile = new AdmZip() | ||
|
|
||
| // For each sheet, create a delimited file and populate it with data | ||
| const totalSheets = sheets.length | ||
| for (const [index, sheet] of sheets.entries()) { | ||
| // Limit sheet name to 31 characters | ||
| const trimmedSheetName = sanitizeFileName(sheet.name).substring(0, 31) | ||
|
|
||
| if (options.debug) { | ||
| console.log(`Processing sheet ${trimmedSheetName}`) | ||
| } | ||
|
|
||
| const { | ||
| data: { | ||
| config: { fields }, | ||
| }, | ||
| } = await api.sheets.get(sheet.id) | ||
|
|
||
| const header = fields.map((field) => field.key) | ||
| const headerLabels = fields.map((field) => field.label) | ||
|
|
||
| const fileName = `${trimmedSheetName}.${options.fileExtension}` | ||
| const filePath = `${dir}/${fileName}` | ||
|
|
||
| // Write header only once before processing records | ||
| const headerContent = stringify([headerLabels], { | ||
| delimiter: options.delimiter, | ||
| }) | ||
| fs.writeFileSync(filePath, headerContent) | ||
|
carlbrugger marked this conversation as resolved.
carlbrugger marked this conversation as resolved.
|
||
|
|
||
| if (options.debug) { | ||
| console.log(`Writing header to ${filePath}`) | ||
| } | ||
|
|
||
| await processRecords( | ||
| sheet.id, | ||
| async (records, pageNumber, totalPageCount) => { | ||
| const rows = records.map((record) => | ||
| header.map((key) => record.values[key].value) | ||
| ) | ||
|
carlbrugger marked this conversation as resolved.
|
||
|
|
||
| const csvContent = stringify(rows, { | ||
| delimiter: options.delimiter, | ||
| }) | ||
|
|
||
| // Append the new records to the existing file | ||
| fs.appendFileSync(filePath, csvContent) | ||
|
carlbrugger marked this conversation as resolved.
|
||
|
|
||
| if (options.debug) { | ||
| console.log( | ||
| `Writing ${records.length} records to ${filePath} (page ${pageNumber} of ${totalPageCount})` | ||
| ) | ||
| } | ||
|
|
||
| // Calculate progress percentage | ||
| const sheetProgress = (pageNumber + 1) / totalPageCount | ||
| const sheetWeight = 1 / totalSheets | ||
| const progress = Math.round( | ||
| (index / totalSheets + sheetProgress * sheetWeight) * 80 + 10 | ||
| ) | ||
|
|
||
| // Acknowledge job progress | ||
| await tick( | ||
| progress, | ||
| `Processed page ${pageNumber + 1} of ${trimmedSheetName}.${options.fileExtension}` | ||
| ) | ||
| }, | ||
| { | ||
| pageSize: 5, | ||
| } | ||
| ) | ||
|
|
||
| zipFile.addFile(fileName, fs.readFileSync(filePath)) | ||
|
carlbrugger marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (options.debug) { | ||
| console.log( | ||
| `Data written to ${options.fileExtension.toUpperCase()} files` | ||
| ) | ||
| } | ||
|
|
||
| const zipFileName = `${sanitizedWorkbookName}_${dateTime}.zip` | ||
| const zipFilePath = path.join(tempDir, zipFileName) | ||
|
|
||
| zipFile.writeZip(zipFilePath) | ||
|
|
||
| if (options.debug) { | ||
| console.log(`Zipped file: ${zipFilePath}`) | ||
| } | ||
|
|
||
| const { data: file } = await api.files.upload( | ||
| fs.createReadStream(zipFilePath), | ||
| { | ||
| spaceId, | ||
| environmentId, | ||
| mode: 'export', | ||
| } | ||
| ) | ||
|
|
||
| // Cleanup temporary files | ||
| for (const sheet of workbook.sheets) { | ||
| const trimmedSheetName = sanitizeFileName(sheet.name).substring(0, 31) | ||
| const fileName = `${trimmedSheetName}.${options.fileExtension}` | ||
| const filePath = path.join(dir, fileName) | ||
|
|
||
| try { | ||
| await fs.promises.unlink(filePath) | ||
| if (options.debug) { | ||
| console.log(`Deleted temporary file: ${filePath}`) | ||
| } | ||
| } catch (error) { | ||
| console.warn(`Failed to delete temporary file: ${filePath}`, error) | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| await fs.promises.unlink(zipFilePath) | ||
| if (options.debug) { | ||
| console.log(`Deleted temporary ZIP file: ${zipFilePath}`) | ||
| } | ||
| } catch (error) { | ||
| console.warn( | ||
| `Failed to delete temporary ZIP file: ${zipFilePath}`, | ||
| error | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| outcome: { | ||
| message: `Data was exported to ${zipFileName}.`, | ||
| next: { | ||
| type: 'files', | ||
| files: [{ fileId: file.id }], | ||
| }, | ||
| }, | ||
| } | ||
| } catch (error) { | ||
| if (options.debug) { | ||
| console.error(error) | ||
| } | ||
|
|
||
| throw new Error( | ||
| `This job failed probably because it couldn't write to the ${options.fileExtension.toUpperCase()} files, compress them into a ZIP file, or upload it.` | ||
| ) | ||
|
carlbrugger marked this conversation as resolved.
|
||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './export.delimited.zip.plugin' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.