@api-client/core 0.11.11 → 0.12.0

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.
Files changed (252) hide show
  1. package/Testing.md +1 -1
  2. package/bin/plugins/events/EventPlugin.ts +61 -0
  3. package/bin/plugins/events/assert.ts +193 -0
  4. package/bin/plugins/events/types.ts +6 -0
  5. package/bin/test.ts +8 -1
  6. package/build/src/amf/AmfShapeGenerator.d.ts +6 -3
  7. package/build/src/amf/AmfShapeGenerator.d.ts.map +1 -1
  8. package/build/src/amf/AmfShapeGenerator.js +4 -1
  9. package/build/src/amf/AmfShapeGenerator.js.map +1 -1
  10. package/build/src/amf/AmfTypes.d.ts +2 -2
  11. package/build/src/amf/AmfTypes.d.ts.map +1 -1
  12. package/build/src/amf/AmfTypes.js.map +1 -1
  13. package/build/src/amf/DataValueGenerator.d.ts +15 -15
  14. package/build/src/amf/DataValueGenerator.d.ts.map +1 -1
  15. package/build/src/amf/DataValueGenerator.js +14 -14
  16. package/build/src/amf/DataValueGenerator.js.map +1 -1
  17. package/build/src/browser.d.ts +12 -9
  18. package/build/src/browser.d.ts.map +1 -1
  19. package/build/src/browser.js +11 -8
  20. package/build/src/browser.js.map +1 -1
  21. package/build/src/exceptions/attach_exception.d.ts +11 -0
  22. package/build/src/exceptions/attach_exception.d.ts.map +1 -0
  23. package/build/src/exceptions/attach_exception.js +11 -0
  24. package/build/src/exceptions/attach_exception.js.map +1 -0
  25. package/build/src/exceptions/detach_exception.d.ts +11 -0
  26. package/build/src/exceptions/detach_exception.d.ts.map +1 -0
  27. package/build/src/exceptions/detach_exception.js +11 -0
  28. package/build/src/exceptions/detach_exception.js.map +1 -0
  29. package/build/src/exceptions/remove_model_exception.d.ts +8 -0
  30. package/build/src/exceptions/remove_model_exception.d.ts.map +1 -0
  31. package/build/src/exceptions/remove_model_exception.js +8 -0
  32. package/build/src/exceptions/remove_model_exception.js.map +1 -0
  33. package/build/src/exceptions/remove_namespace_exception.d.ts +8 -0
  34. package/build/src/exceptions/remove_namespace_exception.d.ts.map +1 -0
  35. package/build/src/exceptions/remove_namespace_exception.js +8 -0
  36. package/build/src/exceptions/remove_namespace_exception.js.map +1 -0
  37. package/build/src/index.d.ts +12 -9
  38. package/build/src/index.d.ts.map +1 -1
  39. package/build/src/index.js +11 -8
  40. package/build/src/index.js.map +1 -1
  41. package/build/src/legacy.d.ts +8 -0
  42. package/build/src/legacy.d.ts.map +1 -1
  43. package/build/src/legacy.js +9 -0
  44. package/build/src/legacy.js.map +1 -1
  45. package/build/src/modeling/Bindings.d.ts +2 -2
  46. package/build/src/modeling/Bindings.d.ts.map +1 -1
  47. package/build/src/modeling/Bindings.js.map +1 -1
  48. package/build/src/modeling/DataDomain.d.ts +601 -0
  49. package/build/src/modeling/DataDomain.d.ts.map +1 -0
  50. package/build/src/modeling/DataDomain.js +1142 -0
  51. package/build/src/modeling/DataDomain.js.map +1 -0
  52. package/build/src/modeling/DataFormat.d.ts +42 -41
  53. package/build/src/modeling/DataFormat.d.ts.map +1 -1
  54. package/build/src/modeling/DataFormat.js +30 -131
  55. package/build/src/modeling/DataFormat.js.map +1 -1
  56. package/build/src/modeling/DomainAssociation.d.ts +281 -0
  57. package/build/src/modeling/DomainAssociation.d.ts.map +1 -0
  58. package/build/src/modeling/DomainAssociation.js +440 -0
  59. package/build/src/modeling/DomainAssociation.js.map +1 -0
  60. package/build/src/modeling/DomainElement.d.ts +33 -0
  61. package/build/src/modeling/DomainElement.d.ts.map +1 -0
  62. package/build/src/modeling/DomainElement.js +32 -0
  63. package/build/src/modeling/DomainElement.js.map +1 -0
  64. package/build/src/modeling/DomainEntity.d.ts +383 -0
  65. package/build/src/modeling/DomainEntity.d.ts.map +1 -0
  66. package/build/src/modeling/DomainEntity.js +718 -0
  67. package/build/src/modeling/DomainEntity.js.map +1 -0
  68. package/build/src/modeling/DomainFile.d.ts +25 -0
  69. package/build/src/modeling/DomainFile.d.ts.map +1 -0
  70. package/build/src/modeling/DomainFile.js +86 -0
  71. package/build/src/modeling/DomainFile.js.map +1 -0
  72. package/build/src/modeling/DomainImpactAnalysis.d.ts +60 -47
  73. package/build/src/modeling/DomainImpactAnalysis.d.ts.map +1 -1
  74. package/build/src/modeling/DomainImpactAnalysis.js +201 -132
  75. package/build/src/modeling/DomainImpactAnalysis.js.map +1 -1
  76. package/build/src/modeling/DomainModel.d.ts +226 -0
  77. package/build/src/modeling/DomainModel.d.ts.map +1 -0
  78. package/build/src/modeling/DomainModel.js +401 -0
  79. package/build/src/modeling/DomainModel.js.map +1 -0
  80. package/build/src/modeling/DomainNamespace.d.ts +268 -0
  81. package/build/src/modeling/DomainNamespace.d.ts.map +1 -0
  82. package/build/src/modeling/DomainNamespace.js +512 -0
  83. package/build/src/modeling/DomainNamespace.js.map +1 -0
  84. package/build/src/modeling/DomainProperty.d.ts +281 -0
  85. package/build/src/modeling/DomainProperty.d.ts.map +1 -0
  86. package/build/src/modeling/DomainProperty.js +560 -0
  87. package/build/src/modeling/DomainProperty.js.map +1 -0
  88. package/build/src/modeling/DomainSerialization.d.ts +40 -0
  89. package/build/src/modeling/DomainSerialization.d.ts.map +1 -0
  90. package/build/src/modeling/DomainSerialization.js +288 -0
  91. package/build/src/modeling/DomainSerialization.js.map +1 -0
  92. package/build/src/modeling/DomainVersioning.d.ts +17 -0
  93. package/build/src/modeling/DomainVersioning.d.ts.map +1 -0
  94. package/build/src/modeling/DomainVersioning.js +124 -0
  95. package/build/src/modeling/DomainVersioning.js.map +1 -0
  96. package/build/src/modeling/GraphUtils.d.ts +8 -0
  97. package/build/src/modeling/GraphUtils.d.ts.map +1 -0
  98. package/build/src/modeling/GraphUtils.js +26 -0
  99. package/build/src/modeling/GraphUtils.js.map +1 -0
  100. package/build/src/modeling/amf/ShapeGenerator.d.ts +164 -0
  101. package/build/src/modeling/amf/ShapeGenerator.d.ts.map +1 -0
  102. package/build/src/modeling/amf/ShapeGenerator.js +492 -0
  103. package/build/src/modeling/amf/ShapeGenerator.js.map +1 -0
  104. package/build/src/modeling/{DataAssociation.d.ts → legacy/DataAssociation.d.ts} +10 -5
  105. package/build/src/modeling/legacy/DataAssociation.d.ts.map +1 -0
  106. package/build/src/modeling/{DataAssociation.js → legacy/DataAssociation.js} +9 -6
  107. package/build/src/modeling/legacy/DataAssociation.js.map +1 -0
  108. package/build/src/modeling/{DataEntity.d.ts → legacy/DataEntity.d.ts} +12 -7
  109. package/build/src/modeling/legacy/DataEntity.d.ts.map +1 -0
  110. package/build/src/modeling/{DataEntity.js → legacy/DataEntity.js} +19 -18
  111. package/build/src/modeling/legacy/DataEntity.js.map +1 -0
  112. package/build/src/modeling/{DataEntityBuilder.d.ts → legacy/DataEntityBuilder.d.ts} +3 -2
  113. package/build/src/modeling/legacy/DataEntityBuilder.d.ts.map +1 -0
  114. package/build/src/modeling/{DataEntityBuilder.js → legacy/DataEntityBuilder.js} +3 -2
  115. package/build/src/modeling/legacy/DataEntityBuilder.js.map +1 -0
  116. package/build/src/modeling/legacy/DataImpactAnalysis.d.ts +298 -0
  117. package/build/src/modeling/legacy/DataImpactAnalysis.d.ts.map +1 -0
  118. package/build/src/modeling/legacy/DataImpactAnalysis.js +441 -0
  119. package/build/src/modeling/legacy/DataImpactAnalysis.js.map +1 -0
  120. package/build/src/modeling/{DataModel.d.ts → legacy/DataModel.d.ts} +6 -4
  121. package/build/src/modeling/legacy/DataModel.d.ts.map +1 -0
  122. package/build/src/modeling/{DataModel.js → legacy/DataModel.js} +7 -6
  123. package/build/src/modeling/legacy/DataModel.js.map +1 -0
  124. package/build/src/modeling/{DataNamespace.d.ts → legacy/DataNamespace.d.ts} +22 -3
  125. package/build/src/modeling/legacy/DataNamespace.d.ts.map +1 -0
  126. package/build/src/modeling/{DataNamespace.js → legacy/DataNamespace.js} +7 -3
  127. package/build/src/modeling/legacy/DataNamespace.js.map +1 -0
  128. package/build/src/modeling/{DataProperty.d.ts → legacy/DataProperty.d.ts} +13 -5
  129. package/build/src/modeling/legacy/DataProperty.d.ts.map +1 -0
  130. package/build/src/modeling/{DataProperty.js → legacy/DataProperty.js} +8 -5
  131. package/build/src/modeling/legacy/DataProperty.js.map +1 -0
  132. package/build/src/modeling/observed.d.ts +67 -0
  133. package/build/src/modeling/observed.d.ts.map +1 -0
  134. package/build/src/modeling/observed.js +230 -0
  135. package/build/src/modeling/observed.js.map +1 -0
  136. package/build/src/modeling/types.d.ts +165 -1
  137. package/build/src/modeling/types.d.ts.map +1 -1
  138. package/build/src/modeling/types.js.map +1 -1
  139. package/build/src/models/Thing.d.ts +26 -5
  140. package/build/src/models/Thing.d.ts.map +1 -1
  141. package/build/src/models/Thing.js +193 -91
  142. package/build/src/models/Thing.js.map +1 -1
  143. package/build/src/models/kinds.d.ts +31 -6
  144. package/build/src/models/kinds.d.ts.map +1 -1
  145. package/build/src/models/kinds.js +31 -6
  146. package/build/src/models/kinds.js.map +1 -1
  147. package/build/src/models/store/DataFile.d.ts +3 -1
  148. package/build/src/models/store/DataFile.d.ts.map +1 -1
  149. package/build/src/models/store/DataFile.js +2 -0
  150. package/build/src/models/store/DataFile.js.map +1 -1
  151. package/build/src/models/types.d.ts +12 -0
  152. package/build/src/models/types.d.ts.map +1 -0
  153. package/build/src/models/types.js +2 -0
  154. package/build/src/models/types.js.map +1 -0
  155. package/build/src/runtime/store/FilesSdk.d.ts +2 -2
  156. package/build/src/runtime/store/FilesSdk.d.ts.map +1 -1
  157. package/build/src/runtime/store/FilesSdk.js.map +1 -1
  158. package/data/models/example-generator-api.json +16 -16
  159. package/package.json +20 -6
  160. package/readme.md +1 -1
  161. package/src/amf/AmfShapeGenerator.ts +7 -4
  162. package/src/amf/AmfTypes.ts +2 -2
  163. package/src/amf/DataValueGenerator.ts +21 -21
  164. package/src/exceptions/attach_exception.ts +11 -0
  165. package/src/exceptions/detach_exception.ts +11 -0
  166. package/src/exceptions/remove_model_exception.ts +8 -0
  167. package/src/exceptions/remove_namespace_exception.ts +8 -0
  168. package/src/modeling/Bindings.ts +2 -2
  169. package/src/modeling/DataDomain.ts +1221 -0
  170. package/src/modeling/DataFormat.ts +54 -163
  171. package/src/modeling/DomainAssociation.ts +476 -0
  172. package/src/modeling/DomainElement.ts +50 -0
  173. package/src/modeling/DomainEntity.ts +769 -0
  174. package/src/modeling/DomainFile.ts +94 -0
  175. package/src/modeling/DomainImpactAnalysis.ts +218 -144
  176. package/src/modeling/DomainModel.ts +421 -0
  177. package/src/modeling/DomainNamespace.ts +537 -0
  178. package/src/modeling/DomainProperty.ts +548 -0
  179. package/src/modeling/DomainSerialization.ts +312 -0
  180. package/src/modeling/DomainVersioning.ts +144 -0
  181. package/src/modeling/GraphUtils.ts +28 -0
  182. package/src/modeling/amf/ShapeGenerator.ts +552 -0
  183. package/src/modeling/graph.md +115 -0
  184. package/src/modeling/{DataAssociation.ts → legacy/DataAssociation.ts} +13 -8
  185. package/src/modeling/{DataEntity.ts → legacy/DataEntity.ts} +28 -23
  186. package/src/modeling/{DataEntityBuilder.ts → legacy/DataEntityBuilder.ts} +4 -3
  187. package/src/modeling/legacy/DataImpactAnalysis.ts +530 -0
  188. package/src/modeling/{DataModel.ts → legacy/DataModel.ts} +10 -8
  189. package/src/modeling/{DataNamespace.ts → legacy/DataNamespace.ts} +23 -5
  190. package/src/modeling/{DataProperty.ts → legacy/DataProperty.ts} +15 -7
  191. package/src/modeling/observed.ts +267 -0
  192. package/src/modeling/types.ts +174 -1
  193. package/src/models/Thing.ts +70 -5
  194. package/src/models/kinds.ts +32 -6
  195. package/src/models/store/DataFile.ts +3 -1
  196. package/src/models/types.ts +11 -0
  197. package/src/runtime/store/FilesSdk.ts +2 -2
  198. package/tests/unit/amf/data_value_generator.spec.ts +15 -15
  199. package/tests/unit/legacy-transformers/ARC-legacy-import.spec.ts +1 -1
  200. package/tests/unit/modeling/amf/shape_generator.spec.ts +1174 -0
  201. package/tests/unit/modeling/data_domain.spec.ts +444 -0
  202. package/tests/unit/modeling/data_domain_associations.spec.ts +279 -0
  203. package/tests/unit/modeling/data_domain_change_observers.spec.ts +681 -0
  204. package/tests/unit/modeling/data_domain_entities.spec.ts +449 -0
  205. package/tests/unit/modeling/data_domain_foreign.spec.ts +355 -0
  206. package/tests/unit/modeling/data_domain_models.spec.ts +658 -0
  207. package/tests/unit/modeling/data_domain_namespaces.spec.ts +668 -0
  208. package/tests/unit/modeling/data_domain_property.spec.ts +264 -0
  209. package/tests/unit/modeling/data_domain_serialization.spec.ts +294 -0
  210. package/tests/unit/modeling/domain.property.spec.ts +822 -0
  211. package/tests/unit/modeling/domain_asociation.spec.ts +643 -0
  212. package/tests/unit/modeling/domain_asociation_targets.spec.ts +350 -0
  213. package/tests/unit/modeling/domain_entity.spec.ts +730 -0
  214. package/tests/unit/modeling/domain_entity_associations.spec.ts +330 -0
  215. package/tests/unit/modeling/domain_entity_example_generator_json.spec.ts +988 -0
  216. package/tests/unit/modeling/domain_entity_example_generator_xml.spec.ts +1451 -0
  217. package/tests/unit/modeling/domain_entity_fields.spec.ts +113 -0
  218. package/tests/unit/modeling/domain_entity_generators.spec.ts +20 -0
  219. package/tests/unit/modeling/domain_entity_parents.spec.ts +291 -0
  220. package/tests/unit/modeling/domain_entity_properties.spec.ts +305 -0
  221. package/tests/unit/modeling/{data_file.spec.ts → domain_file.spec.ts} +29 -85
  222. package/tests/unit/modeling/domain_impact_analysis.spec.ts +452 -0
  223. package/tests/unit/modeling/domain_model.spec.ts +568 -0
  224. package/tests/unit/modeling/domain_model_entities.spec.ts +408 -0
  225. package/tests/unit/modeling/domain_namespace.spec.ts +514 -0
  226. package/tests/unit/modeling/domain_namespace_models.spec.ts +324 -0
  227. package/tests/unit/modeling/domain_namespace_namespaces.spec.ts +404 -0
  228. package/tests/unit/modeling/domain_versioning.spec.ts +140 -0
  229. package/tests/unit/{amf → modeling/legacy}/amf_shape_generator.spec.ts +11 -11
  230. package/tests/unit/modeling/{data_association.spec.ts → legacy/data_association.spec.ts} +3 -11
  231. package/tests/unit/modeling/{data_entity.spec.ts → legacy/data_entity.spec.ts} +10 -8
  232. package/tests/unit/modeling/{data_entity_generator_json.spec.ts → legacy/data_entity_generator_json.spec.ts} +1 -1
  233. package/tests/unit/modeling/{data_entity_generator_xml.spec.ts → legacy/data_entity_generator_xml.spec.ts} +1 -1
  234. package/tests/unit/modeling/{data_model.spec.ts → legacy/data_model.spec.ts} +3 -3
  235. package/tests/unit/modeling/{data_namespace.spec.ts → legacy/data_namespace.spec.ts} +3 -10
  236. package/tests/unit/modeling/{data_property.spec.ts → legacy/data_property.spec.ts} +3 -6
  237. package/tests/unit/modeling/{impact_analysis.spec.ts → legacy/impact_analysis.spec.ts} +9 -9
  238. package/tests/unit/models/File/new.spec.ts +1 -1
  239. package/tests/unit/models/HttpProject.spec.ts +3 -3
  240. package/tsconfig.node.json +35 -0
  241. package/build/src/modeling/DataAssociation.d.ts.map +0 -1
  242. package/build/src/modeling/DataAssociation.js.map +0 -1
  243. package/build/src/modeling/DataEntity.d.ts.map +0 -1
  244. package/build/src/modeling/DataEntity.js.map +0 -1
  245. package/build/src/modeling/DataEntityBuilder.d.ts.map +0 -1
  246. package/build/src/modeling/DataEntityBuilder.js.map +0 -1
  247. package/build/src/modeling/DataModel.d.ts.map +0 -1
  248. package/build/src/modeling/DataModel.js.map +0 -1
  249. package/build/src/modeling/DataNamespace.d.ts.map +0 -1
  250. package/build/src/modeling/DataNamespace.js.map +0 -1
  251. package/build/src/modeling/DataProperty.d.ts.map +0 -1
  252. package/build/src/modeling/DataProperty.js.map +0 -1
@@ -0,0 +1,1221 @@
1
+ import v4 from '../lib/uuid.js'
2
+ import { Graph } from '@api-client/graph'
3
+ import {
4
+ DomainAssociationKind,
5
+ DataDomainKind,
6
+ DomainEntityKind,
7
+ DomainModelKind,
8
+ DomainNamespaceKind,
9
+ DomainPropertyKind,
10
+ } from '../models/kinds.js'
11
+ import type {
12
+ AssociationAddOptions,
13
+ DomainGraphEdge,
14
+ DomainGraphNodeType,
15
+ ForeignDomainDependency,
16
+ SerializedGraph,
17
+ } from './types.js'
18
+ import { type DomainNamespaceSchema, DomainNamespace, type NamespaceOrderedItem } from './DomainNamespace.js'
19
+ import { type DomainModelSchema, DomainModel } from './DomainModel.js'
20
+ import { type DomainEntitySchema, DomainEntity } from './DomainEntity.js'
21
+ import { DomainAssociation } from './DomainAssociation.js'
22
+ import { DomainProperty, DomainPropertySchema } from './DomainProperty.js'
23
+ import { type IThing, Thing } from '../models/Thing.js'
24
+ import { removeGraphNode } from './GraphUtils.js'
25
+ import { serialize, deserialize, mergeGraph, removeForeignGraph } from './DomainSerialization.js'
26
+
27
+ export interface DataDomainSchema {
28
+ info: IThing
29
+ kind: typeof DataDomainKind
30
+ key: string
31
+ graph?: SerializedGraph
32
+ dependencyList?: ForeignDomainDependency[]
33
+ /**
34
+ * The ordered list of fields (namespace and models) in the schema.
35
+ * These only keep references to define the order of these properties
36
+ * in the schema as graph won't do it.
37
+ */
38
+ fields?: NamespaceOrderedItem[]
39
+ }
40
+
41
+ /**
42
+ * Represents the root of a data domain model.
43
+ *
44
+ * The `DataDomain` class serves as the top-level container
45
+ * for a collection of data models, namespaces, entities,
46
+ * properties, and associations. It provides methods for
47
+ * managing and manipulating these data elements, enabling
48
+ * the creation of complex and interconnected data
49
+ * structures.
50
+ *
51
+ * **Key Features:**
52
+ *
53
+ * - **Root Container:** Holds all data elements within a domain.
54
+ * - **Graph-Based Structure:** Uses a graph to represent relationships between data elements.
55
+ * - **Namespace Management:** Supports creating and managing namespaces to organize data models.
56
+ * - **Data Model Management:** Supports creating and managing data models to group entities.
57
+ * - **Entity Management:** Supports creating and managing entities, which define the structure of data.
58
+ * - **Property Management:** Supports creating and managing properties, which define the data elements
59
+ * within entities.
60
+ * - **Association Management:** Supports creating and managing associations, which define relationships
61
+ * between entities.
62
+ * - **Foreign Domain Support:** Allows registering and integrating data from external domains.
63
+ * - **Change Notification:** Notifies listeners when changes occur within the data domain.
64
+ *
65
+ * **Usage:**
66
+ *
67
+ * 1. Create an instance of the `DataDomain`.
68
+ * 2. Use methods like `addNamespace()`, `addModel()`,
69
+ * `addEntity()`, `addProperty()`, and `addAssociation()`
70
+ * to build the data domain structure.
71
+ * 3. Use methods like `findNamespace()`, `findModel()`,
72
+ * `findEntity()`, `findProperty()`, and
73
+ * `findAssociation()` to retrieve data elements.
74
+ * 4. Use methods like `listNamespaces()`, `listGraphNamespaces()`, `listModels()`,
75
+ * and `listEntities()` to iterate over collections of
76
+ * data elements.
77
+ * 5. Use `registerForeignDomain()` to integrate data from
78
+ * external domains.
79
+ * 6. Listen for the `change` event to be notified of
80
+ * changes within the data domain.
81
+ *
82
+ * **Example:**
83
+ *
84
+ * ```typescript
85
+ * const dataDomain = new DataDomain();
86
+ * const userNamespace = dataDomain.addNamespace({
87
+ * key: 'userNamespace',
88
+ * });
89
+ * const userModel = userNamespace.addModel({
90
+ * key: 'userModel',
91
+ * });
92
+ * const userEntity = userModel.addEntity({
93
+ * key: 'user',
94
+ * });
95
+ * const nameProperty = userEntity.addProperty({
96
+ * key: 'name',
97
+ * type: 'string',
98
+ * });
99
+ * ```
100
+ *
101
+ * @fires DataDomain#change {Event} - Fired when the data
102
+ * domain changes.
103
+ *
104
+ * @todo: Implement a mechanism to move an entity to a new
105
+ * parent model.
106
+ */
107
+ export class DataDomain extends EventTarget {
108
+ /**
109
+ * The kind of the domain element.
110
+ */
111
+ kind: typeof DataDomainKind
112
+ /**
113
+ * The unique key of the domain element.
114
+ */
115
+ key: string
116
+
117
+ /**
118
+ * The graph used to store the data domain structure.
119
+ */
120
+ graph: Graph<unknown, DomainGraphNodeType, DomainGraphEdge>
121
+
122
+ /**
123
+ * A map of foreign data domains.
124
+ * Key: The unique identifier of the foreign domain.
125
+ * Value: The foreign DataDomain instance.
126
+ */
127
+ dependencies: Map<string, DataDomain> = new Map<string, DataDomain>()
128
+
129
+ /**
130
+ * The list of foreign domain dependencies.
131
+ */
132
+ dependencyList: ForeignDomainDependency[] = []
133
+
134
+ /**
135
+ * The description of the domain property.
136
+ */
137
+ accessor info: Thing
138
+
139
+ /**
140
+ * When the initializing flag is set to true,
141
+ * the domain is not notified of changes.
142
+ */
143
+ #initializing = true
144
+
145
+ /**
146
+ * When the notifying flag is set to true,
147
+ * the domain is pending a notification.
148
+ * No other notifications will be sent until
149
+ * the current notification is sent.
150
+ */
151
+ #notifying = false
152
+
153
+ /**
154
+ * This is to keep it consistent with the domain elements.
155
+ */
156
+ get domain(): DataDomain {
157
+ return this
158
+ }
159
+
160
+ /**
161
+ * The ordered list of fields (namespace and models) in the schema.
162
+ * These only keep references to define the order of these properties
163
+ * in the schema as graph won't do it.
164
+ */
165
+ accessor fields: NamespaceOrderedItem[]
166
+
167
+ static createSchema(input: Partial<DataDomainSchema> = {}): DataDomainSchema {
168
+ // We have to stay with UUID for data domains as the API expects it for File keys.
169
+ const { key = v4(), fields } = input
170
+ const info = Thing.fromJSON(input.info, { name: 'Unnamed domain' }).toJSON()
171
+ const result: DataDomainSchema = {
172
+ kind: DataDomainKind,
173
+ key,
174
+ info,
175
+ }
176
+ if (input.dependencyList) {
177
+ result.dependencyList = structuredClone(input.dependencyList)
178
+ }
179
+ if (input.graph) {
180
+ result.graph = input.graph
181
+ }
182
+ if (Array.isArray(fields)) {
183
+ result.fields = [...fields]
184
+ }
185
+ return result
186
+ }
187
+
188
+ /**
189
+ * Creates a new instance of the `DataDomain` class.
190
+ *
191
+ * When creating a new Data Domain arguments should not be set.
192
+ * When restoring a Data Domain from a previous state, you should provide
193
+ * the serialized graph state as well as the same list of dependencies
194
+ * used when the graph was deserialized. Edges to missing dependency nodes
195
+ * will be ignored.
196
+ *
197
+ * @param state The previously serialized state of the graph.
198
+ * @param dependencies An array of foreign data domains to register with this domain.
199
+ */
200
+ constructor(state?: Partial<DataDomainSchema>, dependencies?: DataDomain[]) {
201
+ super()
202
+ const init = DataDomain.createSchema(state)
203
+ this.kind = init.kind
204
+ this.key = init.key
205
+ this.info = new Thing(init.info)
206
+ this.graph = deserialize(this, init.graph, dependencies)
207
+ if (Array.isArray(init.fields)) {
208
+ this.fields = [...init.fields]
209
+ } else {
210
+ this.fields = []
211
+ }
212
+ if (Array.isArray(init.dependencyList)) {
213
+ this.dependencyList = [...init.dependencyList]
214
+ } else {
215
+ this.dependencyList = []
216
+ }
217
+ this.#initializing = false
218
+ this.info.addEventListener('change', () => {
219
+ this.notifyChange()
220
+ })
221
+ }
222
+
223
+ /**
224
+ * Serializes the DataDomain instance to a JSON object.
225
+ * It does not serialize the foreign domain dependencies. The serialized nodes
226
+ * are the ones that are local to this data domain.
227
+ * @returns The serialized data domain ready for storage or transport.
228
+ */
229
+ toJSON(): DataDomainSchema {
230
+ const result: DataDomainSchema = {
231
+ info: this.info.toJSON(),
232
+ kind: this.kind,
233
+ key: this.key,
234
+ graph: serialize(this.graph, this.key),
235
+ }
236
+ if (this.dependencyList.length > 0) {
237
+ result.dependencyList = structuredClone(this.dependencyList)
238
+ }
239
+ if (Array.isArray(this.fields) && this.fields.length) {
240
+ result.fields = [...this.fields]
241
+ }
242
+ return result
243
+ }
244
+
245
+ /**
246
+ * This function is used internally by all domain elements to notify that something has changed.
247
+ * Since we want to notify listeners after the operation commits, we use microtask
248
+ * to ensure that the event is dispatched after the current operation.
249
+ */
250
+ notifyChange() {
251
+ if (this.#notifying || this.#initializing) {
252
+ return
253
+ }
254
+ this.#notifying = true
255
+ queueMicrotask(() => {
256
+ this.#notifying = false
257
+ const event = new Event('change')
258
+ this.dispatchEvent(event)
259
+ })
260
+ }
261
+
262
+ private removeField(key: string): void {
263
+ this.fields = this.fields.filter((item) => item.key !== key)
264
+ }
265
+
266
+ /**
267
+ * Checks if this data domain has any fields (namespace and models).
268
+ *
269
+ * @returns True if the data domain has fields.
270
+ * @example
271
+ * ```typescript
272
+ * if (domain.hasFields()) {
273
+ * // ...
274
+ * }
275
+ * ```
276
+ */
277
+ hasFields(): boolean {
278
+ return this.fields.length > 0
279
+ }
280
+
281
+ /**
282
+ * Lists all fields (namespace and models) of this namespace.
283
+ *
284
+ * @returns A generator that yields each `DomainAssociation` or `DomainProperty`.
285
+ * @example
286
+ * ```typescript
287
+ * for (const field of namespace.listFields()) {
288
+ * console.log(field.key);
289
+ * }
290
+ * ```
291
+ */
292
+ *listFields(): Generator<DomainNamespace | DomainModel> {
293
+ for (const { key } of this.fields) {
294
+ const node = this.graph.node(key)
295
+ if (!node) {
296
+ continue
297
+ }
298
+ if (node.kind === DomainNamespaceKind || node.kind === DomainModelKind) {
299
+ yield node
300
+ }
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Adds a new data domain instance to the graph.
306
+ *
307
+ * @param value The partial namespace schema. All missing
308
+ * values will be filled with default values.
309
+ * @param parent The parent namespace key. If not provided,
310
+ * the new namespace will be added to the root of the
311
+ * graph.
312
+ * @returns The created namespace instance.
313
+ * @throws Error When the parent does not exist or is not
314
+ * a namespace.
315
+ * @example
316
+ * ```typescript
317
+ * const newNamespace = dataDomain.addNamespace({
318
+ * key: 'newNamespace',
319
+ * });
320
+ * ```
321
+ */
322
+ addNamespace(value?: Partial<DomainNamespaceSchema>, parent?: string): DomainNamespace {
323
+ if (parent) {
324
+ if (!this.graph.hasNode(parent)) {
325
+ throw new Error(`Parent namespace ${parent} does not exist`)
326
+ }
327
+ const instance = this.graph.node(parent) as DomainNamespace
328
+ if (!instance || instance.kind !== DomainNamespaceKind) {
329
+ throw new Error(`Parent namespace ${parent} is not a valid namespace`)
330
+ }
331
+ if (instance.domain.key !== this.key) {
332
+ throw new Error(`Cannot add a namespace to a foreign domain`)
333
+ }
334
+ return instance.addNamespace(value)
335
+ }
336
+ const item = new DomainNamespace(this, value)
337
+ this.graph.setNode(item.key, item)
338
+ this.fields.push({
339
+ type: 'namespace',
340
+ key: item.key,
341
+ })
342
+ this.notifyChange()
343
+ return item
344
+ }
345
+
346
+ /**
347
+ * Removes a namespace from the graph.
348
+ *
349
+ * @param key The key of the namespace to remove.
350
+ * @returns The current DataDomain instance.
351
+ * @throws Error When the namespace does not exist.
352
+ * @example
353
+ * ```typescript
354
+ * dataDomain.removeNamespace('userNamespace');
355
+ * ```
356
+ */
357
+ removeNamespace(key: string): this {
358
+ if (!this.graph.hasNode(key)) {
359
+ throw new Error(`Namespace ${key} does not exist`)
360
+ }
361
+ const ns = this.graph.node(key) as DomainNamespace
362
+ if (!ns || ns.kind !== DomainNamespaceKind) {
363
+ throw new Error(`Namespace ${key} not found`)
364
+ }
365
+ if (ns.domain.key !== this.key) {
366
+ throw new Error(`Cannot remove a namespace from a foreign domain`)
367
+ }
368
+ const parent = ns.getParentInstance()
369
+ if (parent === this) {
370
+ removeGraphNode(this.graph, key)
371
+ this.removeField(key)
372
+ this.notifyChange()
373
+ } else {
374
+ parent.removeNamespace(key)
375
+ }
376
+ return this
377
+ }
378
+
379
+ /**
380
+ * Lists all namespaces of this data domain that are direct children of it.
381
+ *
382
+ * Note, it accounts for the order of the namespaces as defined in the `fields` array.
383
+ *
384
+ * @returns A generator that yields each `DomainNamespace`.
385
+ * @example
386
+ * ```typescript
387
+ * for (const ns of domain.listNamespaces()) {
388
+ * console.log(ns.key);
389
+ * }
390
+ * ```
391
+ */
392
+ *listNamespaces(): Generator<DomainNamespace> {
393
+ for (const { key, type } of this.fields) {
394
+ if (type !== 'namespace') {
395
+ continue
396
+ }
397
+ const node = this.graph.node(key)
398
+ if (!node || node.kind !== DomainNamespaceKind) {
399
+ continue
400
+ }
401
+ yield node
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Checks if this data domain has any namespaces.
407
+ *
408
+ * @returns True if the data domain has namespaces.
409
+ * @example
410
+ * ```typescript
411
+ * if (domain.hasNamespaces()) {
412
+ * // ...
413
+ * }
414
+ * ```
415
+ */
416
+ hasNamespaces(): boolean {
417
+ return this.fields.some((item) => item.type === 'namespace')
418
+ }
419
+
420
+ /**
421
+ * Lists all namespaces in the graph.
422
+ *
423
+ * @param parent The key of the parent namespace. If not
424
+ * provided, all root namespaces will be listed.
425
+ * @returns A generator that yields each `DomainNamespace`.
426
+ * @example
427
+ * ```typescript
428
+ * for (const ns of dataDomain.listGraphNamespaces()) {
429
+ * console.log(ns.key);
430
+ * }
431
+ * ```
432
+ */
433
+ *listGraphNamespaces(parent?: string): Generator<DomainNamespace> {
434
+ for (const node of this.graph.children(parent)) {
435
+ const value = this.graph.node(node) as DomainGraphNodeType
436
+ if (value.kind === DomainNamespaceKind && value.domain.key === this.key) {
437
+ yield value
438
+ }
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Finds a namespace by its key.
444
+ *
445
+ * @param key The key of the namespace to find.
446
+ * @returns The namespace instance or undefined if not
447
+ * found.
448
+ * @example
449
+ * ```typescript
450
+ * const ns = dataDomain.findNamespace('userNamespace');
451
+ * if (ns) {
452
+ * console.log(ns.key);
453
+ * }
454
+ * ```
455
+ */
456
+ findNamespace(key: string): DomainNamespace | undefined {
457
+ const result = this.graph.node(key) as DomainNamespace | undefined
458
+ if (result && result.kind === DomainNamespaceKind) {
459
+ return result
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Moves a namespace to a new parent.
465
+ *
466
+ * @param key The key of the namespace to move.
467
+ * @param parent The new parent namespace key. If
468
+ * undefined, the namespace will be moved to the root.
469
+ * @returns The current DataDomain instance.
470
+ * @throws Error When the namespace or parent does not
471
+ * exist, or when a namespace is moved to itself or
472
+ * its own child.
473
+ * @example
474
+ * ```typescript
475
+ * dataDomain.moveNamespace('userNamespace', 'root');
476
+ * ```
477
+ */
478
+ moveNamespace(key: string, parent?: string): this {
479
+ if (!this.graph.hasNode(key)) {
480
+ throw new Error(`Namespace ${key} does not exist`)
481
+ }
482
+
483
+ const namespace = this.findNamespace(key)
484
+ if (!namespace) {
485
+ throw new Error(`Namespace ${key} not found in the graph`)
486
+ }
487
+
488
+ if (parent) {
489
+ const instance = this.findNamespace(parent)
490
+ if (instance && instance.domain.key !== this.key) {
491
+ throw new Error(`Cannot move a namespace to a foreign domain`)
492
+ }
493
+ if (!instance) {
494
+ throw new Error(`Parent namespace ${parent} does not exist`)
495
+ }
496
+ }
497
+
498
+ if (key === parent) {
499
+ throw new Error(`Cannot move a namespace to itself`)
500
+ }
501
+
502
+ if (namespace.domain.key !== this.key) {
503
+ throw new Error(`Cannot move a namespace from a foreign domain`)
504
+ }
505
+ let parentNamespace: DomainNamespace | undefined
506
+ if (parent) {
507
+ parentNamespace = this.findNamespace(parent)
508
+ if (!parentNamespace) {
509
+ throw new Error(`Parent namespace ${parent} not found`)
510
+ }
511
+ if (this.isChildOf(parent, key)) {
512
+ throw new Error(`Cannot move a namespace to its own child`)
513
+ }
514
+ }
515
+
516
+ // namespaces can only have one parent
517
+ const currentParentKey = this.graph.parent(key)
518
+ if (currentParentKey && !parent) {
519
+ // The new parent namespace will detach the namespace when attaching to self.
520
+ const currentParent = this.graph.node(currentParentKey) as DomainNamespace
521
+ currentParent.detachNamespace(key)
522
+ } else if (!currentParentKey) {
523
+ // The namespace is a root namespace.
524
+ this.removeField(key)
525
+ }
526
+ // Add to new parent
527
+ if (parent && parentNamespace) {
528
+ parentNamespace.attachNamespace(key)
529
+ } else {
530
+ // The namespace is a root namespace.
531
+ this.fields.push({
532
+ type: 'namespace',
533
+ key,
534
+ })
535
+ }
536
+ this.notifyChange()
537
+ return this
538
+ }
539
+
540
+ /**
541
+ * Checks if a namespace is a child of another namespace.
542
+ * @param parentKey The key of the parent namespace.
543
+ * @param childKey The key of the child namespace.
544
+ * @returns True if the namespace is a child of another namespace.
545
+ */
546
+ private isChildOf(parentKey: string, childKey: string): boolean {
547
+ const children = this.graph.children(childKey)
548
+ for (const child of children) {
549
+ if (child === parentKey) {
550
+ return true
551
+ }
552
+ if (this.isChildOf(parentKey, child)) {
553
+ return true
554
+ }
555
+ }
556
+ return false
557
+ }
558
+
559
+ /**
560
+ * Adds a data model to the graph.
561
+ *
562
+ * @param input The partial data model schema.
563
+ * @param parent The parent namespace key. If not
564
+ * provided, the new data model will be added to the
565
+ * root of the graph.
566
+ * @returns The created data model instance.
567
+ * @throws Error When the parent does not exist or is not
568
+ * a namespace.
569
+ * @example
570
+ * ```typescript
571
+ * const newModel = dataDomain.addModel({
572
+ * key: 'newModel',
573
+ * });
574
+ * ```
575
+ */
576
+ addModel(input?: Partial<DomainModelSchema>, parent?: string): DomainModel {
577
+ if (parent) {
578
+ if (!this.graph.hasNode(parent)) {
579
+ throw new Error(`Parent ${parent} does not exist`)
580
+ }
581
+ const instance = this.findNamespace(parent)
582
+ if (!instance) {
583
+ throw new Error(`Parent namespace ${parent} is not a valid namespace`)
584
+ }
585
+ if (instance.domain.key !== this.key) {
586
+ throw new Error(`Cannot add a model to a foreign domain`)
587
+ }
588
+ return instance.addModel(input)
589
+ }
590
+ const item = new DomainModel(this, input)
591
+ this.graph.setNode(item.key, item)
592
+ this.fields.push({
593
+ type: 'model',
594
+ key: item.key,
595
+ })
596
+ return item
597
+ }
598
+
599
+ /**
600
+ * Removes a data model from the graph.
601
+ *
602
+ * @param key The key of the data model to remove.
603
+ * @returns The current DataDomain instance.
604
+ * @throws Error When the data model does not exist.
605
+ * @example
606
+ * ```typescript
607
+ * dataDomain.removeModel('userModel');
608
+ * ```
609
+ */
610
+ removeModel(key: string): this {
611
+ if (!this.graph.hasNode(key)) {
612
+ throw new Error(`Data model ${key} does not exist`)
613
+ }
614
+ const model = this.graph.node(key) as DomainModel
615
+ if (!model || model.kind !== DomainModelKind) {
616
+ throw new Error(`Data model ${key} not found`)
617
+ }
618
+ if (model.domain.key !== this.key) {
619
+ throw new Error(`Cannot remove a model from a foreign domain`)
620
+ }
621
+ const parent = model.getParentInstance()
622
+ if (parent === this) {
623
+ removeGraphNode(this.graph, key)
624
+ this.removeField(key)
625
+ this.notifyChange()
626
+ } else {
627
+ parent.removeModel(key)
628
+ }
629
+ return this
630
+ }
631
+
632
+ /**
633
+ * Lists all models of this data domain that are direct children of it.
634
+ *
635
+ * Note, it accounts for the order of the models as
636
+ * defined in the `fields` array.
637
+ *
638
+ * @returns A generator that yields each `DomainModel`.
639
+ * @example
640
+ * ```typescript
641
+ * for (const ns of namespace.listModels()) {
642
+ * console.log(ns.key);
643
+ * }
644
+ * ```
645
+ */
646
+ *listModels(): Generator<DomainModel> {
647
+ for (const { key, type } of this.fields) {
648
+ if (type !== 'model') {
649
+ continue
650
+ }
651
+ const node = this.graph.node(key)
652
+ if (!node || node.kind !== DomainModelKind) {
653
+ continue
654
+ }
655
+ yield node
656
+ }
657
+ }
658
+
659
+ /**
660
+ * Checks if this data domain has any direct models.
661
+ *
662
+ * @returns True if the data domain has models.
663
+ * @example
664
+ * ```typescript
665
+ * if (domain.hasModels()) {
666
+ * // ...
667
+ * }
668
+ * ```
669
+ */
670
+ hasModels(): boolean {
671
+ return this.fields.some((item) => item.type === 'model')
672
+ }
673
+
674
+ /**
675
+ * Lists all data models in the graph.
676
+ *
677
+ * @param parent The key of the parent namespace. If not
678
+ * provided, all root data models will be listed.
679
+ * @returns A generator that yields each `DomainModel`.
680
+ * @example
681
+ * ```typescript
682
+ * for (const model of dataDomain.listGraphModels()) {
683
+ * console.log(model.key);
684
+ * }
685
+ * ```
686
+ */
687
+ *listGraphModels(parent?: string): Generator<DomainModel> {
688
+ for (const node of this.graph.children(parent)) {
689
+ const value = this.graph.node(node) as DomainGraphNodeType
690
+ if (value.kind === DomainModelKind && value.domain.key === this.key) {
691
+ yield value
692
+ }
693
+ }
694
+ }
695
+
696
+ /**
697
+ * Finds a data model by its key.
698
+ *
699
+ * @param key The key of the data model to find.
700
+ * @returns The data model instance or undefined if not
701
+ * found.
702
+ * @example
703
+ * ```typescript
704
+ * const model = dataDomain.findModel('userModel');
705
+ * if (model) {
706
+ * console.log(model.key);
707
+ * }
708
+ * ```
709
+ */
710
+ findModel(key: string): DomainModel | undefined {
711
+ const value = this.graph.node(key) as DomainModel | undefined
712
+ if (value && value.kind === DomainModelKind) {
713
+ return value
714
+ }
715
+ return undefined
716
+ }
717
+
718
+ /**
719
+ * Moves a data model to a new parent.
720
+ *
721
+ * @param key The key of the data model to move.
722
+ * @param parent The new parent namespace key. If
723
+ * undefined, the data model will be moved to the root.
724
+ * @returns The current DataDomain instance.
725
+ * @throws Error When the data model or parent does not
726
+ * exist, or when a data model is moved to a foreign
727
+ * domain.
728
+ * @example
729
+ * ```typescript
730
+ * dataDomain.moveModel('userModel', 'newNamespace');
731
+ * ```
732
+ */
733
+ moveModel(key: string, parent?: string): this {
734
+ if (!this.graph.hasNode(key)) {
735
+ throw new Error(`Data model ${key} does not exist`)
736
+ }
737
+
738
+ const model = this.findModel(key)
739
+ if (!model) {
740
+ throw new Error(`Data model ${key} not found in the graph`)
741
+ }
742
+
743
+ if (parent) {
744
+ const instance = this.findNamespace(parent)
745
+ if (instance && instance.domain.key !== this.key) {
746
+ throw new Error(`Cannot move a model to a foreign domain`)
747
+ }
748
+ if (!instance) {
749
+ throw new Error(`Parent namespace ${parent} does not exist`)
750
+ }
751
+ }
752
+
753
+ if (model.domain.key !== this.key) {
754
+ throw new Error(`Cannot move a data model to a foreign domain`)
755
+ }
756
+
757
+ // namespaces can only have one parent
758
+ const currentParentKey = this.graph.parent(key)
759
+ // Remove from current parent
760
+ if (currentParentKey && !parent) {
761
+ // The new parent namespace will detach the model when attaching to self.
762
+ const currentParent = this.findNamespace(currentParentKey)
763
+ if (!currentParent) {
764
+ throw new Error(`The parent namespace ${currentParentKey} not found`)
765
+ }
766
+ currentParent.detachModel(key)
767
+ } else if (!currentParentKey) {
768
+ // The model is a root model.
769
+ this.removeField(key)
770
+ }
771
+
772
+ // Add to new parent
773
+ if (parent) {
774
+ const parentNamespace = this.findNamespace(parent) as DomainNamespace
775
+ parentNamespace.attachModel(key)
776
+ } else {
777
+ // The model is becoming a root model.
778
+ this.fields.push({
779
+ type: 'model',
780
+ key,
781
+ })
782
+ }
783
+
784
+ this.notifyChange()
785
+ return this
786
+ }
787
+
788
+ /**
789
+ * Adds an entity to a data model.
790
+ *
791
+ * @param input The partial entity schema.
792
+ * @param parent The key of the parent data model.
793
+ * @returns The created entity instance.
794
+ * @throws {Error} When the parent does not exist or is not a data model.
795
+ * @example
796
+ * ```typescript
797
+ * const userEntity = dataDomain.addEntity({
798
+ * key: 'user',
799
+ * }, 'userModel');
800
+ * ```
801
+ */
802
+ addEntity(parent: string, input?: Partial<DomainEntitySchema>): DomainEntity {
803
+ if (!parent) {
804
+ throw new Error(`An entity expects a DataModel parent`)
805
+ }
806
+ if (!this.graph.hasNode(parent)) {
807
+ throw new Error(`The parent ${parent} does not exist`)
808
+ }
809
+ const instance = this.findModel(parent)
810
+ if (!instance) {
811
+ throw new Error(`Parent model ${parent} is not a valid model`)
812
+ }
813
+ if (instance.domain.key !== this.key) {
814
+ throw new Error(`Cannot add an entity to a foreign domain`)
815
+ }
816
+ return instance.addEntity(input)
817
+ }
818
+
819
+ /**
820
+ * Removes an entity from the graph.
821
+ *
822
+ * @param key The key of the entity to remove.
823
+ * @returns The current DataDomain instance.
824
+ * @throws Error When the entity does not exist.
825
+ * @example
826
+ * ```typescript
827
+ * dataDomain.removeEntity('user');
828
+ * ```
829
+ */
830
+ removeEntity(key: string): this {
831
+ if (!this.graph.hasNode(key)) {
832
+ throw new Error(`Entity ${key} does not exist`)
833
+ }
834
+ const parentKey = this.graph.parent(key)
835
+ if (!parentKey) {
836
+ throw new Error(`Parent model not found for entity ${key}`)
837
+ }
838
+ const parent = this.findModel(parentKey)
839
+ if (!parent) {
840
+ throw new Error(`Parent model ${parentKey} not found`)
841
+ }
842
+ if (parent.domain.key !== this.key) {
843
+ throw new Error(`Cannot remove an entity from a foreign domain`)
844
+ }
845
+ parent.removeEntity(key)
846
+ return this
847
+ }
848
+
849
+ /**
850
+ * Lists all entities in a data model, or, if the model key is not provided,
851
+ * all entities in the domain.
852
+ *
853
+ * @param parent The key of the parent data model.
854
+ * @returns A generator that yields each `DomainEntity`.
855
+ * @example
856
+ * ```typescript
857
+ * for (const entity of dataDomain.listEntities('userModel')) {
858
+ * console.log(entity.key);
859
+ * }
860
+ * ```
861
+ */
862
+ *listEntities(parent?: string): Generator<DomainEntity> {
863
+ const iterator = parent ? this.graph.children(parent) : this.graph.nodes()
864
+ for (const node of iterator) {
865
+ const value = this.graph.node(node) as DomainGraphNodeType
866
+ if (value.kind === DomainEntityKind && value.domain.key === this.key) {
867
+ yield value
868
+ }
869
+ }
870
+ }
871
+
872
+ /**
873
+ * Finds an entity by its key.
874
+ *
875
+ * @param key The key of the entity to find.
876
+ * @returns The entity instance or undefined if not found.
877
+ * @example
878
+ * ```typescript
879
+ * const entity = dataDomain.findEntity('user');
880
+ * if (entity) {
881
+ * console.log(entity.key);
882
+ * }
883
+ * ```
884
+ */
885
+ findEntity(key: string): DomainEntity | undefined {
886
+ const node = this.graph.node(key) as DomainEntity | undefined
887
+ if (node && node.kind === DomainEntityKind) {
888
+ return node
889
+ }
890
+ return undefined
891
+ }
892
+
893
+ /**
894
+ * Moves an entity from one model to another.
895
+ *
896
+ * @param entityKey The key of the entity to move.
897
+ * @param sourceModelKey The key of the source model.
898
+ * @param targetModelKey The key of the target model.
899
+ * @throws Error When the entity, source model, or target model does not exist.
900
+ * @throws Error When the entity is not in the same domain.
901
+ * @throws Error When the source and target models are the same.
902
+ * @throws Error When moving to an unsupported object.
903
+ */
904
+ moveEntity(entityKey: string, sourceModelKey: string, targetModelKey: string): void {
905
+ if (sourceModelKey === targetModelKey) {
906
+ throw new Error(`Cannot move an entity to the same model`)
907
+ }
908
+ if (!this.graph.hasNode(entityKey)) {
909
+ throw new Error(`Entity ${entityKey} does not exist`)
910
+ }
911
+ if (!this.graph.hasNode(sourceModelKey)) {
912
+ throw new Error(`Source model ${sourceModelKey} does not exist`)
913
+ }
914
+ if (!this.graph.hasNode(targetModelKey)) {
915
+ throw new Error(`Target model ${targetModelKey} does not exist`)
916
+ }
917
+
918
+ const entity = this.findEntity(entityKey)
919
+ const sourceModel = this.findModel(sourceModelKey)
920
+ const targetModel = this.findModel(targetModelKey)
921
+
922
+ if (!entity || !sourceModel || !targetModel) {
923
+ throw new Error(`Entity or models not found in the graph`)
924
+ }
925
+
926
+ if (entity.domain.key !== this.key) {
927
+ // this also applied to the parent model.
928
+ throw new Error(`Cannot move an entity from a foreign domain`)
929
+ }
930
+ if (targetModel.domain.key !== this.key) {
931
+ throw new Error(`Cannot move an entity to a foreign domain`)
932
+ }
933
+ // The target model detaches the entity when attaching to self.
934
+ targetModel.attachEntity(entityKey)
935
+ this.notifyChange()
936
+ }
937
+
938
+ /**
939
+ * Adds an association between two entities.
940
+ *
941
+ * This function is a shortcut that finds the entity and
942
+ * calls the `addAssociation` method on it.
943
+ *
944
+ * @param source The key of the source entity.
945
+ * @param init The association options.
946
+ * @returns The created association.
947
+ * @throws Error When the source entity does not exist.
948
+ * @example
949
+ * ```typescript
950
+ * const addressAssociation = dataDomain.addAssociation(
951
+ * 'user', { key: 'address' }
952
+ * );
953
+ * ```
954
+ */
955
+ addAssociation(source: string, init?: AssociationAddOptions): DomainAssociation {
956
+ if (!this.graph.hasNode(source)) {
957
+ throw new Error(`Source entity ${source} not found`)
958
+ }
959
+ const entity = this.findEntity(source)
960
+ if (!entity) {
961
+ throw new Error(`Source entity ${source} not found`)
962
+ }
963
+ if (entity.domain.key !== this.key) {
964
+ throw new Error(`Cannot add an association to a foreign domain`)
965
+ }
966
+ return entity.addAssociation(init)
967
+ }
968
+
969
+ /**
970
+ * Removes an association from the graph.
971
+ *
972
+ * @param key The key of the association to remove.
973
+ * @returns The current DataDomain instance.
974
+ * @throws Error When the association does not exist or
975
+ * when the parent entity is not found.
976
+ * @example
977
+ * ```typescript
978
+ * dataDomain.removeAssociation('address');
979
+ * ```
980
+ */
981
+ removeAssociation(key: string): this {
982
+ if (!this.graph.hasNode(key)) {
983
+ throw new Error(`Association ${key} does not exist`)
984
+ }
985
+ const instance = this.graph.node(key) as DomainAssociation
986
+ if (!instance || instance.kind !== DomainAssociationKind) {
987
+ throw new Error(`Association ${key} not found`)
988
+ }
989
+ const entity = instance.getParentInstance()
990
+ if (!entity) {
991
+ throw new Error(`Parent entity not found for association ${key}`)
992
+ }
993
+ if (entity.domain.key !== this.key) {
994
+ throw new Error(`Cannot remove an association from a foreign domain`)
995
+ }
996
+ entity.removeAssociation(key)
997
+ return this
998
+ }
999
+
1000
+ /**
1001
+ * Finds an association by its key.
1002
+ *
1003
+ * @param key The key of the association to find.
1004
+ * @returns The association instance or undefined if not
1005
+ * found.
1006
+ * @example
1007
+ * ```typescript
1008
+ * const assoc = dataDomain.findAssociation('address');
1009
+ * if (assoc) {
1010
+ * console.log(assoc.key);
1011
+ * }
1012
+ * ```
1013
+ */
1014
+ findAssociation(key: string): DomainAssociation | undefined {
1015
+ const node = this.graph.node(key) as DomainAssociation | undefined
1016
+ if (node && node.kind === DomainAssociationKind) {
1017
+ return node
1018
+ }
1019
+ return undefined
1020
+ }
1021
+
1022
+ /**
1023
+ * Adds a property to an entity.
1024
+ *
1025
+ * @param parent The key of the parent entity.
1026
+ * @param property The partial property schema.
1027
+ * @returns The created property instance.
1028
+ * @throws Error When the parent does not exist or is not
1029
+ * an entity.
1030
+ * @example
1031
+ * ```typescript
1032
+ * const nameProperty = dataDomain.addProperty(
1033
+ * 'user', { key: 'name', type: 'string' }
1034
+ * );
1035
+ * ```
1036
+ */
1037
+ addProperty(parent: string, property?: Partial<DomainPropertySchema>): DomainProperty {
1038
+ if (!parent) {
1039
+ throw new Error(`A property expects a DataEntity parent`)
1040
+ }
1041
+ if (!this.graph.hasNode(parent)) {
1042
+ throw new Error(`Parent entity ${parent} does not exist`)
1043
+ }
1044
+ const entity = this.findEntity(parent)
1045
+ if (!entity) {
1046
+ throw new Error(`Parent entity ${parent} not found`)
1047
+ }
1048
+ if (entity.domain.key !== this.key) {
1049
+ throw new Error(`Cannot add a property to a foreign domain`)
1050
+ }
1051
+ return entity.addProperty(property)
1052
+ }
1053
+
1054
+ /**
1055
+ * Removes a property from the graph.
1056
+ *
1057
+ * @param key The key of the property to remove.
1058
+ * @returns The current DataDomain instance.
1059
+ * @throws Error When the property does not exist or when
1060
+ * the parent entity is not found.
1061
+ * @example
1062
+ * ```typescript
1063
+ * dataDomain.removeProperty('name');
1064
+ * ```
1065
+ */
1066
+ removeProperty(key: string): this {
1067
+ if (!this.graph.hasNode(key)) {
1068
+ throw new Error(`Property ${key} does not exist`)
1069
+ }
1070
+ const instance = this.graph.node(key) as DomainProperty
1071
+ if (!instance || instance.kind !== DomainPropertyKind) {
1072
+ throw new Error(`Property ${key} not found`)
1073
+ }
1074
+ const entity = instance.getParentInstance()
1075
+ if (!entity) {
1076
+ throw new Error(`Parent entity not found for property ${key}`)
1077
+ }
1078
+ if (entity.domain.key !== this.key) {
1079
+ throw new Error(`Cannot remove a property from a foreign domain`)
1080
+ }
1081
+ entity.removeProperty(key)
1082
+ return this
1083
+ }
1084
+
1085
+ /**
1086
+ * Finds a property by its key.
1087
+ *
1088
+ * @param key The key of the property to find.
1089
+ * @returns The property instance or undefined if not
1090
+ * found.
1091
+ * @example
1092
+ * ```typescript
1093
+ * const prop = dataDomain.findProperty('name');
1094
+ * if (prop) {
1095
+ * console.log(prop.key);
1096
+ * }
1097
+ * ```
1098
+ */
1099
+ findProperty(key: string): DomainProperty | undefined {
1100
+ const node = this.graph.node(key) as DomainProperty | undefined
1101
+ if (node && node.kind === DomainPropertyKind) {
1102
+ return node
1103
+ }
1104
+ return undefined
1105
+ }
1106
+
1107
+ /**
1108
+ * Registers a foreign DataDomain.
1109
+ *
1110
+ * @param domain The foreign DataDomain instance.
1111
+ * @param key The unique identifier for the foreign
1112
+ * domain.
1113
+ * @param version The version of the foreign domain.
1114
+ * @throws Error When a foreign domain with the same key is already registered.
1115
+ * @example
1116
+ * ```typescript
1117
+ * dataDomain.registerForeignDomain(
1118
+ * foreignDomain, 'foreignDomain', '1.0.0'
1119
+ * );
1120
+ * ```
1121
+ */
1122
+ registerForeignDomain(domain: DataDomain): void {
1123
+ const { info, key } = domain
1124
+ if (!info.version) {
1125
+ // @TODO: make the data domain immutable after a version is set.
1126
+ // This will prevent the user from changing the version
1127
+ // after the domain is registered.
1128
+ throw new Error(`Foreign domain ${key} does not have a version set`)
1129
+ }
1130
+ if (this.dependencies.has(key)) {
1131
+ throw new Error(`Foreign domain with key ${key} already registered`)
1132
+ }
1133
+ this.dependencies.set(key, domain)
1134
+ this.dependencyList.push({ key, version: info.version })
1135
+ // Copy the relevant parts of the foreign domain's graph into the local graph.
1136
+ // this.copyForeignDomainGraph(domain)
1137
+ mergeGraph(this.graph, domain.graph, domain.key)
1138
+ this.notifyChange()
1139
+ }
1140
+
1141
+ /**
1142
+ * Un-registers a foreign DataDomain. It removes all defined by the foreign domain
1143
+ * information from the graph.
1144
+ * @param key The key of the foreign domain to unregister.
1145
+ */
1146
+ unregisterForeignDomain(key: string): void {
1147
+ const foreignDomain = this.dependencies.get(key)
1148
+ if (!foreignDomain) {
1149
+ throw new Error(`Foreign domain with key ${key} not found`)
1150
+ }
1151
+ this.dependencies.delete(key)
1152
+ this.dependencyList = this.dependencyList.filter((dependency) => dependency.key !== key)
1153
+ // Remove the foreign domain's nodes from the local graph.
1154
+ removeForeignGraph(this.graph, foreignDomain.key)
1155
+ this.notifyChange()
1156
+ }
1157
+
1158
+ /**
1159
+ * Finds an entity in a foreign domain.
1160
+ *
1161
+ * @param entityKey The key of the entity to find.
1162
+ * @param domainKey The key of the foreign domain.
1163
+ * @returns The entity instance or undefined if not
1164
+ * found.
1165
+ * @example
1166
+ * ```typescript
1167
+ * const foreignUser = dataDomain.findForeignEntity(
1168
+ * 'user', 'foreignDomain'
1169
+ * );
1170
+ * if (foreignUser) {
1171
+ * console.log(foreignUser.key);
1172
+ * }
1173
+ * ```
1174
+ */
1175
+ findForeignEntity(entityKey: string, domainKey: string): DomainEntity | undefined {
1176
+ const foreignDomain = this.dependencies.get(domainKey)
1177
+ if (!foreignDomain) {
1178
+ return undefined
1179
+ }
1180
+ const foreignKey = `${domainKey}:${entityKey}`
1181
+ const node = this.graph.node(foreignKey)
1182
+ if (node && node.kind === DomainEntityKind) {
1183
+ return node as DomainEntity
1184
+ }
1185
+ return undefined
1186
+ }
1187
+
1188
+ /**
1189
+ * A convenience function to remove an object from the
1190
+ * namespace.
1191
+ *
1192
+ * @param key The key of the object to remove.
1193
+ * @param kind The kind of the object to remove.
1194
+ * @throws Error when the kind is not known.
1195
+ * @example
1196
+ * ```typescript
1197
+ * dataDomain.removeDomainElement('user', DomainEntityKind);
1198
+ * ```
1199
+ */
1200
+ removeDomainElement(key: string, kind: string): void {
1201
+ switch (kind) {
1202
+ case DomainNamespaceKind:
1203
+ this.removeNamespace(key)
1204
+ break
1205
+ case DomainModelKind:
1206
+ this.removeModel(key)
1207
+ break
1208
+ case DomainEntityKind:
1209
+ this.removeEntity(key)
1210
+ break
1211
+ case DomainPropertyKind:
1212
+ this.removeProperty(key)
1213
+ break
1214
+ case DomainAssociationKind:
1215
+ this.removeAssociation(key)
1216
+ break
1217
+ default:
1218
+ throw new Error(`Unknown kind ${kind} for the object ${key}`)
1219
+ }
1220
+ }
1221
+ }