19
19
20
20
import { isInt } from '../../integer' ;
21
21
import { Node , Path , PathSegment , Relationship } from '../../graph-types' ;
22
- import { Neo4jError } from '../../error' ;
22
+ import { Neo4jError , PROTOCOL_ERROR } from '../../error' ;
23
+ import { isPoint , Point } from '../../spatial-types' ;
23
24
24
25
const CREDENTIALS_EXPIRED_CODE = 'Neo.ClientError.Security.CredentialsExpired' ;
25
26
@@ -137,11 +138,13 @@ function encodeQueryParameters(parameters) {
137
138
138
139
function encodeQueryParameter ( value ) {
139
140
if ( value instanceof Node ) {
140
- throw new Neo4jError ( 'It is not allowed to pass nodes in query parameters' ) ;
141
+ throw new Neo4jError ( 'It is not allowed to pass nodes in query parameters' , PROTOCOL_ERROR ) ;
141
142
} else if ( value instanceof Relationship ) {
142
- throw new Neo4jError ( 'It is not allowed to pass relationships in query parameters' ) ;
143
+ throw new Neo4jError ( 'It is not allowed to pass relationships in query parameters' , PROTOCOL_ERROR ) ;
143
144
} else if ( value instanceof Path ) {
144
- throw new Neo4jError ( 'It is not allowed to pass paths in query parameters' ) ;
145
+ throw new Neo4jError ( 'It is not allowed to pass paths in query parameters' , PROTOCOL_ERROR ) ;
146
+ } else if ( isPoint ( value ) ) {
147
+ throw newUnsupportedParameterError ( 'points' ) ;
145
148
} else if ( isInt ( value ) ) {
146
149
return value . toNumber ( ) ;
147
150
} else if ( Array . isArray ( value ) ) {
@@ -153,6 +156,11 @@ function encodeQueryParameter(value) {
153
156
}
154
157
}
155
158
159
+ function newUnsupportedParameterError ( name ) {
160
+ return new Neo4jError ( `It is not allowed to pass ${ name } in query parameters when using HTTP endpoint. ` +
161
+ `Please consider using Cypher functions to create ${ name } so that query parameters are plain objects.` , PROTOCOL_ERROR ) ;
162
+ }
163
+
156
164
function extractResult ( response ) {
157
165
const results = response . results ;
158
166
if ( results ) {
@@ -222,23 +230,25 @@ function extractRawRecordElement(index, data, nodesById, relationshipsById) {
222
230
const elementMetadata = data . meta ? data . meta [ index ] : null ;
223
231
224
232
if ( elementMetadata ) {
225
- // element is either a Node, Relationship or Path
226
- return convertComplexValue ( elementMetadata , nodesById , relationshipsById ) ;
233
+ // element is either a graph, spatial or temporal type
234
+ return convertComplexValue ( element , elementMetadata , nodesById , relationshipsById ) ;
227
235
} else {
228
236
// element is a primitive, like number, string, array or object
229
237
return convertPrimitiveValue ( element ) ;
230
238
}
231
239
}
232
240
233
- function convertComplexValue ( elementMetadata , nodesById , relationshipsById ) {
241
+ function convertComplexValue ( element , elementMetadata , nodesById , relationshipsById ) {
234
242
if ( isNodeMetadata ( elementMetadata ) ) {
235
243
return nodesById [ elementMetadata . id ] ;
236
244
} else if ( isRelationshipMetadata ( elementMetadata ) ) {
237
245
return relationshipsById [ elementMetadata . id ] ;
238
246
} else if ( isPathMetadata ( elementMetadata ) ) {
239
247
return convertPath ( elementMetadata , nodesById , relationshipsById ) ;
248
+ } else if ( isPointMetadata ( elementMetadata ) ) {
249
+ return convertPoint ( element ) ;
240
250
} else {
241
- return null ;
251
+ return element ;
242
252
}
243
253
}
244
254
@@ -295,6 +305,42 @@ function createPath(pathSegments) {
295
305
return new Path ( pathStartNode , pathEndNode , pathSegments ) ;
296
306
}
297
307
308
+ function convertPoint ( element ) {
309
+ const type = element . type ;
310
+ if ( type !== 'Point' ) {
311
+ throw new Neo4jError ( `Unexpected Point type received: ${ JSON . stringify ( element ) } ` ) ;
312
+ }
313
+
314
+ const coordinates = element . coordinates ;
315
+ if ( ! Array . isArray ( coordinates ) && ( coordinates . length !== 2 || coordinates . length !== 3 ) ) {
316
+ throw new Neo4jError ( `Unexpected Point coordinates received: ${ JSON . stringify ( element ) } ` ) ;
317
+ }
318
+
319
+ const srid = convertCrsToId ( element ) ;
320
+
321
+ return new Point ( srid , ...coordinates ) ;
322
+ }
323
+
324
+ function convertCrsToId ( element ) {
325
+ const crs = element . crs ;
326
+ if ( ! crs || ! crs . name ) {
327
+ throw new Neo4jError ( `Unexpected Point crs received: ${ JSON . stringify ( element ) } ` ) ;
328
+ }
329
+ const name = crs . name . toLowerCase ( ) ;
330
+
331
+ if ( name === 'wgs-84' ) {
332
+ return 4326 ;
333
+ } else if ( name === 'wgs-84-3d' ) {
334
+ return 4979 ;
335
+ } else if ( name === 'cartesian' ) {
336
+ return 7203 ;
337
+ } else if ( name === 'cartesian-3d' ) {
338
+ return 9157 ;
339
+ } else {
340
+ throw new Neo4jError ( `Unexpected Point crs received: ${ JSON . stringify ( element ) } ` ) ;
341
+ }
342
+ }
343
+
298
344
function convertPrimitiveValue ( element ) {
299
345
if ( element == null || element === undefined ) {
300
346
return null ;
@@ -317,11 +363,19 @@ function convertNumber(value) {
317
363
}
318
364
319
365
function isNodeMetadata ( metadata ) {
320
- return ! Array . isArray ( metadata ) && typeof metadata === 'object' && metadata . type === 'node' ;
366
+ return isMetadataForType ( 'node' , metadata ) ;
321
367
}
322
368
323
369
function isRelationshipMetadata ( metadata ) {
324
- return ! Array . isArray ( metadata ) && typeof metadata === 'object' && metadata . type === 'relationship' ;
370
+ return isMetadataForType ( 'relationship' , metadata ) ;
371
+ }
372
+
373
+ function isPointMetadata ( metadata ) {
374
+ return isMetadataForType ( 'point' , metadata ) ;
375
+ }
376
+
377
+ function isMetadataForType ( name , metadata ) {
378
+ return ! Array . isArray ( metadata ) && typeof metadata === 'object' && metadata . type === name ;
325
379
}
326
380
327
381
function isPathMetadata ( metadata ) {
0 commit comments