@api-client/core 0.18.11 → 0.18.12
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/DomainModel.d.ts.map +1 -1
- package/build/src/modeling/DomainModel.js +11 -4
- package/build/src/modeling/DomainModel.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/data/models/example-generator-api.json +18 -18
- package/package.json +1 -1
- package/src/modeling/DomainModel.ts +11 -4
- package/tests/unit/modeling/domain_model_entities.spec.ts +306 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DomainModel.d.ts","sourceRoot":"","sources":["../../../src/modeling/DomainModel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAoB,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAEvE,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAMzE,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,IAAI,EAAE,QAAQ,CAAA;IAEd;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,MAAM,WAAW,iBAAkB,SAAQ,mBAAmB;IAC5D,IAAI,EAAE,OAAO,eAAe,CAAA;IAC5B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAE3B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;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;OASG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAgB/B;;;;;;;;;;;;;;;;OAgBG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAgC/B;;;;;;;;OAQG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAiB/B;;;;;;;;;;;;;OAaG;IACF,YAAY,IAAI,SAAS,CAAC,YAAY,CAAC;IAaxC;;;;;;;;;;OAUG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,WAAW,IAAI,cAAc,EAAE;IAyB/B;;;;;;;;;;OAUG;IACH,SAAS,IAAI,OAAO;IAIpB,OAAO,CAAC,WAAW;IAInB;;;;;;OAMG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;CAgBhC"}
|
|
@@ -255,11 +255,18 @@ let DomainModel = (() => {
|
|
|
255
255
|
throw new AttachException(`Trying to attach the ${key} entity, but it is already a child of this model`);
|
|
256
256
|
}
|
|
257
257
|
parent.detachEntity(key);
|
|
258
|
-
this.fields.
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
258
|
+
if (!this.fields.some((item) => item.key === key)) {
|
|
259
|
+
this.fields.push({
|
|
260
|
+
type: 'entity',
|
|
261
|
+
key: key,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
262
264
|
this.root.graph.setParent(key, this.key);
|
|
265
|
+
// verify that the entity is moved to the current model
|
|
266
|
+
const graphParent = this.root.graph.parent(key);
|
|
267
|
+
if (graphParent !== this.key) {
|
|
268
|
+
throw new AttachException(`Trying to attach the ${key} entity, but it is not a child of this model`);
|
|
269
|
+
}
|
|
263
270
|
this.root.notifyChange();
|
|
264
271
|
return this;
|
|
265
272
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DomainModel.js","sourceRoot":"","sources":["../../../src/modeling/DomainModel.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAA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SAAS,CAAC,GAAW;YACnB,IAAI,IAAI,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;gBACrB,OAAO,KAAK,CAAA;YACd,CAAC;YACD,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAA;YACvC,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,OAAO,KAAK,CAAA;YACd,CAAC;YACD,IAAI,MAAM,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;gBACvB,OAAO,IAAI,CAAA;YACb,CAAC;YACD,IAAI,MAAM,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;gBACzB,OAAO,KAAK,CAAA;YACd,CAAC;YACD,OAAQ,MAA0B,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;QACnD,CAAC;;;SAxVU,WAAW","sourcesContent":["import type { DataDomain } from './DataDomain.js'\nimport { DomainEntityKind, DomainModelKind } from '../models/kinds.js'\nimport { DomainElement, DomainElementSchema } from './DomainElement.js'\nimport { nanoid } from '../nanoid.js'\nimport { type IThing, Thing } from '../models/Thing.js'\nimport { observed, retargetChange } from './observed.js'\nimport type { DomainNamespace } from './DomainNamespace.js'\nimport type { FileBreadcrumb } from '../models/store/File.js'\nimport { DomainEntity, type DomainEntitySchema } from './DomainEntity.js'\nimport { RemoveEntityException } from '../exceptions/remove_entity_exception.js'\nimport { removeGraphNode } from './GraphUtils.js'\nimport { AttachException } from '../exceptions/attach_exception.js'\nimport { DetachException } from '../exceptions/detach_exception.js'\n\nexport interface ModelOrderedItem {\n /**\n * The type of the ordered item.\n */\n type: 'entity'\n\n /**\n * The key of the ordered item.\n */\n key: string\n}\n\nexport interface DomainModelSchema extends DomainElementSchema {\n kind: typeof DomainModelKind\n /**\n * The data model description.\n */\n info: IThing\n /**\n * The ordered list of entities in the schema.\n */\n fields?: ModelOrderedItem[]\n\n /**\n * Whether this model is deprecated.\n */\n deprecated?: boolean\n}\n\n/**\n * Represents a data model within a data domain.\n *\n * A `DomainModel` groups related `DomainEntity` instances, providing a logical structure for organizing\n * data. It acts as a container for entities that share a common purpose or context.\n *\n * **Key Features:**\n *\n * - **Entity Container:** Holds a collection of `DomainEntity` instances.\n * - **Logical Grouping:** Organizes entities into meaningful groups.\n * - **Metadata:** Supports a description for the data model.\n * - **Namespace Integration:** Belongs to a `DomainNamespace`, creating a hierarchical structure.\n *\n * **Usage:**\n *\n * The preferred way to create a `DomainModel` is through\n * the `DomainNamespace.addModel()` or\n * `DataDomain.addModel()` method.\n *\n * **Example:**\n *\n * ```typescript\n * const dataDomain = new DataDomain();\n * const ns = dataDomain.addNamespace({\n * key: 'myNamespace',\n * });\n * const userModel = ns.addModel({\n * key: 'userModel',\n * info: { name: 'User Model' },\n * });\n * ```\n */\nexport class DomainModel extends DomainElement {\n override kind: typeof DomainModelKind\n\n /**\n * The description of the domain model.\n */\n @retargetChange() accessor info: Thing\n\n /**\n * The ordered list of entities in the schema.\n */\n accessor fields: ModelOrderedItem[]\n /**\n * Whether this model is deprecated.\n */\n @observed() accessor deprecated: boolean | undefined\n\n /**\n * Creates a full data model schema with defaults.\n * @param input The partial data model schema.\n * @returns The data model schema.\n */\n static createSchema(input: Partial<DomainModelSchema> = {}): DomainModelSchema {\n const { key = nanoid(), fields, deprecated } = input\n const info = Thing.fromJSON(input.info, { name: 'New model' }).toJSON()\n const result: DomainModelSchema = {\n kind: DomainModelKind,\n key,\n info,\n }\n if (Array.isArray(fields)) {\n result.fields = [...fields]\n }\n if (typeof deprecated === 'boolean') {\n result.deprecated = deprecated\n }\n return result\n }\n\n /**\n * Creates a new model instance.\n *\n * You should use the `DomainNamespace.addModel()` or\n * `DataDomain.addModel()` method instead.\n *\n * @param root A reference to the root DataDomain instance.\n * @param input The partial model schema.\n */\n constructor(root: DataDomain, input?: Partial<DomainModelSchema>) {\n const init = DomainModel.createSchema(input)\n super(root, init.key)\n this.kind = DomainModelKind\n this.info = new Thing(init.info)\n if (Array.isArray(init.fields)) {\n this.fields = [...init.fields]\n } else {\n this.fields = []\n }\n if (typeof init.deprecated === 'boolean') {\n this.deprecated = init.deprecated\n } else {\n this.deprecated = undefined\n }\n }\n\n /**\n * Generates a JSON representation of the data model.\n *\n * @returns A plain object representation of the\n * `DomainModel`.\n * @example\n * ```typescript\n * const json = userModel.toJSON();\n * console.log(json);\n * ```\n */\n toJSON(): DomainModelSchema {\n const result: DomainModelSchema = {\n kind: this.kind,\n key: this.key,\n info: this.info.toJSON(),\n }\n if (Array.isArray(this.fields) && this.fields.length) {\n result.fields = [...this.fields]\n }\n if (typeof this.deprecated === 'boolean') {\n result.deprecated = this.deprecated\n }\n return result\n }\n\n /**\n * Returns the parent namespace where this data model\n * exists.\n *\n * @returns The parent `DomainNamespace`.\n * @throws Error When no parent is found.\n * @example\n * ```typescript\n * const parent = userModel.getParentInstance();\n * console.log(parent.key);\n * ```\n */\n getParentInstance(): DomainNamespace | DataDomain {\n const parentKey = this.root.graph.parent(this.key)\n if (!parentKey) {\n return this.root\n }\n return this.root.graph.node(parentKey) as DomainNamespace\n }\n\n /**\n * Adds a child entity to the current model.\n *\n * @param value The partial entity schema.\n * @returns The created entity instance.\n * @example\n * ```typescript\n * const entity = model.addEntity({\n * key: 'address',\n * info: { name: 'Address' },\n * })\n * ```\n */\n addEntity(input?: Partial<DomainEntitySchema>): DomainEntity {\n if (input && input.key && this.root.graph.hasNode(input.key)) {\n throw new Error(`Entity with key ${input?.key} already exists`)\n }\n const item = new DomainEntity(this.root, input)\n this.root.graph.setNode(item.key, item)\n this.root.graph.setParent(item.key, this.key)\n this.fields.push({\n key: item.key,\n type: 'entity',\n })\n this.root.notifyChange()\n return item\n }\n\n /**\n * Removes an entity from the current model.\n *\n * @param key The key of the model to remove.\n * @throws {RemoveEntityException} If the model does not exist.\n * @example\n * ```typescript\n * model.removeEntity('address')\n * ```\n */\n removeEntity(key: string): void {\n if (!this.root.graph.hasNode(key)) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it is not an entity`)\n }\n if (node.getParentInstance() !== this) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it is not a child of this model`)\n }\n removeGraphNode(this.root.graph, key)\n this.removeField(key)\n this.root.notifyChange()\n }\n\n /**\n * Attaches an entity to the current model. It changes the parent of the entity\n * to the current model in the graph.\n *\n * The attached entity must be already a child of another model. It detaches the entity\n * from the previous model.\n *\n * @param key The key of the entity to attach.\n * @throws {AttachException} If the entity does not exist.\n * @throws {AttachException} If passed key doesn't belong to an entity.\n * @throws {AttachException} When the entity is already a child of the current model.\n * @throws {AttachException} If the entity is not in the same domain.\n * @example\n * ```typescript\n * model.attachEntity('address')\n * ```\n */\n attachEntity(key: string): this {\n if (!this.root.graph.hasNode(key)) {\n throw new AttachException(`Trying to attach the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is not a entity`)\n }\n if (node.domain.key !== this.root.key) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is not in the same domain`)\n }\n const parent = node.getParentInstance() as DomainModel\n if (parent === this) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is already a child of this model`)\n }\n parent.detachEntity(key)\n this.fields.push({\n type: 'entity',\n key: key,\n })\n this.root.graph.setParent(key, this.key)\n this.root.notifyChange()\n return this\n }\n\n /**\n * Detaches an entity from the current model. It changes the parent of the entity\n * to the root graph.\n * Unlike remove, it doesn't remove the entity from the graph nor notifies the change.\n * @param key The key of the entity to detach.\n * @throws {DetachException} If the entity does not exist.\n * @throws {DetachException} If passed key doesn't belong to an entity.\n * @throws {DetachException} When the entity is not a child of the current model.\n */\n detachEntity(key: string): this {\n const index = this.fields.findIndex((item) => item.key === key)\n if (index === -1) {\n throw new DetachException(`Trying to detach the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new DetachException(`Trying to detach the ${key} entity, but it is not an entity`)\n }\n if (node.getParentInstance() !== this) {\n throw new DetachException(`Trying to detach the ${key} entity, but it is not a child of this model`)\n }\n this.fields.splice(index, 1)\n this.root.graph.setParent(key)\n return this\n }\n\n /**\n * Lists all entities.\n *\n * Note, it accounts for the order of the entities as\n * defined in the `fields` array.\n *\n * @returns A generator that yields each `DomainEntity`.\n * @example\n * ```typescript\n * for (const entity of model.listEntities()) {\n * console.log(entity.key);\n * }\n * ```\n */\n *listEntities(): Generator<DomainEntity> {\n for (const { key, type } of this.fields) {\n if (type !== 'entity') {\n continue\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n continue\n }\n yield node\n }\n }\n\n /**\n * Checks if this model has any entities.\n *\n * @returns True if the model has entities.\n * @example\n * ```typescript\n * if (model.hasEntities()) {\n * // ...\n * }\n * ```\n */\n hasEntities(): boolean {\n return this.fields.some((item) => item.type === 'entity')\n }\n\n /**\n * Creates breadcrumbs from this data model to the root namespace.\n */\n breadcrumbs(): FileBreadcrumb[] {\n const result: FileBreadcrumb[] = []\n result.push({\n key: this.key,\n name: this.info.getLabel('Unnamed model'),\n kind: DomainModelKind,\n })\n let parent: DomainNamespace | DataDomain = this.getParentInstance()\n while (parent && parent !== this.root) {\n result.push({\n key: parent.key,\n kind: parent.kind,\n name: parent.info.getLabel('Unnamed namespace'),\n })\n const typed = parent as DomainNamespace\n parent = typed.getParentInstance()\n }\n result.push({\n key: this.root.key,\n name: this.root.info.getLabel('Unnamed data domain'),\n kind: this.root.kind,\n })\n return result.reverse()\n }\n\n /**\n * Checks if this model has any fields (entities).\n *\n * @returns True if the model has fields.\n * @example\n * ```typescript\n * if (model.hasFields()) {\n * // ...\n * }\n * ```\n */\n hasFields(): boolean {\n return this.fields.length > 0\n }\n\n private removeField(key: string): void {\n this.fields = this.fields.filter((item) => item.key !== key)\n }\n\n /**\n * Checks whether the data model is a child of the given namespace.\n * The relationship doesn't have to be direct, as long as the data model is in the hierarchy it will return true.\n *\n * @param key The key of the parent namespace to check.\n * @returns True if this data model is a child of the given namespace.\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 if (parent === this.root) {\n return false\n }\n return (parent as DomainNamespace).isChildOf(key)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"DomainModel.js","sourceRoot":"","sources":["../../../src/modeling/DomainModel.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,aAAa,EAAuB,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAe,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAGxD,OAAO,EAAE,YAAY,EAA2B,MAAM,mBAAmB,CAAA;AACzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAA;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AA+BnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;IACU,WAAW;sBAAS,aAAa;;;;;;;iBAAjC,WAAY,SAAQ,WAAa;;;gCAM3C,cAAc,EAAE;sCAShB,QAAQ,EAAE;YATO,iKAAS,IAAI,6BAAJ,IAAI,mFAAO;YAS1B,mLAAS,UAAU,6BAAV,UAAU,+FAAqB;;;QAd3C,IAAI,CAAwB;QAKnB,6EAAoB;QAHtC;;WAEG;QACe,IAAS,IAAI,0CAAO;QAApB,IAAS,IAAI,gDAAO;QAKtC,4EAAmC;QA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CAAA;QACnD,CAAC;;;SA/VU,WAAW","sourcesContent":["import type { DataDomain } from './DataDomain.js'\nimport { DomainEntityKind, DomainModelKind } from '../models/kinds.js'\nimport { DomainElement, DomainElementSchema } from './DomainElement.js'\nimport { nanoid } from '../nanoid.js'\nimport { type IThing, Thing } from '../models/Thing.js'\nimport { observed, retargetChange } from './observed.js'\nimport type { DomainNamespace } from './DomainNamespace.js'\nimport type { FileBreadcrumb } from '../models/store/File.js'\nimport { DomainEntity, type DomainEntitySchema } from './DomainEntity.js'\nimport { RemoveEntityException } from '../exceptions/remove_entity_exception.js'\nimport { removeGraphNode } from './GraphUtils.js'\nimport { AttachException } from '../exceptions/attach_exception.js'\nimport { DetachException } from '../exceptions/detach_exception.js'\n\nexport interface ModelOrderedItem {\n /**\n * The type of the ordered item.\n */\n type: 'entity'\n\n /**\n * The key of the ordered item.\n */\n key: string\n}\n\nexport interface DomainModelSchema extends DomainElementSchema {\n kind: typeof DomainModelKind\n /**\n * The data model description.\n */\n info: IThing\n /**\n * The ordered list of entities in the schema.\n */\n fields?: ModelOrderedItem[]\n\n /**\n * Whether this model is deprecated.\n */\n deprecated?: boolean\n}\n\n/**\n * Represents a data model within a data domain.\n *\n * A `DomainModel` groups related `DomainEntity` instances, providing a logical structure for organizing\n * data. It acts as a container for entities that share a common purpose or context.\n *\n * **Key Features:**\n *\n * - **Entity Container:** Holds a collection of `DomainEntity` instances.\n * - **Logical Grouping:** Organizes entities into meaningful groups.\n * - **Metadata:** Supports a description for the data model.\n * - **Namespace Integration:** Belongs to a `DomainNamespace`, creating a hierarchical structure.\n *\n * **Usage:**\n *\n * The preferred way to create a `DomainModel` is through\n * the `DomainNamespace.addModel()` or\n * `DataDomain.addModel()` method.\n *\n * **Example:**\n *\n * ```typescript\n * const dataDomain = new DataDomain();\n * const ns = dataDomain.addNamespace({\n * key: 'myNamespace',\n * });\n * const userModel = ns.addModel({\n * key: 'userModel',\n * info: { name: 'User Model' },\n * });\n * ```\n */\nexport class DomainModel extends DomainElement {\n override kind: typeof DomainModelKind\n\n /**\n * The description of the domain model.\n */\n @retargetChange() accessor info: Thing\n\n /**\n * The ordered list of entities in the schema.\n */\n accessor fields: ModelOrderedItem[]\n /**\n * Whether this model is deprecated.\n */\n @observed() accessor deprecated: boolean | undefined\n\n /**\n * Creates a full data model schema with defaults.\n * @param input The partial data model schema.\n * @returns The data model schema.\n */\n static createSchema(input: Partial<DomainModelSchema> = {}): DomainModelSchema {\n const { key = nanoid(), fields, deprecated } = input\n const info = Thing.fromJSON(input.info, { name: 'New model' }).toJSON()\n const result: DomainModelSchema = {\n kind: DomainModelKind,\n key,\n info,\n }\n if (Array.isArray(fields)) {\n result.fields = [...fields]\n }\n if (typeof deprecated === 'boolean') {\n result.deprecated = deprecated\n }\n return result\n }\n\n /**\n * Creates a new model instance.\n *\n * You should use the `DomainNamespace.addModel()` or\n * `DataDomain.addModel()` method instead.\n *\n * @param root A reference to the root DataDomain instance.\n * @param input The partial model schema.\n */\n constructor(root: DataDomain, input?: Partial<DomainModelSchema>) {\n const init = DomainModel.createSchema(input)\n super(root, init.key)\n this.kind = DomainModelKind\n this.info = new Thing(init.info)\n if (Array.isArray(init.fields)) {\n this.fields = [...init.fields]\n } else {\n this.fields = []\n }\n if (typeof init.deprecated === 'boolean') {\n this.deprecated = init.deprecated\n } else {\n this.deprecated = undefined\n }\n }\n\n /**\n * Generates a JSON representation of the data model.\n *\n * @returns A plain object representation of the\n * `DomainModel`.\n * @example\n * ```typescript\n * const json = userModel.toJSON();\n * console.log(json);\n * ```\n */\n toJSON(): DomainModelSchema {\n const result: DomainModelSchema = {\n kind: this.kind,\n key: this.key,\n info: this.info.toJSON(),\n }\n if (Array.isArray(this.fields) && this.fields.length) {\n result.fields = [...this.fields]\n }\n if (typeof this.deprecated === 'boolean') {\n result.deprecated = this.deprecated\n }\n return result\n }\n\n /**\n * Returns the parent namespace where this data model\n * exists.\n *\n * @returns The parent `DomainNamespace`.\n * @throws Error When no parent is found.\n * @example\n * ```typescript\n * const parent = userModel.getParentInstance();\n * console.log(parent.key);\n * ```\n */\n getParentInstance(): DomainNamespace | DataDomain {\n const parentKey = this.root.graph.parent(this.key)\n if (!parentKey) {\n return this.root\n }\n return this.root.graph.node(parentKey) as DomainNamespace\n }\n\n /**\n * Adds a child entity to the current model.\n *\n * @param value The partial entity schema.\n * @returns The created entity instance.\n * @example\n * ```typescript\n * const entity = model.addEntity({\n * key: 'address',\n * info: { name: 'Address' },\n * })\n * ```\n */\n addEntity(input?: Partial<DomainEntitySchema>): DomainEntity {\n if (input && input.key && this.root.graph.hasNode(input.key)) {\n throw new Error(`Entity with key ${input?.key} already exists`)\n }\n const item = new DomainEntity(this.root, input)\n this.root.graph.setNode(item.key, item)\n this.root.graph.setParent(item.key, this.key)\n this.fields.push({\n key: item.key,\n type: 'entity',\n })\n this.root.notifyChange()\n return item\n }\n\n /**\n * Removes an entity from the current model.\n *\n * @param key The key of the model to remove.\n * @throws {RemoveEntityException} If the model does not exist.\n * @example\n * ```typescript\n * model.removeEntity('address')\n * ```\n */\n removeEntity(key: string): void {\n if (!this.root.graph.hasNode(key)) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it is not an entity`)\n }\n if (node.getParentInstance() !== this) {\n throw new RemoveEntityException(`Trying to remove the ${key} entity, but it is not a child of this model`)\n }\n removeGraphNode(this.root.graph, key)\n this.removeField(key)\n this.root.notifyChange()\n }\n\n /**\n * Attaches an entity to the current model. It changes the parent of the entity\n * to the current model in the graph.\n *\n * The attached entity must be already a child of another model. It detaches the entity\n * from the previous model.\n *\n * @param key The key of the entity to attach.\n * @throws {AttachException} If the entity does not exist.\n * @throws {AttachException} If passed key doesn't belong to an entity.\n * @throws {AttachException} When the entity is already a child of the current model.\n * @throws {AttachException} If the entity is not in the same domain.\n * @example\n * ```typescript\n * model.attachEntity('address')\n * ```\n */\n attachEntity(key: string): this {\n if (!this.root.graph.hasNode(key)) {\n throw new AttachException(`Trying to attach the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is not a entity`)\n }\n if (node.domain.key !== this.root.key) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is not in the same domain`)\n }\n const parent = node.getParentInstance() as DomainModel\n if (parent === this) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is already a child of this model`)\n }\n parent.detachEntity(key)\n if (!this.fields.some((item) => item.key === key)) {\n this.fields.push({\n type: 'entity',\n key: key,\n })\n }\n this.root.graph.setParent(key, this.key)\n // verify that the entity is moved to the current model\n const graphParent = this.root.graph.parent(key)\n if (graphParent !== this.key) {\n throw new AttachException(`Trying to attach the ${key} entity, but it is not a child of this model`)\n }\n this.root.notifyChange()\n return this\n }\n\n /**\n * Detaches an entity from the current model. It changes the parent of the entity\n * to the root graph.\n * Unlike remove, it doesn't remove the entity from the graph nor notifies the change.\n * @param key The key of the entity to detach.\n * @throws {DetachException} If the entity does not exist.\n * @throws {DetachException} If passed key doesn't belong to an entity.\n * @throws {DetachException} When the entity is not a child of the current model.\n */\n detachEntity(key: string): this {\n const index = this.fields.findIndex((item) => item.key === key)\n if (index === -1) {\n throw new DetachException(`Trying to detach the ${key} entity, but it doesn't exist`)\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n throw new DetachException(`Trying to detach the ${key} entity, but it is not an entity`)\n }\n if (node.getParentInstance() !== this) {\n throw new DetachException(`Trying to detach the ${key} entity, but it is not a child of this model`)\n }\n this.fields.splice(index, 1)\n this.root.graph.setParent(key)\n return this\n }\n\n /**\n * Lists all entities.\n *\n * Note, it accounts for the order of the entities as\n * defined in the `fields` array.\n *\n * @returns A generator that yields each `DomainEntity`.\n * @example\n * ```typescript\n * for (const entity of model.listEntities()) {\n * console.log(entity.key);\n * }\n * ```\n */\n *listEntities(): Generator<DomainEntity> {\n for (const { key, type } of this.fields) {\n if (type !== 'entity') {\n continue\n }\n const node = this.root.graph.node(key)\n if (!node || node.kind !== DomainEntityKind) {\n continue\n }\n yield node\n }\n }\n\n /**\n * Checks if this model has any entities.\n *\n * @returns True if the model has entities.\n * @example\n * ```typescript\n * if (model.hasEntities()) {\n * // ...\n * }\n * ```\n */\n hasEntities(): boolean {\n return this.fields.some((item) => item.type === 'entity')\n }\n\n /**\n * Creates breadcrumbs from this data model to the root namespace.\n */\n breadcrumbs(): FileBreadcrumb[] {\n const result: FileBreadcrumb[] = []\n result.push({\n key: this.key,\n name: this.info.getLabel('Unnamed model'),\n kind: DomainModelKind,\n })\n let parent: DomainNamespace | DataDomain = this.getParentInstance()\n while (parent && parent !== this.root) {\n result.push({\n key: parent.key,\n kind: parent.kind,\n name: parent.info.getLabel('Unnamed namespace'),\n })\n const typed = parent as DomainNamespace\n parent = typed.getParentInstance()\n }\n result.push({\n key: this.root.key,\n name: this.root.info.getLabel('Unnamed data domain'),\n kind: this.root.kind,\n })\n return result.reverse()\n }\n\n /**\n * Checks if this model has any fields (entities).\n *\n * @returns True if the model has fields.\n * @example\n * ```typescript\n * if (model.hasFields()) {\n * // ...\n * }\n * ```\n */\n hasFields(): boolean {\n return this.fields.length > 0\n }\n\n private removeField(key: string): void {\n this.fields = this.fields.filter((item) => item.key !== key)\n }\n\n /**\n * Checks whether the data model is a child of the given namespace.\n * The relationship doesn't have to be direct, as long as the data model is in the hierarchy it will return true.\n *\n * @param key The key of the parent namespace to check.\n * @returns True if this data model is a child of the given namespace.\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 if (parent === this.root) {\n return false\n }\n return (parent as DomainNamespace).isChildOf(key)\n }\n}\n"]}
|