-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetting-started.js
110 lines (91 loc) · 2.38 KB
/
getting-started.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const TerminusDBClient = require("@terminusdb/terminusdb-client");
// TODO: change the team name
const team = "cloud";
const client = new TerminusDBClient.WOQLClient(`https://cloud-dev.dcm.ist/${team}/`, {
user: "[email protected]",
organization: team
});
//set the key as an environment variable.
client.setApiKey(process.env.TERMINUSDB_ACCESS_TOKEN)
const connectToServer = async () => {
try {
await client.connect();
} catch (err) {
console.error(err)
}
console.log("Connected to TerminusDB successfully!")
};
const createNewDB = async () => {
try {
await client.createDatabase('ExampleDatabase12e3', {
label: "ExampleDatabase32",
comment: "Created new ExampleDatabase",
});
console.log("Database created Successfully!")
} catch (err) {
console.error(err)
}
};
const insertSchema = async () => {
const schema = {
"@type": "Class",
"@id": "Player",
"@key": {
"@type": "Lexical",
"@fields": ["name"]
},
name: "xsd:string",
position: "xsd:string"
};
await client.addDocument(schema, {
graph_type: "schema"
});
console.log("Schema Inserted Successfully!");
}
const insertDocuments = async () => {
const objects = [{
"@type": "Player",
name: "George",
position: "Center Back",
},
{
"@type": "Player",
name: "Doug",
position: "Full Back",
},
{
"@type": "Player",
name: "Karen",
position: "Center Forward"
}
];
await client.addDocument(objects);
console.log("Documents Inserted Successfully!");
}
const getDocs = async () => {
const documents = await client.getDocument({
as_list: "true"
});
console.log("All Documents",documents)
}
const queryDocuments = async () => {
const query = {
"type": "Player",
"query": {
"position": "Full Back"
},
}
const result = await client.queryDocument(query, {
"as_list": true
});
console.log("Query Documents",result)
}
const runFunctions = async () => {
await connectToServer();
await createNewDB();
await insertSchema();
await insertDocuments();
await getDocs();
await queryDocuments();
}
runFunctions();