forked from Nhogs/nestjs-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.decorator.ts
88 lines (83 loc) · 3.41 KB
/
constraint.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Neo4jMetadataStorage } from '../storage';
/**
* See Neo4j constraint documentation for details:
*
* https://neo4j.com/docs/cypher-manual/current/constraints/
*/
export type ConstraintOptions = {
name?: string;
ifNotExists?: boolean;
additionalKeys?: string[];
useCommonName?: boolean;
};
export function Constraint(
constraintType: string,
options?: ConstraintOptions,
): PropertyDecorator {
return (target: Object, propertyKey: string | symbol) => {
Neo4jMetadataStorage.addPropertyConstraint({
target: target.constructor,
property: propertyKey.toString(),
constraintType,
options: {
...options,
ifNotExists: options?.ifNotExists !== false,
useCommonName: options?.useCommonName !== false,
},
});
};
}
/**
* Node key constraints.
*
* Node key constraints ensure that, for a given label and set of properties:
* All the properties exist on all the nodes with that label.
* The combination of the property values is unique.
*
* CREATE CONSTRAINT FOR (p:Person) REQUIRE (p.firstname, p.surname) IS NODE KEY
* CREATE CONSTRAINT node_key FOR (p:Person) REQUIRE p.firstname IS NODE KEY
*/
export function NodeKey(options?: ConstraintOptions): PropertyDecorator {
return Constraint('IS NODE KEY', options);
}
/**
* Unique node property constraints.
*
* Unique property constraints ensure that property values are unique for all nodes with a specific label.
* For unique property constraints on multiple properties, the combination of the property values is unique.
* Unique constraints do not require all nodes to have a unique value for the properties listed—nodes without all properties are not subject to this rule.
*
* CREATE CONSTRAINT FOR (p:Person) REQUIRE p.name IS UNIQUE
* CREATE CONSTRAINT uniqueness FOR (p:Person) REQUIRE (p.firstname, p.age) IS UNIQUE
*
* Queries attempting to do any of the following will fail:
* - Create new nodes without all the properties or where the combination of property values is not unique.
* - Remove one of the mandatory properties.
* - Update the properties so that the combination of property values is no longer unique.
*/
export function Unique(options?: ConstraintOptions): PropertyDecorator {
return Constraint('IS UNIQUE', options);
}
/**
* - Node property existence constraints.
*
* Node property existence constraints ensure that a property exists for all nodes with a specific label.
* Queries that try to create new nodes of the specified label, but without this property, will fail.
* The same is true for queries that try to remove the mandatory property.
*
* CREATE CONSTRAINT FOR (p:Person) REQUIRE p.name IS NOT NULL
* CREATE CONSTRAINT node_exists IF NOT EXISTS FOR (p:Person) REQUIRE p.name IS NOT NULL * @param options
*
* - Relationship property existence constraints
* Property existence constraints ensure that a property exists for all relationships with a specific type.
* All queries that try to create relationships of the specified type, but without this property, will fail.
* The same is true for queries that try to remove the mandatory property.
*
* CREATE CONSTRAINT FOR ()-[l:LIKED]-() REQUIRE l.when IS NOT NULL
* CREATE CONSTRAINT relationship_exists FOR ()-[l:LIKED]-() REQUIRE l.since IS NOT NULL
*/
export function NotNull(
options?: Omit<ConstraintOptions, 'additionalKeys'>,
): PropertyDecorator {
return Constraint('IS NOT NULL', options);
}