Skip to content
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

feat: zip egress plugin #464

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat: zip egress plugin #464

wants to merge 3 commits into from

Conversation

carlbrugger
Copy link
Contributor

@carlbrugger carlbrugger commented Apr 5, 2024

Please explain how to summarize this PR for the Changelog:

This PR introduces a plugin for egressing a Flatfile Workbook as a collection of zipped CSV files.

Tell code reviewer how and what to test:

Create a workspace with an action workbook:downloadWorkbook, then used this listener to create a zip file containing csv files for each sheet in the workbook.

import type { FlatfileListener } from '@flatfile/listener'
import { zipEgressPlugin } from '@flatfile/plugin-zip-egress'

export default function (listener: FlatfileListener) {
  listener.use(zipEgressPlugin('workbook:downloadWorkbook'))
}

@carlbrugger carlbrugger force-pushed the feat/zip-egress-plugin branch 2 times, most recently from a6aabe7 to ab50ace Compare April 8, 2024 20:43
@carlbrugger carlbrugger marked this pull request as ready for review April 11, 2024 19:46
Copy link
Contributor

coderabbitai bot commented Apr 11, 2024

Walkthrough

The recent updates introduce the @flatfile/plugin-zip-egress plugin, which enables exporting a Flatfile Workbook as a set of CSV files within a ZIP archive. Enhancements to the autocast plugin improve type casting and error handling. The external.sheet.constraint plugin now supports custom constraint validators for multiple fields. Additionally, the RecordTranslator class has been refined to better handle record conversions and linked records.

Changes

File Path Change Summary
.plugins/zip-egress/README.md Added summary for the @flatfile/plugin-zip-egress plugin.
plugins/zip-egress/src/index.ts New file exporting all entities from zip.egress module.
plugins/zip-egress/src/utils.ts New file introducing castBoolean function.
plugins/zip-egress/src/zip.egress.ts Introduced zipEgressPlugin function and PluginOptions interface.
.changeset/dry-apples-teach.md New changeset file introducing the @flatfile/plugin-zip-egress plugin.
plugins/autocast/src/autocast.plugin.ts Enhanced documentation, added type-specific casting functions, and improved error handling.
plugins/constraints/src/external.sheet.constraint.ts Refined external sheet constraint registration, added new functions for schema and record processing.
plugins/record-hook/src/record.translator.ts Added detailed method descriptions and improved record conversion logic.

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f255924 and 1cd8dd8.
Files selected for processing (5)
  • plugins/autocast/src/autocast.plugin.ts (7 hunks)
  • plugins/constraints/src/external.sheet.constraint.ts (4 hunks)
  • plugins/record-hook/src/record.translator.ts (4 hunks)
  • plugins/zip-egress/src/utils.ts (1 hunks)
  • plugins/zip-egress/src/zip.egress.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • plugins/zip-egress/src/utils.ts
Additional Context Used
Biome (36)
plugins/autocast/src/autocast.plugin.ts (12)

48-69: Prefer for...of instead of forEach.


49-68: Prefer for...of instead of forEach.


57-57: Invalid typeof comparison value: this expression is not a string literal


97-101: This else clause can be omitted because previous branches break early.


99-101: This else clause can be omitted because previous branches break early.


115-123: This else clause can be omitted because previous branches break early.


143-154: This else clause can be omitted because previous branches break early.


117-117: isNaN is unsafe. It attempts a type coercion. Use Number.isNaN instead.


119-119: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.


168-168: This let declares a variable that is only assigned once.


168-168: isNaN is unsafe. It attempts a type coercion. Use Number.isNaN instead.


172-172: isNaN is unsafe. It attempts a type coercion. Use Number.isNaN instead.

plugins/constraints/src/external.sheet.constraint.ts (6)

32-32: Unexpected any. Specify a different type.


37-37: Unexpected any. Specify a different type.


56-74: Prefer for...of instead of forEach.


58-73: Prefer for...of instead of forEach.


69-71: Prefer for...of instead of forEach.


6-9: Some named imports are only used as types.

plugins/record-hook/src/record.translator.ts (10)

30-30: Unexpected any. Specify a different type.


34-71: This else clause can be omitted because previous branches break early.


84-84: Unexpected any. Specify a different type.


109-112: This else clause can be omitted because previous branches break early.


40-40: This let declares a variable that is only assigned once.


41-41: This let declares some variables that are only assigned once.


45-45: This let declares a variable that is only assigned once.


46-46: This let declares some variables that are only assigned once.


84-84: This let declares a variable that is only assigned once.


85-85: This let declares some variables that are only assigned once.

plugins/zip-egress/src/zip.egress.ts (8)

9-9: A Node.js builtin module should be imported with the node: protocol.


10-10: A Node.js builtin module should be imported with the node: protocol.


39-42: Prefer for...of instead of forEach.


40-40: Avoid redundant double-negation.


94-94: Do not use template literals if interpolation and special-character handling are not needed.


8-8: Some named imports are only used as types.


65-65: Avoid the use of spread (...) syntax on accumulators.


null-null: Import statements could be sorted:

Additional comments not posted (4)
plugins/constraints/src/external.sheet.constraint.ts (1)

79-85: This function implementation is clear and efficient.

plugins/record-hook/src/record.translator.ts (1)

17-20: The constructor is correctly implemented and straightforward.

plugins/autocast/src/autocast.plugin.ts (2)

86-92: The castString function is correctly implemented and handles various cases efficiently.


132-138: The castBoolean function is correctly implemented and handles various cases efficiently.


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

@carlbrugger carlbrugger force-pushed the feat/zip-egress-plugin branch 2 times, most recently from 6792112 to f2cbace Compare April 17, 2024 16:28
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

plugins/zip-egress/README.md Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

plugins/zip-egress/README.md Show resolved Hide resolved
plugins/zip-egress/README.md Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

plugins/zip-egress/README.md Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Out of diff range and nitpick comments (1)
.changeset/dry-apples-teach.md (1)

5-5: Please check the spelling of "egressing" in this context. If it is intended to mean "exporting" or "outputting," it might be clearer to use one of those terms to avoid confusion.

plugins/zip-egress/src/zip.egress.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Outside diff range and nitpick comments (6)
plugins/constraints/src/external.sheet.constraint.ts (2)

Line range hint 32-32: Replace any with more specific types to improve type safety.

-  ) => any
+  ) => void | Promise<void>

Also applies to: 37-37


Line range hint 56-74: Refactor forEach loops to for...of for better performance and readability.

- constraints.forEach((constraint) => {
+ for (const constraint of constraints) {
-   records.forEach((record) => {
+   for (const record of records) {
-     fields.forEach((key) => {
+     for (const key of fields) {

Also applies to: 58-73, 69-71

plugins/record-hook/src/record.translator.ts (1)

Line range hint 34-71: Remove unnecessary else clause after return statement for cleaner code.

- } else {
+ }
plugins/autocast/src/autocast.plugin.ts (3)

Line range hint 48-69: Refactor forEach loops to for...of for better performance and readability.

- records.forEach((record) => {
+ for (const record of records) {
-   castableFields.forEach((field) => {
+   for (const field of castableFields) {

Also applies to: 49-68


Line range hint 117-117: Use Number.isNaN and Number.isFinite instead of isNaN and isFinite for type safety.

- if (!isNaN(Number(strippedValue))) {
+ if (!Number.isNaN(Number(strippedValue))) {
-   if (isFinite(num)) {
+   if (Number.isFinite(num)) {

Also applies to: 119-119


Line range hint 168-168: Use Number.isNaN instead of isNaN for type safety.

- if (!isNaN(numericTimestamp)) {
+ if (!Number.isNaN(numericTimestamp)) {
- if (!isNaN(date.getTime())) {
+ if (!Number.isNaN(date.getTime())) {

Also applies to: 172-172

const constraints = getSheetConstraints(schema, validator)

constraints.forEach((constraint) => {
const fields = constraint.fields || []
records.forEach((record) => {
try {
// Execute the callback function for each record
cb(partialObject(record, fields), constraint.fields, {
config: constraint.config,
record,
properties: partialProperties(schema, fields),
event,
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize the use of spread syntax in the accumulator to enhance performance.

- (acc, { key }) => ({ ...acc, [key]: '' }),
+ (acc, { key }) => { acc[key] = ''; return acc; },

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
})
})
.reduce((acc, { key }) => { acc[key] = ''; return acc; }, {})

Comment on lines +109 to 112
// If the records are already Flatfile.RecordsWithLinks, return them directly
return this.records as Flatfile.RecordsWithLinks
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary else clause after return statement for cleaner code.

- } else {
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// If the records are already Flatfile.RecordsWithLinks, return them directly
return this.records as Flatfile.RecordsWithLinks
}
}
// If the records are already Flatfile.RecordsWithLinks, return them directly
return this.records as Flatfile.RecordsWithLinks
}

// Prepare the columns for CSV export, excluding fields marked as excludeFromExport
fields.forEach((field) => {
if (!!field.metadata?.excludeFromExport) return
columns.push({ key: field.key, header: field.label })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary double negation for clarity.

- if (!!field.metadata?.excludeFromExport) return
+ if (field.metadata?.excludeFromExport) return

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
columns.push({ key: field.key, header: field.label })
if (field.metadata?.excludeFromExport) return
columns.push({ key: field.key, header: field.label })

import AdmZip from 'adm-zip'
import { ColumnOption, stringify } from 'csv-stringify/sync'
import fs from 'fs'
import path from 'path'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the node: protocol when importing Node.js built-in modules.

- import fs from 'fs'
- import path from 'path'
+ import fs from 'node:fs'
+ import path from 'node:path'

Also applies to: 10-10

Committable suggestion was skipped due low confidence.

Comment on lines +39 to +42
if (!!field.metadata?.excludeFromExport) return
columns.push({ key: field.key, header: field.label })
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor forEach loop to for...of for better performance and readability.

- fields.forEach((field) => {
+ for (const field of fields) {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (!!field.metadata?.excludeFromExport) return
columns.push({ key: field.key, header: field.label })
})
for (const field of fields) {
if (!!field.metadata?.excludeFromExport) return
columns.push({ key: field.key, header: field.label })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant