-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
51 lines (45 loc) · 1.18 KB
/
upload.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
var fs = require("fs");
const csv = require("csv");
const addToDb = require("./addToDb");
/**
* @desc Parses and replaces/inserts lines of CSV data downloaded from Rutgers
* @param {int} offset - which record to start at
* @param {string} textFile - path of local text file
* @param {MongoClient.Db.Collection} collection - collection to insert into
* @param {csv.parse.Options.onRecord} onRecord - function to clean a record
*
* @throws {Error} is possible
*/
const updateFromTextFile = async ({
offset = 1,
textFile = "",
collection = null,
onRecord,
}) => {
const parser = csv.parse({
delimiter: "\t",
skipEmptyLines: true,
fromLine: 15,
skipLinesWithError: true,
trim: true,
onRecord,
});
const cleanedDocuments = [];
parser.on("data", (csvData) => {
cleanedDocuments.push(csvData);
});
const rs = fs.createReadStream(textFile);
await streamPipeline(rs, parser);
const { nSuccess, nFail, nFailByCodeName } = await addToDb({
documents: cleanedDocuments,
collection,
});
return {
offset,
nDocuments: cleanedDocuments.length,
nSuccess,
nFail,
nFailByCodeName,
};
};
module.exports = { updateFromTextFile };