2.0.5 • Published 10 months ago

@prairielearn/csv v2.0.5

Weekly downloads
-
License
-
Repository
github
Last release
10 months ago

@prairielearn/csv

A few helpful wrappers on top of the functionality from csv-stringify.

Usage

Here's an example taking data from @prairielearn/postgres#queryCursor() and writing it to a file, though this should be applicable to any source and destination streams:

import { stringifyStream } from '@prairielearn/csv';
import { queryCursor } from '@prairielearn/postgres';
import { pipeline } from 'node:stream/promises';
import { createWriteStream } from 'node:fs';

const cursor = await queryCursor('SELECT id FROM workspaces;', {});
const output = createWriteStream('workspaces.csv');

const stringifier = stringifyStream({
  header: true,
  columns: [{ key: 'id', header: 'ID' }],
  // Optionally provide a function to transform each item in the stream.
  transform(record) {
    return {
      id: `workspace-${id}`,
    };
  },
});

await pipeline(cursor.stream(100), stringifier, output);

Note that this works best when the source stream is producing data asynchronously, such as though an async iterator. If you use a synchronous data source like Readable.from([...]), the conversion will still occur synchronously. If you have a large array of data in memory and want to convert it to a CSV, you can use stringifyNonblocking:

import { stringifyNonblocking } from '@prairielearn/csv';
import { createWriteStream } from 'node:fs';

const data = Array.from(new Array(100_000), (_, i) => ({ id: i }));
const output = createWriteStream('numbers.csv');
stringifyNonblocking(data, {
  header: true,
  columns: [{ key: 'id', header: 'ID' }],
}).pipe(output);

For lower-level usage, stringify and Stringifier are also re-exported from csv-stringify:

import { stringify, Stringifier } from '@prairielearn/csv';
2.0.3

11 months ago

2.0.2

1 year ago

2.0.5

10 months ago

2.0.4

11 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

2 years ago

1.0.2

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago