/
index.js
63 lines (54 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Module Dependencies
*/
var CsvBuilder = require('csv-builder');
/**
* Create csv streams from a mongoose schema
* @param {mongoose.Schema} schema
* @param {Object} options CsvBuilder options
* @param {String|Array} options.headers Space separated headers, or array of headers
* @param {String} [options.delimiter = ','] Value delimiter for csv data
* @param {String} [options.terminator = '\n'] Line terminator for csv data
* @param {Object} options.constraints {"header": "prop"}
* @param {Object} options.virtuals Virtual properties.
*/
module.exports = function mongooseToCsv(schema, options) {
// need options.headers
if (!options.headers) throw new Error('MongooseToCsv requires the `headers` option');
var builder = new CsvBuilder(options);
if (options.virtuals) {
for (var v in options.virtuals) {
builder.virtual(v, options.virtuals[v]);
}
}
/**
* Static Method `csvReadStream`
* @param {Array<Documents>} docs Array of mongoose documents
* @return {Stream} Csv read stream.
*/
schema.static('csvReadStream', function(docs) {
if (!docs) {
throw new Error('[Model].csvReadStream requires an array of documents.');
}
var data = docs.map(function(obj) {
return obj._doc;
});
return builder.createReadStream(data);
});
/**
* Create a Csv stream from a query Object.
* @param {Object} query Mongoose query
* @return {Stream} Csv transform stream
*/
schema.static('findAndStreamCsv', function(query) {
query = query || {};
return this.find(query).stream().pipe(builder.createTransformStream());
});
/**
* Create a transform stream
* @return {Stream} transform stream
*/
schema.static('csvTransformStream', function() {
return builder.createTransformStream();
});
};