Managing large datasets effectively in NestJS using fast-csv

Currently leveraging NestJS v9, fast-csv v4, and BigQuery for my project.

  1. Controller (CSV Upload):

@Post('upload')
@ApiOperation({ description: 'Upload CSV File' })
@ApiConsumes('multipart/form-data')
...
// Code shortened for brevity
  return await this.filesService.uploadCsv(file, uploadCsvDto);
}

  1. Service Logic:

async uploadCsv(
...
// Code shortened for brevity
  console.log(`Inserted ${rows.length} rows`);
  ...
}

// More service functions not shown here
...

export function memoryUsage(): void {
 return console.log(
  `APP is using ${
    Math.round((process.memoryUsage().rss / 1024 / 1024) * 100) / 100
  } MB of memory.`,
 );
}

  1. Explanation of Service Process:
  • Initial check in BigQuery for duplicates from previous CSV uploads.
  • Utilization of stream (fast-csv) to avoid loading all CSV data into memory at once.
  • ... // Details on processing every 200 rows and saving batches into BigQuery ...

Issue: Encounter memory problems when handling larger files above 50,000 rows:

50,000 row CSV example: https://i.sstatic.net/jkaL1.png

250,000 row CSV example: https://i.sstatic.net/PMcUg.png

Unable to identify why the memory issues persist even after clearing unnecessary variables.

Current node server indicates a maximum memory size of 512MB.

Memory Checking Function Used:

export function memoryUsage(): void {
 return console.log(
  `APP is using ${
    Math.round((process.memoryUsage().rss / 1024 / 1024) * 100) / 100
  } MB of memory.`,
 );
}

Answer №1

If you're facing difficulties with this issue, I have a solution. Simply break down the buffer from the CSV file into smaller chunks.

Here is an example:

const ROWS_BATCH = 200;
const blockSize = 100 * 100 * 100; // 0.9537 MB.
const csvHeaders: string[] = [];
let offset = 0;

// 1. Chunk buffer, 2. Chunk rows, 3. Chunk insertions into BigQuery
while (offset < buffer.length) {
  const remainingBytes = buffer.length - offset;
  const currentBlockSize = Math.min(blockSize, remainingBytes);
  const chunk = buffer.slice(offset, offset + currentBlockSize);

  const streamFromBuffer = Readable.from(chunk);
  // rest of code ...
}

This way, you don't need to load the entire buffer into memory at once. Instead, you can load it in manageable chunks.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for effectively highlighting search text within HTML content without causing any issues

I am in search of a solution that can assist me in searching for specific terms within an HTML string while also highlighting them. I have the ability to remove the HTML content from the string, but this poses the problem of losing the context of the origi ...

An issue occurred while trying to retrieve the js file, as an error with code net::ERR_ABORTED 404 (Not

Although this question has been asked before, I am unsure where to find the solution. My express backend server needs to render an HTML page with main.js in it upon launch. app.js code: var createError = require('http-errors'); var express = req ...

Using Vue with Express (specifically NestJS) may seem like a daunting challenge, especially if you are working with the runtime-only build

I am facing difficulties in integrating Vue as a view system with a framework called Nest using Express. I underestimated the complexity of adapting Vue. I am seeking guidance to ensure I am on the right track and avoid using Vue directly. Firstly, the e ...

Unexpected behavior encountered when using onClick in a material-ui List component containing Buttons

Looking to implement a customized list using React and Material-UI, where each item features a Checkbox, a text label, and a button. The onClick event for the Checkbox should be managed by a separate function from the onClick event for the Button. Encount ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

Using a variable in a Joomla module to create a JavaScript object with PHP

I am currently developing a Joomla module that features a progress bar utilizing the ProgressBar.js plugin. Since this module is designed to load multiple objects on a single page, hardcoding the IDs of these objects is not feasible. To address this, I uti ...

Guide to assigning an integer value to a string in Angular

Is there a way to change integer values in a table to actual names and display them on a webpage by setting a scope in the controller and passing that string to the HTML? For example, this is an example of the HTML code for the table row: <thead> ...

Recover Checkmark Status from Data

Currently, I am storing form data in json format and encountering an issue when trying to load values from a previously created json file. The plain old JavaScript function provided below successfully loads values into a form from a file, effectively resto ...

The Safari 7.1+ browser seems to be having trouble with the spotlight effect

Why is the CodePen not functioning properly in Safari? Despite working well in Chrome and Firefox, it completely fails to work in Safari 7.1+. Can anyone provide some insights? http://codepen.io/cchambers/pen/Dyldj $(document).on("mousemove", function( ...

rxjs iterates through an array executing each item in sequential order

Is there a way to make observables wait until the previous one has completed when they are created from an array? Any help is appreciated! export class AppComponent{ arr: number[] = [5, 4, 1, 2, 3]; fetchWithObs() { from(this.arr) ...

Using Knex.js to perform a case-insensitive search on an object with the whereIL

Still searching for a solution to this problem. I am attempting to pass an object with filters as keys and values. ex. const filters = { 'id': 12, 'first_name': john } function findBy(filter) { return db('quotes') ...

Clarification on the syntax for using SWR with Next.js

While following a tutorial, I stumbled upon this code snippet: import useSWR from "swr" import { fetcher } from "./fetcher" export function useFeed() { const { data: feed } = useSWR("/api/feed", fetcher) return { feed } } ...

Node.js encountering an error caused by a lengthy SQL procedure execution, triggered by a POST request initiated from

Currently, my website utilizes Angular, NodeJs, and a SQL database. Most of the site's calls are made from the frontend to the backend and everything runs smoothly. However, I encountered an issue when running a stored procedure that had a longer proc ...

Client-Side Isomorphic JS: A Guide to Using HTTP Requests

Seeking advice on data population for isomorphic flux apps. (Using react, alt, iso, and node but principles are transferable) In my flux 'store' (), I need to fetch data from an api: getState() { return { data : makeHttpRequest(url) ...

Display a hidden form field in Rails depending on the object's value

As a programmer learning Ruby on Rails without much knowledge of Javascript, I faced a problem with a form that creates an object called Unit. This Unit model is related to Category which in turn is related to Product. The issue was that while selecting a ...

Is it possible to create a mouse-following effect with lighting using Javascript

Recently, I've been honing my Javascript skills and decided to create a follow-mouse function. After successfully implementing it, I started brainstorming a new concept that I'm unsure is achievable. Is there a way to develop an "orb of vision" ...

Choose between creating an observable pipe within a function or storing it in a variable

Currently, I have a functional code snippet that leverages the Angular service to create an Observable pipeline. This pipeline utilizes operators like mergeMap, filter, map, and shareReplay(1) to manage user authentication and fetch the onboarding status f ...

Unleashing the power of async/await in React: A comprehensive guide

Looking to simplify my code, I am interested in implementing async and await for the following code snippet. However, I am unsure of how to proceed. Any examples would be greatly appreciated. deletePost = (post) => { var post_id = post; try { ...

It appears that Yarn is having trouble properly retrieving all the necessary files for a package

Recently, I encountered a strange issue in our project involving 3 microservices, all using the exceljs library. Two of the microservices successfully download all necessary files for this package through yarn. However, the third microservice is missing ...

Issue: The content of the text does not align with the HTML generated by the server

I'm struggling with an algorithm in next.js and encountering hydration errors. Here is the code I am using: import numbers from "../../functions/numberGenerators.js" export default function test() { ...