@@ -241,23 +241,17 @@ function getObjectOrInterfaceFields(type) {
241
241
}
242
242
243
243
/**
244
- * Get the annotations of an edge and convert them to string to be used in insert operations
245
- * Would have prefered using createEdge, but we seem unable to pass a properly accesible "context variable"._id as parameter
246
- * @param annotations
244
+ * Transforms the doc (field_name, field_value) collection to JSON stringify, strip leading and ending '{' and '}' and insert leading ', '
245
+ * This allows docs to be appanded to already existing input parameters.
246
+ * @param doc (should already have passed through getScalarsAndEnums)
247
247
* @returns String
248
248
*/
249
- function getAnnotations ( annotations ) {
250
- // Remeber that annotations should already have passed through getScalarsAndEnums
251
- // So injections on field should not be possible
252
- // The values need to be checked separatly.
253
- let ret = '' ;
254
- for ( let field in annotations ) {
255
- if ( typeof annotations [ field ] === 'string' ) {
256
- // Treat strings as strings
257
- ret += `, ${ field } : "${ annotations [ field ] } "` ;
258
- }
259
- else
260
- ret += `, ${ field } : ${ annotations [ field ] } ` ;
249
+ function convertToInputAppendString ( doc ) {
250
+ ret = ''
251
+ if ( doc != null && doc . size > 0 ) {
252
+ ret = JSON . stringify ( doc ) ;
253
+ ret [ 0 ] = ',' ;
254
+ ret = ret - slice ( 0 , - 1 ) ;
261
255
}
262
256
return ret ;
263
257
}
@@ -384,7 +378,7 @@ async function create(isRoot, ctxt, data, returnType, info) {
384
378
let typeToConnect = value [ 'connect' ] . split ( '/' ) [ 0 ] ;
385
379
// add edge
386
380
ctxt . trans . code . push ( `if(db._collection('${ typeToConnect } ').exists('${ value [ 'connect' ] } ')){` ) ;
387
- ctxt . trans . code . push ( ` db._query(aql\`INSERT {_from: ${ from } ._id, _to: "${ value [ 'connect' ] } " } IN ${ edgeCollection } RETURN NEW\`);` ) ;
381
+ ctxt . trans . code . push ( ` db._query(aql\`INSERT {_from: ${ from } ._id, _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
388
382
ctxt . trans . code . push ( `} else { ` ) ;
389
383
ctxt . trans . code . push ( ` throw "${ value [ 'connect' ] } does not exist in ${ typeToConnect } ";` ) ;
390
384
ctxt . trans . code . push ( `}` ) ;
@@ -398,22 +392,22 @@ async function create(isRoot, ctxt, data, returnType, info) {
398
392
let typeToCreate = key . replace ( / ^ c r e a t e ( .+ ) $ / , '$1' ) ;
399
393
let to = asAQLVar ( getVar ( ctxt ) ) ; // reference to the object to be added
400
394
await create ( false , ctxt , value [ key ] , info . schema . getType ( typeToCreate ) , info ) ;
401
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: ${ from } ._id, _to: ${ to } ._id ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
395
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: ${ from } ._id, _to: ${ to } ._id ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
402
396
}
403
397
} else { // type
404
398
if ( value [ 'connect' ] ) {
405
399
validateType ( ctxt , value [ 'connect' ] , innerFieldType , info . schema ) ;
406
400
let typeToConnect = value [ 'connect' ] . split ( '/' ) [ 0 ] ;
407
401
// add edge
408
402
ctxt . trans . code . push ( `if(db._collection('${ typeToConnect } ').exists('${ value [ 'connect' ] } ')){` ) ;
409
- ctxt . trans . code . push ( ` db._query(aql\`INSERT {_from: ${ from } ._id, _to: "${ value [ 'connect' ] } " } IN ${ edgeCollection } RETURN NEW\`);` ) ;
403
+ ctxt . trans . code . push ( ` db._query(aql\`INSERT {_from: ${ from } ._id, _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
410
404
ctxt . trans . code . push ( `} else { ` ) ;
411
405
ctxt . trans . code . push ( ` throw "${ value [ 'connect' ] } does not exist in ${ typeToConnect } ";` ) ;
412
406
ctxt . trans . code . push ( `}` ) ;
413
407
} else { // create
414
408
let to = asAQLVar ( getVar ( ctxt ) ) ; // reference to the object to be added
415
409
await create ( false , ctxt , value [ 'create' ] , innerFieldType , info ) ;
416
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: ${ from } ._id, _to: ${ to } ._id ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
410
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: ${ from } ._id, _to: ${ to } ._id ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
417
411
}
418
412
}
419
413
}
@@ -671,11 +665,11 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
671
665
// add edge
672
666
if ( ! disableEdgeValidation ) { // check the database
673
667
ctxt . trans . code . push ( `if(db._collection('${ typeToConnect } ').exists('${ value [ 'connect' ] } ')){` ) ;
674
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
668
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
675
669
ctxt . trans . code . push ( `} else { throw "${ value [ 'connect' ] } does not exist in ${ typeToConnect } "; }` ) ;
676
670
} else {
677
671
console . warn ( `Adding connection to ${ value [ 'connect' ] } in ${ edge } without validating ID` ) ;
678
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
672
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
679
673
}
680
674
} else {
681
675
// create
@@ -686,7 +680,7 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
686
680
}
687
681
let to = asAQLVar ( getVar ( ctxt ) ) ; // reference to the object to be added
688
682
await create ( false , ctxt , value [ key ] , info . schema . getType ( typeToCreate ) , info ) ;
689
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: ${ to } ._id } IN ${ edgeCollection } RETURN NEW\`);` ) ;
683
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: ${ to } ._id ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
690
684
}
691
685
} else {
692
686
// type field
@@ -699,17 +693,17 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
699
693
// add edge
700
694
if ( ! disableEdgeValidation ) { // check the database
701
695
ctxt . trans . code . push ( `if(db._collection('${ typeToConnect } ').exists('${ value [ 'connect' ] } ')){` ) ;
702
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
696
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
703
697
ctxt . trans . code . push ( `} else { throw "${ value [ 'connect' ] } does not exist in ${ typeToConnect } "; }` ) ;
704
698
} else {
705
699
console . warn ( `Adding connection to ${ value [ 'connect' ] } in ${ edge } without validating ID` ) ;
706
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
700
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: "${ value [ 'connect' ] } " ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
707
701
}
708
702
} else {
709
703
// create
710
704
let to = asAQLVar ( getVar ( ctxt ) ) ; // reference to the object to be added
711
705
await create ( false , ctxt , value [ 'create' ] , nestedReturnType , info ) ;
712
- ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: ${ to } ._id ${ getAnnotations ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
706
+ ctxt . trans . code . push ( `db._query(aql\`INSERT {_from: "${ id } ", _to: ${ to } ._id ${ convertToInputAppendString ( annotations ) } } IN ${ edgeCollection } RETURN NEW\`);` ) ;
713
707
}
714
708
}
715
709
}
@@ -777,11 +771,11 @@ function formatFixVariable(_type, v) {
777
771
if ( Array . isArray ( v ) ) {
778
772
let newV = [ ]
779
773
for ( date of v )
780
- newV . push ( aql `DATE_TIMESTAMP(${ date } )` ) ;
774
+ newV . push ( aql `DATE_TIMESTAMP(" ${ date } " )` ) ;
781
775
return newV ;
782
776
}
783
777
else
784
- return aql `DATE_TIMESTAMP(${ v } )` ;
778
+ return aql `DATE_TIMESTAMP(" ${ v } " )` ;
785
779
else
786
780
return v ;
787
781
}
0 commit comments