@cubicweb/client 2.1.1 → 3.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +28 -16
  2. package/lib/api/Api.d.ts +34 -14
  3. package/lib/api/Api.d.ts.map +1 -1
  4. package/lib/api/Api.js +82 -44
  5. package/lib/api/Api.js.map +1 -1
  6. package/lib/api/Client.d.ts +17 -6
  7. package/lib/api/Client.d.ts.map +1 -1
  8. package/lib/api/Client.js +30 -11
  9. package/lib/api/Client.js.map +1 -1
  10. package/lib/api/Transaction.d.ts +33 -20
  11. package/lib/api/Transaction.d.ts.map +1 -1
  12. package/lib/api/Transaction.js +44 -37
  13. package/lib/api/Transaction.js.map +1 -1
  14. package/lib/schema/classes/EntitySchema.d.ts +16 -22
  15. package/lib/schema/classes/EntitySchema.d.ts.map +1 -1
  16. package/lib/schema/classes/EntitySchema.js +17 -25
  17. package/lib/schema/classes/EntitySchema.js.map +1 -1
  18. package/lib/schema/classes/RelationDefinitionSchema.d.ts +8 -10
  19. package/lib/schema/classes/RelationDefinitionSchema.d.ts.map +1 -1
  20. package/lib/schema/classes/RelationDefinitionSchema.js +7 -21
  21. package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -1
  22. package/lib/schema/classes/Schema.d.ts +2 -0
  23. package/lib/schema/classes/Schema.d.ts.map +1 -1
  24. package/lib/schema/classes/Schema.js +2 -0
  25. package/lib/schema/classes/Schema.js.map +1 -1
  26. package/lib/schema/classes/Utils.d.ts +135 -1
  27. package/lib/schema/classes/Utils.d.ts.map +1 -1
  28. package/lib/schema/classes/Utils.js +165 -3
  29. package/lib/schema/classes/Utils.js.map +1 -1
  30. package/lib/schema/raw/Constraints.d.ts +17 -9
  31. package/lib/schema/raw/Constraints.d.ts.map +1 -1
  32. package/lib/schema/raw/Entities.d.ts +4 -0
  33. package/lib/schema/raw/Entities.d.ts.map +1 -1
  34. package/lib/schema/raw/RelationDefinitions.d.ts +15 -2
  35. package/lib/schema/raw/RelationDefinitions.d.ts.map +1 -1
  36. package/lib/schema/raw/Schema.d.ts +2 -0
  37. package/lib/schema/raw/Schema.d.ts.map +1 -1
  38. package/lib/schema/raw/Utils.d.ts +6 -0
  39. package/lib/schema/raw/Utils.d.ts.map +1 -1
  40. package/package.json +4 -8
  41. package/src/api/Api.ts +107 -54
  42. package/src/api/Client.ts +33 -20
  43. package/src/api/Transaction.ts +56 -37
  44. package/src/schema/classes/EntitySchema.ts +35 -41
  45. package/src/schema/classes/RelationDefinitionSchema.ts +10 -25
  46. package/src/schema/classes/Schema.ts +2 -0
  47. package/src/schema/classes/Utils.ts +178 -1
  48. package/src/schema/raw/Constraints.ts +20 -8
  49. package/src/schema/raw/Entities.ts +4 -0
  50. package/src/schema/raw/RelationDefinitions.ts +15 -2
  51. package/src/schema/raw/Schema.ts +2 -0
  52. package/src/schema/raw/Utils.ts +6 -0
@@ -7,12 +7,26 @@ import {
7
7
  import { Schema } from "./Schema";
8
8
  import { EntitySchema } from "./EntitySchema";
9
9
 
10
+ // This prefix is often applied to relation types as convention
11
+ // indicating a relation is viewed from the object's point of view
12
+ // We do not export it and instead encourage users to use the utility functions
13
+ const REVERSE_INDICATOR = "reverse_";
14
+
15
+ /**
16
+ * An entity role in a relation.
17
+ *
18
+ * @category Utils
19
+ */
20
+ export type EntityRole = "subject" | "object";
21
+
10
22
  /**
11
23
  * Returns the given schema using loose typing.
12
24
  * Can be useful when working with generic schemas
13
25
  *
14
26
  * @param schema The schema to create a loose type for
15
27
  * @returns A loosely typed schema
28
+ *
29
+ * @category Utils
16
30
  */
17
31
  export const getLooseSchema = <
18
32
  E extends EntityRawSchemaArray = EntityRawSchemaArray,
@@ -28,13 +42,174 @@ export const getLooseSchema = <
28
42
  *
29
43
  * @param cardinality The cardinality to check
30
44
  * @returns True if the cardinality is 1 or +
45
+ *
46
+ * @category Utils
31
47
  */
32
- export function cardinalityRequired(cardinality: Cardinality): boolean {
48
+ export function isCardinalityRequired(cardinality: Cardinality): boolean {
33
49
  return cardinality === "1" || cardinality === "+";
34
50
  }
35
51
 
52
+ /**
53
+ * Checks if the cardinality allows multiple entities to be present
54
+ *
55
+ * @param cardinality The cardinility to check
56
+ * @returns True if the cardinality is `*` or `+`
57
+ *
58
+ * @category Utils
59
+ */
60
+ export const isCardinalityMultiple = (cardinality: Cardinality): boolean => {
61
+ return cardinality === "*" || cardinality === "+";
62
+ };
63
+
64
+ /**
65
+ * Checks if the cardinality allows entities to be absent
66
+ *
67
+ * @param cardinality The cardinility to check
68
+ * @returns True if the cardinality is `?` or `*`
69
+ *
70
+ * @category Utils
71
+ */
72
+ export const isCardinalityOptional = (cardinality: Cardinality): boolean => {
73
+ return cardinality === "?" || cardinality === "*";
74
+ };
75
+
76
+ /**
77
+ * Reverses the given role name
78
+ *
79
+ * @param role The role to reverse
80
+ * @returns "object" if the given role was "subject", "subject" otherwise
81
+ *
82
+ * @category Utils
83
+ */
84
+ export function getOppositeRelationRole(role: EntityRole): EntityRole {
85
+ return role === "subject" ? "object" : "subject";
86
+ }
87
+
88
+ /**
89
+ * Checks if the given relation type is reversed.
90
+ *
91
+ * A reversed relation means the relation is considered from the object entity
92
+ * point of view.
93
+ * The reverse indicator is a prefix added as a convention in CubicWeb.
94
+ *
95
+ * @param relationType The relation type to check
96
+ * @returns True if the relation type starts with the reverse indicator
97
+ *
98
+ * @category Utils
99
+ */
100
+ export const isRelationTypeReversed = (relationType: string): boolean => {
101
+ return relationType.startsWith(REVERSE_INDICATOR);
102
+ };
103
+
104
+ /**
105
+ * Gets the entity role in the given relation type by
106
+ * checking for the reverse indicator
107
+ *
108
+ * A reversed relation means the relation is considered from the object entity
109
+ * point of view.
110
+ * The reverse indicator is a prefix added as a convention in CubicWeb.
111
+ *
112
+ * @param relationType The relation type to check
113
+ * @returns "subject" if the relation is not reversed, "object" otherwise
114
+ *
115
+ * @category Utils
116
+ */
117
+ export const getRoleFromRelationType = (relationType: string): EntityRole => {
118
+ return isRelationTypeReversed(relationType) ? "object" : "subject";
119
+ };
120
+
121
+ /**
122
+ * Adds the reverse indicator from the relation type if it is not already present.
123
+ *
124
+ * A reversed relation means the relation is considered from the object entity
125
+ * point of view.
126
+ * The reverse indicator is a prefix added as a convention in CubicWeb.
127
+ *
128
+ * @param relationType The relation type to transform
129
+ * @returns The relation type with the reverse indicator added or removed
130
+ *
131
+ * @category Utils
132
+ */
133
+ export const getReverseRelationType = (relationType: string): string => {
134
+ if (!isRelationTypeReversed(relationType)) {
135
+ return `${REVERSE_INDICATOR}${relationType}`;
136
+ } else {
137
+ return relationType;
138
+ }
139
+ };
140
+
141
+ /**
142
+ * Clears the reverse indicator from the relation type if it is present.
143
+ *
144
+ * A reversed relation means the relation is considered from the object entity
145
+ * point of view.
146
+ * The reverse indicator is a prefix added as a convention in CubicWeb.
147
+ *
148
+ * @param relationType The relation type to transform
149
+ * @returns The relation type with the reverse indicator added or removed
150
+ *
151
+ * @category Utils
152
+ */
153
+ export const getRawRelationType = (relationType: string): string => {
154
+ if (isRelationTypeReversed(relationType)) {
155
+ return relationType.replace(REVERSE_INDICATOR, "");
156
+ } else {
157
+ return relationType;
158
+ }
159
+ };
160
+
161
+ /**
162
+ * Returns the relation type with the reverse indicator added if necessary.
163
+ *
164
+ * A reversed relation means the relation is considered from the object entity
165
+ * point of view.
166
+ * The reverse indicator is a prefix added as a convention in CubicWeb.
167
+ *
168
+ * @param relationType The relation type to transform
169
+ * @param role The entity role in the relation
170
+ * @returns A relation type with the reverse indicator if necessary
171
+ *
172
+ * @category Utils
173
+ */
174
+ export const getRelationType = (relationType: string, role: EntityRole) => {
175
+ return role === "subject"
176
+ ? relationType
177
+ : getReverseRelationType(relationType);
178
+ };
179
+
180
+ /**
181
+ * Finds the relation matching the provided source, relation typ and target
182
+ *
183
+ * @param entitySchema The entity schema the relation is from
184
+ * @param sourceType The entity type acting as source for the relation
185
+ * @param relationType The type of the relation to use. Can have the reverse indicator to decide the role of the source/target entities
186
+ * @param targetType The entity type acting as target for the relation
187
+ * @returns The relation definition matching the triplet source relation target
188
+ *
189
+ * @category Utils
190
+ */
191
+ export const getRelationDefinition = (
192
+ entitySchema: EntitySchema,
193
+ sourceType: string,
194
+ relationType: string,
195
+ targetType: string
196
+ ) => {
197
+ const role = getRoleFromRelationType(relationType);
198
+ const match = entitySchema.matchRelationDefinitions({
199
+ [role]: sourceType,
200
+ type: getRawRelationType(relationType),
201
+ [getOppositeRelationRole(role)]: targetType,
202
+ });
203
+ if (match.length > 0) {
204
+ return match[0];
205
+ }
206
+ return undefined;
207
+ };
208
+
36
209
  /**
37
210
  * Partial raw relation definition schema type.
211
+ *
212
+ * @category Utils
38
213
  */
39
214
  export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<
40
215
  RelationDefinitionRawSchema<E>
@@ -45,6 +220,8 @@ export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<
45
220
  * If it finds a relation definition matching the partial one given, it will return its type.
46
221
  * If it cannot find any relation definition matching, it will return the `never` type
47
222
  * and TypeScript will give compile errors.
223
+ *
224
+ * @category Utils
48
225
  */
49
226
  export type MatchedRelationDefinition<
50
227
  E extends EntityRawSchemaArray,
@@ -10,7 +10,7 @@ type ConstraintBase<
10
10
  };
11
11
 
12
12
  /**
13
- * THe type of a constraint message when one is present.
13
+ * The type of a constraint message when one is present.
14
14
  */
15
15
  type ConstraintMessage = string | null;
16
16
 
@@ -33,6 +33,8 @@ export type RegexpConstraint = ConstraintBase<
33
33
  * Reference to an attribute used in constraints.
34
34
  *
35
35
  * @property \_\_attribute\_\_ name of the attribute in the same entity
36
+ *
37
+ * @category Schema
36
38
  */
37
39
  export interface Attribute {
38
40
  __attribute__: string;
@@ -50,6 +52,8 @@ export interface TimeDelta {
50
52
  * @property \_\_today\_\_ always true
51
53
  * @property offset delta to current date at the time of the check
52
54
  * @property type name of the Python class used to do the comparison
55
+ *
56
+ * @category Schema
53
57
  */
54
58
  export interface Today {
55
59
  __today__: true;
@@ -62,32 +66,36 @@ export interface Today {
62
66
  *
63
67
  * @property \_\_now\_\_ always true
64
68
  * @property offset delta to current date at the time of the check
69
+ *
70
+ * @category Schema
65
71
  */
66
72
  export interface Now {
67
73
  __now__: true;
68
- offset: TimeDelta;
74
+ offset: TimeDelta | null;
69
75
  }
70
76
 
71
77
  /**
72
78
  * All the possible operators inside a constraint.
79
+ *
80
+ * @category Schema
73
81
  */
74
82
  export type ConstraintOperator = ">" | ">=" | "<" | "<=";
75
83
 
76
- /**
77
- * @property value.boundary boudary value to compare the attribut
78
- *
79
- */
80
84
  export type BoundaryConstraint = ConstraintBase<
81
85
  "BoundaryConstraint",
82
86
  {
83
- boundary: Attribute | Today | Now | number | null;
87
+ boundary: Attribute | Today | Now | number | string | null;
84
88
  op: ConstraintOperator;
85
89
  msg: ConstraintMessage;
86
90
  }
87
91
  >;
88
92
  export type IntervalBoundConstraint = ConstraintBase<
89
93
  "IntervalBoundConstraint",
90
- { minvalue: number | null; maxvalue: number | null; msg: ConstraintMessage }
94
+ {
95
+ minvalue: number | string | null;
96
+ maxvalue: number | string | null;
97
+ msg: ConstraintMessage;
98
+ }
91
99
  >;
92
100
 
93
101
  export type StaticVocabularyConstraint = ConstraintBase<
@@ -115,6 +123,8 @@ export type RQLConstraint = ConstraintBase<
115
123
 
116
124
  /**
117
125
  * Type representing all possible constraints for attributes.
126
+ *
127
+ * @category Schema
118
128
  */
119
129
  export type AttributeConstraint = Readonly<
120
130
  | UniqueConstraint
@@ -129,6 +139,8 @@ export type AttributeConstraint = Readonly<
129
139
 
130
140
  /**
131
141
  * Type representing all possible constraints for relations.
142
+ *
143
+ * @category Schema
132
144
  */
133
145
  export type RelationConstraint = Readonly<
134
146
  RQLVocabularyConstraint | RQLConstraint
@@ -1,5 +1,7 @@
1
1
  /**
2
2
  * Type representing an entity schema.
3
+ *
4
+ * @category Schema
3
5
  */
4
6
  export type EntityRawSchema = {
5
7
  type: string;
@@ -9,5 +11,7 @@ export type EntityRawSchema = {
9
11
 
10
12
  /**
11
13
  * Type representing a list of entity schemas.
14
+ *
15
+ * @category Schema
12
16
  */
13
17
  export type EntityRawSchemaArray = ReadonlyArray<EntityRawSchema>;
@@ -1,13 +1,18 @@
1
+ import { EntityRole } from "../classes/Utils";
1
2
  import { AttributeConstraint, RelationConstraint } from "./Constraints";
2
3
  import { EntityRawSchema, EntityRawSchemaArray } from "./Entities";
3
4
 
4
5
  /**
5
6
  * Type representing all the possible cardinalities in CubicWeb.
7
+ *
8
+ * @category Schema
6
9
  */
7
10
  export type Cardinality = "*" | "1" | "?" | "+";
8
11
 
9
12
  /**
10
13
  * Type representing a cardinality pair as used in relation definitions.
14
+ *
15
+ * @category Schema
11
16
  */
12
17
  export type CardinalityPair = `${Cardinality}${Cardinality}`;
13
18
 
@@ -109,6 +114,8 @@ export type BuildObj = Readonly<
109
114
 
110
115
  /**
111
116
  * Type representing the possible options for an attribute.
117
+ *
118
+ * @category Schema
112
119
  */
113
120
  export type AttributeOptions = {
114
121
  indexed?: boolean;
@@ -118,12 +125,14 @@ export type AttributeOptions = {
118
125
 
119
126
  /**
120
127
  * Type representing the possible options for a relation definition.
128
+ *
129
+ * @category Schema
121
130
  */
122
131
  export type RelationOptions = {
123
132
  symmetric?: boolean;
124
133
  inlined?: boolean;
125
- fulltext_container?: "subject" | "object";
126
- composite?: "subject" | "object";
134
+ fulltext_container?: EntityRole;
135
+ composite?: EntityRole;
127
136
  };
128
137
 
129
138
  /**
@@ -131,6 +140,8 @@ export type RelationOptions = {
131
140
  * It can represent both attributes and relations depending on the value of Final.
132
141
  *
133
142
  * It can infer default values type depending on the object type if the relation is final.
143
+ *
144
+ * @category Schema
134
145
  */
135
146
  export type RelationDefinitionRawSchema<
136
147
  ESchemas extends EntityRawSchemaArray,
@@ -157,6 +168,8 @@ export type RelationDefinitionRawSchema<
157
168
 
158
169
  /**
159
170
  * Type representing a list of relation definitions.
171
+ *
172
+ * @category Schema
160
173
  */
161
174
  export type RelationDefinitionRawSchemaArray<
162
175
  ESchemas extends EntityRawSchemaArray
@@ -6,6 +6,8 @@ import { RelationDefinitionRawSchemaArray } from "./RelationDefinitions";
6
6
  * You can export this schema from your CubicWeb instance using the command:
7
7
  *
8
8
  * `cubicweb-ctl export-schema -f typescript <YOUR_INSTANCE_NAME>`
9
+ *
10
+ * @category Schema
9
11
  */
10
12
  export type RawSchema<
11
13
  E extends EntityRawSchemaArray = EntityRawSchemaArray,
@@ -6,6 +6,8 @@ import { EntityRawSchemaArray } from "./Entities";
6
6
  *
7
7
  * In the given EntityRawSchemaArray, if the entities type is a string, the schema is loosely typed.
8
8
  * If the schema is strongly typed, the entities type would be a word (which is stricter than string).
9
+ *
10
+ * @category Schema
9
11
  */
10
12
  export type IsLooseSchema<E extends EntityRawSchemaArray> =
11
13
  string extends E[number]["type"] ? true : false;
@@ -13,6 +15,8 @@ export type IsLooseSchema<E extends EntityRawSchemaArray> =
13
15
  /**
14
16
  * Checks if the given entity type is present in the EntityRawSchemaArray.
15
17
  * Use this to create conditional types.
18
+ *
19
+ * @category Schema
16
20
  */
17
21
  export type IsETypeInStrictSchema<
18
22
  E extends EntityRawSchemaArray,
@@ -22,6 +26,8 @@ export type IsETypeInStrictSchema<
22
26
  /**
23
27
  * Type representing the entity's raw schema matching this type.
24
28
  * This type only works for EntityRawSchemaArray with literals as "type" property.
29
+ *
30
+ * @category Schema
25
31
  */
26
32
  export type EntityRawSchemaByType<
27
33
  E extends EntityRawSchemaArray,