-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathGraphDatabase._coffee
More file actions
855 lines (724 loc) · 27.1 KB
/
GraphDatabase._coffee
File metadata and controls
855 lines (724 loc) · 27.1 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# TODO many of these functions take a callback but, in some cases, call the
# callback immediately (e.g. if a value is cached). we should probably make
# sure to always call callbacks asynchronously, to prevent race conditions.
# this can be done in Streamline syntax by adding one line before cases where
# we're returning immediately: process.nextTick _
PACKAGE = require '../package'
status = require 'http-status'
util = require './util'
adjustError = util.adjustError
Relationship = require './Relationship'
Node = require './Node'
# The key we use in our serialized JSON to identify this library's objects.
JSON_KEY = '_nodeNeo4j'
#
# The class corresponding to a Neo4j graph database. Start here.
#
module.exports = class GraphDatabase
#
# Construct a new client for the Neo4j graph database available at the
# given (root) URL.
#
# @overload constructor(url)
# @param url {String} The root URL where the Neo4j graph database is
# available, e.g. `'http://localhost:7474/'`. This URL should include
# HTTP Basic Authentication info if needed, e.g.
# `'http://user:password@example.com/'`.
#
# @overload constructor(opts)
# @param opts {Object}
# @option opts url {String} The root URL where the Neo4j graph database
# is available, e.g. `'http://localhost:7474/'`. This URL should
# include HTTP Basic Authentication info if needed, e.g.
# `'http://user:password@example.com/'`.
# @option opts proxy {String} An optional proxy URL for all requests.
#
constructor: (opts) ->
# normalize arg:
opts =
if typeof opts is 'string' then {url: opts}
else opts
{@url} = opts
@_request = util.wrapRequest opts
# Cache
@_root = null
@_services = null
### Database: ###
#
# Purge this client's cache of API endpoints for this graph database.
#
# @private
#
_purgeCache: ->
@_root = null
@_services = null
#
# Fetch, cache, and "return" (via callback) the API root data for this
# graph database.
#
# @private
# @param callback {Function}
# @return {Object}
#
_getRoot: (_) ->
if @_root?
return @_root
try
response = @_request.get @url, _
if response.statusCode isnt status.OK
throw response
return @_root = response.body
catch error
throw adjustError error
#
# Fetch, cache, and "return" (via callback) the API services data for this
# graph database.
#
# @private
# @param callback {Function}
# @return {Object}
#
getServices: (_) ->
if @_services?
return @_services
try
root = @_getRoot _
response = @_request.get root.data, _
if response.statusCode isnt status.OK
throw response
return @_services = response.body
catch error
throw adjustError error
### Nodes: ###
#
# Create and immediately return a new, unsaved node with the given
# properties.
#
# @note This node will *not* be persisted to the database until and unless
# its {Node#save save()} method is called.
# @todo We should consider changing this method to persist the node (i.e.
# call its {Node#save save()} method) as well in the next version of
# this library. That'd be a breaking change, but it'd simplify both this
# API and its usage (e.g. the node's `id` will be known then).
#
# @param data {Object} The properties this new node should have.
# @return {Node}
#
createNode: (data) ->
data = data || {}
node = new Node this,
data: data
return node
#
# Fetch and "return" (via callback) the node at the given URL.
#
# @todo Should this indeed throw an error if no node exists at this URL?
# Or should we be returning undefined?
#
# @private
# @param url {String}
# @param callback {Function}
# @return {Node}
# @throw {Error} If no node exists at this URL.
#
getNode: (url, _) ->
try
response = @_request.get url, _
if response.statusCode isnt status.OK
# Node not found
if response.statusCode is status.NOT_FOUND
throw new Error "No node at #{url}"
# Other unknown errors
throw response
return new Node this, response.body
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the node with the given Neo4j ID.
#
# @todo Should this indeed throw an error if no node exists with this ID?
# Or should we be returning undefined?
#
# @param id {Number} The integer ID of the node, e.g. `1234`.
# @param callback {Function}
# @return {Node}
# @throw {Error} If no node exists with this ID.
#
getNodeById: (id, _) ->
try
services = @getServices _
url = "#{services.node}/#{id}"
node = @getNode url, _
return node
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the node indexed under the given
# property and value in the given index. If none exists, returns
# undefined.
#
# @note With this method, at most one node is returned. See
# {#getIndexedNodes} for returning multiple nodes.
# @todo We should consider removing this method in the next version of
# this library. Client code should be aware of multiple hits instead of
# this library hiding that information and arbitrarily returning only
# the first hit.
#
# @param index {String} The name of the index, e.g. `'node_auto_index'`.
# @param property {String} The name of the property, e.g. `'username'`.
# @param value {Object} The value of the property, e.g. `'aseemk'`.
# @param callback {Function}
# @return {Node}
#
getIndexedNode: (index, property, value, _) ->
try
nodes = @getIndexedNodes index, property, value, _
node = null
if nodes and nodes.length > 0
node = nodes[0]
return node
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the nodes indexed under the given
# property and value in the given index. If no such nodes exist, an
# empty array is returned.
#
# @note This method will return multiple nodes if there are multiple hits.
# See {#getIndexedNode} for returning at most one node.
#
# @param index {String} The name of the index, e.g. `'node_auto_index'`.
# @param property {String} The name of the property, e.g. `'platform'`.
# @param value {Object} The value of the property, e.g. `'xbox'`.
# @param callback {Function}
# @return {Array<Node>}
#
getIndexedNodes: (index, property, value, _) ->
try
services = @getServices _
key = encodeURIComponent property
val = encodeURIComponent value
url = "#{services.node_index}/#{index}/#{key}/#{val}"
response = @_request.get url, _
if response.statusCode isnt status.OK
# Database error
throw response
# Success
return response.body.map (node) =>
new Node this, node
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the nodes matching the given query (in
# {http://lucene.apache.org/core/old_versioned_docs/versions/3_1_0/queryparsersyntax.html Lucene
# syntax}) from the given index. If no such nodes exist, an empty array is
# returned.
#
# @param index {String} The name of the index, e.g. `node_auto_index`.
# @param query {String} The Lucene query, e.g. `foo:bar AND hello:world`.
# @param callback {Function}
# @return {Array<Node>}
#
queryNodeIndex: (index, query, _) ->
try
services = @getServices _
url = "#{services.node_index}/#{index}?query=#{encodeURIComponent query}"
response = @_request.get url, _
if response.statusCode isnt status.OK
# Database error
throw response
# Success
return response.body.map (node) =>
new Node this, node
catch error
throw adjustError error
### Relationships: ###
# @private
createRelationship: (startNode, endNode, type, _) ->
# TODO: Implement?
#
# Fetch and "return" (via callback) the relationship at the given URL.
#
# @todo Should this indeed throw an error if no relationship exists at
# this URL? Or should we be returning undefined?
#
# @private
# @param url {String}
# @param callback {Function}
# @return {Relationship}
# @throw {Error} If no relationship exists at this URL.
#
getRelationship: (url, _) ->
try
response = @_request.get url, _
if response.statusCode isnt status.OK
# TODO: Handle 404
throw response
return new Relationship this, response.body
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the relationship with the given Neo4j
# ID.
#
# @todo Should this indeed throw an error if no relationship exists with
# this ID? Or should we be returning undefined?
#
# @param id {Number} The integer ID of the relationship, e.g. `1234`.
# @param callback {Function}
# @return {Relationship}
# @throw {Error} If no relationship exists with this ID.
#
getRelationshipById: (id, _) ->
services = @getServices _
# FIXME: Neo4j doesn't expose the path to relationships
relationshipURL = services.node.replace('node', 'relationship')
url = "#{relationshipURL}/#{id}"
@getRelationship url, _
#
# Fetch and "return" (via callback) the relationship indexed under the
# given property and value in the given index. If none exists, returns
# undefined.
#
# @note With this method, at most one relationship is returned. See
# {#getIndexedRelationships} for returning multiple relationships.
# @todo We should consider removing this method in the next version of
# this library. Client code should be aware of multiple hits instead of
# this library hiding that information and arbitrarily returning only
# the first hit.
#
# @param index {String} The name of the index, e.g. `'relationship_auto_index'`.
# @param property {String} The name of the property, e.g. `'created'`.
# @param value {Object} The value of the property, e.g. `1346713658393`.
# @param callback {Function}
# @return {Relationship}
#
getIndexedRelationship: (index, property, value, _) ->
try
relationships = @getIndexedRelationships index, property, value, _
return relationships?[0] or null
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the relationships indexed under the
# given property and value in the given index. If no such relationships
# exist, an empty array is returned.
#
# @note This method will return multiple relationships if there are
# multiple hits. See {#getIndexedRelationship} for returning at most one
# relationship.
#
# @param index {String} The name of the index, e.g. `'relationship_auto_index'`.
# @param property {String} The name of the property, e.g. `'favorite'`.
# @param value {Object} The value of the property, e.g. `true`.
# @param callback {Function}
# @return {Array<Relationship>}
#
getIndexedRelationships: (index, property, value, _) ->
try
services = @getServices _
key = encodeURIComponent property
val = encodeURIComponent value
url = "#{services.relationship_index}/#{index}/#{key}/#{val}"
response = @_request.get url, _
if response.statusCode isnt status.OK
# Database error
throw response
# Success
return response.body.map (relationship) =>
new Relationship this, relationship
catch error
throw adjustError error
#
# Fetch and "return" (via callback) the relationships matching the given query (in
# {http://lucene.apache.org/core/old_versioned_docs/versions/3_1_0/queryparsersyntax.html Lucene
# syntax}) from the given index. If no such relationship exist, an empty array is
# returned.
#
# @param index {String} The name of the index, e.g. `relationship_auto_index`.
# @param query {String} The Lucene query, e.g. `foo:bar AND hello:world`.
# @param callback {Function}
# @return {Array<Relationship>}
#
queryRelationshipIndex: (index, query, _) ->
try
services = @getServices _
url = "#{services.relationship_index}/#{index}?query=#{encodeURIComponent query}"
response = @_request.get url, _
if response.statusCode isnt status.OK
# Database error
throw response
# Success
return response.body.map (relationship) =>
new Relationship this, relationship
catch error
throw adjustError error
### Indexes: ###
#
# Get the current existing node indexes.
# "Returns" (via callback) an array of string index names, but the array
# also serves as a dictionary of index name to its config properties.
#
# @param callback {Function}
# @return {Array<String>}
#
# db.getNodeIndexes(function (err, indexes) {
# if (err) throw err;
# indexes.forEach(function (name) {
# console.log('Index', name, 'has config:', indexes[name]);
# });
# });
#
getNodeIndexes: (_) ->
try
services = @getServices _
response = @_request.get services.node_index, _
if response.statusCode not in [status.OK, status.NO_CONTENT]
# Database error
throw response
# Success: transform the map into an array-map hybrid.
map = response.body or {}
arr = []
for name, props of map
arr.push name
arr[name] = props
return arr
catch error
throw adjustError error
#
# Create node index.
#
# @param name {String}
# @param config {Object} Node index configuration
# @param callback {Function}
#
createNodeIndex: (name, config={}, _) ->
try
services = @getServices _
response = @_request.post
url: services.node_index
json: {name, config}
, _
if response.statusCode isnt status.CREATED
# Database error
throw response
# Success
return
catch error
throw adjustError error
# helper for overloaded createNodeIndex() method:
do (actual = @::createNodeIndex) =>
@::createNodeIndex = (name, config, callback) ->
if typeof config is 'function'
callback = config
config = null
actual.call @, name, config, callback
#
# Delete a node index.
#
# @param name {String}
# @param callback {Function}
#
deleteNodeIndex: (name, _) ->
try
services = @getServices _
response = @_request.del
url: "#{services.node_index}/#{encodeURIComponent name}"
, _
if response.statusCode isnt status.NO_CONTENT
# Database error
throw response
# Success
return
catch error
throw adjustError error
#
# Get the current existing relationship indexes.
# "Returns" (via callback) an array of string index names, but the array
# also serves as a dictionary of index name to its config properties.
#
# @param callback {Function}
# @return {Array<String>}
#
# db.getRelationshipIndexes(function (err, indexes) {
# if (err) throw err;
# indexes.forEach(function (name) {
# console.log('Index', name, 'has config:', indexes[name]);
# });
# });
#
getRelationshipIndexes: (_) ->
try
services = @getServices _
response = @_request.get services.relationship_index, _
if response.statusCode not in [status.OK, status.NO_CONTENT]
# Database error
throw response
# Success: transform the map into an array-map hybrid.
map = response.body or {}
arr = []
for name, props of map
arr.push name
arr[name] = props
return arr
catch error
throw adjustError error
#
# Create relationship index.
#
# @param name {String}
# @param config {Object} Relationship index configuration
# @param callback {Function}
#
createRelationshipIndex: (name, config={}, _) ->
try
services = @getServices _
response = @_request.post
url: "#{services.relationship_index}/"
json: {name, config}
, _
if response.statusCode isnt status.CREATED
# Database error
throw response
# Success
return
catch error
throw adjustError error
# helper for overloaded createRelationshipIndex() method:
do (actual = @::createRelationshipIndex) =>
@::createRelationshipIndex = (name, config, callback) ->
if typeof config is 'function'
callback = config
config = null
actual.call @, name, config, callback
#
# Delete a relationship index.
#
# @param name {String}
# @param callback {Function}
#
deleteRelationshipIndex: (name, _) ->
try
services = @getServices _
response = @_request.del
url: "#{services.relationship_index}/#{encodeURIComponent name}"
, _
if response.statusCode isnt status.NO_CONTENT
# Database error
throw response
# Success
return
catch error
throw adjustError error
## Serialization: ##
#
# Helper for other classes to serialize their data in a format that this
# GraphDatabase class will understand for *de*-serialization.
#
# @private
# @param obj {PropertyContainer}
# @return {Object}
#
_toJSON: (obj) ->
json = {}
# save this lib's basic info both for identification purposes and in
# case we ever need it in the future (e.g. for breaking changes):
json[JSON_KEY] =
version: PACKAGE.version
# save the object's constructor name, so we can deserialize it:
constructor: obj.constructor.name
# important: we don't save this db's URL, because it might contain a
# basic auth password!
json
#
# Transforms the given node or relationship object, parsed from JSON,
# to its appropriate node or relationship instance.
#
# @param obj {Object}
# @return {PropertyContainer}
#
fromJSON: (obj) ->
meta = obj?[JSON_KEY]
if typeof meta?.constructor isnt 'string'
throw new Error "Invalid JSON object: #{JSON.stringify obj}"
Constructor = require "./#{meta.constructor}"
Constructor._fromJSON @, obj
#
# A "reviver" function for JSON.parse() that'll transform any serialized
# nodes or relationships into their appropriate instances.
#
# To use, pass this method as the second parameter to JSON.parse().
# For convenience, it'll be bound to this GraphDatabase instance.
#
# @param key {String}
# @param val {Object}
# @return {Object}
# @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
# @example Serialize and deserialize nodes and relationships.
#
# var obj = {foo: node, bar: [relationship]};
# var str = JSON.stringify(obj);
# var res = JSON.parse(str, db.reviveJSON);
# // res.foo and res.bar[0] are Node and Relationship instances
#
reviveJSON: (key, val) =>
# only transform objects we recognize; ignore (pass through) the rest:
if typeof val?[JSON_KEY]?.constructor is 'string'
@fromJSON val
else
val
### Misc/Other: ###
#
# Fetch and "return" (via callback) the results of the given
# {http://docs.neo4j.org/chunked/stable/cypher-query-lang.html Cypher}
# query, optionally passing along the given query parameters (recommended
# to avoid Cypher injection security vulnerabilities). The returned
# results are an array of "rows" (matches), where each row is a map from
# key name (as given in the query) to value. Any values that represent
# nodes, relationships or paths are returned as {Node}, {Relationship} or
# {Path} instances.
#
# @param query {String} The Cypher query. Can be multi-line.
# @param params {Object} A map of parameters for the Cypher query.
# @param callback {Function}
# @return {Array<Object>}
# @example Fetch a user's likes.
#
# var query = [
# 'START user=node({userId})',
# 'MATCH (user) -[:likes]-> (other)',
# 'RETURN other'
# ].join('\n');
#
# var params = {
# userId: currentUser.id
# };
#
# db.query(query, params, function (err, results) {
# if (err) throw err;
# var likes = results.map(function (result) {
# return result['other'];
# });
# // ...
# });
#
query: (query, params={}, _) ->
try
services = @getServices _
endpoint = services.cypher or
services.extensions?.CypherPlugin?['execute_query']
if not endpoint
throw new Error 'Cypher plugin not installed'
if typeof query isnt 'string'
throw new Error "Expected string query; got #{typeof query}."
response = @_request.post
uri: endpoint
json: {query, params}
, _
# XXX workaround for neo4j silent failures for invalid queries:
if response.statusCode is status.NO_CONTENT
throw new Error """
Unknown Neo4j error for query:
#{query}
"""
if response.statusCode isnt status.OK
# Database error
throw response
# Success: build result maps, and transform nodes/relationships
body = response.body
# Update: guard against streaming errors where the response code
# is still 200, but the body is malformed JSON, and node-request
# swallows the error parsing the JSON:
# https://github.com/thingdom/node-neo4j/issues/71
# This is a manual fix for only this operation, since we know
# here that the response should always be a JSON object.
if typeof body isnt 'object'
throw new Error """
Malformed Cypher response for query:
#{query}
Neo4j may have run out of memory processing this query.
Maybe try a more efficient query?
"""
columns = body.columns
results = for row in body.data
map = {}
for value, i in row
map[columns[i]] = util.transform value, this
map
return results
catch error
throw adjustError error
# XXX temporary backwards compatibility shim for query() argument order,
# and also to support overloaded method signature:
do (actual = @::query) =>
@::query = (query, params, callback) ->
if typeof query is 'function' and typeof params is 'string'
# instantiate a new error to derive the current stack, and
# show the relevant source line in a warning:
console.warn 'neo4j.GraphDatabase::query()’s signature is ' +
'now (query, params, callback). Please update your code!\n' +
new Error().stack.split('\n')[2] # includes indentation
callback = query
query = params
params = null
else if typeof params is 'function'
callback = params
params = null
actual.call @, query, params, callback
#
# Execute and "return" (via callback) the results of the given
# {http://docs.neo4j.org/chunked/snapshot/gremlin-plugin.html Gremlin}
# script, optionally passing along the given script parameters
# (recommended to avoid Gremlin injection security vulnerabilities). Any
# values in the returned results that represent nodes, relationships or
# paths are returned as {Node}, {Relationship} or {Path} instances.
#
# @param script {String} The Gremlin script. Can be multi-line.
# @param params {Object} A map of parameters for the Gremlin script.
# @param callback {Function}
# @return {Object}
# @example Fetch a user's likes.
#
# var script = "g.v(userId).out('likes')";
#
# var params = {
# userId: currentUser.id
# };
#
# db.execute(script, params, function (err, likes) {
# if (err) throw err;
# likes.forEach(function (node) {
# // ...
# });
# });
#
execute: (script, params={}, _) ->
try
services = @getServices _
endpoint = services.extensions?.GremlinPlugin?['execute_script']
if not endpoint
throw new Error 'Gremlin plugin not installed'
response = @_request.post
uri: endpoint
json: {script, params}
, _
# XXX workaround for neo4j silent failures for invalid queries:
if response.statusCode is status.NO_CONTENT
throw new Error """
Unknown Neo4j error for Gremlin script:
#{script}
"""
if response.statusCode isnt status.OK
# Database error
throw response
# Success: transform nodes/relationships
return util.transform response.body, this
catch error
throw adjustError error
# helper for overloaded execute() method:
do (actual = @::execute) =>
@::execute = (script, params, callback) ->
if typeof params is 'function'
callback = params
params = null
actual.call @, script, params, callback