@api-client/core 0.11.1 → 0.11.3
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/modeling/DataAssociation.d.ts +10 -9
- package/build/src/modeling/DataAssociation.d.ts.map +1 -1
- package/build/src/modeling/DataAssociation.js +14 -0
- package/build/src/modeling/DataAssociation.js.map +1 -1
- package/build/src/modeling/DataEntity.d.ts +64 -9
- package/build/src/modeling/DataEntity.d.ts.map +1 -1
- package/build/src/modeling/DataEntity.js +128 -16
- package/build/src/modeling/DataEntity.js.map +1 -1
- package/build/src/modeling/DataFormat.d.ts +8 -0
- package/build/src/modeling/DataFormat.d.ts.map +1 -1
- package/build/src/modeling/DataFormat.js +1 -0
- package/build/src/modeling/DataFormat.js.map +1 -1
- package/build/src/modeling/DataNamespace.d.ts +23 -1
- package/build/src/modeling/DataNamespace.d.ts.map +1 -1
- package/build/src/modeling/DataNamespace.js +39 -0
- package/build/src/modeling/DataNamespace.js.map +1 -1
- package/data/models/example-generator-api.json +8 -8
- package/package.json +1 -1
- package/src/modeling/DataAssociation.ts +15 -15
- package/src/modeling/DataEntity.ts +152 -16
- package/src/modeling/DataFormat.ts +10 -0
- package/src/modeling/DataNamespace.ts +45 -1
- package/src/modeling/readme.md +134 -0
- package/tests/unit/modeling/data_entity.spec.ts +481 -0
- package/tests/unit/modeling/data_namespace.spec.ts +245 -0
|
@@ -4,6 +4,7 @@ import { DataEntity, IDataEntity } from './DataEntity.js';
|
|
|
4
4
|
import { DataNamespace } from './DataNamespace.js';
|
|
5
5
|
import { DataAssociationKind } from '../models/kinds.js';
|
|
6
6
|
import type { AssociationBinding, AssociationBindings, AssociationWebBindings } from './Bindings.js';
|
|
7
|
+
import { type DataAttributeAttribute } from './DataFormat.js';
|
|
7
8
|
/**
|
|
8
9
|
* Describes association target of an entity
|
|
9
10
|
*/
|
|
@@ -218,17 +219,11 @@ export declare class DataAssociation {
|
|
|
218
219
|
*/
|
|
219
220
|
addTarget(entity: IDataEntity | string, namespace?: string): void;
|
|
220
221
|
/**
|
|
221
|
-
* Removes a target entity from the
|
|
222
|
+
* Removes a target entity from the targets list.
|
|
222
223
|
*
|
|
223
|
-
* @param
|
|
224
|
+
* @param init The key of an entity, its instance, or schema.
|
|
224
225
|
*/
|
|
225
|
-
removeTarget(
|
|
226
|
-
/**
|
|
227
|
-
* Removes a target entity from the association.
|
|
228
|
-
*
|
|
229
|
-
* @param key - The key of the target entity to remove.
|
|
230
|
-
*/
|
|
231
|
-
removeTarget(key: string): void;
|
|
226
|
+
removeTarget(init: string | DataEntity | IDataEntity): void;
|
|
232
227
|
/**
|
|
233
228
|
* Creates a Property Shape of AMF.
|
|
234
229
|
* The property itself is auto-generated. If the `schema` is defined then it is used
|
|
@@ -251,6 +246,12 @@ export declare class DataAssociation {
|
|
|
251
246
|
* @returns The schema object.
|
|
252
247
|
*/
|
|
253
248
|
ensureSchema(): IApiAssociationShape;
|
|
249
|
+
/**
|
|
250
|
+
* Checks whether the passed value is one of the supported data proprty attributes.
|
|
251
|
+
* @param value The value to test
|
|
252
|
+
* @returns True when the passed value is one of the supported data proprty attributes.
|
|
253
|
+
*/
|
|
254
|
+
static isValidAttribute(value: unknown): value is DataAttributeAttribute;
|
|
254
255
|
/**
|
|
255
256
|
* Creates if not existing and returns web bindings definition.
|
|
256
257
|
* @returns The web binding definition
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataAssociation.d.ts","sourceRoot":"","sources":["../../../src/modeling/DataAssociation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAEtF,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,EAAE,mBAA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M,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI;IAiCjE;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,IAAI;IAa3D;;;;;;;;;OASG;IACH,UAAU,IAAI,iBAAiB;IAK/B;;;;OAIG;IACH,SAAS,IAAI,UAAU;IAMvB;;;OAGG;IACH,YAAY,IAAI,oBAAoB;IAOpC;;;;OAIG;IACH,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,sBAAsB;IAOxE;;;OAGG;IACH,aAAa,IAAI,sBAAsB;IASvC;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,KAAK,GAAG,UAAU,GAAG,mBAAmB,GAAG,SAAS;CAIvE"}
|
|
@@ -4,6 +4,7 @@ import v4 from '../lib/uuid.js';
|
|
|
4
4
|
import { Thing } from '../models/Thing.js';
|
|
5
5
|
import { ValidationError } from '../exceptions/validation_error.js';
|
|
6
6
|
import { DataAssociationKind } from '../models/kinds.js';
|
|
7
|
+
import { DataAttributeAttributes } from './DataFormat.js';
|
|
7
8
|
/**
|
|
8
9
|
* Describes an association between entities. An association is a relationship defined on an entity
|
|
9
10
|
* that links it to one or more other entities. The association has a `name` which is used as the
|
|
@@ -287,6 +288,8 @@ export class DataAssociation {
|
|
|
287
288
|
}
|
|
288
289
|
/**
|
|
289
290
|
* @param init The key of an entity, its instance, or schema.
|
|
291
|
+
* @param namespace - The key of the foreign namespace this entity belongs to.
|
|
292
|
+
* Do not set this value for local entities.
|
|
290
293
|
*/
|
|
291
294
|
addTarget(init, namespace) {
|
|
292
295
|
let key;
|
|
@@ -366,6 +369,17 @@ export class DataAssociation {
|
|
|
366
369
|
}
|
|
367
370
|
return this.schema;
|
|
368
371
|
}
|
|
372
|
+
/**
|
|
373
|
+
* Checks whether the passed value is one of the supported data proprty attributes.
|
|
374
|
+
* @param value The value to test
|
|
375
|
+
* @returns True when the passed value is one of the supported data proprty attributes.
|
|
376
|
+
*/
|
|
377
|
+
static isValidAttribute(value) {
|
|
378
|
+
if (typeof value !== 'string') {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
return DataAttributeAttributes.includes(value);
|
|
382
|
+
}
|
|
369
383
|
/**
|
|
370
384
|
* Creates if not existing and returns web bindings definition.
|
|
371
385
|
* @returns The web binding definition
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataAssociation.js","sourceRoot":"","sources":["../../../src/modeling/DataAssociation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAA;AAE/D,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAC/B,OAAO,EAAU,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAGlD,OAAO,EAA+B,eAAe,EAAE,MAAM,mCAAmC,CAAA;AAChG,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAwExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,OAAO,eAAe;IA0FjB;IAzFT,IAAI,GAAG,mBAAmB,CAAA;IAE1B,GAAG,GAAG,EAAE,CAAA;IAER;;OAEG;IACH,IAAI,GAAU,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IAEhC;;OAEG;IACH,QAAQ,CAAU;IAElB;;OAEG;IACH,QAAQ,CAAU;IAElB;;;;OAIG;IACH,OAAO,GAAwB,EAAE,CAAA;IAEjC;;;;;;OAMG;IACH,MAAM,CAAuB;IAE7B;;;;;;OAMG;IACH,QAAQ,GAAyB,EAAE,CAAA;IAEnC;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAA;IAChC,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,UAAU,CAAC,IAAmB,EAAE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,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAuB,CAAA;QAC7E,OAAO,IAAI,EAAE,MAAM,CAAA;IACrB,CAAC;CACF","sourcesContent":["import { Json } from '@api-client/json'\nimport { AmfShapeGenerator } from '../amf/AmfShapeGenerator.js'\nimport { IApiAssociationShape, IApiPropertyShape } from '../amf/definitions/Shapes.js'\nimport v4 from '../lib/uuid.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'\n\n/**\n * Describes association target of an entity\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 */\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 */\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: v4(),\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 = v4(), 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 = Json.clone(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) => Json.clone(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 */\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 = Json.clone(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) => Json.clone(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 === this))\n if (entity) {\n const assocIndex = entity.associations.findIndex((i) => i === this)\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 */\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 (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 association.\n *\n * @param entity - The `DataEntity` or `IDataEntity` instance to remove.\n */\n removeTarget(entity: DataEntity | IDataEntity): void\n\n /**\n * Removes a target entity from the association.\n *\n * @param key - The key of the target entity to remove.\n */\n // eslint-disable-next-line @typescript-eslint/unified-signatures\n removeTarget(key: string): void\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 * Retrieves the parent entity of this association.\n *\n * @returns The `DataEntity` instance that contains this association.\n */\n getParent(): 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.includes(this)) 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 * 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"]}
|
|
1
|
+
{"version":3,"file":"DataAss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C,CAAC,CAAC,IAAI,KAAK,IAAI,CAAuB,CAAA;QAC7E,OAAO,IAAI,EAAE,MAAM,CAAA;IACrB,CAAC;CACF","sourcesContent":["import { Json } from '@api-client/json'\nimport { AmfShapeGenerator } from '../amf/AmfShapeGenerator.js'\nimport { IApiAssociationShape, IApiPropertyShape } from '../amf/definitions/Shapes.js'\nimport v4 from '../lib/uuid.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 */\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 */\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 */\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: v4(),\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 = v4(), 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 = Json.clone(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) => Json.clone(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 */\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 = Json.clone(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) => Json.clone(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 === this))\n if (entity) {\n const assocIndex = entity.associations.findIndex((i) => i === this)\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 (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 * Retrieves the parent entity of this association.\n *\n * @returns The `DataEntity` instance that contains this association.\n */\n getParent(): 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.includes(this)) 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 proprty attributes.\n * @param value The value to test\n * @returns True when the passed value is one of the supported data proprty attributes.\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"]}
|
|
@@ -8,6 +8,10 @@ import { DataEntityKind } from '../models/kinds.js';
|
|
|
8
8
|
import { DataModel } from './DataModel.js';
|
|
9
9
|
import { IApiNodeShape, IShapeUnion } from '../amf/definitions/Shapes.js';
|
|
10
10
|
import { IShapeRenderOptions } from '../amf/shape/ShapeBase.js';
|
|
11
|
+
interface OrderedItem {
|
|
12
|
+
type: 'property' | 'association';
|
|
13
|
+
key: string;
|
|
14
|
+
}
|
|
11
15
|
/**
|
|
12
16
|
* Data entity is the smallest description of a data in the system
|
|
13
17
|
* It contains properties and associations. At least one entity describe a data model.
|
|
@@ -40,6 +44,13 @@ export interface IDataEntity {
|
|
|
40
44
|
* The list of keys of associations that belong to this entity.
|
|
41
45
|
*/
|
|
42
46
|
associations?: string[];
|
|
47
|
+
/**
|
|
48
|
+
* The ordered list of fields (properties and associations) in the schema.
|
|
49
|
+
* These only keep references to the properties and associations.
|
|
50
|
+
* The order of the fields is important as it defines the order of the
|
|
51
|
+
* properties in the schema.
|
|
52
|
+
*/
|
|
53
|
+
fields?: OrderedItem[];
|
|
43
54
|
/**
|
|
44
55
|
* The list of keys of entities that are parents to this entity.
|
|
45
56
|
*
|
|
@@ -48,6 +59,9 @@ export interface IDataEntity {
|
|
|
48
59
|
* with the same name to shadow parent property. When the property is
|
|
49
60
|
* not shadowed this may cause unexpected results as the processing could result
|
|
50
61
|
* with inconsistent definition of a schema because the last read property wins.
|
|
62
|
+
*
|
|
63
|
+
* @todo(jarrodek): This should also hold a reference to a namespace to support
|
|
64
|
+
* foreign entities as parents.
|
|
51
65
|
*/
|
|
52
66
|
parents?: string[];
|
|
53
67
|
/**
|
|
@@ -89,6 +103,13 @@ export declare class DataEntity {
|
|
|
89
103
|
* The list of keys of associations that belong to this entity.
|
|
90
104
|
*/
|
|
91
105
|
associations: DataAssociation[];
|
|
106
|
+
/**
|
|
107
|
+
* The ordered list of fields (properties and associations) in the schema.
|
|
108
|
+
* These only keep references to the properties and associations.
|
|
109
|
+
* The order of the fields is important as it defines the order of the
|
|
110
|
+
* properties in the schema.
|
|
111
|
+
*/
|
|
112
|
+
fields: OrderedItem[];
|
|
92
113
|
/**
|
|
93
114
|
* The list of keys of entities that are parents to this entity.
|
|
94
115
|
*
|
|
@@ -109,6 +130,7 @@ export declare class DataEntity {
|
|
|
109
130
|
*/
|
|
110
131
|
constructor(root: DataNamespace, input?: string | IDataEntity);
|
|
111
132
|
new(init: IDataEntity): void;
|
|
133
|
+
private createOrderedFields;
|
|
112
134
|
static validate(input: unknown): void;
|
|
113
135
|
toJSON(): IDataEntity;
|
|
114
136
|
protected _readAssociation(key: string): DataAssociation | undefined;
|
|
@@ -130,11 +152,29 @@ export declare class DataEntity {
|
|
|
130
152
|
* @param key The key of the property to remove.
|
|
131
153
|
*/
|
|
132
154
|
removeProperty(key: string): void;
|
|
155
|
+
private removeField;
|
|
133
156
|
/**
|
|
134
157
|
* Lists all properties for this Entity.
|
|
135
158
|
* @returns The list of properties that belong to this entity.
|
|
136
159
|
*/
|
|
137
160
|
listProperties(): DataProperty[];
|
|
161
|
+
/**
|
|
162
|
+
* Lists all associations for this Entity.
|
|
163
|
+
* @returns The list of associations that belong to this entity.
|
|
164
|
+
*/
|
|
165
|
+
listAssociations(): DataAssociation[];
|
|
166
|
+
/**
|
|
167
|
+
* Generates an ordered list of fields (properties and associations) for this entity.
|
|
168
|
+
*
|
|
169
|
+
* @returns The list of fields (properties and associations) that belong to this entity.
|
|
170
|
+
*/
|
|
171
|
+
listFields(): (DataAssociation | DataProperty)[];
|
|
172
|
+
/**
|
|
173
|
+
* Moves the field to a new index.
|
|
174
|
+
* @param key The key of the field to move.
|
|
175
|
+
* @param toIndex The new index to which move the field to.
|
|
176
|
+
*/
|
|
177
|
+
moveField(key: string, toIndex: number): void;
|
|
138
178
|
/**
|
|
139
179
|
* Creates an association for a given name, adds it to definitions, and returns it.
|
|
140
180
|
* @param name The name of the association
|
|
@@ -186,6 +226,12 @@ export declare class DataEntity {
|
|
|
186
226
|
* Returns a parent data model where this entity exist.
|
|
187
227
|
*/
|
|
188
228
|
getParent(): DataModel | undefined;
|
|
229
|
+
/**
|
|
230
|
+
* Adds a parent reference to this entity.
|
|
231
|
+
* This will not add the parent to the namespace. It only adds a reference to the parent.
|
|
232
|
+
* @param key The key of the parent entity to add.
|
|
233
|
+
* @returns `this` for chaining.
|
|
234
|
+
*/
|
|
189
235
|
addParent(key: string): this;
|
|
190
236
|
/**
|
|
191
237
|
* Checks if the entity has a circular parent relationship when attempting to add a new parent.
|
|
@@ -215,22 +261,30 @@ export declare class DataEntity {
|
|
|
215
261
|
*/
|
|
216
262
|
associationPath(toEntity: string): Generator<string[]>;
|
|
217
263
|
/**
|
|
218
|
-
* Returns a list of entities that are associated with this entity
|
|
219
|
-
*
|
|
264
|
+
* Returns a list of entities that are associated with this entity as a target.
|
|
265
|
+
*
|
|
266
|
+
* This method identifies entities that have an association where this entity is a target.
|
|
267
|
+
* In other words, it finds entities that "point to" this entity through an association.
|
|
220
268
|
*
|
|
221
|
-
*
|
|
222
|
-
* result with `[A]`.
|
|
269
|
+
* For example, consider the following relationships:
|
|
223
270
|
*
|
|
224
271
|
* ```plain
|
|
225
|
-
* A -> B
|
|
226
|
-
*
|
|
272
|
+
* A -> B (A has an association that targets B)
|
|
273
|
+
* C -> B (C has an association that targets B)
|
|
274
|
+
* B -> D (B has an association that targets D)
|
|
227
275
|
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
276
|
+
* Calling `getRelatedEntities()` on B would return [A, C]
|
|
277
|
+
* Calling `getRelatedEntities()` on D would return [B]
|
|
278
|
+
* Calling `getRelatedEntities()` on A would return []
|
|
230
279
|
* ```
|
|
280
|
+
*
|
|
281
|
+
* Note, this method only returns entities that are directly associated with this entity as a target.
|
|
282
|
+
* It does not traverse the association graph to find indirectly related entities.
|
|
283
|
+
*
|
|
284
|
+
* @returns An array of `DataEntity` instances that have an association targeting this entity.
|
|
231
285
|
*/
|
|
232
286
|
getRelatedEntities(): DataEntity[];
|
|
233
|
-
static getRelatedEntities(namespace: DataNamespace,
|
|
287
|
+
static getRelatedEntities(namespace: DataNamespace, entityKey: string): DataEntity[];
|
|
234
288
|
/**
|
|
235
289
|
* Creates breadcrumbs from this entity to the root namespace.
|
|
236
290
|
*/
|
|
@@ -266,4 +320,5 @@ export declare class DataEntity {
|
|
|
266
320
|
toExample(mime: string, opts?: IShapeRenderOptions): string | number | boolean | null | undefined;
|
|
267
321
|
hasClosedCycle(rootEntity: string, targetEntity: string): boolean;
|
|
268
322
|
}
|
|
323
|
+
export {};
|
|
269
324
|
//# sourceMappingURL=DataEntity.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataEntity.d.ts","sourceRoot":"","sources":["../../../src/modeling/DataEntity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAA;AAGzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAA;AAO/D;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,OAAO,cAAc,CAAA;IAC3B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;O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;;;OAKG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IA8D5B;;;;OAIG;IACH,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAsBvC;;;;;;OAMG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO;IAItE;;;;OAIG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAMrC;;;;;OAKG;IACF,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC;IAOvD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,kBAAkB,IAAI,UAAU,EAAE;IAclC,MAAM,CAAC,kBAAkB,CAAC,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,EAAE;IAYpF;;OAEG;IACH,WAAW,IAAI,cAAc,EAAE;IAgC/B;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAgBzB;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAY5B;;;;;;;;;OASG;IACH,UAAU,IAAI,WAAW;IAKzB;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,mBAAwB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS;IAWrG,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO;CAyBlE"}
|
|
@@ -40,6 +40,13 @@ export class DataEntity {
|
|
|
40
40
|
* The list of keys of associations that belong to this entity.
|
|
41
41
|
*/
|
|
42
42
|
associations = [];
|
|
43
|
+
/**
|
|
44
|
+
* The ordered list of fields (properties and associations) in the schema.
|
|
45
|
+
* These only keep references to the properties and associations.
|
|
46
|
+
* The order of the fields is important as it defines the order of the
|
|
47
|
+
* properties in the schema.
|
|
48
|
+
*/
|
|
49
|
+
fields = [];
|
|
43
50
|
/**
|
|
44
51
|
* The list of keys of entities that are parents to this entity.
|
|
45
52
|
*
|
|
@@ -82,7 +89,7 @@ export class DataEntity {
|
|
|
82
89
|
}
|
|
83
90
|
new(init) {
|
|
84
91
|
DataEntity.validate(init);
|
|
85
|
-
const { info, key = v4(), kind = DataEntityKind, tags, taxonomy, parents, properties, associations, deprecated, } = init;
|
|
92
|
+
const { info, key = v4(), kind = DataEntityKind, tags, taxonomy, parents, properties, associations, fields, deprecated, } = init;
|
|
86
93
|
this.kind = kind;
|
|
87
94
|
this.key = key;
|
|
88
95
|
if (info) {
|
|
@@ -127,6 +134,12 @@ export class DataEntity {
|
|
|
127
134
|
}
|
|
128
135
|
});
|
|
129
136
|
}
|
|
137
|
+
if (Array.isArray(fields)) {
|
|
138
|
+
this.fields = [...fields];
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this.fields = this.createOrderedFields();
|
|
142
|
+
}
|
|
130
143
|
if (typeof deprecated === 'boolean') {
|
|
131
144
|
this.deprecated = deprecated;
|
|
132
145
|
}
|
|
@@ -134,6 +147,22 @@ export class DataEntity {
|
|
|
134
147
|
this.deprecated = undefined;
|
|
135
148
|
}
|
|
136
149
|
}
|
|
150
|
+
createOrderedFields() {
|
|
151
|
+
const result = [];
|
|
152
|
+
this.properties.forEach((i) => {
|
|
153
|
+
result.push({
|
|
154
|
+
type: 'property',
|
|
155
|
+
key: i.key,
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
this.associations.forEach((i) => {
|
|
159
|
+
result.push({
|
|
160
|
+
type: 'association',
|
|
161
|
+
key: i.key,
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
137
166
|
static validate(input) {
|
|
138
167
|
const typed = input;
|
|
139
168
|
const messages = [];
|
|
@@ -177,6 +206,15 @@ export class DataEntity {
|
|
|
177
206
|
if (Array.isArray(this.associations) && this.associations.length) {
|
|
178
207
|
result.associations = this.associations.map((i) => i.key);
|
|
179
208
|
}
|
|
209
|
+
if (Array.isArray(this.fields) && this.fields.length) {
|
|
210
|
+
result.fields = [...this.fields];
|
|
211
|
+
}
|
|
212
|
+
if (Array.isArray(this.taxonomy) && this.taxonomy.length) {
|
|
213
|
+
result.taxonomy = [...this.taxonomy];
|
|
214
|
+
}
|
|
215
|
+
if (Array.isArray(this.tags) && this.tags.length) {
|
|
216
|
+
result.tags = [...this.tags];
|
|
217
|
+
}
|
|
180
218
|
if (typeof this.deprecated === 'boolean') {
|
|
181
219
|
result.deprecated = this.deprecated;
|
|
182
220
|
}
|
|
@@ -200,6 +238,7 @@ export class DataEntity {
|
|
|
200
238
|
}
|
|
201
239
|
this.root.definitions.properties.push(property);
|
|
202
240
|
this.properties.push(property);
|
|
241
|
+
this.fields.push({ type: 'property', key: property.key });
|
|
203
242
|
return property;
|
|
204
243
|
}
|
|
205
244
|
/**
|
|
@@ -211,6 +250,7 @@ export class DataEntity {
|
|
|
211
250
|
const property = DataProperty.fromName(this.root, name);
|
|
212
251
|
this.root.definitions.properties.push(property);
|
|
213
252
|
this.properties.push(property);
|
|
253
|
+
this.fields.push({ type: 'property', key: property.key });
|
|
214
254
|
return property;
|
|
215
255
|
}
|
|
216
256
|
/**
|
|
@@ -228,22 +268,65 @@ export class DataEntity {
|
|
|
228
268
|
if (defIndex >= 0) {
|
|
229
269
|
this.root.definitions.properties.splice(defIndex, 1);
|
|
230
270
|
}
|
|
271
|
+
this.removeField(key);
|
|
231
272
|
propertyToRemove.remove();
|
|
232
273
|
}
|
|
274
|
+
removeField(key) {
|
|
275
|
+
this.fields = this.fields.filter((item) => item.key !== key);
|
|
276
|
+
}
|
|
233
277
|
/**
|
|
234
278
|
* Lists all properties for this Entity.
|
|
235
279
|
* @returns The list of properties that belong to this entity.
|
|
236
280
|
*/
|
|
237
281
|
listProperties() {
|
|
282
|
+
return [...this.properties];
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Lists all associations for this Entity.
|
|
286
|
+
* @returns The list of associations that belong to this entity.
|
|
287
|
+
*/
|
|
288
|
+
listAssociations() {
|
|
289
|
+
return [...this.associations];
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Generates an ordered list of fields (properties and associations) for this entity.
|
|
293
|
+
*
|
|
294
|
+
* @returns The list of fields (properties and associations) that belong to this entity.
|
|
295
|
+
*/
|
|
296
|
+
listFields() {
|
|
238
297
|
const result = [];
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
298
|
+
this.fields.forEach((i) => {
|
|
299
|
+
if (i.type === 'property') {
|
|
300
|
+
const property = this.properties.find((p) => p.key === i.key);
|
|
301
|
+
if (property) {
|
|
302
|
+
result.push(property);
|
|
303
|
+
}
|
|
243
304
|
}
|
|
244
|
-
|
|
305
|
+
else if (i.type === 'association') {
|
|
306
|
+
const association = this.associations.find((a) => a.key === i.key);
|
|
307
|
+
if (association) {
|
|
308
|
+
result.push(association);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
});
|
|
245
312
|
return result;
|
|
246
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Moves the field to a new index.
|
|
316
|
+
* @param key The key of the field to move.
|
|
317
|
+
* @param toIndex The new index to which move the field to.
|
|
318
|
+
*/
|
|
319
|
+
moveField(key, toIndex) {
|
|
320
|
+
const fromIndex = this.fields.findIndex((i) => i.key === key);
|
|
321
|
+
if (fromIndex < 0) {
|
|
322
|
+
throw new ValidationError([{ field: 'fields', message: `Field ${key} not found.`, rule: 'not-found' }]);
|
|
323
|
+
}
|
|
324
|
+
if (toIndex < 0 || toIndex >= this.fields.length) {
|
|
325
|
+
throw new ValidationError([{ field: 'fields', message: `Invalid index ${toIndex}.`, rule: 'invalid' }]);
|
|
326
|
+
}
|
|
327
|
+
const [item] = this.fields.splice(fromIndex, 1);
|
|
328
|
+
this.fields.splice(toIndex, 0, item);
|
|
329
|
+
}
|
|
247
330
|
/**
|
|
248
331
|
* Creates an association for a given name, adds it to definitions, and returns it.
|
|
249
332
|
* @param name The name of the association
|
|
@@ -253,6 +336,7 @@ export class DataEntity {
|
|
|
253
336
|
const result = DataAssociation.fromName(this.root, name);
|
|
254
337
|
this.root.definitions.associations.push(result);
|
|
255
338
|
this.associations.push(result);
|
|
339
|
+
this.fields.push({ type: 'association', key: result.key });
|
|
256
340
|
return result;
|
|
257
341
|
}
|
|
258
342
|
/**
|
|
@@ -277,6 +361,7 @@ export class DataEntity {
|
|
|
277
361
|
}
|
|
278
362
|
this.root.definitions.associations.push(result);
|
|
279
363
|
this.associations.push(result);
|
|
364
|
+
this.fields.push({ type: 'association', key: result.key });
|
|
280
365
|
return result;
|
|
281
366
|
}
|
|
282
367
|
/**
|
|
@@ -303,6 +388,7 @@ export class DataEntity {
|
|
|
303
388
|
}
|
|
304
389
|
this.root.definitions.associations.push(result);
|
|
305
390
|
this.associations.push(result);
|
|
391
|
+
this.fields.push({ type: 'association', key: result.key });
|
|
306
392
|
return result;
|
|
307
393
|
}
|
|
308
394
|
/**
|
|
@@ -321,6 +407,7 @@ export class DataEntity {
|
|
|
321
407
|
if (defIndex >= 0) {
|
|
322
408
|
this.root.definitions.associations.splice(defIndex, 1);
|
|
323
409
|
}
|
|
410
|
+
this.removeField(key);
|
|
324
411
|
associationToRemove.remove();
|
|
325
412
|
}
|
|
326
413
|
/**
|
|
@@ -407,6 +494,12 @@ export class DataEntity {
|
|
|
407
494
|
getParent() {
|
|
408
495
|
return this.root.definitions.models.find((m) => m.entities.some((e) => e === this));
|
|
409
496
|
}
|
|
497
|
+
/**
|
|
498
|
+
* Adds a parent reference to this entity.
|
|
499
|
+
* This will not add the parent to the namespace. It only adds a reference to the parent.
|
|
500
|
+
* @param key The key of the parent entity to add.
|
|
501
|
+
* @returns `this` for chaining.
|
|
502
|
+
*/
|
|
410
503
|
addParent(key) {
|
|
411
504
|
// Prevent adding self as parent
|
|
412
505
|
if (key === this.key) {
|
|
@@ -442,6 +535,17 @@ export class DataEntity {
|
|
|
442
535
|
},
|
|
443
536
|
], { message });
|
|
444
537
|
}
|
|
538
|
+
const has = this.parents.some((i) => i === key);
|
|
539
|
+
if (has) {
|
|
540
|
+
const message = `Parent ${key} already exists.`;
|
|
541
|
+
throw new ValidationError([
|
|
542
|
+
{
|
|
543
|
+
field: 'parents',
|
|
544
|
+
message,
|
|
545
|
+
rule: 'exists',
|
|
546
|
+
},
|
|
547
|
+
], { message });
|
|
548
|
+
}
|
|
445
549
|
this.parents.push(key);
|
|
446
550
|
return this;
|
|
447
551
|
}
|
|
@@ -504,19 +608,27 @@ export class DataEntity {
|
|
|
504
608
|
}
|
|
505
609
|
}
|
|
506
610
|
/**
|
|
507
|
-
* Returns a list of entities that are associated with this entity
|
|
508
|
-
* that the target property points to this entity.
|
|
611
|
+
* Returns a list of entities that are associated with this entity as a target.
|
|
509
612
|
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
613
|
+
* This method identifies entities that have an association where this entity is a target.
|
|
614
|
+
* In other words, it finds entities that "point to" this entity through an association.
|
|
615
|
+
*
|
|
616
|
+
* For example, consider the following relationships:
|
|
512
617
|
*
|
|
513
618
|
* ```plain
|
|
514
|
-
* A -> B
|
|
515
|
-
*
|
|
619
|
+
* A -> B (A has an association that targets B)
|
|
620
|
+
* C -> B (C has an association that targets B)
|
|
621
|
+
* B -> D (B has an association that targets D)
|
|
516
622
|
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
623
|
+
* Calling `getRelatedEntities()` on B would return [A, C]
|
|
624
|
+
* Calling `getRelatedEntities()` on D would return [B]
|
|
625
|
+
* Calling `getRelatedEntities()` on A would return []
|
|
519
626
|
* ```
|
|
627
|
+
*
|
|
628
|
+
* Note, this method only returns entities that are directly associated with this entity as a target.
|
|
629
|
+
* It does not traverse the association graph to find indirectly related entities.
|
|
630
|
+
*
|
|
631
|
+
* @returns An array of `DataEntity` instances that have an association targeting this entity.
|
|
520
632
|
*/
|
|
521
633
|
getRelatedEntities() {
|
|
522
634
|
const { key, root } = this;
|
|
@@ -531,9 +643,9 @@ export class DataEntity {
|
|
|
531
643
|
});
|
|
532
644
|
return result;
|
|
533
645
|
}
|
|
534
|
-
static getRelatedEntities(namespace,
|
|
646
|
+
static getRelatedEntities(namespace, entityKey) {
|
|
535
647
|
const result = [];
|
|
536
|
-
const inverse = namespace.definitions.associations.filter((i) => i.targets.some((j) => j.key ===
|
|
648
|
+
const inverse = namespace.definitions.associations.filter((i) => i.targets.some((j) => j.key === entityKey));
|
|
537
649
|
inverse.forEach((assoc) => {
|
|
538
650
|
const entity = namespace.definitions.entities.find((e) => e.associations.includes(assoc));
|
|
539
651
|
if (entity && !result.includes(entity)) {
|