-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathinsert_data.js
More file actions
98 lines (81 loc) · 2.64 KB
/
insert_data.js
File metadata and controls
98 lines (81 loc) · 2.64 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const fs = require("fs");
const path = require("path");
const csv = require("fast-csv");
const TerminusClient = require("@terminusdb/terminusdb-client");
// TODO: Change teamname and username
const teamName = "yourTeam"
const username = "yourUser"
const client = new TerminusClient.WOQLClient(
`https://cloud.terminusdb.com/${teamName}/`,
{ user: username, organization: teamName , db:"GettingStartedDB" }
);
//Assign your key to environment variable TERMINUSDB_ACCESS_TOKEN
client.setApiKey(process.env.TERMINUSDB_ACCESS_TOKEN);
const contact_numbers = {};
const addresses = {};
const employees = [];
// function to load and parse huge CSV files
const readCsv = (fileName) => {
// TODO: change the directoryPath to point were the csv are stored
return new Promise((resolve, reject) => {
const data = [];
fs.createReadStream(
path.resolve(
__dirname,
fileName
)
)
.pipe(csv.parse({ headers: true, ignoreEmpty: true }))
.on("error", reject)
.on("data", (row) => data.push(row))
.on("end", () => {
console.log(`Parsed ${data.length} rows`);
resolve(data);
});
});
};
const insertData = async () => {
// read Contact.csv
let resultContacts = await readCsv("Contact.csv");
resultContacts.forEach((element) => {
contact_numbers[element["Employee id"]] = element["Contact number"];
let street = element["Home address"].split(",")[0];
let street_num = Number(street.split(" ")[0]);
let street_name = street.split(" ").slice(1).join(" ");
let town = element["Home address"].split(",")[1].substr(1);
addresses[element["Employee id"]] = {
"@type": "Address",
street: street_name,
street_num,
town,
postcode: element["Postcode"],
};
});
let resultEmployees = await readCsv("Employees.csv");
resultEmployees.forEach((element) => {
let employee = {
"@type": "Employee",
name: element["Name"],
title: element["Title"],
team: element["Team"],
address: addresses[element["Employee id"]],
contact_number: contact_numbers[element["Employee id"]],
employee_id: element["Employee id"],
};
if (element["Manager"] !== "")
employee.manager = "Employee/" + element["Manager"];
employees.push(employee);
});
console.log("Inserting Employees ", employees);
client
.addDocument(employees)
.then((res) => {
console.log("Employees inserted successfully", res);
})
.catch((error) => {
console.log(error);
});
const result = await client.getDocument({"as_list":true});
console.log(result);
};
insertData();