npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-csv-streamer

v0.1.2

Published

Stream large datasets and export to CSV - streaming, memory-efficient exporter

Readme

node-csv-streamer

Stream large datasets efficiently and export to CSV — framework-agnostic and NestJS-friendly.

🚀 Features

  • ✅ Stream data in chunks (pagination-friendly)
  • 🧠 Memory-efficient (uses Node.js streams)
  • 🔄 Supports sync or async data transformation
  • 🧩 Flexible field mapping (supports concatenation like firstName-lastName)
  • 🛠️ Works seamlessly with NestJS, Express, or pure Node.js

Install

npm install node-csv-streamer

🧠 Concept

node-csv-streamer is designed for large dataset exports where loading everything into memory isn’t feasible.

Instead of fetching all data at once, it streams your data in batches using a fetchFn(skip, limit, query) function.

This approach ensures low memory usage and smooth CSV generation — ideal for analytics dashboards, data exports, or reporting systems.

🧩 Fetch Function Pattern

Your data-fetching method must follow this pattern:

async function fetchFn(skip: number, limit: number, query: any): Promise<any[]> {
  // Fetch data from your source
  return await dataSource.find(query).skip(skip).limit(limit); //This is just an example
}

skip → starting point for batch retrieval

limit → batch size (default is 1000)

query → optional filtering parameters

Each batch is streamed and written directly to the CSV file.

Usage

When using node-csv-streamer, the assumption is you are working with a large data set or you want a memory efficient way to get and send data as csv to a writable stream.

To achieve this, your data fetching method must be setup to get data in batches. You can set the batch use the existing 1000.

Your data fetching method should be define in this pattern: fetchFn(skip,limit,query)

The skip is currently used to define how where to start from in fetching data, the limit is the batchSize(1000) to retrieve at each call to your source data location and query is the criteria for selecting the data.

This fetching pattern is used over using a method to gets all the data you need into memory.

Example : Express Controller

import { NodeCsvStream } from "node-csv-streamer";
import express from "express";

const app = express();

app.get("/download-csv", async (req, res) => {
  // Set response headers for CSV download
  res.setHeader("Content-Type", "text/csv");
  res.setHeader(
    "Content-Disposition",
    `attachment; filename="example-csv-file.csv"`
  );

  // Define mapping between CSV headers and your data source fields
  // You can combine multiple fields using a hyphen ("-")
  const csvHeaderMapping = {
    Email: "email",
    Name: "firstName-lastName",
    "Phone Number": "phoneNumber",
  };

  // Stream CSV data directly to the HTTP response
  await NodeCsvStream.download(
    res,
    csvHeaderMapping,
    aggregateEmployeeRecords, // or this.aggregateEmployeeRecords.bind(this)
    {}, // optional query parameters
    undefined, // optional formatting function
    100 // batch size
  );
});

⚙️ API Reference

NodeCsvStream.download(res, fileMapping, fetchFn, query?, docsFormattingFn?, batchSize?)

| Parameter | Type | Description | | --------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | res | Writable | Writable stream (e.g., Express or NestJS Response object). Typically the HTTP response where the CSV will be streamed directly. | | fileMapping | Record<string, string> | Defines the mapping between CSV column headers and data source keys. Supports concatenation with a hyphen (e.g., "firstName-lastName"). | | fetchFn | (skip: number, limit: number, query: any) => Promise<any[]> | Function responsible for fetching data in batches. It is called repeatedly until no more records are returned. | | query (optional) | any | Query object passed to fetchFn for data filtering or scoping. | | docsFormattingFn (optional) | (doc: any, mapping: Record<string, string>) => any | Optional transformation function to modify each record before converting to CSV. | | batchSize (optional) | number | Number of records to fetch per batch. Defaults to 1000. |


🧩 Example Usage

await NodeCsvStream.download(
  res,
  {
    Email: "email",
    Name: "firstName-lastName",
    "Phone Number": "phoneNumber",
  },
  aggregateEmployeeRecords, // async function (skip, limit, query)
  { active: true }, // optional query
  undefined, // optional transform function
  500 // optional batch size
);

🪶 License

MIT © 2025 — Maintained by Adeleke Bright