@@ -68,6 +68,7 @@ async function init(args) {
68
68
let drop = args . drop || false ;
69
69
disableDirectivesChecking = args [ 'disableDirectivesChecking' ] || false ;
70
70
disableEdgeValidation = args [ 'disableEdgeValidation' ] || false ;
71
+ cachingEnabled = args [ 'cache' ] === false ? false : true ;
71
72
db = new arangojs . Database ( { url : url } ) ;
72
73
73
74
// wait for ArangoDB
@@ -809,8 +810,17 @@ function deleteObject(isRoot, ctxt, varOrID, typeToDelete, info, resVar = null)
809
810
810
811
// directives handling
811
812
addFinalDirectiveChecksForType ( ctxt , typeToDelete , aql `${ asAQLVar ( resVar ) } ._id` , info . schema ) ;
813
+
814
+ // invalidate cache
815
+ ctxt . trans . code . push ( `
816
+ // remove from cache
817
+ cacheKey = ${ idVar } ;
818
+ delete cache[cacheKey];
819
+ ` ) ;
820
+
812
821
// return promises for roots and null for nested result
813
822
ctxt . trans . code . push ( `}` ) ;
823
+
814
824
return isRoot ? getResult ( ctxt , info , resVar ) : null ;
815
825
}
816
826
@@ -1246,7 +1256,7 @@ function validateEdge(ctxt, source, sourceType, sourceField, target, targetType,
1246
1256
* @param typeOrInterface
1247
1257
* @param schema
1248
1258
*/
1249
- function exists ( ctxt , id , typeOrInterface , schema ) {
1259
+ function exists ( ctxt , varOrID , typeOrInterface , schema ) {
1250
1260
let aqlCollectionVars = [ ] ;
1251
1261
if ( graphql . isInterfaceType ( typeOrInterface ) || graphql . isUnionType ( typeOrInterface ) ) {
1252
1262
for ( let possibleType of Object . values ( schema . getPossibleTypes ( typeOrInterface ) ) ) {
@@ -1255,7 +1265,22 @@ function exists(ctxt, id, typeOrInterface, schema) {
1255
1265
} else {
1256
1266
aqlCollectionVars . push ( asAQLVar ( getCollectionVar ( typeOrInterface . name ) ) ) ;
1257
1267
}
1258
- ctxt . trans . code . push ( `if(!db._query(aql\`FOR doc IN FLATTEN(FOR i IN [${ aqlCollectionVars . join ( ', ' ) } ] RETURN i) FILTER doc._id == ${ id } RETURN doc\`).next()){ throw \`Object '${ id } ' does not exist as instance of ${ typeOrInterface } \`; }` ) ;
1268
+
1269
+ let queries = [ ] ;
1270
+ for ( let collection of aqlCollectionVars ) {
1271
+ queries . push ( `LENGTH(FOR d IN ${ collection } FILTER d._id == ${ varOrID } LIMIT 1 RETURN true) > 0` ) ;
1272
+ }
1273
+
1274
+ // check cache
1275
+ ctxt . trans . code . push ( `cacheKey = ${ varOrID . substring ( 2 , varOrID . length - 1 ) } ;
1276
+ cache[cacheKey] = cache[cacheKey] === undefined ? [] : cache[cacheKey];
1277
+ if(${ cachingEnabled } && cache[cacheKey].includes('${ typeOrInterface } ')){
1278
+ // skip cached
1279
+ } else if(!db._query(aql\`RETURN ${ queries . join ( ' || ' ) } \`).next()){
1280
+ throw \`Object '${ varOrID } ' does not exist as instance of ${ typeOrInterface } \`;
1281
+ }
1282
+ cache[cacheKey] = '${ typeOrInterface } ';
1283
+ ` ) ;
1259
1284
}
1260
1285
1261
1286
/**
@@ -1283,7 +1308,9 @@ function initTransaction(ctxt) {
1283
1308
code : [
1284
1309
'const db = require("@arangodb").db;' ,
1285
1310
'const {aql} = require("@arangodb");' ,
1286
- 'let result = Object.create(null);'
1311
+ 'let result = Object.create(null);' ,
1312
+ 'let cache = Object.create(null);' ,
1313
+ 'let cacheKey = null;'
1287
1314
] ,
1288
1315
finalConstraintChecks : [ ] ,
1289
1316
exportedVariables : { }
@@ -1312,6 +1339,7 @@ async function executeTransaction(ctxt) {
1312
1339
1313
1340
try {
1314
1341
let action = `function(params){\n\t${ ctxt . trans . code . join ( '\n\t' ) } \n\treturn result;\n}` ;
1342
+
1315
1343
console . debug ( action ) ;
1316
1344
console . debug ( ctxt . trans . params ) ;
1317
1345
ctxt . trans . results = await db . transaction (
0 commit comments