@cubicweb/client 1.4.0 → 1.5.0

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 (74) hide show
  1. package/lib/api/Api.d.ts +87 -3
  2. package/lib/api/Api.d.ts.map +1 -1
  3. package/lib/api/Api.js +99 -10
  4. package/lib/api/Api.js.map +1 -1
  5. package/lib/api/Client.d.ts +39 -1
  6. package/lib/api/Client.d.ts.map +1 -1
  7. package/lib/api/Client.js +41 -1
  8. package/lib/api/Client.js.map +1 -1
  9. package/lib/api/Transaction.d.ts +42 -0
  10. package/lib/api/Transaction.d.ts.map +1 -1
  11. package/lib/api/Transaction.js +42 -0
  12. package/lib/api/Transaction.js.map +1 -1
  13. package/lib/index.d.ts +9 -3
  14. package/lib/index.d.ts.map +1 -1
  15. package/lib/index.js +13 -9
  16. package/lib/index.js.map +1 -1
  17. package/lib/schema/classes/EntitySchema.d.ts +71 -0
  18. package/lib/schema/classes/EntitySchema.d.ts.map +1 -0
  19. package/lib/schema/classes/EntitySchema.js +83 -0
  20. package/lib/schema/classes/EntitySchema.js.map +1 -0
  21. package/lib/schema/classes/RelationDefinitionSchema.d.ts +87 -0
  22. package/lib/schema/classes/RelationDefinitionSchema.d.ts.map +1 -0
  23. package/lib/schema/classes/RelationDefinitionSchema.js +128 -0
  24. package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -0
  25. package/lib/schema/classes/Schema.d.ts +54 -0
  26. package/lib/schema/classes/Schema.d.ts.map +1 -0
  27. package/lib/schema/classes/Schema.js +75 -0
  28. package/lib/schema/classes/Schema.js.map +1 -0
  29. package/lib/schema/classes/Utils.d.ts +30 -0
  30. package/lib/schema/classes/Utils.d.ts.map +1 -0
  31. package/lib/schema/classes/Utils.js +25 -0
  32. package/lib/schema/classes/Utils.js.map +1 -0
  33. package/lib/schema/raw/Constraints.d.ts +58 -0
  34. package/lib/schema/raw/Constraints.d.ts.map +1 -0
  35. package/lib/schema/raw/Constraints.js +3 -0
  36. package/lib/schema/raw/Constraints.js.map +1 -0
  37. package/lib/schema/raw/Entities.d.ts +13 -0
  38. package/lib/schema/raw/Entities.d.ts.map +1 -0
  39. package/lib/schema/{SchemaJSON.js → raw/Entities.js} +1 -1
  40. package/lib/schema/raw/Entities.js.map +1 -0
  41. package/lib/schema/raw/RelationDefinitions.d.ts +113 -0
  42. package/lib/schema/raw/RelationDefinitions.d.ts.map +1 -0
  43. package/lib/schema/raw/RelationDefinitions.js +3 -0
  44. package/lib/schema/raw/RelationDefinitions.js.map +1 -0
  45. package/lib/schema/raw/Schema.d.ts +13 -0
  46. package/lib/schema/raw/Schema.d.ts.map +1 -0
  47. package/lib/schema/raw/Schema.js +3 -0
  48. package/lib/schema/raw/Schema.js.map +1 -0
  49. package/lib/schema/raw/Utils.d.ts +22 -0
  50. package/lib/schema/raw/Utils.d.ts.map +1 -0
  51. package/lib/schema/raw/Utils.js +3 -0
  52. package/lib/schema/raw/Utils.js.map +1 -0
  53. package/package.json +1 -1
  54. package/src/api/Api.ts +128 -15
  55. package/src/api/Client.ts +42 -1
  56. package/src/api/Transaction.ts +42 -0
  57. package/src/index.ts +15 -8
  58. package/src/schema/classes/EntitySchema.ts +125 -0
  59. package/src/schema/classes/RelationDefinitionSchema.ts +156 -0
  60. package/src/schema/classes/Schema.ts +122 -0
  61. package/src/schema/classes/Utils.ts +52 -0
  62. package/src/schema/raw/Constraints.ts +77 -0
  63. package/src/schema/raw/Entities.ts +13 -0
  64. package/src/schema/{SchemaJSON.ts → raw/RelationDefinitions.ts} +36 -101
  65. package/src/schema/raw/Schema.ts +16 -0
  66. package/src/schema/raw/Utils.ts +33 -0
  67. package/lib/schema/Schema.d.ts +0 -156
  68. package/lib/schema/Schema.d.ts.map +0 -1
  69. package/lib/schema/Schema.js +0 -246
  70. package/lib/schema/Schema.js.map +0 -1
  71. package/lib/schema/SchemaJSON.d.ts +0 -140
  72. package/lib/schema/SchemaJSON.d.ts.map +0 -1
  73. package/lib/schema/SchemaJSON.js.map +0 -1
  74. package/src/schema/Schema.ts +0 -371
@@ -1,140 +0,0 @@
1
- declare type ConstraintBase<T extends string, V extends Record<string, unknown> | null> = {
2
- type: T;
3
- value: V;
4
- };
5
- export declare type ConstraintOperator = ">" | ">=" | "<" | "<=";
6
- declare type ConstraintMessage = string | null;
7
- export declare type UniqueConstraint = ConstraintBase<"UniqueConstraint", null>;
8
- export declare type SizeConstraint = ConstraintBase<"SizeConstraint", {
9
- min: number | null;
10
- max: number | null;
11
- msg: ConstraintMessage;
12
- }>;
13
- export declare type RegexpConstraint = ConstraintBase<"RegexpConstraint", {
14
- regexp: string;
15
- flags: number;
16
- msg: ConstraintMessage;
17
- }>;
18
- export declare type BoundaryConstraint = ConstraintBase<"BoundaryConstraint", {
19
- boundary: number | null;
20
- op: ConstraintOperator;
21
- msg: ConstraintMessage;
22
- }>;
23
- export declare type IntervalBoundConstraint = ConstraintBase<"IntervalBoundConstraint", {
24
- minvalue: number | null;
25
- maxvalue: number | null;
26
- msg: ConstraintMessage;
27
- }>;
28
- export declare type StaticVocabularyConstraint = ConstraintBase<"StaticVocabularyConstraint", {
29
- values: string[];
30
- msg: ConstraintMessage;
31
- }>;
32
- export declare type FormatConstraint = ConstraintBase<"FormatConstraint", {
33
- values: ["text/rest", "text/markdown", "text/html", "text/plain"];
34
- msg: ConstraintMessage;
35
- }>;
36
- export declare type RQLVocabularyConstraint = ConstraintBase<"RQLVocabularyConstraint", {
37
- expression: string;
38
- mainvars: string[];
39
- }>;
40
- export declare type AttributeConstraint = Readonly<UniqueConstraint | SizeConstraint | RegexpConstraint | BoundaryConstraint | IntervalBoundConstraint | StaticVocabularyConstraint | FormatConstraint>;
41
- export declare type RelationConstraint = Readonly<RQLVocabularyConstraint>;
42
- declare type BuildObjBase<T extends string> = {
43
- object: T;
44
- final: true;
45
- required?: boolean;
46
- };
47
- interface StringBuildObj extends BuildObjBase<"String"> {
48
- default: string | null;
49
- }
50
- interface PasswordBuildObj extends BuildObjBase<"Password"> {
51
- default: string | null;
52
- }
53
- interface BytesBuildObj extends BuildObjBase<"Bytes"> {
54
- default: string | null;
55
- }
56
- interface IntBuildObj extends BuildObjBase<"Int"> {
57
- default: number | null;
58
- }
59
- interface BigIntBuildObj extends BuildObjBase<"BigInt"> {
60
- default: number | null;
61
- }
62
- interface FloatBuildObj extends BuildObjBase<"Float"> {
63
- default: number | null;
64
- }
65
- interface BooleanBuildObj extends BuildObjBase<"Boolean"> {
66
- default: boolean | null;
67
- }
68
- interface DecimalBuildObj extends BuildObjBase<"Decimal"> {
69
- default: number | null;
70
- }
71
- interface DateBuildObj extends BuildObjBase<"Date"> {
72
- default: unknown;
73
- }
74
- interface TimeBuildObj extends BuildObjBase<"Time"> {
75
- default: unknown;
76
- }
77
- interface DatetimeBuildObj extends BuildObjBase<"Datetime"> {
78
- default: Date | "NOW" | "TODAY" | null;
79
- }
80
- interface TZTimeBuildObj extends BuildObjBase<"TZTime"> {
81
- default: unknown;
82
- }
83
- interface TZDatetimeBuildObj extends BuildObjBase<"TZDatetime"> {
84
- default: unknown;
85
- }
86
- interface IntervalBuildObj extends BuildObjBase<"Interval"> {
87
- default: unknown;
88
- }
89
- export declare type BuildObj = Readonly<StringBuildObj | PasswordBuildObj | BytesBuildObj | IntBuildObj | BigIntBuildObj | FloatBuildObj | BooleanBuildObj | DecimalBuildObj | DateBuildObj | TimeBuildObj | DatetimeBuildObj | TZTimeBuildObj | TZDatetimeBuildObj | IntervalBuildObj>;
90
- export declare type Cardinality = "*" | "1" | "?" | "+";
91
- export declare type CardinalityPair = `${Cardinality}${Cardinality}`;
92
- export declare type EntityRawSchema = {
93
- type: string;
94
- final: boolean;
95
- description: string;
96
- };
97
- export declare type EntityRawSchemaArray = ReadonlyArray<EntityRawSchema>;
98
- export declare type AttributeOptions = {
99
- indexed?: boolean;
100
- fulltextindexed?: boolean;
101
- internationalizable?: boolean;
102
- };
103
- export declare type RelationOptions = {
104
- symmetric?: boolean;
105
- inlined?: boolean;
106
- fulltext_container?: "subject" | "object";
107
- composite?: "subject" | "object";
108
- };
109
- export declare type RelationDefinitionRawSchema<ESchemas extends EntityRawSchemaArray, Final extends boolean = boolean> = Readonly<{
110
- type: string;
111
- description: string;
112
- cardinality: CardinalityPair;
113
- subject: ESchemas[number]["type"];
114
- } & (Final extends true ? BuildObj & {
115
- final: true;
116
- constraints: AttributeConstraint[];
117
- options: AttributeOptions;
118
- } : {
119
- object: EntityRawSchema["type"];
120
- final: false;
121
- constraints: RelationConstraint[];
122
- options: RelationOptions;
123
- })>;
124
- export declare type RelationDefinitionRawSchemaArray<ESchemas extends EntityRawSchemaArray> = ReadonlyArray<RelationDefinitionRawSchema<ESchemas, boolean>>;
125
- export declare type RawSchema<E extends EntityRawSchemaArray = EntityRawSchemaArray, R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E>> = {
126
- entities: E;
127
- relations_definitions: R;
128
- };
129
- export declare type IsLooseSchema<E extends EntityRawSchemaArray> = string extends E[number]["type"] ? true : false;
130
- export declare type IsETypeInStrictSchema<E extends EntityRawSchemaArray, EType extends string> = EType extends E[number]["type"] ? true : false;
131
- /**
132
- * Type representing the entity's raw schema matching this type.
133
- * This type only works for EntityRawSchemaArray with literals as "type" property.
134
- *
135
- **/
136
- export declare type EntityRawSchemaByType<E extends EntityRawSchemaArray, T extends string> = E[number] extends infer ESchema ? ESchema extends {
137
- type: T;
138
- } ? ESchema : never : never;
139
- export {};
140
- //# sourceMappingURL=SchemaJSON.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SchemaJSON.d.ts","sourceRoot":"","sources":["../../src/schema/SchemaJSON.ts"],"names":[],"mappings":"AAAA,aAAK,cAAc,CACjB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,IACtC;IACF,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,CAAC,CAAC;CACV,CAAC;AAEF,oBAAY,kBA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,CAAC,CAAC,SAAS,oBAAoB,IACtD,MAAM,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAElD,oBAAY,qBAAqB,CAC/B,CAAC,SAAS,oBAAoB,EAC9B,KAAK,SAAS,MAAM,IAClB,KAAK,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAEnD;;;;IAII;AACJ,oBAAY,qBAAqB,CAC/B,CAAC,SAAS,oBAAoB,EAC9B,CAAC,SAAS,MAAM,IACd,CAAC,CAAC,MAAM,CAAC,SAAS,MAAM,OAAO,GAC/B,OAAO,SAAS;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,GACzB,OAAO,GACP,KAAK,GACP,KAAK,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"SchemaJSON.js","sourceRoot":"","sources":["../../src/schema/SchemaJSON.ts"],"names":[],"mappings":""}
@@ -1,371 +0,0 @@
1
- import { Cardinality } from "..";
2
- import { InconsistentSchemaError } from "../Errors";
3
- import {
4
- RawSchema,
5
- RelationDefinitionRawSchema,
6
- RelationDefinitionRawSchemaArray,
7
- EntityRawSchemaArray,
8
- EntityRawSchemaByType,
9
- IsLooseSchema,
10
- IsETypeInStrictSchema,
11
- } from "./SchemaJSON";
12
-
13
- const rawSchemaKey: unique symbol = Symbol("Indexer of the rawSchema");
14
-
15
- /**
16
- * Returns the given schema using loose typing.
17
- * Can be useful when working for generic schemas
18
- *
19
- * @param schema The schema to create a loose type for
20
- * @returns
21
- */
22
- export const getLooseSchema = <
23
- E extends EntityRawSchemaArray = EntityRawSchemaArray, // List of all entities
24
- R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E> // List of all relation defintions
25
- >(
26
- schema: Schema<E, R>
27
- ) => {
28
- return schema as unknown as Schema;
29
- };
30
-
31
- /**
32
- * Wrapper around the raw JSON Schema
33
- */
34
- export class Schema<
35
- E extends EntityRawSchemaArray = EntityRawSchemaArray, // List of all entities
36
- R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E> // List of all relation defintions
37
- > {
38
- [rawSchemaKey]: RawSchema<E, R>;
39
-
40
- constructor(rawSchema: RawSchema<E, R>) {
41
- this[rawSchemaKey] = rawSchema;
42
- }
43
-
44
- get entities(): EntitySchema<E, R, E[number]>[] {
45
- return this[rawSchemaKey].entities.map(
46
- (eSchema) => new EntitySchema(this, eSchema)
47
- );
48
- }
49
-
50
- get relationsDefinitions(): RelationDefinitionSchema<E, R, R[number]>[] {
51
- return this[rawSchemaKey].relations_definitions.map(
52
- (rdefSchema) => new RelationDefinitionSchema(this, rdefSchema)
53
- );
54
- }
55
-
56
- /**
57
- * Returns the entity schema for the given entity type if it exists.
58
- * When using TypeScript, this method can detect if the given type does not exist
59
- * in the schema.
60
- *
61
- * @param entityType The entity type string
62
- * @returns A new EntitySchema or null if the type does not exist
63
- */
64
- getEntity<EType extends string>(
65
- entityType: EType
66
- ): IsLooseSchema<E> extends true
67
- ? EntitySchema<E, R, E[number]> | null
68
- : IsETypeInStrictSchema<E, EType> extends true
69
- ? EntitySchema<E, R, EntityRawSchemaByType<E, EType>>
70
- : null {
71
- const entityRawSchema = this[rawSchemaKey].entities.filter(
72
- (eSchema) => eSchema.type === entityType
73
- )[0];
74
- if (!entityRawSchema) {
75
- // Is only null when the given type is not in the schema.
76
- // Thus the return type will be (EntitySchema | null)
77
- // but TypeScript does not understand this
78
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
79
- // @ts-ignore
80
- return null;
81
- }
82
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
83
- // @ts-ignore
84
- return new EntitySchema<E, R, E[number]>(
85
- this,
86
- entityRawSchema as EntityRawSchemaByType<E, EType>
87
- );
88
- }
89
-
90
- /**
91
- * Finds relations in the Schema matching the partial relation given
92
- *
93
- * @param matcher Partial relation definition to match against
94
- * @returns Matched relation schema classes
95
- */
96
- matchRelationDefinitions<M extends RelationDefinitionMatcher<E>>(
97
- matcher: M
98
- ): RelationDefinitionSchema<
99
- E,
100
- R,
101
- MatchedRelationDefinition<E, R, { subject: E[number]["type"] }>
102
- >[] {
103
- const matchArray = this[rawSchemaKey].relations_definitions.filter(
104
- (relDef) => {
105
- for (const key of Object.keys(
106
- matcher
107
- ) as (keyof RelationDefinitionRawSchema<E>)[]) {
108
- if (matcher[key] !== relDef[key]) {
109
- return false;
110
- }
111
- }
112
- return true;
113
- }
114
- ) as MatchedRelationDefinition<
115
- E,
116
- R,
117
- MatchedRelationDefinition<E, R, { subject: E[number]["type"] }>
118
- >[];
119
- return matchArray.map((m) => new RelationDefinitionSchema(this, m));
120
- }
121
- }
122
-
123
- /**
124
- * Wrapper around raw entities from the Schema
125
- */
126
- export class EntitySchema<
127
- E extends EntityRawSchemaArray = EntityRawSchemaArray, // List of all entities
128
- R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E>, // List of all relation defintions
129
- ERawSchema extends E[number] = E[number] // JSON of the represented entity
130
- > {
131
- constructor(
132
- private schema: Schema<E, R>,
133
- private entityRawSchema: ERawSchema
134
- ) {}
135
-
136
- /**
137
- * The type of the entity. This can also be referred to as the entity' name.
138
- */
139
- get type(): ERawSchema["type"] {
140
- return this.entityRawSchema.type;
141
- }
142
-
143
- /**
144
- * A short description which can be used as documentation for the entity.
145
- */
146
- get description(): ERawSchema["description"] {
147
- return this.entityRawSchema.description;
148
- }
149
-
150
- /**
151
- * True if the current entity is a primitive type such as string or int
152
- */
153
- get final(): ERawSchema["final"] {
154
- return this.entityRawSchema.final;
155
- }
156
-
157
- /**
158
- * Gets the list of relation definitions with this entity as subject
159
- */
160
- get subjectRelationDefinitions(): RelationDefinitionSchema<
161
- E,
162
- R,
163
- MatchedRelationDefinition<
164
- E,
165
- R,
166
- { subject: ERawSchema["type"]; final: false }
167
- >
168
- >[] {
169
- return this.schema.matchRelationDefinitions({
170
- subject: this.type,
171
- final: false,
172
- });
173
- }
174
-
175
- /**
176
- * Gets the list of relation definitions with this entity as object
177
- */
178
- get objectRelationsDefinitions(): RelationDefinitionSchema<
179
- E,
180
- R,
181
- MatchedRelationDefinition<
182
- E,
183
- R,
184
- { object: ERawSchema["type"]; final: false }
185
- >
186
- >[] {
187
- return this.schema.matchRelationDefinitions({
188
- object: this.type,
189
- final: false,
190
- });
191
- }
192
-
193
- /**
194
- * Finds relations in the Schema matching the partial relation given
195
- *
196
- * @param matcher Partial relation definition to match against
197
- * @returns Matched relation schema classes
198
- */
199
- matchRelationDefinitions<M extends RelationDefinitionMatcher<E>>(
200
- matcher: M
201
- ): RelationDefinitionSchema<
202
- E,
203
- R,
204
- MatchedRelationDefinition<E, R, { subject: ERawSchema["type"] }>
205
- >[] {
206
- return this.schema.matchRelationDefinitions(matcher);
207
- }
208
-
209
- /**
210
- * Gets the list of attributes names for this entity
211
- */
212
- get attributes(): RelationDefinitionSchema<
213
- E,
214
- R,
215
- MatchedRelationDefinition<
216
- E,
217
- R,
218
- { subject: ERawSchema["type"]; final: true }
219
- >
220
- >[] {
221
- return this.schema.matchRelationDefinitions({
222
- final: true,
223
- subject: this.type,
224
- });
225
- }
226
- }
227
-
228
- /**
229
- * Wrapper around raw relation definitions from the Schema
230
- */
231
- export class RelationDefinitionSchema<
232
- E extends EntityRawSchemaArray = EntityRawSchemaArray, // List of all entities
233
- R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E>, // List of all relation defintions
234
- RDef extends R[number] = R[number] // JSON of the represented relation definition
235
- > {
236
- constructor(private schema: Schema<E, R>, private relDef: RDef) {}
237
-
238
- /**
239
- * The type of the relation. This can also be referred to as the relation's name.
240
- */
241
- get type(): RDef["type"] {
242
- return this.relDef.type;
243
- }
244
-
245
- /**
246
- * A short description which can be used as documentation for the relation.
247
- */
248
- get description(): RDef["description"] {
249
- return this.relDef.description;
250
- }
251
-
252
- /**
253
- * True if the object of the relation is a primitive type.
254
- * In this case the relation iscalled an "attribute"
255
- */
256
- get final(): RDef["final"] {
257
- return this.relDef.final;
258
- }
259
- /**
260
- * The list of options set for this relation
261
- */
262
- get options(): RDef["options"] {
263
- return this.relDef.options;
264
- }
265
-
266
- /**
267
- * The list of constraints if any. Only attributes (relations with final = true) have constraints.
268
- * In other cases this returns null.
269
- */
270
- get constraints(): RDef["constraints"] {
271
- return this.relDef.constraints;
272
- }
273
-
274
- /**
275
- * The default value for this relation.
276
- * It can be used when creating new relations if user did not enter a value (even if required).
277
- */
278
- get default(): RDef extends { default: infer V } ? V : undefined {
279
- type Default = RDef extends { default: infer V } ? V : undefined;
280
-
281
- return (
282
- "default" in this.relDef ? this.relDef.default : undefined
283
- ) as Default;
284
- }
285
-
286
- get cardinality(): RDef["cardinality"] {
287
- return this.relDef.cardinality;
288
- }
289
-
290
- get subjectCardinality(): Cardinality {
291
- return this.relDef.cardinality[0] as Cardinality;
292
- }
293
-
294
- get objectCardinality(): Cardinality {
295
- return this.relDef.cardinality[1] as Cardinality;
296
- }
297
-
298
- /**
299
- * Gets the entity associated to this relation's subject
300
- *
301
- * @throws InconsistentSchemaError if the relation's subject is not a valid entity
302
- */
303
- get subject(): IsLooseSchema<E> extends true
304
- ? EntitySchema<E, R, E[number]>
305
- : EntitySchema<E, R, EntityRawSchemaByType<E, RDef["subject"]>> {
306
- const subjectSchema = this.schema.getEntity(this.relDef.subject);
307
- if (subjectSchema === null) {
308
- throw new InconsistentSchemaError(
309
- `entity type ${this.relDef.subject} used as subject of relation ${this.type} is not declared`
310
- );
311
- }
312
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
313
- // @ts-ignore
314
- return subjectSchema;
315
- }
316
-
317
- /**
318
- * Gets the entity associated to this relation's object
319
- *
320
- * @throws InconsistentSchemaError if the relation's object is not a valid entity
321
- */
322
- get object(): IsLooseSchema<E> extends true
323
- ? EntitySchema<E, R, E[number]>
324
- : EntitySchema<E, R, EntityRawSchemaByType<E, RDef["object"]>> {
325
- const objectSchema = this.schema.getEntity(this.relDef.object);
326
- if (objectSchema === null) {
327
- throw new InconsistentSchemaError(
328
- `entity type ${this.relDef.object} used as object of relation ${this.type} is not declared`
329
- );
330
- }
331
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
332
- // @ts-ignore
333
- return objectSchema;
334
- }
335
-
336
- /**
337
- * Checks if the relation is required when using the subject or object pointof view
338
- *
339
- * @param role The point of view to use
340
- * @returns
341
- */
342
- isRequired(role: "subject" | "object") {
343
- let cardinality: Cardinality;
344
- if (role === "subject") {
345
- cardinality = this.cardinality[0] as Cardinality;
346
- } else {
347
- cardinality = this.cardinality[1] as Cardinality;
348
- }
349
- return cardinalityRequired(cardinality);
350
- }
351
- }
352
-
353
- /**
354
- * Checks if the given cardinality requires at least one entity to be present
355
- *
356
- * @param cardinality The cardinality to check
357
- * @returns
358
- */
359
- function cardinalityRequired(cardinality: Cardinality): boolean {
360
- return cardinality === "1" || cardinality === "+";
361
- }
362
-
363
- export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<
364
- RelationDefinitionRawSchema<E>
365
- >;
366
-
367
- export type MatchedRelationDefinition<
368
- E extends EntityRawSchemaArray,
369
- R extends RelationDefinitionRawSchemaArray<EntityRawSchemaArray>,
370
- M extends RelationDefinitionMatcher<E>
371
- > = R[number] extends infer RDef ? (RDef extends M ? RDef : never) : never;