forked from philippkueng/node-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
438 lines (365 loc) · 13.7 KB
/
main.js
File metadata and controls
438 lines (365 loc) · 13.7 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
var request = require('superagent'),
Step = require('step');
module.exports = Neo4j;
function Neo4j(url){
if(typeof url !== 'undefined' && url !== ''){
this.url = url;
} else {
this.url = 'http://localhost:7474';
}
};
/* Insert a Node --------- */
Neo4j.prototype.insertNode = function(node, callback){
var that = this;
request
.post(this.url + '/db/data/node')
.send(node)
.type('form')
.set('Accept', 'application/json')
.end(function(result){
if(typeof result.body !== 'undefined'){
that.addNodeId(result.body, callback);
} else {
callback(new Error('Response is empty'), null);
}
});
};
/* Delete a Node --------- */
// Nodes with Relationships cannot be deleted -> deliver proper error message
Neo4j.prototype.deleteNode = function(node_id, callback){
request
.del(this.url + '/db/data/node/' + node_id)
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 204:
callback(null, true); // Node was deleted.
break;
case 404:
callback(null, false); // Node doesn't exist.
break;
case 409:
callback(null, false); // Node has Relationships and cannot be deleted.
break;
default:
callback(new Error('Unknown Error while deleting Node'), null);
}
});
};
/* Read a Node ---------- */
Neo4j.prototype.readNode = function(node_id, callback){
var that = this;
request
.get(this.url + '/db/data/node/' + node_id)
.set('Accept', 'application/json')
.end(function(result){
if(typeof result.body !== 'undefined'){
if(result.statusCode === 200){
that.addNodeId(result.body, callback);
} else if(result.statusCode === 404){
callback(null, null);
} else {
callback(new Error('HTTP Error ' + result.statusCode + ' occurred.'), null);
}
} else {
callback(new Error('Response is empty'), null);
}
});
};
/* Update a Node ---------- */
Neo4j.prototype.updateNode = function(node_id, node_data, callback){
var that = this;
request
.put(this.url + '/db/data/node/' + node_id + '/properties')
.send(that.stringifyValueObjects(that.replaceNullWithString(node_data)))
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 204:
callback(null, true);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when updating a Node.'), null);
}
});
};
/* Insert a Relationship ------ */
Neo4j.prototype.insertRelationship = function(root_node_id, other_node_id, relationship_type, relationship_data, callback){
var that = this;
request
.post(that.url + '/db/data/node/' + root_node_id + '/relationships')
.send({
to: that.url + '/db/data/node/' + other_node_id,
type: relationship_type,
data: that.stringifyValueObjects(that.replaceNullWithString(relationship_data))
})
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 201:
that.addRelationshipId(result.body, callback);
break;
case 400: // Endnode not found exception
callback(null, false);
break;
case 404: // Startnode not found exception
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when inserting a Relationship.'), null);
}
});
};
/* Delete a Relationship --------- */
Neo4j.prototype.deleteRelationship = function(relationship_id, callback){
var that = this;
request
.del(that.url + '/db/data/relationship/' + relationship_id)
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 204:
callback(null, true);
break;
case 404: // Relationship with that id doesn't exist.
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when deleting a Relationship.'), null);
}
});
};
/* Read a Relationship ----------- */
Neo4j.prototype.readRelationship = function(relationship_id, callback){
var that = this;
request
.get(that.url + '/db/data/relationship/' + relationship_id)
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 200:
that.addRelationshipId(result.body, callback);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when reading a Relationship'), null);
}
});
};
/* Update a Relationship -------- */
Neo4j.prototype.updateRelationship = function(relationship_id, relationship_data, callback){
var that = this;
request
.put(that.url + '/db/data/relationship/' + relationship_id + '/properties')
.send(that.stringifyValueObjects(that.replaceNullWithString(relationship_data)))
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 204:
callback(null, true);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when updating a Relationship.'), null);
}
});
};
/* ADVANCED FUNCTIONS ---------- */
/* Get all Relationship Types -------- */
Neo4j.prototype.readRelationshipTypes = function(callback){
var that = this;
request
.get(that.url + '/db/data/relationship/types')
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 200:
callback(null, result.body);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when retrieving relationship types.'), null);
}
});
};
/* Get all Relationships of a Node --------- */
Neo4j.prototype.readAllRelationshipsOfNode = function(node_id, callback){
var that = this;
request
.get(that.url + '/db/data/node/' + node_id + '/relationships/all')
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 200:
that.addRelationshipIdForArray(result.body, callback);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when retrieving all relationships for node ' + node_id), null);
}
});
};
/* Get all the incoming Relationships of a Node --------- */
Neo4j.prototype.readIncomingRelationshipsOfNode = function(node_id, callback){
var that = this;
request
.get(that.url + '/db/data/node/' + node_id + '/relationships/in')
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 200:
that.addRelationshipIdForArray(result.body, callback);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when retrieving incoming relationships for node ' + node_id), null);
}
});
};
/* Get all the outgoing Relationships of a Node -------- */
Neo4j.prototype.readOutgoingRelationshipsOfNode = function(node_id, callback){
var that = this;
request
.get(that.url + '/db/data/node/' + node_id + '/relationships/out')
.set('Accept', 'application/json')
.end(function(result){
switch(result.statusCode){
case 200:
that.addRelationshipIdForArray(result.body, callback);
break;
case 404:
callback(null, false);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when retrieving outgoing relationships for node ' + node_id), null);
}
});
};
/* Run Cypher Query -------- */
Neo4j.prototype.cypherQuery = function(query, callback){
var that = this;
request
.post(that.url + '/db/data/cypher')
.set('Content-Type', 'application/json')
.send({query: query})
.end(function(result){
switch(result.statusCode){
case 200:
if(result.body && result.body.data.length >= 1){
Step(
function addIds(){
var group = this.group();
result.body.data.forEach(function(node){
that.addNodeId(node[0], group());
});
},
function sumUp(err, nodes){
if(err){
throw err;
} else {
result.body.data = nodes;
callback(null, result.body);
}
});
} else {
callback(null, result.body);
}
break;
case 404:
callback(null, null);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when running the cypher query against neo4j'), null);
}
});
};
/* Run Batch Queries -------- */
Neo4j.prototype.batchQuery = function(query, callback){
var that = this;
request
.post(that.url + '/db/data/batch')
.set('Content-Type', 'application/json')
.send(query)
.end(function(result){
switch(result.statusCode){
case 200:
callback(null, result.body);
break;
case 404:
callback(null, null);
break;
default:
callback(new Error('HTTP Error ' + result.statusCode + ' when running the batch query against neo4j'), null);
}
});
};
/* HELPER METHODS --------- */
/* Strips username and password from URL so that the node_id can be extracted. */
Neo4j.prototype.removeCredentials = function(path){
if(typeof path !== 'undefined' && path !== ''){
return path.replace(/[a-z0-9]+\:[a-z0-9]+\@/, '');
} else {
return '';
}
};
/* Extract node_id and add it as a property. */
Neo4j.prototype.addNodeId = function(node, callback){
if (node.self) {
node.id = node.self.replace(this.removeCredentials(this.url) + '/db/data/node/', '');
}
callback(null, node);
};
/* Extract relationship_id and add it as a property. */
Neo4j.prototype.addRelationshipId = function(relationship, callback){
relationship.start_node_id = relationship.start.replace(this.removeCredentials(this.url) + '/db/data/node/', '');
relationship.end_node_id = relationship.end.replace(this.removeCredentials(this.url) + '/db/data/node/', '');
relationship.id = relationship.self.replace(this.removeCredentials(this.url) + '/db/data/relationship/', '');
callback(null, relationship);
};
/* Add relationship_id for an array of relationships */
Neo4j.prototype.addRelationshipIdForArray = function(relationships, callback){
var that = this;
Step(
function process_relationships(){
var group = this.group();
relationships.forEach(function(relationship){
that.addRelationshipId(relationship, group());
});
},
function sum_up(err, results){
if(err) {
callback(err, null);
} else {
callback(null, results);
}
}
);
};
/* Replace null values with an empty string */
Neo4j.prototype.replaceNullWithString = function(node_data, callback){
for(var key in node_data){
if(node_data.hasOwnProperty(key) && node_data[key] === null){
node_data[key] = '';
}
}
return node_data;
};
/* Turn values that are objects themselves into strings. */
Neo4j.prototype.stringifyValueObjects = function(node_data, callback){
for(var key in node_data){
if(node_data.hasOwnProperty(key) && typeof node_data[key] === 'object'){
node_data[key] = JSON.stringify(node_data[key]);
}
}
return node_data;
};