Skip to content

Commit a41ab3c

Browse files
Igor Drobiazkojhorbulyk
authored andcommitted
Config
1 parent 9589450 commit a41ab3c

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

component.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"write_attachment": {
4242
"main": "./lib/actions/write.js",
4343
"title": "Write CSV attachment",
44+
"fields": {
45+
"reader": {
46+
"viewClass": "CSVReadView"
47+
}
48+
},
4449
"metadata": {
4550
"in": {},
4651
"out": {}

lib/actions/write.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const request = require('request-promise');
22
const co = require('co');
33
const csv = require('csv');
4+
const _ = require('lodash');
45
const messages = require('elasticio-node').messages;
56
const client = require('elasticio-rest-node')();
67

@@ -12,12 +13,22 @@ let rowCount = 0;
1213
exports.init = init;
1314
exports.process = processAction;
1415

15-
function init() {
16+
function init(cfg) {
1617
return co(function* gen() {
1718

18-
stringifier = csv.stringify({
19-
header: true
20-
});
19+
const delimiter = cfg.reader.delimiter || ',';
20+
console.log('Using delimiter: \'%s\'', delimiter);
21+
const options = {
22+
header: true,
23+
delimiter
24+
};
25+
26+
if (cfg.reader.columns) {
27+
console.log('Configured columns:', cfg.reader.columns);
28+
options.columns = cfg.reader.columns;
29+
}
30+
31+
stringifier = csv.stringify(options);
2132

2233
signedUrl = yield client.resources.storage.createSignedUrl();
2334

@@ -28,7 +39,7 @@ function init() {
2839
});
2940
}
3041

31-
function processAction(msg) {
42+
function processAction(msg, cfg) {
3243
const self = this;
3344

3445
if (timeout) {
@@ -59,7 +70,9 @@ function processAction(msg) {
5970
});
6071
}, 10000);
6172

62-
stringifier.write(msg.body);
73+
const columns = cfg.reader.columns;
74+
const row = columns ? _.pick(msg.body, columns) : msg.body;
75+
stringifier.write(row);
6376
rowCount++;
6477
this.emit('end');
6578
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"elasticio-node": "0.0.7",
3232
"elasticio-sailor-nodejs": "2.1.3",
3333
"elasticio-rest-node": "1.0.2",
34+
"lodash": "^4.17.4",
3435
"moment": "^2.10.6",
3536
"node-uuid": "^1.4.3",
3637
"q": "^1.4.1",

0 commit comments

Comments
 (0)