@api-client/core 0.6.5 → 0.6.8
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.
- package/build/browser.d.ts +6 -0
- package/build/browser.js +6 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +6 -0
- package/build/index.js +6 -0
- package/build/index.js.map +1 -1
- package/build/src/authorization/lib/Utils.js +5 -2
- package/build/src/authorization/lib/Utils.js.map +1 -1
- package/build/src/models/data/DataAssociation.d.ts +76 -0
- package/build/src/models/data/DataAssociation.js +151 -0
- package/build/src/models/data/DataAssociation.js.map +1 -0
- package/build/src/models/data/DataAssociationSchema.d.ts +32 -0
- package/build/src/models/data/DataAssociationSchema.js +2 -0
- package/build/src/models/data/DataAssociationSchema.js.map +1 -0
- package/build/src/models/data/DataEntity.d.ts +195 -0
- package/build/src/models/data/DataEntity.js +415 -0
- package/build/src/models/data/DataEntity.js.map +1 -0
- package/build/src/models/data/DataModel.d.ts +74 -0
- package/build/src/models/data/DataModel.js +173 -0
- package/build/src/models/data/DataModel.js.map +1 -0
- package/build/src/models/data/DataNamespace.d.ts +174 -0
- package/build/src/models/data/DataNamespace.js +424 -0
- package/build/src/models/data/DataNamespace.js.map +1 -0
- package/build/src/models/data/DataProperty.d.ts +159 -0
- package/build/src/models/data/DataProperty.js +216 -0
- package/build/src/models/data/DataProperty.js.map +1 -0
- package/build/src/models/data/DataPropertySchema.d.ts +125 -0
- package/build/src/models/data/DataPropertySchema.js +33 -0
- package/build/src/models/data/DataPropertySchema.js.map +1 -0
- package/build/src/models/store/File.d.ts +17 -0
- package/build/src/models/store/File.js +53 -1
- package/build/src/models/store/File.js.map +1 -1
- package/build/src/runtime/store/FilesSdk.d.ts +41 -6
- package/build/src/runtime/store/FilesSdk.js +77 -6
- package/build/src/runtime/store/FilesSdk.js.map +1 -1
- package/build/src/runtime/store/Http.d.ts +1 -0
- package/build/src/runtime/store/Http.js.map +1 -1
- package/build/src/runtime/store/HttpNode.d.ts +1 -0
- package/build/src/runtime/store/HttpNode.js +6 -4
- package/build/src/runtime/store/HttpNode.js.map +1 -1
- package/build/src/runtime/store/HttpWeb.d.ts +1 -0
- package/build/src/runtime/store/HttpWeb.js +4 -0
- package/build/src/runtime/store/HttpWeb.js.map +1 -1
- package/build/src/runtime/store/SdkBase.d.ts +1 -1
- package/package.json +1 -1
- package/src/authorization/lib/Utils.ts +5 -2
- package/src/models/data/DataAssociation.ts +189 -0
- package/src/models/data/DataAssociationSchema.ts +32 -0
- package/src/models/data/DataEntity.ts +496 -0
- package/src/models/data/DataModel.ts +206 -0
- package/src/models/data/DataNamespace.ts +503 -0
- package/src/models/data/DataProperty.ts +306 -0
- package/src/models/data/DataPropertySchema.ts +156 -0
- package/src/models/store/File.ts +55 -1
- package/src/runtime/store/FilesSdk.ts +95 -9
- package/src/runtime/store/Http.ts +2 -0
- package/src/runtime/store/HttpNode.ts +7 -4
- package/src/runtime/store/HttpWeb.ts +5 -0
- package/src/runtime/store/SdkBase.ts +1 -1
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import { IThing, Thing } from "../Thing.js";
|
|
2
|
+
import v4 from '../../lib/uuid.js';
|
|
3
|
+
import { DataNamespace } from "./DataNamespace.js";
|
|
4
|
+
import { DataProperty, DataPropertyType } from "./DataProperty.js";
|
|
5
|
+
import { DataAssociation } from "./DataAssociation.js";
|
|
6
|
+
import { IBreadcrumb } from "../store/Breadcrumb.js";
|
|
7
|
+
import { DataModel } from "./DataModel.js";
|
|
8
|
+
|
|
9
|
+
export const Kind = 'Core#DataEntity';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Data entity is the smallest description of a data in the system
|
|
13
|
+
* It contains properties and associations. At least one entity describe a data model.
|
|
14
|
+
*/
|
|
15
|
+
export interface IDataEntity {
|
|
16
|
+
kind: typeof Kind;
|
|
17
|
+
/**
|
|
18
|
+
* The key of the namespace.
|
|
19
|
+
*/
|
|
20
|
+
key: string;
|
|
21
|
+
/**
|
|
22
|
+
* The data entity description.
|
|
23
|
+
*/
|
|
24
|
+
info: IThing;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Optional general purpose tags for the UI.
|
|
28
|
+
*/
|
|
29
|
+
tags?: string[];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* For future use.
|
|
33
|
+
*
|
|
34
|
+
* The keys of the taxonomy items associated with the entity.
|
|
35
|
+
*/
|
|
36
|
+
taxonomy?: string[];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The list of keys of properties that belong to this entity.
|
|
40
|
+
*/
|
|
41
|
+
properties?: string[];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The list of keys of associations that belong to this entity.
|
|
45
|
+
*/
|
|
46
|
+
associations?: string[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The list of keys of entities that are parents to this entity.
|
|
50
|
+
*
|
|
51
|
+
* This potentially may cause a conflict when two parents declare the same
|
|
52
|
+
* property. In such situation this entity should define own property
|
|
53
|
+
* with the same name to shadow parent property. When the property is
|
|
54
|
+
* not shadowed this may cause unexpected results as the processing could result
|
|
55
|
+
* with inconsistent definition of a schema because the last read property wins.
|
|
56
|
+
*/
|
|
57
|
+
parents?: string[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Data entity is the smallest description of a data in the system
|
|
62
|
+
* It contains properties and associations. At least one entity describe a data model.
|
|
63
|
+
*/
|
|
64
|
+
export class DataEntity {
|
|
65
|
+
kind = Kind;
|
|
66
|
+
|
|
67
|
+
key = '';
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The description of the data namespace.
|
|
71
|
+
*/
|
|
72
|
+
info: Thing = Thing.fromName('');
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Optional general purpose tags for the UI.
|
|
76
|
+
*/
|
|
77
|
+
tags: string[] = [];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Reserved for future use.
|
|
81
|
+
*
|
|
82
|
+
* The keys of the taxonomy items associated with the entity.
|
|
83
|
+
*/
|
|
84
|
+
taxonomy: string[] = [];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The list of keys of properties that belong to this entity.
|
|
88
|
+
*/
|
|
89
|
+
properties: DataProperty[] = [];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The list of keys of associations that belong to this entity.
|
|
93
|
+
*/
|
|
94
|
+
associations: DataAssociation[] = [];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The list of keys of entities that are parents to this entity.
|
|
98
|
+
*
|
|
99
|
+
* This potentially may cause a conflict when two parents declare the same
|
|
100
|
+
* property. In such situation this entity should define own property
|
|
101
|
+
* with the same name to shadow parent property. When the property is
|
|
102
|
+
* not shadowed this may cause unexpected results as the processing could result
|
|
103
|
+
* with inconsistent definition of a schema because the last read property wins.
|
|
104
|
+
*/
|
|
105
|
+
parents: string[] = [];
|
|
106
|
+
|
|
107
|
+
static fromName(root: DataNamespace, name: string): DataEntity {
|
|
108
|
+
const entity = new DataEntity(root);
|
|
109
|
+
entity.info = Thing.fromName(name);
|
|
110
|
+
return entity;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param input The data entity definition to restore.
|
|
115
|
+
*/
|
|
116
|
+
constructor(protected root: DataNamespace, input?: string | IDataEntity) {
|
|
117
|
+
let init: IDataEntity;
|
|
118
|
+
if (typeof input === 'string') {
|
|
119
|
+
init = JSON.parse(input);
|
|
120
|
+
} else if (typeof input === 'object') {
|
|
121
|
+
init = input;
|
|
122
|
+
} else {
|
|
123
|
+
init = {
|
|
124
|
+
kind: Kind,
|
|
125
|
+
key: v4(),
|
|
126
|
+
info: Thing.fromName('').toJSON(),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
this.new(init);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
new(init: IDataEntity): void {
|
|
133
|
+
if (!DataEntity.isDataEntity(init)) {
|
|
134
|
+
throw new Error(`Not a data entity.`);
|
|
135
|
+
}
|
|
136
|
+
const { info, key = v4(), kind = Kind, tags, taxonomy, parents, properties, associations } = init;
|
|
137
|
+
this.kind = kind;
|
|
138
|
+
this.key = key;
|
|
139
|
+
if (info) {
|
|
140
|
+
this.info = new Thing(info);
|
|
141
|
+
} else {
|
|
142
|
+
this.info = Thing.fromName('');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (Array.isArray(tags)) {
|
|
146
|
+
this.tags = [...tags];
|
|
147
|
+
} else {
|
|
148
|
+
this.tags = [];
|
|
149
|
+
}
|
|
150
|
+
if (Array.isArray(taxonomy)) {
|
|
151
|
+
this.taxonomy = [...taxonomy];
|
|
152
|
+
} else {
|
|
153
|
+
this.taxonomy = [];
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(parents)) {
|
|
156
|
+
this.parents = [...parents];
|
|
157
|
+
} else {
|
|
158
|
+
this.parents = [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.properties = [];
|
|
162
|
+
if (Array.isArray(properties)) {
|
|
163
|
+
properties.forEach(key => {
|
|
164
|
+
const value = this._readProperty(key);
|
|
165
|
+
if (value) {
|
|
166
|
+
this.properties.push(value);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.associations = [];
|
|
172
|
+
if (Array.isArray(associations)) {
|
|
173
|
+
associations.forEach(key => {
|
|
174
|
+
const value = this._readAssociation(key);
|
|
175
|
+
if (value) {
|
|
176
|
+
this.associations.push(value);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static isDataEntity(input: unknown): boolean {
|
|
183
|
+
const typed = input as IDataEntity;
|
|
184
|
+
if (!input || typed.kind !== Kind) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toJSON(): IDataEntity {
|
|
191
|
+
const result: IDataEntity = {
|
|
192
|
+
kind: Kind,
|
|
193
|
+
key: this.key,
|
|
194
|
+
info: this.info.toJSON(),
|
|
195
|
+
};
|
|
196
|
+
if (Array.isArray(this.tags) && this.tags.length) {
|
|
197
|
+
result.tags = [...this.tags];
|
|
198
|
+
}
|
|
199
|
+
if (Array.isArray(this.taxonomy) && this.taxonomy.length) {
|
|
200
|
+
result.taxonomy = [...this.taxonomy];
|
|
201
|
+
}
|
|
202
|
+
if (Array.isArray(this.parents) && this.parents.length) {
|
|
203
|
+
result.parents = [...this.parents];
|
|
204
|
+
}
|
|
205
|
+
if (Array.isArray(this.properties) && this.properties.length) {
|
|
206
|
+
result.properties = this.properties.map(i => i.key);
|
|
207
|
+
}
|
|
208
|
+
if (Array.isArray(this.associations) && this.associations.length) {
|
|
209
|
+
result.associations = this.associations.map(i => i.key);
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
protected _readAssociation(key: string): DataAssociation | undefined {
|
|
215
|
+
return this.root.definitions.associations.find(i => i.key === key);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected _readProperty(key: string): DataProperty | undefined {
|
|
219
|
+
return this.root.definitions.properties.find(i => i.key === key);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Creates a property with a passed type.
|
|
224
|
+
* @param type The type of the property
|
|
225
|
+
* @returns The created property
|
|
226
|
+
*/
|
|
227
|
+
addTypedProperty(type: DataPropertyType): DataProperty {
|
|
228
|
+
const property = DataProperty.fromType(this.root, type);
|
|
229
|
+
this.root.definitions.properties.push(property);
|
|
230
|
+
this.properties.push(property);
|
|
231
|
+
return property;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Creates a property with a passed type.
|
|
236
|
+
* @param name The name of the property.
|
|
237
|
+
* @returns The created property
|
|
238
|
+
*/
|
|
239
|
+
addNamedProperty(name: string): DataProperty {
|
|
240
|
+
const property = DataProperty.fromName(this.root, name);
|
|
241
|
+
this.root.definitions.properties.push(property);
|
|
242
|
+
this.properties.push(property);
|
|
243
|
+
return property;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Removes the property from the entity and namespace definitions.
|
|
248
|
+
* @param key The key of the property to remove.
|
|
249
|
+
*/
|
|
250
|
+
removeProperty(key: string): void {
|
|
251
|
+
const thisIndex = this.properties.findIndex(i => i.key === key);
|
|
252
|
+
if (thisIndex < 0) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.properties.splice(thisIndex, 1);
|
|
256
|
+
const defIndex = this.root.definitions.properties.findIndex(i => i.key === key);
|
|
257
|
+
if (defIndex >= 0) {
|
|
258
|
+
this.root.definitions.properties.splice(defIndex, 1);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Creates an association for a given name, adds it to definitions, and returns it.
|
|
264
|
+
* @param name The name of the association
|
|
265
|
+
* @returns The created association
|
|
266
|
+
*/
|
|
267
|
+
addNamedAssociation(name: string): DataAssociation {
|
|
268
|
+
const result = DataAssociation.fromName(this.root, name);
|
|
269
|
+
this.root.definitions.associations.push(result);
|
|
270
|
+
this.associations.push(result);
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Creates an association for a given target, adds it to definitions, and returns it.
|
|
276
|
+
* @param target The target entity key of the association
|
|
277
|
+
* @returns The created association
|
|
278
|
+
*/
|
|
279
|
+
addTargetAssociation(target: string): DataAssociation {
|
|
280
|
+
const result = DataAssociation.fromTarget(this.root, target);
|
|
281
|
+
this.root.definitions.associations.push(result);
|
|
282
|
+
this.associations.push(result);
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Removes an association from the entity and namespace definitions.
|
|
288
|
+
* @param key The key of the association to remove.
|
|
289
|
+
*/
|
|
290
|
+
removeAssociation(key: string): void {
|
|
291
|
+
const thisIndex = this.associations.findIndex(i => i.key === key);
|
|
292
|
+
if (thisIndex < 0) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
this.associations.splice(thisIndex, 1);
|
|
296
|
+
const defIndex = this.root.definitions.associations.findIndex(i => i.key === key);
|
|
297
|
+
if (defIndex >= 0) {
|
|
298
|
+
this.root.definitions.associations.splice(defIndex, 1);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Reads the list of parents for the entity, inside the root namespace. The computed list contains the list of all
|
|
304
|
+
* parents in the inheritance chain in no particular order.
|
|
305
|
+
*/
|
|
306
|
+
getComputedParents(): DataEntity[] {
|
|
307
|
+
const { entities } = this.root.definitions;
|
|
308
|
+
let result: DataEntity[] = [];
|
|
309
|
+
this.parents.forEach((key) => {
|
|
310
|
+
const parent = entities.find(i => i.key === key);
|
|
311
|
+
if (parent) {
|
|
312
|
+
result.push(parent);
|
|
313
|
+
result = result.concat(parent.getComputedParents());
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Computes list of all children, inside the root namespace, that extends this entity.
|
|
321
|
+
* The children are not ordered.
|
|
322
|
+
*/
|
|
323
|
+
getComputedChildren(): DataEntity[] {
|
|
324
|
+
const { entities } = this.root.definitions;
|
|
325
|
+
return entities.filter(i => i.parents.includes(this.key));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Computes a list of entities that are associated with the current entity.
|
|
330
|
+
*/
|
|
331
|
+
getComputedAssociations(): DataEntity[] {
|
|
332
|
+
const { root, associations } = this;
|
|
333
|
+
const { entities } = root.definitions;
|
|
334
|
+
const result: DataEntity[] = [];
|
|
335
|
+
associations.forEach((assoc) => {
|
|
336
|
+
if (!assoc.target) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const entity = entities.find(i => i.key === assoc.target);
|
|
340
|
+
if (entity) {
|
|
341
|
+
result.push(entity);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Removes self from the namespace with all properties and attributes.
|
|
349
|
+
*/
|
|
350
|
+
remove(): void {
|
|
351
|
+
const { key, properties, associations, root } = this;
|
|
352
|
+
// remove own stuff
|
|
353
|
+
properties.forEach(p => this.removeProperty(p.key));
|
|
354
|
+
associations.forEach(a => this.removeAssociation(a.key));
|
|
355
|
+
// remove from the root
|
|
356
|
+
const index = root.definitions.entities.findIndex(i => i.key === key);
|
|
357
|
+
if (index >= 0) {
|
|
358
|
+
root.definitions.entities.splice(index, 1);
|
|
359
|
+
}
|
|
360
|
+
// remove from the parent
|
|
361
|
+
const model = this.getParent();
|
|
362
|
+
if (model) {
|
|
363
|
+
const entityIndex = model.entities.findIndex(e => e === this);
|
|
364
|
+
model.entities.splice(entityIndex, 1);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Returns a parent data model where this entity exist.
|
|
370
|
+
*/
|
|
371
|
+
getParent(): DataModel | undefined {
|
|
372
|
+
return this.root.definitions.models.find(m => m.entities.some(e => e === this));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Tests whether one entity is associated with another.
|
|
377
|
+
*
|
|
378
|
+
* @param entity1 The source entity
|
|
379
|
+
* @param entity2 The target entity
|
|
380
|
+
* @returns true when there's any path from one entity to another.
|
|
381
|
+
*/
|
|
382
|
+
static isAssociated(entity1: DataEntity, entity2: DataEntity): boolean {
|
|
383
|
+
return entity1.isAssociated(entity2.key);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Tests whether this entity is somehow associated with another entity.
|
|
388
|
+
* @param target The key of the target entity to test for association with.
|
|
389
|
+
* @returns true if this entity has any association to the `target` entity.
|
|
390
|
+
*/
|
|
391
|
+
isAssociated(target: string): boolean {
|
|
392
|
+
const it = this.associationPath(target);
|
|
393
|
+
const path = it.next().value;
|
|
394
|
+
return !!path;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Prints out all associations from one entity to another through all entities that may be in between.
|
|
399
|
+
* @param toEntity The key to the target entity
|
|
400
|
+
* @yields The path containing keys of entities from this entity to the `toEntity` (inclusive) and all entities in between.
|
|
401
|
+
*/
|
|
402
|
+
* associationPath(toEntity: string): Generator<string[]> {
|
|
403
|
+
const graph = this._associationGraph();
|
|
404
|
+
for (const path of this._associationPath(this.key, toEntity, graph)) {
|
|
405
|
+
yield path;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The actual implementation of the graph search.
|
|
411
|
+
*
|
|
412
|
+
* @param from The current from node
|
|
413
|
+
* @param to The target node
|
|
414
|
+
* @param g The graph
|
|
415
|
+
* @param path The current list of entity ids.
|
|
416
|
+
* @param visited The list of visited paths to avoid cycles
|
|
417
|
+
*/
|
|
418
|
+
protected * _associationPath(from: string, to: string, g: Record<string, string[]>, path: string[] = [], visited: Set<string> = new Set()): Generator<string[]> {
|
|
419
|
+
if (from === to) {
|
|
420
|
+
yield path.concat(to);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (visited.has(from)) {
|
|
424
|
+
// it's a cycle
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (g[from]) {
|
|
428
|
+
visited.add(from);
|
|
429
|
+
path.push(from);
|
|
430
|
+
|
|
431
|
+
for (const neighbor of g[from]) {
|
|
432
|
+
yield *this._associationPath(neighbor, to, g, path, visited);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
visited.delete(from);
|
|
436
|
+
path.pop();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @returns The graph of associations where keys are the source entities and the value is the list of all target entities.
|
|
442
|
+
*/
|
|
443
|
+
protected _associationGraph(): Record<string, string[]> {
|
|
444
|
+
const graph: Record<string, string[]> = {};
|
|
445
|
+
const { associations, entities } = this.root.definitions;
|
|
446
|
+
for (const assoc of associations) {
|
|
447
|
+
if (!assoc.target) {
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const srcEntity = entities.find(i => i.associations.some(a => a === assoc));
|
|
451
|
+
if (!srcEntity) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (!graph[srcEntity.key]) {
|
|
455
|
+
graph[srcEntity.key] = [];
|
|
456
|
+
}
|
|
457
|
+
graph[srcEntity.key].push(assoc.target);
|
|
458
|
+
}
|
|
459
|
+
return graph;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Creates breadcrumbs from this entity to the root namespace.
|
|
464
|
+
*/
|
|
465
|
+
breadcrumbs(): IBreadcrumb[] {
|
|
466
|
+
const result: IBreadcrumb[] = [];
|
|
467
|
+
result.push({
|
|
468
|
+
key: this.key,
|
|
469
|
+
displayName: this.info.name || 'Unnamed entity',
|
|
470
|
+
kind: Kind,
|
|
471
|
+
});
|
|
472
|
+
const model = this.getParent();
|
|
473
|
+
if (model) {
|
|
474
|
+
result.push({
|
|
475
|
+
key: model.key,
|
|
476
|
+
kind: model.kind,
|
|
477
|
+
displayName: model.info.name || 'Unnamed data model',
|
|
478
|
+
});
|
|
479
|
+
let parent = model.getParent();
|
|
480
|
+
while (parent && parent !== this.root) {
|
|
481
|
+
result.push({
|
|
482
|
+
key: parent.key,
|
|
483
|
+
kind: parent.kind,
|
|
484
|
+
displayName: parent.info.name || 'Unnamed namespace',
|
|
485
|
+
});
|
|
486
|
+
parent = parent.getParent();
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
result.push({
|
|
490
|
+
key: this.root.key,
|
|
491
|
+
displayName: this.root.info.name || 'Unnamed namespace',
|
|
492
|
+
kind: this.root.kind,
|
|
493
|
+
});
|
|
494
|
+
return result.reverse();
|
|
495
|
+
}
|
|
496
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { IThing, Thing } from "../Thing.js";
|
|
2
|
+
import v4 from '../../lib/uuid.js';
|
|
3
|
+
import { DataEntity, IDataEntity } from "./DataEntity.js";
|
|
4
|
+
import { DataNamespace } from "./DataNamespace.js";
|
|
5
|
+
import { IBreadcrumb } from "../store/Breadcrumb.js";
|
|
6
|
+
|
|
7
|
+
export const Kind = 'Core#DataModel';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Data model creates a logical structure around data entities.
|
|
11
|
+
* It groups entities that represents a whole schema, like a Product data model
|
|
12
|
+
* can have entities that describe: the product entity, price history entity,
|
|
13
|
+
* product location, etc.
|
|
14
|
+
*/
|
|
15
|
+
export interface IDataModel {
|
|
16
|
+
kind: typeof Kind;
|
|
17
|
+
/**
|
|
18
|
+
* The key of the namespace.
|
|
19
|
+
*/
|
|
20
|
+
key: string;
|
|
21
|
+
/**
|
|
22
|
+
* The data model description.
|
|
23
|
+
*/
|
|
24
|
+
info: IThing;
|
|
25
|
+
/**
|
|
26
|
+
* The list of keys of entities that this data model contain.
|
|
27
|
+
*/
|
|
28
|
+
entities?: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Data model creates a logical structure around data entities.
|
|
33
|
+
* It groups entities that represents a whole schema, like a Product data model
|
|
34
|
+
* can have entities that describe: the product entity, price history entity,
|
|
35
|
+
* product location, etc.
|
|
36
|
+
*/
|
|
37
|
+
export class DataModel {
|
|
38
|
+
kind = Kind;
|
|
39
|
+
|
|
40
|
+
key = '';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The description of the data namespace.
|
|
44
|
+
*/
|
|
45
|
+
info: Thing = Thing.fromName('');
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The list of keys of entities that this data model contain.
|
|
49
|
+
*/
|
|
50
|
+
entities: DataEntity[] = [];
|
|
51
|
+
|
|
52
|
+
static fromName(root: DataNamespace, name: string): DataModel {
|
|
53
|
+
const entity = new DataModel(root);
|
|
54
|
+
entity.info = Thing.fromName(name);
|
|
55
|
+
return entity;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param root the root namespace.
|
|
60
|
+
* @param input The data model definition to restore.
|
|
61
|
+
*/
|
|
62
|
+
constructor(protected root: DataNamespace, input?: string | IDataModel) {
|
|
63
|
+
let init: IDataModel;
|
|
64
|
+
if (typeof input === 'string') {
|
|
65
|
+
init = JSON.parse(input);
|
|
66
|
+
} else if (typeof input === 'object') {
|
|
67
|
+
init = input;
|
|
68
|
+
} else {
|
|
69
|
+
init = {
|
|
70
|
+
kind: Kind,
|
|
71
|
+
key: v4(),
|
|
72
|
+
info: Thing.fromName('').toJSON(),
|
|
73
|
+
entities: [],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
this.new(init);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
new(init: IDataModel): void {
|
|
80
|
+
if (!DataModel.isDataModel(init)) {
|
|
81
|
+
throw new Error(`Not a data model.`);
|
|
82
|
+
}
|
|
83
|
+
const { info, key = v4(), kind = Kind, entities } = init;
|
|
84
|
+
this.kind = kind;
|
|
85
|
+
this.key = key;
|
|
86
|
+
if (info) {
|
|
87
|
+
this.info = new Thing(info);
|
|
88
|
+
} else {
|
|
89
|
+
this.info = Thing.fromName('');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.entities = [];
|
|
93
|
+
if (Array.isArray(entities)) {
|
|
94
|
+
entities.forEach(key => {
|
|
95
|
+
const value = this._readEntity(key);
|
|
96
|
+
if (value) {
|
|
97
|
+
this.entities.push(value);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static isDataModel(input: unknown): boolean {
|
|
104
|
+
const typed = input as IDataModel;
|
|
105
|
+
if (!input || typed.kind !== Kind) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
toJSON(): IDataModel {
|
|
112
|
+
const result: IDataModel = {
|
|
113
|
+
kind: Kind,
|
|
114
|
+
key: this.key,
|
|
115
|
+
info: this.info.toJSON(),
|
|
116
|
+
};
|
|
117
|
+
if (Array.isArray(this.entities) && this.entities.length) {
|
|
118
|
+
result.entities = this.entities.map(i => i.key);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
protected _readEntity(key: string): DataEntity | undefined {
|
|
124
|
+
return this.root.definitions.entities.find(i => i.key === key);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Removes self from the namespace with all entities.
|
|
129
|
+
*/
|
|
130
|
+
remove(): void {
|
|
131
|
+
const { key, entities, root } = this;
|
|
132
|
+
// remove children
|
|
133
|
+
entities.forEach(e => e.remove());
|
|
134
|
+
|
|
135
|
+
// remove self from the parent
|
|
136
|
+
const parent = this.getParent();
|
|
137
|
+
if (parent) {
|
|
138
|
+
const itemIndex = parent.items.findIndex(i => i.key === key);
|
|
139
|
+
if (itemIndex >= 0) {
|
|
140
|
+
parent.items.splice(itemIndex, 1);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// remove self from definitions
|
|
144
|
+
const index = root.definitions.models.findIndex(i => i.key === key);
|
|
145
|
+
if (index >= 0) {
|
|
146
|
+
root.definitions.models.splice(index, 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Adds an entity to this data model.
|
|
152
|
+
*
|
|
153
|
+
* @param init The name of the entity to create, the instance of the entity or its schema
|
|
154
|
+
* @returns A reference to the created entity.
|
|
155
|
+
*/
|
|
156
|
+
addEntity(init: string | DataEntity | IDataEntity): DataEntity {
|
|
157
|
+
let definition: DataEntity;
|
|
158
|
+
if (typeof init === 'string') {
|
|
159
|
+
definition = DataEntity.fromName(this.root, init);
|
|
160
|
+
} else if (init instanceof DataEntity) {
|
|
161
|
+
definition = init;
|
|
162
|
+
} else {
|
|
163
|
+
definition = new DataEntity(this.root, init);
|
|
164
|
+
}
|
|
165
|
+
this.root.definitions.entities.push(definition);
|
|
166
|
+
this.entities.push(definition);
|
|
167
|
+
return definition;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns a parent namespace where this data model exist.
|
|
172
|
+
*/
|
|
173
|
+
getParent(): DataNamespace | undefined {
|
|
174
|
+
if (this.root.items.some(e => e.key === this.key)) {
|
|
175
|
+
return this.root;
|
|
176
|
+
}
|
|
177
|
+
return this.root.definitions.namespaces.find(n => n.items.some(e => e.key === this.key));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates breadcrumbs from this data model to the root namespace.
|
|
182
|
+
*/
|
|
183
|
+
breadcrumbs(): IBreadcrumb[] {
|
|
184
|
+
const result: IBreadcrumb[] = [];
|
|
185
|
+
result.push({
|
|
186
|
+
key: this.key,
|
|
187
|
+
displayName: this.info.name || 'Unnamed data model',
|
|
188
|
+
kind: Kind,
|
|
189
|
+
});
|
|
190
|
+
let parent = this.getParent();
|
|
191
|
+
while (parent && parent !== this.root) {
|
|
192
|
+
result.push({
|
|
193
|
+
key: parent.key,
|
|
194
|
+
kind: parent.kind,
|
|
195
|
+
displayName: parent.info.name || 'Unnamed namespace',
|
|
196
|
+
});
|
|
197
|
+
parent = parent.getParent();
|
|
198
|
+
}
|
|
199
|
+
result.push({
|
|
200
|
+
key: this.root.key,
|
|
201
|
+
displayName: this.root.info.name || 'Unnamed namespace',
|
|
202
|
+
kind: this.root.kind,
|
|
203
|
+
});
|
|
204
|
+
return result.reverse();
|
|
205
|
+
}
|
|
206
|
+
}
|