feat: implement stream based processing of the files (#12574)

Fixes: FRMW-2960

This PR adds support for processing large CSV files by breaking them into chunks and processing one chunk at a time. This is how it works in nutshell.

- The CSV file is read as a stream and each chunk of the stream is one CSV row.
- We read upto 1000 rows (plus a few more to ensure product variants of a product are not split into multiple chunks).
- Each chunk is then normalized using the `CSVNormalizer` and validated using zod schemas. If there is an error, the entire process will be aborted and the existing chunks will be deleted.
- Each chunk is written to a JSON file, so that we can process them later (after user confirms) without re-processing or validating the CSV file.
- The confirmation process will start consuming one chunk at a time and create/update products using the `batchProducts` workflow.

## Resume or not to resume processing of chunks

Let's imagine during processing of chunks, we find that chunk 3 leads to a database error. However, till this time we have processed the first two chunks already. How do we deal with this situation? Options are:

- We store at which chunk we failed and then during the re-upload we ignore chunks before the failed one. In my conversation with @olivermrbl we discovered that resuming will have to work with certain assumptions if we decide to implement it.
   - What if a user updates the CSV rows which are part of the already processed chunks? These changes will be ignored and they will never notice it.
   - Resuming works if the file name is still the same. What if they made changes and saved the file with "Save as - New name". In that case we will anyways process the entire file.
   - We will have to fetch the old workflow from the workflow engine using some `ilike` search, so that we can see at which chunk the last run failed for the given file.

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Harminder Virk
2025-05-29 05:42:16 +00:00
committed by GitHub
co-authored by Carlos R. L. Rodrigues
parent 40e73c6ea2
commit cf0297f74a
12 changed files with 360 additions and 141 deletions
+18
View File
@@ -6086,6 +6086,7 @@ __metadata:
"@swc/core": ^1.7.28
"@swc/jest": ^0.2.36
awilix: ^8.0.1
csv-parse: ^5.6.0
expect-type: ^0.20.0
jest: ^29.7.0
json-2-csv: ^5.5.4
@@ -6343,6 +6344,7 @@ __metadata:
typescript: ^5.6.2
vite: ^5.4.14
zod: 3.22.4
zod-validation-error: ^3.4.1
peerDependencies:
"@aws-sdk/client-dynamodb": ^3.218.0
"@medusajs/cli": 2.8.3
@@ -19320,6 +19322,13 @@ __metadata:
languageName: node
linkType: hard
"csv-parse@npm:^5.6.0":
version: 5.6.0
resolution: "csv-parse@npm:5.6.0"
checksum: 52f5e6c45359902e0c8e57fc2eeed41366dc6b6d283b495b538dd50c8e8510413d6f924096ea056319cbbb8ed26e111c3a3485d7985c021bcf5abaa9e92425c7
languageName: node
linkType: hard
"csv-stringify@npm:^5.6.5":
version: 5.6.5
resolution: "csv-stringify@npm:5.6.5"
@@ -35567,6 +35576,15 @@ __metadata:
languageName: node
linkType: hard
"zod-validation-error@npm:^3.4.1":
version: 3.4.1
resolution: "zod-validation-error@npm:3.4.1"
peerDependencies:
zod: ^3.24.4
checksum: cf16f12fccb3e515d18c876c8a75ae4a87219b28e8e7f6334b8d423bebfa2c08b3382d7c53842ba05af8c5caabf66ee8df1ce2862b3b41c2e96eba26e70a995f
languageName: node
linkType: hard
"zod@npm:3.22.4":
version: 3.22.4
resolution: "zod@npm:3.22.4"