@api-client/core 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/amf/AmfTypes.d.ts +1 -1
- package/build/src/amf/AmfTypes.js +1 -1
- package/build/src/amf/AmfTypes.js.map +1 -1
- package/build/src/amf/Utils.d.ts +0 -6
- package/build/src/amf/Utils.d.ts.map +1 -1
- package/build/src/amf/Utils.js +0 -14
- package/build/src/amf/Utils.js.map +1 -1
- package/build/src/browser.d.ts +1 -0
- package/build/src/browser.d.ts.map +1 -1
- package/build/src/browser.js +1 -0
- package/build/src/browser.js.map +1 -1
- package/build/src/index.d.ts +1 -0
- package/build/src/index.d.ts.map +1 -1
- package/build/src/index.js +1 -0
- package/build/src/index.js.map +1 -1
- package/build/src/legacy.d.ts +0 -8
- package/build/src/legacy.d.ts.map +1 -1
- package/build/src/legacy.js +0 -9
- package/build/src/legacy.js.map +1 -1
- package/build/src/modeling/Bindings.d.ts +1 -1
- package/build/src/modeling/Bindings.js.map +1 -1
- package/build/src/modeling/DataDomain.js +2 -2
- package/build/src/modeling/DataDomain.js.map +1 -1
- package/build/src/modeling/DataFormat.d.ts +0 -40
- package/build/src/modeling/DataFormat.d.ts.map +1 -1
- package/build/src/modeling/DataFormat.js +0 -27
- package/build/src/modeling/DataFormat.js.map +1 -1
- package/build/src/modeling/DomainAssociation.d.ts +28 -0
- package/build/src/modeling/DomainAssociation.d.ts.map +1 -1
- package/build/src/modeling/DomainAssociation.js +73 -4
- package/build/src/modeling/DomainAssociation.js.map +1 -1
- package/build/src/modeling/DomainEntity.d.ts +25 -9
- package/build/src/modeling/DomainEntity.d.ts.map +1 -1
- package/build/src/modeling/DomainEntity.js +65 -21
- package/build/src/modeling/DomainEntity.js.map +1 -1
- package/build/src/modeling/DomainFile.d.ts +1 -1
- package/build/src/modeling/DomainFile.js +1 -1
- package/build/src/modeling/DomainFile.js.map +1 -1
- package/build/src/modeling/DomainImpactAnalysis.d.ts +1 -1
- package/build/src/modeling/DomainImpactAnalysis.d.ts.map +1 -1
- package/build/src/modeling/DomainImpactAnalysis.js +3 -3
- package/build/src/modeling/DomainImpactAnalysis.js.map +1 -1
- package/build/src/modeling/DomainModel.d.ts +2 -2
- package/build/src/modeling/DomainModel.js +2 -2
- package/build/src/modeling/DomainModel.js.map +1 -1
- package/build/src/modeling/DomainProperty.d.ts +28 -12
- package/build/src/modeling/DomainProperty.d.ts.map +1 -1
- package/build/src/modeling/DomainProperty.js +61 -26
- package/build/src/modeling/DomainProperty.js.map +1 -1
- package/build/src/modeling/Semantics.d.ts +109 -0
- package/build/src/modeling/Semantics.d.ts.map +1 -0
- package/build/src/modeling/Semantics.js +97 -0
- package/build/src/modeling/Semantics.js.map +1 -0
- package/build/src/models/kinds.d.ts +0 -24
- package/build/src/models/kinds.d.ts.map +1 -1
- package/build/src/models/kinds.js +0 -24
- package/build/src/models/kinds.js.map +1 -1
- package/build/src/models/store/data_catalog.d.ts +1 -1
- package/build/src/models/store/data_catalog.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/data/models/example-generator-api.json +11 -11
- package/package.json +1 -1
- package/src/amf/AmfTypes.ts +1 -1
- package/src/amf/Utils.ts +0 -15
- package/src/modeling/Bindings.ts +1 -1
- package/src/modeling/DataDomain.ts +2 -2
- package/src/modeling/DataFormat.ts +0 -48
- package/src/modeling/DomainAssociation.ts +66 -3
- package/src/modeling/DomainEntity.ts +56 -17
- package/src/modeling/DomainFile.ts +1 -1
- package/src/modeling/DomainImpactAnalysis.ts +3 -3
- package/src/modeling/DomainModel.ts +2 -2
- package/src/modeling/DomainProperty.ts +60 -21
- package/src/modeling/Semantics.ts +178 -0
- package/src/modeling/graph.md +14 -14
- package/src/modeling/readme.md +29 -29
- package/src/models/kinds.ts +0 -25
- package/src/models/store/data_catalog.ts +1 -1
- package/tests/unit/modeling/data_domain_change_observers.spec.ts +11 -10
- package/tests/unit/modeling/data_domain_entities.spec.ts +129 -1
- package/tests/unit/modeling/data_domain_property.spec.ts +1 -1
- package/tests/unit/modeling/domain_asociation.spec.ts +177 -0
- package/tests/unit/modeling/domain_entity.spec.ts +27 -26
- package/tests/unit/modeling/domain_entity_example_generator_json.spec.ts +11 -11
- package/tests/unit/modeling/domain_entity_example_generator_xml.spec.ts +10 -10
- package/tests/unit/modeling/{domain.property.spec.ts → domain_property.spec.ts} +139 -23
- package/build/src/amf/AmfShapeGenerator.d.ts +0 -103
- package/build/src/amf/AmfShapeGenerator.d.ts.map +0 -1
- package/build/src/amf/AmfShapeGenerator.js +0 -416
- package/build/src/amf/AmfShapeGenerator.js.map +0 -1
- package/build/src/modeling/legacy/DataAssociation.d.ts +0 -284
- package/build/src/modeling/legacy/DataAssociation.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataAssociation.js +0 -443
- package/build/src/modeling/legacy/DataAssociation.js.map +0 -1
- package/build/src/modeling/legacy/DataEntity.d.ts +0 -358
- package/build/src/modeling/legacy/DataEntity.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataEntity.js +0 -855
- package/build/src/modeling/legacy/DataEntity.js.map +0 -1
- package/build/src/modeling/legacy/DataEntityBuilder.d.ts +0 -162
- package/build/src/modeling/legacy/DataEntityBuilder.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataEntityBuilder.js +0 -221
- package/build/src/modeling/legacy/DataEntityBuilder.js.map +0 -1
- package/build/src/modeling/legacy/DataImpactAnalysis.d.ts +0 -298
- package/build/src/modeling/legacy/DataImpactAnalysis.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataImpactAnalysis.js +0 -441
- package/build/src/modeling/legacy/DataImpactAnalysis.js.map +0 -1
- package/build/src/modeling/legacy/DataModel.d.ts +0 -99
- package/build/src/modeling/legacy/DataModel.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataModel.js +0 -237
- package/build/src/modeling/legacy/DataModel.js.map +0 -1
- package/build/src/modeling/legacy/DataNamespace.d.ts +0 -340
- package/build/src/modeling/legacy/DataNamespace.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataNamespace.js +0 -784
- package/build/src/modeling/legacy/DataNamespace.js.map +0 -1
- package/build/src/modeling/legacy/DataProperty.d.ts +0 -332
- package/build/src/modeling/legacy/DataProperty.d.ts.map +0 -1
- package/build/src/modeling/legacy/DataProperty.js +0 -415
- package/build/src/modeling/legacy/DataProperty.js.map +0 -1
- package/build/src/models/store/DataFile.d.ts +0 -29
- package/build/src/models/store/DataFile.d.ts.map +0 -1
- package/build/src/models/store/DataFile.js +0 -87
- package/build/src/models/store/DataFile.js.map +0 -1
- package/src/amf/AmfShapeGenerator.ts +0 -477
- package/src/modeling/legacy/DataAssociation.ts +0 -554
- package/src/modeling/legacy/DataEntity.ts +0 -1019
- package/src/modeling/legacy/DataEntityBuilder.ts +0 -236
- package/src/modeling/legacy/DataImpactAnalysis.ts +0 -530
- package/src/modeling/legacy/DataModel.ts +0 -276
- package/src/modeling/legacy/DataNamespace.ts +0 -929
- package/src/modeling/legacy/DataProperty.ts +0 -630
- package/src/models/store/DataFile.ts +0 -95
- package/tests/unit/modeling/legacy/amf_shape_generator.spec.ts +0 -1041
- package/tests/unit/modeling/legacy/data_association.spec.ts +0 -710
- package/tests/unit/modeling/legacy/data_entity.spec.ts +0 -2061
- package/tests/unit/modeling/legacy/data_entity_generator_json.spec.ts +0 -987
- package/tests/unit/modeling/legacy/data_entity_generator_xml.spec.ts +0 -1451
- package/tests/unit/modeling/legacy/data_model.spec.ts +0 -395
- package/tests/unit/modeling/legacy/data_namespace.spec.ts +0 -1312
- package/tests/unit/modeling/legacy/data_property.spec.ts +0 -887
- package/tests/unit/modeling/legacy/impact_analysis.spec.ts +0 -373
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
import { AmfShapeGenerator } from '../../amf/AmfShapeGenerator.js';
|
|
2
|
-
import { nanoid } from '../../nanoid.js';
|
|
3
|
-
import { Thing } from '../../models/Thing.js';
|
|
4
|
-
import { ValidationError } from '../../exceptions/validation_error.js';
|
|
5
|
-
import { DataAssociationKind } from '../../models/kinds.js';
|
|
6
|
-
import { DataAttributeAttributes } from '../DataFormat.js';
|
|
7
|
-
/**
|
|
8
|
-
* Describes an association between entities. An association is a relationship defined on an entity
|
|
9
|
-
* that links it to one or more other entities. The association has a `name` which is used as the
|
|
10
|
-
* property name in the context of the parent entity, and it connects to a set of `targets`
|
|
11
|
-
* representing the associated entities.
|
|
12
|
-
*
|
|
13
|
-
* An association can have multiple targets to allow describing the model as `allOf`, `anyOf`, or `oneOf`
|
|
14
|
-
* schemas. When the association has only one target, then the generated schema cannot have union types.
|
|
15
|
-
*
|
|
16
|
-
* Depending on the schema translation, an association can be embedded as a sub-object in the generated
|
|
17
|
-
* schema, or it can be referenced via a primary key.
|
|
18
|
-
*
|
|
19
|
-
* ## Key Concepts
|
|
20
|
-
*
|
|
21
|
-
* - **Targets**: An association's `targets` specify the entities that this association points to.
|
|
22
|
-
* Each target is defined by:
|
|
23
|
-
* - `key`: The key of the target entity.
|
|
24
|
-
* - `namespace` (optional): The key of the namespace where the target entity is defined.
|
|
25
|
-
* If not specified, it defaults to the current namespace.
|
|
26
|
-
*
|
|
27
|
-
* - **Cardinality**:
|
|
28
|
-
* - `multiple` (boolean): Indicates whether the association allows multiple target entities.
|
|
29
|
-
* If `true`, it's a to-many relationship; otherwise, it's a to-one relationship.
|
|
30
|
-
*
|
|
31
|
-
* - **Schema**:
|
|
32
|
-
* - `schema` (optional): A custom schema for the association, allowing manual definition of the
|
|
33
|
-
* association's structure in specific formats (e.g., JSON, RAML, XML).
|
|
34
|
-
* When provided, this schema overrides the automatically generated schema based on the
|
|
35
|
-
* target entities.
|
|
36
|
-
* - **Important**: If a custom schema is defined and changes are made to the referenced
|
|
37
|
-
* entities, those changes **will not** be automatically propagated to the custom schema.
|
|
38
|
-
* This means the custom schema may become outdated and no longer accurately reflect the
|
|
39
|
-
* structure of the target entities.
|
|
40
|
-
*
|
|
41
|
-
* - **Hidden**:
|
|
42
|
-
* - `hidden`: (boolean) Defines if this association is a part of the schema or not.
|
|
43
|
-
* @deprecated
|
|
44
|
-
*/
|
|
45
|
-
export class DataAssociation {
|
|
46
|
-
root;
|
|
47
|
-
kind = DataAssociationKind;
|
|
48
|
-
key = '';
|
|
49
|
-
/**
|
|
50
|
-
* The description of the data association.
|
|
51
|
-
*/
|
|
52
|
-
info = Thing.fromName('');
|
|
53
|
-
/**
|
|
54
|
-
* Wether the data association is required.
|
|
55
|
-
*/
|
|
56
|
-
required;
|
|
57
|
-
/**
|
|
58
|
-
* Whether the data association allows multiple items.
|
|
59
|
-
*/
|
|
60
|
-
multiple;
|
|
61
|
-
/**
|
|
62
|
-
* The list of keys associated with the entity through this association.
|
|
63
|
-
* An association without a target is considered invalid and discarded when processing the values.
|
|
64
|
-
* When multiple associations are set then we are dealing with an union.
|
|
65
|
-
*/
|
|
66
|
-
targets = [];
|
|
67
|
-
/**
|
|
68
|
-
* The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
|
|
69
|
-
*
|
|
70
|
-
* When this is defined then it is used as the schema. When this is not defined it uses
|
|
71
|
-
* referenced entities schemas. Note, changes in the referenced entities may not be propagated
|
|
72
|
-
* to schemas altered by the user.
|
|
73
|
-
*/
|
|
74
|
-
schema;
|
|
75
|
-
/**
|
|
76
|
-
* The list of bindings for this property.
|
|
77
|
-
*
|
|
78
|
-
* A binding defines a translation from a data model to a specific format.
|
|
79
|
-
* For example allows to define properties required to generate AMF shape and therefore RAML/OAS shapes for web APIs
|
|
80
|
-
* or a protocol buffer schema.
|
|
81
|
-
*/
|
|
82
|
-
bindings = [];
|
|
83
|
-
/**
|
|
84
|
-
* Returns true when the association has 0 or 1 targets.
|
|
85
|
-
*/
|
|
86
|
-
get isSingle() {
|
|
87
|
-
return this.targets.length < 2;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Creates an instance of DataAssociation from a target entity key.
|
|
91
|
-
* Note, this entity will have no name. The default name is used instead.
|
|
92
|
-
*
|
|
93
|
-
* @param root The namespace root.
|
|
94
|
-
* @param target The target entity key.
|
|
95
|
-
* @param namespace The optional key of the target association when different to the current one.
|
|
96
|
-
* @returns A new `DataAssociation` instance.
|
|
97
|
-
*/
|
|
98
|
-
static fromTarget(root, target, namespace) {
|
|
99
|
-
const assoc = new DataAssociation(root);
|
|
100
|
-
const info = {
|
|
101
|
-
key: target,
|
|
102
|
-
};
|
|
103
|
-
if (namespace) {
|
|
104
|
-
info.namespace = namespace;
|
|
105
|
-
}
|
|
106
|
-
assoc.targets = [info];
|
|
107
|
-
return assoc;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Creates an instance of DataAssociation from a name, without defining a target.
|
|
111
|
-
*
|
|
112
|
-
* @param root The namespace root.
|
|
113
|
-
* @param name The name of the association.
|
|
114
|
-
* @returns A new `DataAssociation` instance.
|
|
115
|
-
*/
|
|
116
|
-
static fromName(root, name) {
|
|
117
|
-
const assoc = new DataAssociation(root);
|
|
118
|
-
assoc.info = Thing.fromName(name);
|
|
119
|
-
return assoc;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* @param input The data association definition to restore.
|
|
123
|
-
*/
|
|
124
|
-
constructor(root, input) {
|
|
125
|
-
this.root = root;
|
|
126
|
-
let init;
|
|
127
|
-
if (typeof input === 'string') {
|
|
128
|
-
try {
|
|
129
|
-
init = JSON.parse(input);
|
|
130
|
-
}
|
|
131
|
-
catch {
|
|
132
|
-
throw new ValidationError([
|
|
133
|
-
{
|
|
134
|
-
field: '',
|
|
135
|
-
message: 'Unable to parse the data association input as JSON.',
|
|
136
|
-
rule: 'object_required',
|
|
137
|
-
},
|
|
138
|
-
]);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (typeof input === 'object') {
|
|
142
|
-
init = input;
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
init = {
|
|
146
|
-
kind: DataAssociationKind,
|
|
147
|
-
key: nanoid(),
|
|
148
|
-
info: Thing.fromName('Unnamed association').toJSON(),
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
this.new(init);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Initializes the association using provided data.
|
|
155
|
-
*
|
|
156
|
-
* @param init - The data association definition to restore.
|
|
157
|
-
*/
|
|
158
|
-
new(init) {
|
|
159
|
-
DataAssociation.validate(init);
|
|
160
|
-
const { info, key = nanoid(), kind = DataAssociationKind, schema, multiple, required, targets, bindings } = init;
|
|
161
|
-
this.kind = kind;
|
|
162
|
-
this.key = key;
|
|
163
|
-
if (info) {
|
|
164
|
-
this.info = new Thing(info);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
this.info = Thing.fromName('');
|
|
168
|
-
}
|
|
169
|
-
if (schema) {
|
|
170
|
-
this.schema = structuredClone(schema);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
this.schema = undefined;
|
|
174
|
-
}
|
|
175
|
-
if (typeof multiple === 'boolean') {
|
|
176
|
-
this.multiple = multiple;
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
this.multiple = undefined;
|
|
180
|
-
}
|
|
181
|
-
if (typeof required === 'boolean') {
|
|
182
|
-
this.required = required;
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
this.required = undefined;
|
|
186
|
-
}
|
|
187
|
-
if (Array.isArray(targets)) {
|
|
188
|
-
this.targets = targets.map((i) => ({ ...i }));
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
this.targets = [];
|
|
192
|
-
}
|
|
193
|
-
if (Array.isArray(bindings)) {
|
|
194
|
-
this.bindings = bindings.map((i) => structuredClone(i));
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
this.bindings = [];
|
|
198
|
-
}
|
|
199
|
-
return this;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Validates if the input object is a valid `DataAssociation` definition.
|
|
203
|
-
* Throws a `ValidationError` if the input is invalid.
|
|
204
|
-
*
|
|
205
|
-
* @param input The object to validate.
|
|
206
|
-
* @throws {ValidationError} When the validation fails.
|
|
207
|
-
* @deprecated
|
|
208
|
-
*/
|
|
209
|
-
static validate(input) {
|
|
210
|
-
const typed = input;
|
|
211
|
-
const messages = [];
|
|
212
|
-
if (!typed) {
|
|
213
|
-
messages.push({
|
|
214
|
-
field: '',
|
|
215
|
-
message: 'The input is required. None given',
|
|
216
|
-
rule: 'required',
|
|
217
|
-
});
|
|
218
|
-
throw new ValidationError(messages);
|
|
219
|
-
}
|
|
220
|
-
if (typed.kind !== DataAssociationKind) {
|
|
221
|
-
messages.push({
|
|
222
|
-
field: 'kind',
|
|
223
|
-
message: `The kind property must be ${DataAssociationKind}`,
|
|
224
|
-
rule: 'invalid',
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
if (typed.info && typeof typed.info !== 'object') {
|
|
228
|
-
messages.push({
|
|
229
|
-
field: 'info',
|
|
230
|
-
message: 'The info property must be an object',
|
|
231
|
-
rule: 'invalid',
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
if (messages.length) {
|
|
235
|
-
throw new ValidationError(messages);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Generates an object representing the association, suitable for serialization.
|
|
240
|
-
*
|
|
241
|
-
* @returns A plain object representation of the `DataAssociation`.
|
|
242
|
-
*/
|
|
243
|
-
toJSON() {
|
|
244
|
-
const result = {
|
|
245
|
-
kind: DataAssociationKind,
|
|
246
|
-
key: this.key,
|
|
247
|
-
info: this.info.toJSON(),
|
|
248
|
-
};
|
|
249
|
-
if (this.schema) {
|
|
250
|
-
result.schema = structuredClone(this.schema);
|
|
251
|
-
}
|
|
252
|
-
if (typeof this.multiple === 'boolean') {
|
|
253
|
-
result.multiple = this.multiple;
|
|
254
|
-
}
|
|
255
|
-
if (typeof this.required === 'boolean') {
|
|
256
|
-
result.required = this.required;
|
|
257
|
-
}
|
|
258
|
-
if (Array.isArray(this.targets) && this.targets.length) {
|
|
259
|
-
result.targets = this.targets.map((i) => ({ ...i }));
|
|
260
|
-
}
|
|
261
|
-
if (Array.isArray(this.bindings) && this.bindings.length) {
|
|
262
|
-
result.bindings = this.bindings.map((i) => structuredClone(i));
|
|
263
|
-
}
|
|
264
|
-
return result;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Retrieves the list of target entities.
|
|
268
|
-
*
|
|
269
|
-
* @returns An array of `DataEntity` instances representing the targets of this association.
|
|
270
|
-
*/
|
|
271
|
-
getTargets() {
|
|
272
|
-
const { targets } = this;
|
|
273
|
-
return this.root.findAssociatedEntities(targets).filter((i) => !!i);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Removes self from the parent entity and the namespace definition.
|
|
277
|
-
*/
|
|
278
|
-
remove() {
|
|
279
|
-
const { root } = this;
|
|
280
|
-
const entity = root.definitions.entities.find((i) => i.associations.some((j) => j.key === this.key));
|
|
281
|
-
if (entity) {
|
|
282
|
-
const assocIndex = entity.associations.findIndex((i) => i.key === this.key);
|
|
283
|
-
entity.associations.splice(assocIndex, 1);
|
|
284
|
-
}
|
|
285
|
-
const defIndex = this.root.definitions.associations.findIndex((i) => i.key === this.key);
|
|
286
|
-
if (defIndex >= 0) {
|
|
287
|
-
this.root.definitions.associations.splice(defIndex, 1);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* @param init The key of an entity, its instance, or schema.
|
|
292
|
-
* @param namespace - The key of the foreign namespace this entity belongs to.
|
|
293
|
-
* Do not set this value for local entities.
|
|
294
|
-
*/
|
|
295
|
-
addTarget(init, namespace) {
|
|
296
|
-
let key;
|
|
297
|
-
if (typeof init === 'string') {
|
|
298
|
-
key = init;
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
key = init.key;
|
|
302
|
-
const typed = init;
|
|
303
|
-
if (!namespace && typed.root && typed.root !== this.root) {
|
|
304
|
-
namespace = typed.root.key;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
if (this.targets.some((i) => i.key === key)) {
|
|
308
|
-
const message = `Target ${key} already exists.`;
|
|
309
|
-
throw new ValidationError([
|
|
310
|
-
{
|
|
311
|
-
field: 'targets',
|
|
312
|
-
message,
|
|
313
|
-
rule: 'unique',
|
|
314
|
-
},
|
|
315
|
-
], { message });
|
|
316
|
-
}
|
|
317
|
-
if (namespace && namespace !== this.root.key) {
|
|
318
|
-
const foreignNamespace = this.root.foreign.find((ns) => ns.key === namespace);
|
|
319
|
-
if (!foreignNamespace) {
|
|
320
|
-
throw new Error(`Foreign namespace "${namespace}" does not exist.`);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
const info = {
|
|
324
|
-
key,
|
|
325
|
-
};
|
|
326
|
-
if (namespace) {
|
|
327
|
-
info.namespace = namespace;
|
|
328
|
-
}
|
|
329
|
-
this.targets.push(info);
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Removes a target entity from the targets list.
|
|
333
|
-
*
|
|
334
|
-
* @param init The key of an entity, its instance, or schema.
|
|
335
|
-
*/
|
|
336
|
-
removeTarget(init) {
|
|
337
|
-
let key;
|
|
338
|
-
if (typeof init === 'string') {
|
|
339
|
-
key = init;
|
|
340
|
-
}
|
|
341
|
-
else {
|
|
342
|
-
key = init.key;
|
|
343
|
-
}
|
|
344
|
-
const index = this.targets.findIndex((i) => i.key === key);
|
|
345
|
-
if (index >= 0) {
|
|
346
|
-
this.targets.splice(index, 1);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Creates a Property Shape of AMF.
|
|
351
|
-
* The property itself is auto-generated. If the `schema` is defined then it is used
|
|
352
|
-
* as the `range` of the property. Otherwise basic shape is generated for the range.
|
|
353
|
-
*
|
|
354
|
-
* This is a preferred way of reading the AMF shape as this synchronizes changed
|
|
355
|
-
* data properties with the shape definition.
|
|
356
|
-
*
|
|
357
|
-
* @returns AMF property shape definition.
|
|
358
|
-
*/
|
|
359
|
-
toApiShape() {
|
|
360
|
-
const serializer = new AmfShapeGenerator();
|
|
361
|
-
return serializer.associationProperty(this);
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* @deprecated Use the `getParentInstance()` method instead.
|
|
365
|
-
*/
|
|
366
|
-
getParent() {
|
|
367
|
-
return this.getParentInstance();
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Retrieves the parent entity of this association.
|
|
371
|
-
*
|
|
372
|
-
* @returns The `DataEntity` instance that contains this association.
|
|
373
|
-
*/
|
|
374
|
-
getParentInstance() {
|
|
375
|
-
// this is forced as an association is only created when an entity is created
|
|
376
|
-
// so it has to be defined.
|
|
377
|
-
return this.root.definitions.entities.find((i) => i.associations.some((a) => a.key === this.key));
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Creates the `schema` object if missing and returns it.
|
|
381
|
-
* @returns The schema object.
|
|
382
|
-
*/
|
|
383
|
-
ensureSchema() {
|
|
384
|
-
if (!this.schema) {
|
|
385
|
-
this.schema = {};
|
|
386
|
-
}
|
|
387
|
-
return this.schema;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Checks whether the passed value is one of the supported data property attributes.
|
|
391
|
-
* @param value The value to test
|
|
392
|
-
* @returns True when the passed value is one of the supported data property attributes.
|
|
393
|
-
* @deprecated
|
|
394
|
-
*/
|
|
395
|
-
static isValidAttribute(value) {
|
|
396
|
-
if (typeof value !== 'string') {
|
|
397
|
-
return false;
|
|
398
|
-
}
|
|
399
|
-
return DataAttributeAttributes.includes(value);
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Creates if not existing and returns web bindings definition.
|
|
403
|
-
* @returns The web binding definition
|
|
404
|
-
*/
|
|
405
|
-
getWebBinding() {
|
|
406
|
-
let object = this.bindings.find((i) => i.type === 'web');
|
|
407
|
-
if (!object) {
|
|
408
|
-
object = { type: 'web', schema: {} };
|
|
409
|
-
this.bindings.push(object);
|
|
410
|
-
}
|
|
411
|
-
return object.schema;
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Returns the schema value of the binding, if any was created.
|
|
415
|
-
* @param type The type of the binding to read.
|
|
416
|
-
* @returns The binding schema, if any
|
|
417
|
-
*/
|
|
418
|
-
readBinding(type) {
|
|
419
|
-
const item = this.bindings.find((i) => i.type === type);
|
|
420
|
-
return item?.schema;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Checks whether the association is a child of the given namespace, data model, or an entity.
|
|
424
|
-
* The relationship doesn't have to be direct, as long as the association is in the hierarchy it will return true.
|
|
425
|
-
*
|
|
426
|
-
* @param key The key of the parent to check.
|
|
427
|
-
* @returns True if this data association is a child of the given namespace, data model, or an entity
|
|
428
|
-
*/
|
|
429
|
-
isChildOf(key) {
|
|
430
|
-
if (this.key === key) {
|
|
431
|
-
return false;
|
|
432
|
-
}
|
|
433
|
-
const parent = this.getParentInstance();
|
|
434
|
-
if (!parent) {
|
|
435
|
-
return false;
|
|
436
|
-
}
|
|
437
|
-
if (parent.key === key) {
|
|
438
|
-
return true;
|
|
439
|
-
}
|
|
440
|
-
return parent.isChildOf(key);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
//# sourceMappingURL=DataAssociation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DataAssociation.js","sourceRoot":"","sources":["../../../../src/modeling/legacy/DataAssociation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAElE,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAU,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAGrD,OAAO,EAA+B,eAAe,EAAE,MAAM,sCAAsC,CAAA;AACnG,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAE3D,OAAO,EAAE,uBAAuB,EAA+B,MAAM,kBAAkB,CAAA;AAyEvF;;;;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C,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAuB,CAAA;QAC7E,OAAO,IAAI,EAAE,MAAM,CAAA;IACrB,CAAC;IAED;;;;;;OAMG;IACH,SAAS,CAAC,GAAW;QACnB,IAAI,IAAI,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;YACrB,OAAO,KAAK,CAAA;QACd,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAA;QACvC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,MAAM,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;YACvB,OAAO,IAAI,CAAA;QACb,CAAC;QACD,OAAO,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;CACF","sourcesContent":["import { AmfShapeGenerator } from '../../amf/AmfShapeGenerator.js'\nimport { IApiAssociationShape, IApiPropertyShape } from '../../amf/definitions/Shapes.js'\nimport { nanoid } from '../../nanoid.js'\nimport { IThing, Thing } from '../../models/Thing.js'\nimport { DataEntity, IDataEntity } from './DataEntity.js'\nimport { DataNamespace } from './DataNamespace.js'\nimport { type FieldValidationMessage, ValidationError } from '../../exceptions/validation_error.js'\nimport { DataAssociationKind } from '../../models/kinds.js'\nimport type { AssociationBinding, AssociationBindings, AssociationWebBindings } from '../Bindings.js'\nimport { DataAttributeAttributes, type DataAttributeAttribute } from '../DataFormat.js'\n\n/**\n * Describes association target of an entity\n * @deprecated\n */\nexport interface AssociationTarget {\n /**\n * The key of the associated entity.\n */\n key: string\n /**\n * The key of the namespace to look for entities.\n * By default it is the current (root) namespace. When this is set the program\n * will look into `foreign` of the root namespace for the definition\n * of the entity.\n */\n namespace?: string\n}\n\n/**\n * Describes an association between entities. An association is another property of an entity.\n * The `name` is the name of the property and the value is the associated target or targets.\n *\n * An association can have multiple targets to allow describing the model as `allOf`, `anyOf` and `oneOf` schemas.\n * When the association has only one target, then generated schema cannot have union types.\n *\n * Depending on the schema translation an association can be embedded as a sub-object in the generated schema\n * or can be references via a primary key.\n * @deprecated\n */\nexport interface IDataAssociation {\n kind: typeof DataAssociationKind\n /**\n * The key of the association.\n */\n key: string\n /**\n * The data association description.\n */\n info: IThing\n /**\n * Wether the data association is required.\n */\n required?: boolean\n /**\n * Whether the data association allows multiple items.\n */\n multiple?: boolean\n /**\n * The list of keys associated with the entity through this association.\n * An association without a target is considered invalid and discarded when processing the values.\n * When multiple associations are set then we are dealing with an union.\n */\n targets?: AssociationTarget[]\n /**\n * The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)\n *\n * When this is defined then it is used as the schema. When this is not defined it uses\n * referenced entities schemas. Note, changes in the referenced entities may not be propagated\n * to schemas altered by the user.\n */\n schema?: IApiAssociationShape\n /**\n * The list of bindings for this property.\n *\n * A binding defines a translation from a data model to a specific format.\n * For example allows to define properties required to generate AMF shape and therefore RAML/OAS shapes for web APIs\n * or a protocol buffer schema.\n */\n bindings?: AssociationBinding[]\n}\n\n/**\n * Describes an association between entities. An association is a relationship defined on an entity\n * that links it to one or more other entities. The association has a `name` which is used as the\n * property name in the context of the parent entity, and it connects to a set of `targets`\n * representing the associated entities.\n *\n * An association can have multiple targets to allow describing the model as `allOf`, `anyOf`, or `oneOf`\n * schemas. When the association has only one target, then the generated schema cannot have union types.\n *\n * Depending on the schema translation, an association can be embedded as a sub-object in the generated\n * schema, or it can be referenced via a primary key.\n *\n * ## Key Concepts\n *\n * - **Targets**: An association's `targets` specify the entities that this association points to.\n * Each target is defined by:\n * - `key`: The key of the target entity.\n * - `namespace` (optional): The key of the namespace where the target entity is defined.\n * If not specified, it defaults to the current namespace.\n *\n * - **Cardinality**:\n * - `multiple` (boolean): Indicates whether the association allows multiple target entities.\n * If `true`, it's a to-many relationship; otherwise, it's a to-one relationship.\n *\n * - **Schema**:\n * - `schema` (optional): A custom schema for the association, allowing manual definition of the\n * association's structure in specific formats (e.g., JSON, RAML, XML).\n * When provided, this schema overrides the automatically generated schema based on the\n * target entities.\n * - **Important**: If a custom schema is defined and changes are made to the referenced\n * entities, those changes **will not** be automatically propagated to the custom schema.\n * This means the custom schema may become outdated and no longer accurately reflect the\n * structure of the target entities.\n *\n * - **Hidden**:\n * - `hidden`: (boolean) Defines if this association is a part of the schema or not.\n * @deprecated\n */\nexport class DataAssociation {\n kind = DataAssociationKind\n\n key = ''\n\n /**\n * The description of the data association.\n */\n info: Thing = Thing.fromName('')\n\n /**\n * Wether the data association is required.\n */\n required?: boolean\n\n /**\n * Whether the data association allows multiple items.\n */\n multiple?: boolean\n\n /**\n * The list of keys associated with the entity through this association.\n * An association without a target is considered invalid and discarded when processing the values.\n * When multiple associations are set then we are dealing with an union.\n */\n targets: AssociationTarget[] = []\n\n /**\n * The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)\n *\n * When this is defined then it is used as the schema. When this is not defined it uses\n * referenced entities schemas. Note, changes in the referenced entities may not be propagated\n * to schemas altered by the user.\n */\n schema?: IApiAssociationShape\n\n /**\n * The list of bindings for this property.\n *\n * A binding defines a translation from a data model to a specific format.\n * For example allows to define properties required to generate AMF shape and therefore RAML/OAS shapes for web APIs\n * or a protocol buffer schema.\n */\n bindings: AssociationBinding[] = []\n\n /**\n * Returns true when the association has 0 or 1 targets.\n */\n get isSingle(): boolean {\n return this.targets.length < 2\n }\n\n /**\n * Creates an instance of DataAssociation from a target entity key.\n * Note, this entity will have no name. The default name is used instead.\n *\n * @param root The namespace root.\n * @param target The target entity key.\n * @param namespace The optional key of the target association when different to the current one.\n * @returns A new `DataAssociation` instance.\n */\n static fromTarget(root: DataNamespace, target: string, namespace?: string): DataAssociation {\n const assoc = new DataAssociation(root)\n const info: AssociationTarget = {\n key: target,\n }\n if (namespace) {\n info.namespace = namespace\n }\n assoc.targets = [info]\n return assoc\n }\n\n /**\n * Creates an instance of DataAssociation from a name, without defining a target.\n *\n * @param root The namespace root.\n * @param name The name of the association.\n * @returns A new `DataAssociation` instance.\n */\n static fromName(root: DataNamespace, name: string): DataAssociation {\n const assoc = new DataAssociation(root)\n assoc.info = Thing.fromName(name)\n return assoc\n }\n\n /**\n * @param input The data association definition to restore.\n */\n constructor(\n public root: DataNamespace,\n input?: string | IDataAssociation\n ) {\n let init: IDataAssociation\n if (typeof input === 'string') {\n try {\n init = JSON.parse(input)\n } catch {\n throw new ValidationError([\n {\n field: '',\n message: 'Unable to parse the data association input as JSON.',\n rule: 'object_required',\n },\n ])\n }\n } else if (typeof input === 'object') {\n init = input\n } else {\n init = {\n kind: DataAssociationKind,\n key: nanoid(),\n info: Thing.fromName('Unnamed association').toJSON(),\n }\n }\n this.new(init)\n }\n\n /**\n * Initializes the association using provided data.\n *\n * @param init - The data association definition to restore.\n */\n new(init: IDataAssociation): this {\n DataAssociation.validate(init)\n const { info, key = nanoid(), kind = DataAssociationKind, schema, multiple, required, targets, bindings } = init\n this.kind = kind\n this.key = key\n if (info) {\n this.info = new Thing(info)\n } else {\n this.info = Thing.fromName('')\n }\n if (schema) {\n this.schema = structuredClone(schema)\n } else {\n this.schema = undefined\n }\n if (typeof multiple === 'boolean') {\n this.multiple = multiple\n } else {\n this.multiple = undefined\n }\n if (typeof required === 'boolean') {\n this.required = required\n } else {\n this.required = undefined\n }\n if (Array.isArray(targets)) {\n this.targets = targets.map((i) => ({ ...i }))\n } else {\n this.targets = []\n }\n if (Array.isArray(bindings)) {\n this.bindings = bindings.map((i) => structuredClone(i))\n } else {\n this.bindings = []\n }\n return this\n }\n\n /**\n * Validates if the input object is a valid `DataAssociation` definition.\n * Throws a `ValidationError` if the input is invalid.\n *\n * @param input The object to validate.\n * @throws {ValidationError} When the validation fails.\n * @deprecated\n */\n static validate(input: unknown): void {\n const typed = input as IDataAssociation\n const messages: FieldValidationMessage[] = []\n if (!typed) {\n messages.push({\n field: '',\n message: 'The input is required. None given',\n rule: 'required',\n })\n throw new ValidationError(messages)\n }\n if (typed.kind !== DataAssociationKind) {\n messages.push({\n field: 'kind',\n message: `The kind property must be ${DataAssociationKind}`,\n rule: 'invalid',\n })\n }\n if (typed.info && typeof typed.info !== 'object') {\n messages.push({\n field: 'info',\n message: 'The info property must be an object',\n rule: 'invalid',\n })\n }\n if (messages.length) {\n throw new ValidationError(messages)\n }\n }\n\n /**\n * Generates an object representing the association, suitable for serialization.\n *\n * @returns A plain object representation of the `DataAssociation`.\n */\n toJSON(): IDataAssociation {\n const result: IDataAssociation = {\n kind: DataAssociationKind,\n key: this.key,\n info: this.info.toJSON(),\n }\n if (this.schema) {\n result.schema = structuredClone(this.schema)\n }\n if (typeof this.multiple === 'boolean') {\n result.multiple = this.multiple\n }\n if (typeof this.required === 'boolean') {\n result.required = this.required\n }\n if (Array.isArray(this.targets) && this.targets.length) {\n result.targets = this.targets.map((i) => ({ ...i }))\n }\n if (Array.isArray(this.bindings) && this.bindings.length) {\n result.bindings = this.bindings.map((i) => structuredClone(i))\n }\n return result\n }\n\n /**\n * Retrieves the list of target entities.\n *\n * @returns An array of `DataEntity` instances representing the targets of this association.\n */\n getTargets(): DataEntity[] {\n const { targets } = this\n return this.root.findAssociatedEntities(targets).filter((i) => !!i) as DataEntity[]\n }\n\n /**\n * Removes self from the parent entity and the namespace definition.\n */\n remove(): void {\n const { root } = this\n const entity = root.definitions.entities.find((i) => i.associations.some((j) => j.key === this.key))\n if (entity) {\n const assocIndex = entity.associations.findIndex((i) => i.key === this.key)\n entity.associations.splice(assocIndex, 1)\n }\n const defIndex = this.root.definitions.associations.findIndex((i) => i.key === this.key)\n if (defIndex >= 0) {\n this.root.definitions.associations.splice(defIndex, 1)\n }\n }\n\n /**\n * Adds a target entity to the association.\n * If the entity comes from another namespace then it is set as a foreign entity.\n *\n * @param entity - The `DataEntity` instance to add.\n */\n addTarget(entity: DataEntity): void\n\n /**\n * Adds a target entity to the association.\n *\n * @param entity - The `IDataEntity` definition or just its key.\n * @param namespace - The key of the foreign namespace this entity belongs to.\n * Do not set this value for local entities.\n */\n addTarget(entity: IDataEntity | string, namespace?: string): void\n\n /**\n * @param init The key of an entity, its instance, or schema.\n * @param namespace - The key of the foreign namespace this entity belongs to.\n * Do not set this value for local entities.\n */\n addTarget(init: string | DataEntity | IDataEntity, namespace?: string): void {\n let key: string\n if (typeof init === 'string') {\n key = init\n } else {\n key = init.key\n const typed = init as IDataEntity & { root?: DataNamespace }\n if (!namespace && typed.root && typed.root !== this.root) {\n namespace = typed.root.key\n }\n }\n if (this.targets.some((i) => i.key === key)) {\n const message = `Target ${key} already exists.`\n throw new ValidationError(\n [\n {\n field: 'targets',\n message,\n rule: 'unique',\n },\n ],\n { message }\n )\n }\n if (namespace && namespace !== this.root.key) {\n const foreignNamespace = this.root.foreign.find((ns) => ns.key === namespace)\n if (!foreignNamespace) {\n throw new Error(`Foreign namespace \"${namespace}\" does not exist.`)\n }\n }\n const info: AssociationTarget = {\n key,\n }\n if (namespace) {\n info.namespace = namespace\n }\n this.targets.push(info)\n }\n\n /**\n * Removes a target entity from the targets list.\n *\n * @param init The key of an entity, its instance, or schema.\n */\n removeTarget(init: string | DataEntity | IDataEntity): void {\n let key: string\n if (typeof init === 'string') {\n key = init\n } else {\n key = init.key\n }\n const index = this.targets.findIndex((i) => i.key === key)\n if (index >= 0) {\n this.targets.splice(index, 1)\n }\n }\n\n /**\n * Creates a Property Shape of AMF.\n * The property itself is auto-generated. If the `schema` is defined then it is used\n * as the `range` of the property. Otherwise basic shape is generated for the range.\n *\n * This is a preferred way of reading the AMF shape as this synchronizes changed\n * data properties with the shape definition.\n *\n * @returns AMF property shape definition.\n */\n toApiShape(): IApiPropertyShape {\n const serializer = new AmfShapeGenerator()\n return serializer.associationProperty(this)\n }\n\n /**\n * @deprecated Use the `getParentInstance()` method instead.\n */\n getParent(): DataEntity {\n return this.getParentInstance()\n }\n\n /**\n * Retrieves the parent entity of this association.\n *\n * @returns The `DataEntity` instance that contains this association.\n */\n getParentInstance(): DataEntity {\n // this is forced as an association is only created when an entity is created\n // so it has to be defined.\n return this.root.definitions.entities.find((i) => i.associations.some((a) => a.key === this.key)) as DataEntity\n }\n\n /**\n * Creates the `schema` object if missing and returns it.\n * @returns The schema object.\n */\n ensureSchema(): IApiAssociationShape {\n if (!this.schema) {\n this.schema = {}\n }\n return this.schema\n }\n\n /**\n * Checks whether the passed value is one of the supported data property attributes.\n * @param value The value to test\n * @returns True when the passed value is one of the supported data property attributes.\n * @deprecated\n */\n static isValidAttribute(value: unknown): value is DataAttributeAttribute {\n if (typeof value !== 'string') {\n return false\n }\n return DataAttributeAttributes.includes(value as DataAttributeAttribute)\n }\n\n /**\n * Creates if not existing and returns web bindings definition.\n * @returns The web binding definition\n */\n getWebBinding(): AssociationWebBindings {\n let object = this.bindings.find((i) => i.type === 'web') as AssociationBinding | undefined\n if (!object) {\n object = { type: 'web', schema: {} } as AssociationBinding\n this.bindings.push(object)\n }\n return object.schema as AssociationWebBindings\n }\n\n /**\n * Returns the schema value of the binding, if any was created.\n * @param type The type of the binding to read.\n * @returns The binding schema, if any\n */\n readBinding(type: 'web' | 'protobuf'): AssociationBindings | undefined {\n const item = this.bindings.find((i) => i.type === type) as AssociationBinding\n return item?.schema\n }\n\n /**\n * Checks whether the association is a child of the given namespace, data model, or an entity.\n * The relationship doesn't have to be direct, as long as the association is in the hierarchy it will return true.\n *\n * @param key The key of the parent to check.\n * @returns True if this data association is a child of the given namespace, data model, or an entity\n */\n isChildOf(key: string): boolean {\n if (this.key === key) {\n return false\n }\n const parent = this.getParentInstance()\n if (!parent) {\n return false\n }\n if (parent.key === key) {\n return true\n }\n return parent.isChildOf(key)\n }\n}\n"]}
|