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