-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-csv.js
55 lines (51 loc) · 1.52 KB
/
import-csv.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
"use strict";
const program = require("commander");
const csv = require("csv-parser");
const fs = require("fs");
const superagent = require("superagent");
const vision = require("@google-cloud/vision");
program
.version("1.0.0")
.option("-f, --file", "import csv file in mongo database")
.option("-g, --google", "pick up informations about picture");
program.parse(process.argv);
if (program.file) {
let stream = fs.createReadStream(program.args[0]);
let results = [];
stream
.pipe(csv({ separator: ";" }))
.on("data", data => results.push(data))
.on("end", () => {
results.forEach(product => {
superagent
.post("localhost:8080/product/create")
.send(product)
.end((error, res) => {
error
? console.log(`Error in create product ${product.title}`)
: console.log(`Product ${product.title} created`);
});
});
});
}
if (program.google) {
let stream = fs.createReadStream(program.args[0]);
let results = [];
stream
.pipe(csv({ separator: ";" }))
.on("data", data => results.push(data))
.on("end", () => {
results.forEach(product => {
superagent
.put("localhost:8080/product/dominantColor")
.send(product)
.end((error, res) => {
error
? console.log(`Error in create product ${product.title}`)
: console.log(
`Product dominant color for ${product.title} added`
);
});
});
});
}