Constraints Class
The constraints are used to check the data integrity in the Neo4J database.
There are:
- Count constraints
- Bool constraints
- Free javascript constraints
Item Index
Methods
check_all_fragments
-
bool_constraints -
count_constraints -
free_constraints
Checks bool, count and free constraints If one fails, the promise is rejected and contains the rejected_constraint
A constraint is an object with different attributes example attributes: query, min, max, code but also: error, error_code
Parameters:
-
bool_constraintsArray -
count_constraintsArray -
free_constraintsArray
Returns:
constraint_has_changes
-
constraint -
constraint_type -
fragment_id
Compares a new constraint to a saved constraint in order to detect whether changes were made
Example query:
// session = a neo4j session\r\n// session.run(cypher_string) returns a promise (see the docs)\r\nnew Promise(function(resolve, reject){\r\n var variables = {"fragment_id": fragment_id};\r\n session.run("MATCH(f:Fragment)-[]-(t:Token {value: 'Token'}) WHERE ID(f) = {fragment_id} RETURN t.value as value;", variables)\r\n .then(function(result){ \r\n var value;\r\n result.records.forEach(function(res){\r\n value = res.get('value');\r\n if (value === "Token2") {\r\n reject("There was a token called Token.");\r\n }\r\n });\r\n resolve();\r\n }).catch(function(err){\r\n reject(err);\r\n });\r\n});
Parameters:
-
constraintObject -
constraint_typeStringeither count_constraint, bool_constraint or free_constraint
-
fragment_idNumber
Returns:
run_bool_constraint
-
constraint -
fragment_id
Compares the output of the constraint's cypher query to the boolean 'true'
Necessary attribute of the constraint: query
Example query:
MATCH(f:Fragment)-[]-(t:Token {value: 'Token'}) WHERE ID(f) = {fragment_id} RETURN COUNT(t) > 0;
Parameters:
-
constraintObject -
fragment_idNumber
Returns:
run_bool_constraint
-
constraint -
fragment_id
Runs javascript code that has to return a new Promise(...) If it resolves then the constraint is fulfilled
Necessary attribute of the constraint: query
Example query:
// session = a neo4j session\r\n// session.run(cypher_string) returns a promise (see the docs)\r\nnew Promise(function(resolve, reject){\r\n var variables = {"fragment_id": fragment_id};\r\n session.run("MATCH(f:Fragment)-[]-(t:Token {value: 'Token'}) WHERE ID(f) = {fragment_id} RETURN t.value as value;", variables)\r\n .then(function(result){ \r\n var value;\r\n result.records.forEach(function(res){\r\n value = res.get('value');\r\n if (value === "Token2") {\r\n reject("There was a token called Token.");\r\n }\r\n });\r\n resolve();\r\n }).catch(function(err){\r\n reject(err);\r\n });\r\n});
Parameters:
-
constraintObject -
fragment_idNumber
Returns:
run_count_constraint
-
constraint -
fragment_id
Counts the number of results retrieved from the database and compares it to given boundaries
Necessary attributes of the constraint: query, min, max
Example query:
MATCH(f:Fragment)-[]-(t:Token) WHERE ID(f) = {fragment_id} RETURN t
Parameters:
-
constraintObject -
fragment_idNumber
