@@ -118,14 +118,13 @@ module.exports = class Node extends PropertyContainer
118118 #
119119 # @param index {String} The name of the index, e.g. `'users'`.
120120 # @param key {String} The key to index under, e.g. `'username'`.
121- # @param value {Object } The value to index under, e.g. `'aseemk'`.
121+ # @param value {String } The value to index under, e.g. `'aseemk'`.
122122 # @param callback {Function}
123123 #
124124 index : (index , key , value , _ ) ->
125125 try
126- # TODO
127126 if not @exists
128- throw new Error ' Node must exists before indexing properties '
127+ throw new Error ' Node must exist before indexing. '
129128
130129 services = @db .getServices _
131130 version = @db .getVersion _
@@ -162,30 +161,94 @@ module.exports = class Node extends PropertyContainer
162161 throw adjustError error
163162
164163 #
165- # Create and "return" (via callback) a relationship of the given type and
166- # with the given properties from this node to another node.
164+ # Delete this node from the given index, optionally under the given key
165+ # or key-value pair. (A key is required if a value is given.)
166+ #
167+ # @param index {String} The name of the index, e.g. `'users'`.
168+ # @param key {String} (Optional) The key to unindex from, e.g. `'username'`.
169+ # @param value {String} (Optional) The value to unindex from, e.g. `'aseemk'`.
170+ # @param callback {Function}
171+ #
172+ unindex : (index , key , value , _ ) ->
173+ # see below for the code that normalizes the args;
174+ # this function assumes all args are present (but may be null/etc.).
175+ try
176+ if not @exists
177+ throw new Error ' Node must exist before unindexing.'
178+
179+ services = @db .getServices _
180+
181+ key = encodeURIComponent key if key
182+ value = encodeURIComponent value if value
183+ base = " #{ services .node_index } /#{ encodeURIComponent index} "
184+ url =
185+ if key and value
186+ " #{ base} /#{ key} /#{ value} /#{ @id } "
187+ else if key
188+ " #{ base} /#{ key} /#{ @id } "
189+ else
190+ " #{ base} /#{ @id } "
191+
192+ response = @_request .del url, _
193+
194+ if response .statusCode isnt status .NO_CONTENT
195+ # database error
196+ throw response
197+
198+ # success
199+ return
200+
201+ catch error
202+ throw adjustError error
203+
204+ # helper for overloaded unindex() method:
205+ do (actual = @ :: unindex ) =>
206+ @ :: unindex = (index , key , value , callback ) ->
207+ if typeof key is ' function'
208+ callback = key
209+ key = null
210+ value = null
211+ else if typeof value is ' function'
212+ callback = value
213+ value = null
214+
215+ actual .call @ , index, key, value, callback
216+
217+ #
218+ # Create and "return" (via callback) a relationship of the given type, and
219+ # optionally with the given properties, from this node to another node.
167220 #
168221 # @param otherNode {Node}
169222 # @param type {String}
170- # @param data {Object} The properties this relationship should have.
223+ # @param data {Object} (Optional) The properties this relationship should have.
171224 # @param callback {Function}
172225 # @return {Relationship}
173226 #
174- createRelationshipTo : (otherNode , type , data , _ ) ->
175- @ _createRelationship this , otherNode, type, data, _
227+ createRelationshipTo : (otherNode , type , data , cb ) ->
228+ # support omitting data:
229+ if typeof data is ' function'
230+ cb = data
231+ data = null
232+
233+ @ _createRelationship this , otherNode, type, data, cb
176234
177235 #
178- # Create and "return" (via callback) a relationship of the given type and
179- # with the given properties from another node to this node.
236+ # Create and "return" (via callback) a relationship of the given type, and
237+ # optionally with the given properties, from another node to this node.
180238 #
181239 # @param otherNode {Node}
182240 # @param type {String}
183- # @param data {Object} The properties this relationship should have.
241+ # @param data {Object} (Optional) The properties this relationship should have.
184242 # @param callback {Function}
185243 # @return {Relationship}
186244 #
187- createRelationshipFrom : (otherNode , type , data , _ ) ->
188- @ _createRelationship otherNode, this , type, data, _
245+ createRelationshipFrom : (otherNode , type , data , cb ) ->
246+ # support omitting data:
247+ if typeof data is ' function'
248+ cb = data
249+ data = null
250+
251+ @ _createRelationship otherNode, this , type, data, cb
189252
190253 #
191254 # Create and "return" (via callback) a relationship of the given type and
@@ -199,7 +262,7 @@ module.exports = class Node extends PropertyContainer
199262 # @param callback {Function}
200263 # @return {Relationship}
201264 #
202- _createRelationship : (from , to , type , data , _ ) ->
265+ _createRelationship : (from , to , type , data = {} , _ ) ->
203266 try
204267 # ensure this node exists
205268 # ensure otherNode exists
0 commit comments