Skip to content

Commit ff655e9

Browse files
committed
Indexing code cleanup and minor API refinements.
In particular, get{Node|Relationship}Indexes() now returns an array of string names rather than a straight map, but has the map properties too. And it returns an empty array in the 204 case rather than null.
1 parent 533fc3b commit ff655e9

2 files changed

Lines changed: 102 additions & 74 deletions

File tree

lib/GraphDatabase._coffee

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -409,144 +409,167 @@ module.exports = class GraphDatabase
409409
### Indexes: ###
410410

411411
#
412-
# Get list of node indexes.
412+
# Get the current existing node indexes.
413+
# "Returns" (via callback) an array of string index names, but the array
414+
# also serves as a dictionary of index name to its config properties.
413415
#
414416
# @param callback {Function}
417+
# @return {Array<String>}
418+
#
419+
# db.getNodeIndexes(function (err, indexes) {
420+
# if (err) throw err;
421+
# indexes.forEach(function (name) {
422+
# console.log('Index', name, 'has config:', indexes[name]);
423+
# });
424+
# });
415425
#
416426
getNodeIndexes: (_) ->
417427
try
418428
services = @getServices _
429+
response = @_request.get services.node_index, _
419430

420-
url = "#{services.node_index}/"
421-
422-
response = @_request.get url, _
423-
424-
if response.statusCode isnt status.OK and response.statusCode isnt status.NO_CONTENT
431+
if response.statusCode not in [status.OK, status.NO_CONTENT]
425432
# Database error
426433
throw response
427434

428-
# Success
429-
return response.body
435+
# Success: transform the map into an array-map hybrid.
436+
map = response.body or {}
437+
arr = []
438+
for name, props of map
439+
arr.push name
440+
arr[name] = props
441+
return arr
430442

431443
catch error
432444
throw adjustError error
433445

434446
#
435447
# Create node index.
436448
#
437-
# @param index {String}
449+
# @param name {String}
438450
# @param callback {Function}
439451
#
440-
createNodeIndex: (index, _) ->
452+
createNodeIndex: (name, _) ->
441453
try
442454
services = @getServices _
443455

444456
response = @_request.post
445-
url: "#{services.node_index}/"
446-
json:
447-
name: index
457+
url: services.node_index
458+
json: {name}
448459
, _
449460

450461
if response.statusCode isnt status.CREATED
451462
# Database error
452463
throw response
453464

454465
# Success
455-
return response.body
466+
return
456467

457468
catch error
458469
throw adjustError error
459470

460471
#
461472
# Delete a node index.
462473
#
463-
# @param index {String}
474+
# @param name {String}
464475
# @param callback {Function}
465476
#
466-
deleteNodeIndex: (index, _) ->
477+
deleteNodeIndex: (name, _) ->
467478
try
468479
services = @getServices _
469-
470-
url = "#{services.node_index}/#{index}"
471-
response = @_request.del url, _
480+
response = @_request.del
481+
url: "#{services.node_index}/#{encodeURIComponent name}"
482+
, _
472483

473484
if response.statusCode isnt status.NO_CONTENT
474485
# Database error
475486
throw response
476487

477488
# Success
478-
return null
489+
return
479490

480491
catch error
481492
throw adjustError error
482493

483494
#
484-
# Get list of relationship indexes.
495+
# Get the current existing relationship indexes.
496+
# "Returns" (via callback) an array of string index names, but the array
497+
# also serves as a dictionary of index name to its config properties.
485498
#
486499
# @param callback {Function}
500+
# @return {Array<String>}
501+
#
502+
# db.getRelationshipIndexes(function (err, indexes) {
503+
# if (err) throw err;
504+
# indexes.forEach(function (name) {
505+
# console.log('Index', name, 'has config:', indexes[name]);
506+
# });
507+
# });
487508
#
488509
getRelationshipIndexes: (_) ->
489510
try
490511
services = @getServices _
512+
response = @_request.get services.relationship_index, _
491513

492-
url = "#{services.relationship_index}/"
493-
response = @_request.get url, _
494-
495-
if response.statusCode isnt status.OK and response.statusCode isnt status.NO_CONTENT
514+
if response.statusCode not in [status.OK, status.NO_CONTENT]
496515
# Database error
497516
throw response
498517

499-
# Success
500-
return response.body
518+
# Success: transform the map into an array-map hybrid.
519+
map = response.body or {}
520+
arr = []
521+
for name, props of map
522+
arr.push name
523+
arr[name] = props
524+
return arr
501525

502526
catch error
503527
throw adjustError error
504528

505529
#
506530
# Create relationship index.
507531
#
508-
# @param index {String}
532+
# @param name {String}
509533
# @param callback {Function}
510534
#
511-
createRelationshipIndex: (index, _) ->
535+
createRelationshipIndex: (name, _) ->
512536
try
513537
services = @getServices _
514538

515539
response = @_request.post
516540
url: "#{services.relationship_index}/"
517-
json:
518-
name: index
541+
json: {name}
519542
, _
520543

521544
if response.statusCode isnt status.CREATED
522545
# Database error
523546
throw response
524547

525548
# Success
526-
return response.body
549+
return
527550

528551
catch error
529552
throw adjustError error
530553

531554
#
532555
# Delete a relationship index.
533556
#
534-
# @param index {String}
557+
# @param name {String}
535558
# @param callback {Function}
536559
#
537-
deleteRelationshipIndex: (index, _) ->
560+
deleteRelationshipIndex: (name, _) ->
538561
try
539562
services = @getServices _
540-
541-
url = "#{services.relationship_index}/#{index}"
542-
response = @_request.del url, _
563+
response = @_request.del
564+
url: "#{services.relationship_index}/#{encodeURIComponent name}"
565+
, _
543566

544567
if response.statusCode isnt status.NO_CONTENT
545568
# Database error
546569
throw response
547570

548571
# Success
549-
return null
572+
return
550573

551574
catch error
552575
throw adjustError error

test/crud._coffee

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,51 @@ mat = null
2323
relationship = null
2424

2525
# index list
26-
nodeIndexes = null
27-
relIndexes = null
2826
nodeIndexName = 'testUsers'
2927
relIndexName = 'testFollows'
3028

29+
30+
## TESTS:
31+
3132
@crud =
3233

3334
'getNodeIndexes': (_) ->
3435
nodeIndexes = db.getNodeIndexes _
35-
if nodeIndexes
36-
expect(nodeIndexes).to.be.an 'object'
37-
else
38-
# requires a clean db to test
39-
expect(nodeIndexes).to.be undefined
36+
37+
# we should always get back an array of names, but the array should
38+
# have map-like properties for the index config details too:
39+
expect(nodeIndexes).to.be.an 'array'
40+
for name in nodeIndexes
41+
expect(nodeIndexes).to.have.key name
42+
expect(nodeIndexes[name]).to.be.an 'object'
43+
expect(nodeIndexes[name].type).to.be.a 'string'
4044

4145
'getRelationshipIndexes': (_) ->
4246
relIndexes = db.getRelationshipIndexes _
43-
if relIndexes
44-
expect(relIndexes).to.be.an 'object'
45-
else
46-
# requires a clean db to test
47-
expect(relIndexes).to.be undefined
47+
48+
# we should always get back an array of names, but the array should
49+
# have map-like properties for the index config details too:
50+
expect(relIndexes).to.be.an 'array'
51+
for name in relIndexes
52+
expect(relIndexes).to.have.key name
53+
expect(relIndexes[name]).to.be.an 'object'
54+
expect(relIndexes[name].type).to.be.a 'string'
4855

4956
'createNodeIndex': (_) ->
50-
testIndex = db.createNodeIndex nodeIndexName, _
51-
expect(testIndex).to.be.an 'object'
57+
db.createNodeIndex nodeIndexName, _
58+
59+
# our newly created index should now be in the list of indexes:
5260
nodeIndexes = db.getNodeIndexes _
53-
expect(nodeIndexes).to.be.an 'object'
54-
expect(nodeIndexes[nodeIndexName]).to.be.an 'object'
61+
expect(nodeIndexes).to.contain nodeIndexName
62+
expect(nodeIndexes).to.have.key nodeIndexName
5563

5664
'createRelationshipIndex': (_) ->
57-
testIndex = db.createRelationshipIndex relIndexName, _
58-
expect(testIndex).to.be.an 'object'
65+
db.createRelationshipIndex relIndexName, _
66+
67+
# our newly created index should now be in the list of indexes:
5968
relIndexes = db.getRelationshipIndexes _
60-
expect(relIndexes).to.be.an 'object'
61-
expect(relIndexes[relIndexName]).to.be.an 'object'
69+
expect(relIndexes).to.contain relIndexName
70+
expect(relIndexes).to.have.key relIndexName
6271

6372
'create nodes': (_) ->
6473
daniel = db.createNode danielData
@@ -222,30 +231,26 @@ relIndexName = 'testFollows'
222231
expect(matRelationship).to.be null
223232
expect(idRelationship).to.be null
224233

225-
226-
# TODO delete tests! that's the 'd' in 'crud'!
234+
# TODO test deleting nodes and relationships!
227235

228236
'deleteNodeIndex': (_) ->
229-
testIndex = db.deleteNodeIndex nodeIndexName, _
230-
expect(testIndex).to.be null
237+
db.deleteNodeIndex nodeIndexName, _
238+
239+
# our index should no longer be in the list of indexes:
231240
nodeIndexes = db.getNodeIndexes _
232-
if nodeIndexes
233-
expect(nodeIndexes).to.be.an 'object'
234-
expect(nodeIndexes[nodeIndexName]).to.be undefined
235-
else
236-
expect(nodeIndexes).to.be undefined
241+
expect(nodeIndexes).to.not.contain nodeIndexName
242+
expect(nodeIndexes).to.not.have.key nodeIndexName
237243

238244
'deleteRelationshipIndex': (_) ->
239-
testIndex = db.deleteRelationshipIndex relIndexName, _
240-
expect(testIndex).to.be null
245+
db.deleteRelationshipIndex relIndexName, _
246+
247+
# our index should no longer be in the list of indexes:
241248
relIndexes = db.getRelationshipIndexes _
242-
if relIndexes
243-
expect(relIndexes).to.be.an 'object'
244-
expect(relIndexes[relIndexName]).to.be undefined
245-
else
246-
expect(relIndexes).to.be undefined
249+
expect(relIndexes).to.not.contain relIndexName
250+
expect(relIndexes).to.not.have.key relIndexName
247251

248252

253+
## HELPERS:
249254

250255
testRelationship = (relationship) ->
251256
expect(relationship).to.be.an 'object'

0 commit comments

Comments
 (0)