0.3.0 • Published 4 months ago

@autotelic/fastify-stream-to-csv v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

fastify-stream-to-csv

Stream CSVs from Fastify routes. Uses fast-csv for formatting.

Installation

npm i -S @autotelic/fastify-stream-to-csv

Usage

const { fastifyStreamToCsv } = require('@autotelic/fastify-stream-to-csv')

const fastify = Fastify()

fastify.register(fastifyStreamToCsv)

fastify.get('/report', async function (req, reply) {
  // create a readable stream
  const readStream = Readable.from(Array.from(Array(100000).keys()))

  // create a row formatter
  const rowFormatter = num => {
    return [`a${num}`, `b${num}`, `c${num}`]
  }

  // these are fast-csv format options
  const csvOptions = {
    delimiter: '\t',
    headers: ['a', 'b', 'c']
  }

  // use reply decorator
  await reply
    .header('Content-disposition', 'attachment; filename=basic-example.csv')
    .streamToCsv(readStream, rowFormatter, { csvOptions })

  return reply
}

Github Actions/Workflows

  • Trigger the release workflow via release tag

    git checkout main && git pull
    npm version { minor | major | path }
    git push --follow-tags