@api-client/core 0.6.5 → 0.6.6
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/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/runtime/store/FilesSdk.d.ts +4 -2
- package/build/src/runtime/store/FilesSdk.js +1 -1
- package/build/src/runtime/store/FilesSdk.js.map +1 -1
- package/package.json +1 -1
- 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/runtime/store/FilesSdk.ts +5 -2
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import v4 from '../../lib/uuid.js';
|
|
2
|
+
import { IThing, Thing } from "../Thing.js";
|
|
3
|
+
import { IDataAssociationSchema } from './DataAssociationSchema.js';
|
|
4
|
+
import { DataEntity } from './DataEntity.js';
|
|
5
|
+
import { DataNamespace } from './DataNamespace.js';
|
|
6
|
+
|
|
7
|
+
export const Kind = 'Core#DataAssociation';
|
|
8
|
+
|
|
9
|
+
export interface IDataAssociation {
|
|
10
|
+
kind: typeof Kind;
|
|
11
|
+
/**
|
|
12
|
+
* The key of the namespace.
|
|
13
|
+
*/
|
|
14
|
+
key: string;
|
|
15
|
+
/**
|
|
16
|
+
* The data association description.
|
|
17
|
+
*/
|
|
18
|
+
info: IThing;
|
|
19
|
+
/**
|
|
20
|
+
* Wether the data association is required.
|
|
21
|
+
*/
|
|
22
|
+
required?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Whether the data association allows multiple items.
|
|
25
|
+
*/
|
|
26
|
+
multiple?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The target entity of this association.
|
|
29
|
+
* An association without a target is considered invalid and discarded when processing the values.
|
|
30
|
+
*/
|
|
31
|
+
target?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
|
|
34
|
+
*/
|
|
35
|
+
schema?: IDataAssociationSchema;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class DataAssociation {
|
|
39
|
+
kind = Kind;
|
|
40
|
+
|
|
41
|
+
key = '';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The description of the data namespace.
|
|
45
|
+
*/
|
|
46
|
+
info: Thing = Thing.fromName('');
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Wether the data association is required.
|
|
50
|
+
*/
|
|
51
|
+
required?: boolean;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether the data association allows multiple items.
|
|
55
|
+
*/
|
|
56
|
+
multiple?: boolean;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The target entity of this association.
|
|
60
|
+
* An association without a target is considered invalid and discarded when processing the values.
|
|
61
|
+
*/
|
|
62
|
+
target?: string;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
|
|
66
|
+
*
|
|
67
|
+
* Implementation note, when an entity is removed this property is not changed. The target can't
|
|
68
|
+
* be read but it can be tracked to a deleted entity.
|
|
69
|
+
*/
|
|
70
|
+
schema?: IDataAssociationSchema;
|
|
71
|
+
|
|
72
|
+
static fromTarget(root: DataNamespace, target: string): DataAssociation {
|
|
73
|
+
const assoc = new DataAssociation(root);
|
|
74
|
+
assoc.target = target;
|
|
75
|
+
return assoc;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static fromName(root: DataNamespace, name: string): DataAssociation {
|
|
79
|
+
const assoc = new DataAssociation(root);
|
|
80
|
+
assoc.info = Thing.fromName(name);
|
|
81
|
+
return assoc;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param input The data association definition to restore.
|
|
86
|
+
*/
|
|
87
|
+
constructor(protected root: DataNamespace, input?: string | IDataAssociation) {
|
|
88
|
+
let init: IDataAssociation;
|
|
89
|
+
if (typeof input === 'string') {
|
|
90
|
+
init = JSON.parse(input);
|
|
91
|
+
} else if (typeof input === 'object') {
|
|
92
|
+
init = input;
|
|
93
|
+
} else {
|
|
94
|
+
init = {
|
|
95
|
+
kind: Kind,
|
|
96
|
+
key: v4(),
|
|
97
|
+
info: Thing.fromName('').toJSON(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
this.new(init);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
new(init: IDataAssociation): void {
|
|
104
|
+
if (!DataAssociation.isDataAssociation(init)) {
|
|
105
|
+
throw new Error(`Not a data association.`);
|
|
106
|
+
}
|
|
107
|
+
const { info, key = v4(), kind = Kind, schema, multiple, required, target } = init;
|
|
108
|
+
this.kind = kind;
|
|
109
|
+
this.key = key;
|
|
110
|
+
if (info) {
|
|
111
|
+
this.info = new Thing(info);
|
|
112
|
+
} else {
|
|
113
|
+
this.info = Thing.fromName('');
|
|
114
|
+
}
|
|
115
|
+
if (schema) {
|
|
116
|
+
this.schema = { ...schema };
|
|
117
|
+
} else {
|
|
118
|
+
this.schema = undefined;
|
|
119
|
+
}
|
|
120
|
+
if (typeof multiple === 'boolean') {
|
|
121
|
+
this.multiple = multiple;
|
|
122
|
+
} else {
|
|
123
|
+
this.multiple = undefined;
|
|
124
|
+
}
|
|
125
|
+
if (typeof required === 'boolean') {
|
|
126
|
+
this.required = required;
|
|
127
|
+
} else {
|
|
128
|
+
this.required = undefined;
|
|
129
|
+
}
|
|
130
|
+
if (typeof target === 'string') {
|
|
131
|
+
this.target = target;
|
|
132
|
+
} else {
|
|
133
|
+
this.target = undefined;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static isDataAssociation(input: unknown): boolean {
|
|
138
|
+
const typed = input as IDataAssociation;
|
|
139
|
+
if (!input || typed.kind !== Kind) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
toJSON(): IDataAssociation {
|
|
146
|
+
const result: IDataAssociation = {
|
|
147
|
+
kind: Kind,
|
|
148
|
+
key: this.key,
|
|
149
|
+
info: this.info.toJSON(),
|
|
150
|
+
};
|
|
151
|
+
if (this.schema) {
|
|
152
|
+
result.schema = { ...this.schema };
|
|
153
|
+
}
|
|
154
|
+
if (typeof this.multiple === 'boolean') {
|
|
155
|
+
result.multiple = this.multiple;
|
|
156
|
+
}
|
|
157
|
+
if (typeof this.required === 'boolean') {
|
|
158
|
+
result.required = this.required;
|
|
159
|
+
}
|
|
160
|
+
if (typeof this.target === 'string') {
|
|
161
|
+
result.target = this.target;
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getTarget(): DataEntity | undefined {
|
|
167
|
+
const { root, target } = this;
|
|
168
|
+
if (!target) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
return root.definitions.entities.find(i => i.key === target);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Removes self from the parent entity and the namespace definition.
|
|
176
|
+
*/
|
|
177
|
+
remove(): void {
|
|
178
|
+
const { root } = this;
|
|
179
|
+
const entity = root.definitions.entities.find(i => i.associations.some(j => j === this));
|
|
180
|
+
if (entity) {
|
|
181
|
+
const assocIndex = entity.associations.findIndex(i => i === this);
|
|
182
|
+
entity.associations.splice(assocIndex, 1);
|
|
183
|
+
}
|
|
184
|
+
const defIndex = this.root.definitions.associations.findIndex(i => i.key === this.key);
|
|
185
|
+
if (defIndex >= 0) {
|
|
186
|
+
this.root.definitions.associations.splice(defIndex, 1);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface IDataAssociationSchema {
|
|
2
|
+
/**
|
|
3
|
+
* Whether the target entity should be embedded under the property name.
|
|
4
|
+
* When false, this association is just an information that one entity depend on another.
|
|
5
|
+
* When true, it changes the definition of the schema having this association to
|
|
6
|
+
* add the target schema properties inline with this property.
|
|
7
|
+
*
|
|
8
|
+
* **When true**
|
|
9
|
+
*
|
|
10
|
+
* ```javascript
|
|
11
|
+
* // generated schema for `address` association
|
|
12
|
+
* {
|
|
13
|
+
* "name": "example value",
|
|
14
|
+
* "address": {
|
|
15
|
+
* "city": "example value",
|
|
16
|
+
* ...
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* **When false**
|
|
22
|
+
*
|
|
23
|
+
* ```javascript
|
|
24
|
+
* // generated schema for `address` association
|
|
25
|
+
* {
|
|
26
|
+
* "name": "example value",
|
|
27
|
+
* "address": "the key of the referenced schema"
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
embedded?: boolean;
|
|
32
|
+
}
|
|
@@ -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
|
+
}
|