@cubicweb/client 1.0.3 → 1.1.1

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.
@@ -1,5 +1,5 @@
1
1
  import { Cardinality } from "..";
2
- import { InconsistentSchemaError, RelationNotFoundError } from "../Errors";
2
+ import { InconsistentSchemaError } from "../Errors";
3
3
  import {
4
4
  RawSchema,
5
5
  RelationDefinitionRawSchema,
@@ -186,71 +186,19 @@ export class EntitySchema<
186
186
  }
187
187
 
188
188
  /**
189
- * Finds the first relation of the given type with this entity as subject
190
- *
191
- * @param relationType The relation type to find
192
- * @throws RelationNotFoundError if the current entity is not a subject of the given relation
193
- * @returns The found relation
194
- */
195
- getSubjectRelationDefinition<
196
- RelationType extends MatchedRelationDefinition<
197
- E,
198
- R,
199
- { subject: ERawSchema["type"] }
200
- >["type"]
201
- >(
202
- relationType: RelationType
203
- ): RelationDefinitionSchema<
204
- E,
205
- R,
206
- MatchedRelationDefinition<
207
- E,
208
- R,
209
- { subject: ERawSchema["type"]; type: RelationType }
210
- >
211
- > {
212
- const resultArray = this.schema.matchRelationDefinitions({
213
- subject: this.type,
214
- type: relationType,
215
- });
216
- if (resultArray.length > 0) {
217
- return resultArray[0];
218
- }
219
- throw new RelationNotFoundError(relationType, "subject", this.type);
220
- }
221
-
222
- /**
223
- * Finds the first relation of the given type with this entity as object
189
+ * Finds relations in the Schema matching the partial relation given
224
190
  *
225
- * @param relationType The relation type to find
226
- * @throws RelationNotFoundError if the current entity is not an object of the given relation
227
- * @returns The found relation
191
+ * @param matcher Partial relation definition to match against
192
+ * @returns Matched relation schema classes
228
193
  */
229
- getObjectRelationDefinition<
230
- RelationType extends MatchedRelationDefinition<
231
- E,
232
- R,
233
- { object: ERawSchema["type"] }
234
- >["type"]
235
- >(
236
- relationType: RelationType
194
+ matchRelationDefinitions<M extends RelationDefinitionMatcher<E>>(
195
+ matcher: M
237
196
  ): RelationDefinitionSchema<
238
197
  E,
239
198
  R,
240
- MatchedRelationDefinition<
241
- E,
242
- R,
243
- { object: ERawSchema["type"]; type: RelationType }
244
- >
245
- > {
246
- const resultArray = this.schema.matchRelationDefinitions({
247
- object: this.type,
248
- type: relationType,
249
- });
250
- if (resultArray.length > 0) {
251
- return resultArray[0];
252
- }
253
- throw new RelationNotFoundError(relationType, "object", this.type);
199
+ MatchedRelationDefinition<E, R, { subject: ERawSchema["type"] }>
200
+ >[] {
201
+ return this.schema.matchRelationDefinitions(matcher);
254
202
  }
255
203
 
256
204
  /**
@@ -262,32 +210,6 @@ export class EntitySchema<
262
210
  subject: this.type,
263
211
  });
264
212
  }
265
-
266
- /**
267
- * Checks whether the current entity is required for the given relation and role
268
- *
269
- * @param relationType The relation to check
270
- * @param role The role for this entity in the given relation
271
- * @returns True if this entity is required in the relation for the given role
272
- */
273
- isRelationRequired<
274
- RelationType extends MatchedRelationDefinition<
275
- E,
276
- R,
277
- { subject: ERawSchema["type"] } | { object: ERawSchema["type"] }
278
- >["type"]
279
- >(relationType: RelationType, role: "subject" | "object"): boolean {
280
- let relation;
281
- let cardinality: Cardinality;
282
- if (role === "subject") {
283
- relation = this.getSubjectRelationDefinition(relationType);
284
- cardinality = relation.cardinality[0];
285
- } else {
286
- relation = this.getObjectRelationDefinition(relationType);
287
- cardinality = relation.cardinality[1];
288
- }
289
- return cardinalityRequired(cardinality);
290
- }
291
213
  }
292
214
 
293
215
  /**
@@ -391,6 +313,22 @@ export class RelationDefinitionSchema<
391
313
  // @ts-ignore
392
314
  return objectSchema;
393
315
  }
316
+
317
+ /**
318
+ * Checks if the relation is required when using the subject or object pointof view
319
+ *
320
+ * @param role The point of view to use
321
+ * @returns
322
+ */
323
+ isRequired(role: "subject" | "object") {
324
+ let cardinality: Cardinality;
325
+ if (role === "subject") {
326
+ cardinality = this.cardinality[0] as Cardinality;
327
+ } else {
328
+ cardinality = this.cardinality[1] as Cardinality;
329
+ }
330
+ return cardinalityRequired(cardinality);
331
+ }
394
332
  }
395
333
 
396
334
  /**