express-json2csv
PLEASE NOTE THAT THIS IS A WORK IN PROGRESS MODULE. EXPECT SYNTAX CHANGES FREQUENTLY WHILE I WORK THROUGH EDGE CASES AND REAL WORLD USES.
This is currently being tested in an application that has to create 500,000+ row csv files.
Installation
$ npm install express-json2csv
Adding to Express
var express = ;var json2csv = ; var app = ; app;
Syntax
res;
Filename
This can be any string you wish. The .csv
extension is automatically appended to the filename so it is not required.
Data
Type: Array
Data is a required argument. The columns prop property will reference the keys in each of the data objects you pass in to define the order.
Columns
Type: Array
Columns is a required argument and will define the order / rendering of your CSV file. You must provide at least the label and prop attribute for each column. There is also a optional render attribute you can pass if you want to mutate your data before rendering it to the CSV.
var columns = prop: 'column1' label: 'First Column' { return data + ' rendered content!'; } prop: 'column2' label: 'Second Column';
Options
Option | Type | Default | Description |
---|---|---|---|
includeHeader | Boolean | true | Include header columns |
includeFooter | Boolean | false | Include footer columns |
Examples
var express = ;var json2csv = ; var app = ; app; // mimicking a mongodb esque datasetvar data = _id: '5422d576df3d571846f7927a' name: 'John Doe' occupation: 'Doctor' age: '28' status: 'Single' _id: '5422d576df3d571846f7927b' name: 'Mary Poppins' occupation: 'Nurse' age: '32' status: 'Divorced'; // define the order of the columns and their labels herevar columns = prop: 'name' label: 'Name' { return data + ', US citizen'; } prop: 'occupation' label: 'Occupation' prop: 'age' label: 'Age' prop: 'status' label: 'Current Status' app;
Todo
- Finish tests