-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
362 lines (337 loc) · 11.9 KB
/
app.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
var express = require('express');
var cfenv = require('cfenv');
var app = express();
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var request = require('request-promise'); /* Note: using promise-friendly lib */
var uuid = require('node-uuid');
var appEnv = cfenv.getAppEnv();
var util = require('util');
app.set('port', process.env.PORT);
app.use(express.static(__dirname + '/public'));
/* Please provision an instance of IBMGraph and update these variables with your personal data */
var gURL = null;
var gUsername = null;
var gPassword = null;
if (!gURL || !gUsername || !gPassword) {
console.log('Please provision your own instance of IBM Graph and replace' +
'your credentials in the code');
process.exit(1);
}
var gBaseURL = gURL.split('/g').join('');
//Some env variables
var vertex1; var vertex2; var vertex3; var vertex4;
var edge1; var edge2;
// Get session token
console.log('Getting session token');
var sessionToken;
var graphUserPassPostOpts = {
method: 'GET',
uri: gBaseURL + '/_session',
auth: {user: gUsername, pass: gPassword},
json: true // Automatically parses the JSON string in the response
};
request(graphUserPassPostOpts).then(function (body) {
sessionToken = 'gds-token ' + body['gds-token'];
console.log('received sessionToken: ', sessionToken);
console.log('\n*******************\n');
//Now we will create a new graph
//Since schemas are immutable, this is useful to begin in a clean environment
console.log('Creating new graph');
var graphCreateOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gBaseURL + '/_graphs',
json: true
};
return request(graphCreateOpts);
}).then(function (body) {
gURL = body.dbUrl;
console.log('Successfully created a graph. The new apiURL is :', gURL);
console.log('\n*******************\n');
//Now we will POST a schema
//First we open read the schema from a file
console.log('Creating a schema');
return fs.readFileAsync('./public/json/schema.json', 'utf-8');
}).then(function (data) {
//Now send the request to IBM Graph
var postSchemaOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/schema',
json: JSON.parse(data.toString())
};
return request(postSchemaOpts);
}).then(function (body) {
console.log('successfuly post schema and here is the response : ' + JSON.stringify(body));
console.log('\n*******************\n');
//Now we will create a vertex
var body = {
'label': 'tweet',
'properties': {
'tweet': 'I love the movies #movies @joseph',
'tone': 'sad',
'sentiment': 'enduring'
}
};
var postVertexOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices',
json: body
};
console.log('Creating a tweet vertex');
return request(postVertexOpts);
}).then(function (body) {
vertex1 = body.result.data[0].id;
console.log('successfully created tweet vertex and its id is : ', vertex1);
console.log('\n*******************\n');
//Create hashtag vertex
console.log('Creating hashtag vertex');
var body = {
'label': 'hashtag',
'properties': {
'hashtag': 'movies',
'numTimes': 1000
}
};
var postVertexOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices',
json: body
};
return request(postVertexOpts);
}).then(function (body) {
vertex2 = body.result.data[0].id;
console.log('successfully created hashtag vertex and its id is : ', vertex2);
console.log('\n*******************\n');
//Create person vertex, for the person who tweeted the tweet
console.log('Creating person vertex, for person who tweeted the tweet');
var body = {
'label': 'person',
'properties': {
'name': 'Jessica',
'verified': false
}
};
var postVertexOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices',
json: body
};
return request(postVertexOpts);
}).then(function (body) {
vertex3 = body.result.data[0].id;
console.log('successfully created person vertex and its id is : ', vertex3);
console.log('\n*******************\n');
//Create person vertex, for the person who tweeted the tweet
console.log('Creating person vertex, for person who was mentioned in the tweet');
var body = {
'label': 'person',
'properties': {
'name': 'Joseph',
'verified': true
}
};
var postVertexOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices',
json: body
};
return request(postVertexOpts);
}).then(function (body) {
vertex4 = body.result.data[0].id;
console.log('successfully created person vertex and its id is : ', vertex4);
console.log('\n*******************\n');
//Now we will update the tweet vertex
var body = {
'properties': {
'tone': 'happy',
'sentiment': 'loving'
}
};
var puttVertexOpts = {
method: 'PUT',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices/' + vertex1,
json: body
};
console.log('Updating a vertex');
return request(puttVertexOpts);
}).then(function (body) {
console.log('successfully updated the tweet vertex and its new values are' +
JSON.stringify(body.result.data[0]));
console.log('\n*******************\n');
//Now we will create an edge
//between person who tweeted -> tweet
//Edges *must* include 'inV', 'outV', and 'label' in the POST body and
//these values are immutable
//Note: you can also include edgeIndexes for querying these edges,
//however, this example does not include edgeIndexes. edgeIndexes are mutable
var body = {
'inV': vertex1,
'outV': vertex3,
'label': 'tweets'
};
var postEdgeOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/edges',
json: body
};
console.log('Creating create an edge between person who tweeted -> tweet');
return request(postEdgeOpts);
}).then(function (body) {
edge1 = body.result.data[0].id;
console.log('successfully created the edge and its id is : ', edge1);
console.log('\n*******************\n');
//Now we will create an edge
//between person who tweet -> hashtag
var body = {
'inV': vertex2,
'outV': vertex1,
'label': 'hashes'
};
var postEdgeOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/edges',
json: body
};
console.log('Creating create an edge between tweet -> hashtag');
return request(postEdgeOpts);
}).then(function (body) {
edge2 = body.result.data[0].id;
console.log('successfully created the edge and its id is : ', edge2);
console.log('\n*******************\n');
//Now we will create an edge
//between person who tweet -> person mentioned in tweet
var body = {
'inV': vertex2,
'outV': vertex1,
'label': 'mentions'
};
var postEdgeOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/edges',
json: body
};
console.log('Creating create an edge between tweet -> person mentioned in tweet');
return request(postEdgeOpts);
}).then(function (body) {
edge3 = body.result.data[0].id;
console.log('successfully created the edge and its id is : ', edge3);
console.log('\n*******************\n');
//Now we will GET a vertex by its ID
console.log('getting a vertex by its id');
var getVertexOpts = {
method: 'GET',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices/' + vertex1,
json: true
};
return request(getVertexOpts);
}).then(function (body) {
console.log('successfully got the vertex and its data is : ' +
JSON.stringify(body.result.data[0]));
console.log('\n*******************\n');
//Now we will GET a vertex by an indexed property
console.log('getting a vertex by an indexed property');
var getVertexOpts = {
method: 'GET',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices?name=Jessica',
json: true
};
return request(getVertexOpts);
}).then(function (body) {
console.log('successfully got the vertex and its data is : ' +
JSON.stringify(body.result.data[0]));
console.log('\n*******************\n');
//Now we will GET a vertex by multiple indexed properties
console.log('getting a vertex by multiple indexed properties');
var getVertexOpts = {
method: 'GET',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices?tone=happy&sentiment=loving',
json: true
};
return request(getVertexOpts);
}).then(function (body) {
console.log('successfully got the vertex and its data is : ' +
JSON.stringify(body.result.data[0]));
console.log('\n*******************\n');
//Now we will GET a vertex by querying based on label and indexed property
//Note: you must still query based on indexed property
//You cannot query based on label alone
console.log('getting a vertex by an indexed property and label');
var getVertexOpts = {
method: 'GET',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices?tone=happy&sentiment=loving&label=tweet',
json: true
};
return request(getVertexOpts);
}).then(function (body) {
console.log('successfully got the vertex and its data is : ' +
JSON.stringify(body.result.data[0]));
console.log('\n*******************\n');
//Now we will perform a basic gremlin query
//This query will return all vertices connected by an outgoing edge
//from our tweet vertex
console.log('running a gremlin query');
console.log('this example should return all vertices connected by ' +
'an outgoing edge from our tweet vertex');
var body = {
'gremlin': 'def g = graph.traversal(); g.V(' + vertex1 + ').outE().inV()'
};
var gremlinQueryOpts = {
method: 'POST',
headers: {'Authorization': sessionToken},
uri: gURL + '/gremlin',
json: body
};
return request(gremlinQueryOpts);
}).then(function (body) {
for (var i = 0; i < body.result.data.length; i++) {
console.log('successfully found this vertex : ' +
JSON.stringify(body.result.data[i]));
}
console.log('\n*******************\n');
//delete a vertex
console.log('deleting a vertex');
var deleteVertexOpts = {
method: 'DELETE',
headers: {'Authorization': sessionToken},
uri: gURL + '/vertices/' + vertex1,
};
return request(deleteVertexOpts);
}).then(function (body) {
console.log('successfully deleted a vertex and response was' +
JSON.stringify(body));
console.log('\n*******************\n');
//Delete an edge
console.log('deleting an edge');
var deleteEdgeOpts = {
method: 'DELETE',
headers: {'Authorization': sessionToken},
uri: gURL + '/edges/' + edge2,
};
return request(deleteEdgeOpts);
}).then(function (body) {
console.log('successfully deleted an edge and response was' +
JSON.stringify(body));
console.log('\n*******************\n');
process.exit(0);
}).catch(function (error) {
console.log('Encountered the following error: ', error);
});
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function () {
// print a message when the server starts listening
console.log('server starting on ' + appEnv.url);
});