Skip to content

Commit a3af620

Browse files
committed
Added getting started tutorial for javascript client
Signed-off-by: NeelParihar <[email protected]>
1 parent c7f1809 commit a3af620

32 files changed

+1162
-0
lines changed
File renamed without changes.
File renamed without changes.

Diff for: getting_started/javascript-client/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Getting started using TerminusDB Javascript Client
2+
3+
This is a step by step tutorial to get you started using TerminusDB/ TerminusX for the first time. This will cover all the basics that you can work with TerminusDB/ TerminusX using the Javascript client.
4+
5+
- [Lesson 1 - Installing, start project and create an empty database with schema](lesson_1.md)
6+
- [Lesson 2 - Importing data form CSV using Javascript script](lesson_2.md)
7+
- [Lesson 3 - Update and import new data that links to old data](lesson_3.md)
8+
- [Lesson 4 - Query on the database and get result back as json](lesson_4.md)
9+
- [Lesson 5 - Version control: time travel, branching and rebase](lesson_5.md)
10+
11+
## Extra lessons:
12+
13+
### Working with Singer.io
14+
15+
Here are a few example/ tutorials showing how to work with Singer.io, an open-source ELT tool to build data pipelines with taps and targets.
16+
17+
[Exporting Data to Google Sheets with Singer.io](https://github.com/terminusdb/terminusdb-tutorials/tree/master/google_sheets/README.md)
18+
19+
Example of showing how to export data from TerminusDB/ TerminusX to Google Sheets with Singer.io target.
20+
21+
[Putting GitHub Data into TerminusDB/ TerminusX](https://github.com/terminusdb/terminusdb-tutorials/tree/master/github_data/README.md)
22+
23+
Example of showing how to import data from GitHub and store it in TerminusDB/ TerminusX with Singer.io tap.
24+
25+
[The COVID-19 Public Data with Singer.io](https://github.com/terminusdb/terminusdb-tutorials/tree/master/covid_data/README.md)
26+
27+
Example of showing how to import data (COVID-19 Public Data) from a Singer.io tap to TerminusDB/ TerminusX.
28+
29+
### Working with JupyterLab/ Jupyter notebook
30+
(Coming soon)

Diff for: getting_started/javascript-client/add_contractors.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const TerminusClient = require("@terminusdb/terminusdb-client");
2+
3+
// Connecting to TerminusX
4+
// TODO: Change teamname
5+
const client = new TerminusClient.WOQLClient(
6+
"https://cloud.terminusdb.com/cloudux/",
7+
{ user: "username", organization: "cloudux", db: "GettingStartedDB" }
8+
);
9+
10+
//Assign your key to environment variable TERMINUSDB_ACCESS_TOKEN
11+
client.setApiKey(process.env.TERMINUSDB_ACCESS_TOKEN);
12+
13+
const getCommitHistory = async (branch) => {
14+
15+
const woqlLib = TerminusClient.WOQL;
16+
const commitQuery = woqlLib.lib().commits(branch);
17+
18+
const res = await client.query(commitQuery);
19+
console.log(res.bindings);
20+
};
21+
22+
const addContractors = async () => {
23+
24+
const rhys = {
25+
"@type": "Employee",
26+
employee_id: "006",
27+
name: "Rhys Arnold",
28+
title: "UX Designer",
29+
team: "IT",
30+
contact_number: "078 3951 7569",
31+
address: {
32+
"@type": "Address",
33+
postcode: "DG4 2ZQ",
34+
street: "Helland Bridge",
35+
street_num: 1,
36+
town: "Ulzieside",
37+
},
38+
};
39+
40+
const maya = {
41+
"@type": "Employee",
42+
employee_id: "007",
43+
name: "Maya O'Brien",
44+
title: "Creative Content Creator",
45+
team: "Marketing",
46+
contact_number: "078 1788 9177",
47+
address: {
48+
"@type": "Address",
49+
postcode: "GU3 3AF",
50+
street: "Tadcaster Rd",
51+
street_num: 24,
52+
town: "Pitch Place",
53+
},
54+
};
55+
await client.addDocument([rhys, maya],{},"","Adding contractors");
56+
};
57+
58+
59+
const runScript = async () => {
60+
61+
const defaultBranches = await client.getBranches();
62+
console.log("Default Branches: ", defaultBranches);
63+
64+
// Create new contractor branch
65+
await client.branch("contractors");
66+
console.log("Branch created successfully!")
67+
68+
const newBranches = await client.getBranches();
69+
console.log("New Branches: ", newBranches);
70+
71+
// checkout to new branch contractors
72+
client.checkout("contractors");
73+
74+
await addContractors();
75+
console.log("Added Contractors successfully!")
76+
77+
78+
console.log("Main Commit History: ")
79+
await getCommitHistory("main");
80+
81+
82+
console.log("Contractors Commit History: ")
83+
await getCommitHistory("contractors");
84+
85+
client.checkout("main");
86+
87+
await client.rebase({rebase_from: "cloudux/GettingStartedDB/local/branch/contractors/", message: "Merging from contractors" , author: "USer"});
88+
console.log("Rebase done successfully!");
89+
90+
console.log("main Commit History: ")
91+
await getCommitHistory("main");
92+
93+
await client.resetBranch("main", "2zt3shmtvrrdsk63kvdxiwtzakthwb3");
94+
console.log("Reset done successfully!");
95+
96+
console.log("Main Commit History: ")
97+
await getCommitHistory("main");
98+
}
99+
runScript();

Diff for: getting_started/javascript-client/insert_data.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const csv = require("fast-csv");
4+
5+
const TerminusClient = require("@terminusdb/terminusdb-client");
6+
7+
8+
// Connecting to TerminusX
9+
// TODO: Change teamname
10+
const client = new TerminusClient.WOQLClient(
11+
"https://cloud.terminusdb.com/teamname/",
12+
{ user: "username", organization: "teamname", db: "GettingStartedDB"}
13+
);
14+
15+
//Assign your key to environment variable TERMINUSDB_ACCESS_TOKEN
16+
client.setApiKey(process.env.TERMINUSDB_ACCESS_TOKEN);
17+
18+
const contact_numbers = {};
19+
const addresses = {};
20+
const employees = [];
21+
22+
// function to load and parse huge CSV files
23+
const readCsv = (fileName) => {
24+
// TODO: change the directoryPath to point were the csv are stored
25+
const directoryPath = "../../terminusdb-tutorials/getting_started/javascript/"
26+
return new Promise((resolve, reject) => {
27+
const data = [];
28+
29+
fs.createReadStream(
30+
path.resolve(
31+
__dirname,
32+
directoryPath,
33+
fileName
34+
)
35+
)
36+
.pipe(csv.parse({ headers: true, ignoreEmpty: true }))
37+
.on("error", reject)
38+
.on("data", (row) => data.push(row))
39+
.on("end", () => {
40+
console.log(`Parsed ${data.length} rows`);
41+
resolve(data);
42+
});
43+
});
44+
};
45+
46+
const insertData = async () => {
47+
// read Contact.csv
48+
let resultContacts = await readCsv("Contact.csv");
49+
resultContacts.forEach((element) => {
50+
contact_numbers[element["Employee id"]] = element["Contact number"];
51+
52+
let street = element["Home address"].split(",")[0];
53+
let street_num = Number(street.split(" ")[0]);
54+
let street_name = street.split(" ").slice(1).join(" ");
55+
let town = element["Home address"].split(",")[1].substr(1);
56+
57+
addresses[element["Employee id"]] = {
58+
"@type": "Address",
59+
street: street_name,
60+
street_num,
61+
town,
62+
postcode: element["Postcode"],
63+
};
64+
});
65+
66+
let resultEmployees = await readCsv("Employees.csv");
67+
resultEmployees.forEach((element) => {
68+
let employee = {
69+
"@type": "Employee",
70+
name: element["Name"],
71+
title: element["Title"],
72+
team: element["Team"],
73+
address: addresses[element["Employee id"]],
74+
contact_number: contact_numbers[element["Employee id"]],
75+
employee_id: element["Employee id"],
76+
};
77+
78+
if (element["Manager"] !== "")
79+
employee.manager = "Employee/" + element["Manager"];
80+
81+
employees.push(employee);
82+
});
83+
console.log("Inserting Employees ", employees);
84+
85+
client
86+
.addDocument(employees)
87+
.then((res) => {
88+
console.log("Employees inserted successfully", res);
89+
})
90+
.catch((error) => {
91+
console.log(error);
92+
});
93+
94+
const result = await client.getDocument({"as_list":true});
95+
console.log(result);
96+
};
97+
98+
insertData();

0 commit comments

Comments
 (0)