-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
79 lines (71 loc) · 2.75 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const faker = require('faker');
const fs = require('fs');
const fastcsv = require("fast-csv");
const db = require('./db');
const contains = require('validator/lib/contains');
const args = require('minimist')(process.argv.slice(2))
// The path to write the csv file to
const output = './src/output.csv';
// Create a stream to write to the csv file
const stream = fs.createWriteStream(output)
// Create some fake data using the faker lib. Returns a template string to be inserted into a csv file as a single line
function createTranslation() {
const key = faker.address.country();
const lang = faker.address.countryCode();
const content = faker.random.word();
return `${key},${lang},${content}\n`;
}
async function writeToCsvFile() {
// The user can specify how many rows they want to create (yarn seed --rows=20), if they dont specify anything then defaults to 10
let rows = args['rows'] || 10;
// Iterate x number of times and write a new line to the csv file using the createTranslation function
for (let index = 0; index < rows; index++) {
stream.write(createTranslation(), 'utf-8')
}
stream.end();
}
function insertFromCsv() {
let csvData = [];
return fastcsv
.parse()
// validate that the column key doesn't contain any commas, as some countries do. This will break our insertion as it would be treated as an extra column and our table expects only 3 columns
.validate((data) => !contains(data[0], ','))
// triggered when a new record is parsed, we then add it to the data array
.on("data", (data) => {
csvData.push(data);
})
.on('data-invalid', (row, rowNumber) =>
console.log(`Invalid [rowNumber=${rowNumber}] [row=${JSON.stringify(row)}]`),
)
// once parsing is finished and all the data is added to the array we can then insert it into the db table
.on("end", () => {
// The insert statement
const query = "INSERT INTO translations (key, lang, content) VALUES ($1, $2, $3)";
// Connect to the db instance
db.connect((err, client, done) => {
if (err) throw err;
try {
// loop over the lines stored in the csv file
csvData.forEach(row => {
// For each line we run the insert query with the row providing the column values
client.query(query, row, (err, res) => {
if (err) {
// We can just console.log any errors
console.log(err.stack);
} else {
console.log("inserted " + res.rowCount + " row:", row);
}
});
});
} finally {
done();
}
});
});
}
async function seed() {
await writeToCsvFile()
let stream = fs.createReadStream(output);
stream.pipe(insertFromCsv());
}
seed()