Csvbuilder
Create csv formated streams from Arrays of Objects. CsvBuilder is one of many Csv stream/generator implementations
on npm. The goal of CsvBuilder is to create Schema's for csv output and let the consumer spawn as many streams
as needed from a single instance to maintain a specific format. This means the user gets control of the headers, the
order of the headers, how the headers correspond to consumed objects, virtual properties, value delimiters, and line
terminators.
Getting Started
var CsvBuilder = require('csv-builder');
var usersBuilder = new CsvBuilder({
headers: 'Firstname Lastname Email',
constraints: {
'Email': 'email'
'Lastname': 'lastname'
}
})
.virtual('Firstname', function(obj) {
return obj.split(' ')[0];
})
.virtual('lastname', function(obj) {
return obj.split(' ')[1];
});
Model.find().stream()
.pipe(usersBuilder.createTransformStream())
.pipe(fs.createWriteStream('output.csv'));
usersBuilder.createReadStream(payload)
.pipe(fs.createWriteStream('output.csv'));
Installation
$ npm install csv-builder
Usage
CsvBuilder([options])
- delimiter String The value delimiter. Default ','
- terminator String The line terminator. Default '\n'
- mutate Boolean Mutate incoming objects when creating virtuals. Default true
- headers String|Array Space separated headers, or array of headers
- constraints Object {"header": "prop"}
Methods
- headers String|Array Space separated headers, or array of headers
Set single or multiple contraints. If header
is an object, it will extend any existing constraints, not replace.
- header String|Object Either object {"header": "property"} Or a string "Header"
- prop String Property to correspond to header, omit if using object.
CsvBuilder#virtual(prop, fn)
Create a virtual property. Virtual properties are treated the same as normal
properties, so if no header matches the virtual property name, or no constraint is
set the virtual property will be omitted.
- prop String Virtual property name
- fn Function Returns virtual value, takes the object to be created/mutated as the only argument.
CsvBuilder#createReadStream(payload)
Create's a readable stream and consumes the payload.