|
1 | 1 | # Constraint
|
2 | 2 |
|
3 |
| -Constraints are entities which contain the following attributes: |
| 3 | +Constraints are used to enforce specific data schemas within a database. |
| 4 | +There are two different types of constraints, one for nodes and one for relationships. |
| 5 | +They can be created as following: |
4 | 6 |
|
5 |
| -- **ConstraintName**: Zero or one constraint name, usually one. They are basically strings with validation. They must be |
6 |
| - snake_case as per [Neo4j's examples](https://neo4j.com/docs/cypher-manual/current/constraints/examples/). |
7 |
| - Constraint names can start with a single underscore, although this is reserved for internal logic. |
8 |
| - You can overwrite the validation part by creating your own implementation of |
9 |
| - `Syndesi\CypherDataStructures\Contract\ConstraintNameInterface`. |
10 |
| -- **ConstraintType**: Defines how the constraint works, must be set manually. |
11 |
| -- **For**: Can be either a `NodeLabel` or `RelationType`. |
12 |
| -- **Properties**: Properties on which the constraint applies to. At least one is required. |
13 |
| -- **Options**: Options which configure constraint dependant settings, usually empty. |
| 7 | +```php |
| 8 | +use Syndesi\CypherDataStructures\Type\NodeConstraint; |
| 9 | +use Syndesi\CypherDataStructures\Type\RelationConstraint; |
| 10 | + |
| 11 | +$nodeConstraint = new NodeConstraint(); |
| 12 | +$relationConstraint = new RelationConstraint(); |
| 13 | + |
| 14 | +// note: the later examples use $someConstraint when the specific type does not matter |
| 15 | +``` |
| 16 | + |
| 17 | +!> **Important**: Constraints are not part of the OpenCypher specification and are different for each database type. |
| 18 | + |
| 19 | +!> **Note**: The creation of constraints might create internal indexes as well. |
| 20 | + |
| 21 | +## Name |
| 22 | + |
| 23 | +The name of constraints must be unique across the whole database. |
| 24 | +The name is usually written in [lowercase snake case](https://neo4j.com/docs/cypher-manual/current/constraints/examples/). |
| 25 | + |
| 26 | +```php |
| 27 | +// set the name of a constraint: |
| 28 | +$someConstraint->setName('some_name'); |
| 29 | + |
| 30 | +// get the name of a constraint: |
| 31 | +$someConstraint->getName(); |
| 32 | +``` |
| 33 | + |
| 34 | +## For |
| 35 | + |
| 36 | +Constraints are always created for a specific node or relationship label/type. |
| 37 | + |
| 38 | +```php |
| 39 | +// set the node label for a node label constraint: |
| 40 | +$nodeConstraint->setFor('NodeLabel'); |
| 41 | + |
| 42 | +// set the relationship type for a relationship constraint: |
| 43 | +$relationConstraint->setFor('RELATIONSHIP_TYPE'); |
| 44 | + |
| 45 | +// get the node label or relationship type from a constraint, depending on the constraint type: |
| 46 | +$someConstraint->getFor(); |
| 47 | +``` |
| 48 | + |
| 49 | +## Type |
| 50 | + |
| 51 | +Constraints have a specific type, e.g. `UNIQUE`. These types depend on the database as well as the database version. |
| 52 | + |
| 53 | +!> **Important**: Depending on the database, not all constraint types are available for nodes as well as relationships. |
| 54 | + |
| 55 | +```php |
| 56 | +// set the type of constraint: |
| 57 | +$someConstraint->setType('UNIQUE'); |
| 58 | + |
| 59 | +// get the type of constraint: |
| 60 | +$someConstraint->getType(); |
| 61 | +``` |
| 62 | + |
| 63 | +## Properties |
14 | 64 |
|
15 |
| -## Examples |
| 65 | +Constraints can specify the properties of a node/relationship on which they should act. |
| 66 | + |
| 67 | +!> **Note**: Most of the time only the property names are important. Setting the property values to null is therefore |
| 68 | +ok. |
16 | 69 |
|
17 | 70 | ```php
|
18 |
| -use Syndesi\CypherDataStructures\Type\ConstraintName; |
19 |
| -use Syndesi\CypherDataStructures\Type\Constraint; |
20 |
| -use Syndesi\CypherDataStructures\Type\ConstraintType; |
21 |
| -use Syndesi\CypherDataStructures\Type\NodeLabel; |
22 |
| -use Syndesi\CypherDataStructures\Type\PropertyName; |
23 |
| - |
24 |
| -$constraint = new Constraint(); |
25 |
| -$constraint |
26 |
| - ->setConstraintName(new ConstraintName('some_name')) |
27 |
| - ->setConstraintType(ConstraintType::UNIQUE) |
28 |
| - ->setFor(new NodeLabel('SomeNode')) |
29 |
| - ->addProperty(new PropertyName('id')); |
| 71 | +// add property to a constraint with default value null: |
| 72 | +$someConstraint->addProperty('propertyName'); |
| 73 | + |
| 74 | +// add multiple properties to a constraint: |
| 75 | +$someConstraint->addProperties([ |
| 76 | + 'id' => null, |
| 77 | + 'hello' => 'world :D' |
| 78 | +]); |
| 79 | + |
| 80 | +// check if a constraint has a specific property: |
| 81 | +$someConstraint->hasProperty('id'); |
| 82 | + |
| 83 | +// get the value of a specific property: |
| 84 | +$someConstraint->getProperty('id'); |
| 85 | + |
| 86 | +// get all properties from a constraint: |
| 87 | +$someConstraint->getProperties(); |
| 88 | + |
| 89 | +// remove a specific property from a constraint: |
| 90 | +$someConstraint->removeProperty('hello'); |
| 91 | + |
| 92 | +// remove all properties from a constraint: |
| 93 | +$someConstraint->removeProperties(); |
30 | 94 | ```
|
| 95 | + |
| 96 | +## Options |
| 97 | + |
| 98 | +WIP |
0 commit comments