@api-client/core 0.11.11 → 0.12.1

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