@contember/binding 1.0.0-rc.21 → 1.0.0-rc.24

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 (457) hide show
  1. package/dist/development/BindingError.js +0 -1
  2. package/dist/development/BindingError.js.map +1 -1
  3. package/dist/development/accessorPropagation/AccessorProvider.js +3 -5
  4. package/dist/development/accessorPropagation/AccessorProvider.js.map +1 -1
  5. package/dist/development/accessorPropagation/BindingOperationsContext.js +2 -4
  6. package/dist/development/accessorPropagation/BindingOperationsContext.js.map +1 -1
  7. package/dist/development/accessorPropagation/BindingOperationsProvider.js +2 -4
  8. package/dist/development/accessorPropagation/BindingOperationsProvider.js.map +1 -1
  9. package/dist/development/accessorPropagation/EntityKeyContext.js +1 -3
  10. package/dist/development/accessorPropagation/EntityKeyContext.js.map +1 -1
  11. package/dist/development/accessorPropagation/EntityKeyProvider.js +2 -4
  12. package/dist/development/accessorPropagation/EntityKeyProvider.js.map +1 -1
  13. package/dist/development/accessorPropagation/EnvironmentContext.js +2 -4
  14. package/dist/development/accessorPropagation/EnvironmentContext.js.map +1 -1
  15. package/dist/development/accessorPropagation/TreeRootIdContext.js +1 -3
  16. package/dist/development/accessorPropagation/TreeRootIdContext.js.map +1 -1
  17. package/dist/development/accessorPropagation/TreeRootIdProvider.js +2 -4
  18. package/dist/development/accessorPropagation/TreeRootIdProvider.js.map +1 -1
  19. package/dist/development/accessorPropagation/useAccessorUpdateSubscription.js +2 -4
  20. package/dist/development/accessorPropagation/useAccessorUpdateSubscription.js.map +1 -1
  21. package/dist/development/accessorPropagation/useBindingOperations.js +2 -4
  22. package/dist/development/accessorPropagation/useBindingOperations.js.map +1 -1
  23. package/dist/development/accessorPropagation/useDesugaredRelativeEntityList.js +3 -5
  24. package/dist/development/accessorPropagation/useDesugaredRelativeEntityList.js.map +1 -1
  25. package/dist/development/accessorPropagation/useDesugaredRelativeSingleEntity.js +3 -5
  26. package/dist/development/accessorPropagation/useDesugaredRelativeSingleEntity.js.map +1 -1
  27. package/dist/development/accessorPropagation/useDesugaredRelativeSingleField.js +3 -5
  28. package/dist/development/accessorPropagation/useDesugaredRelativeSingleField.js.map +1 -1
  29. package/dist/development/accessorPropagation/useEntity.js +7 -9
  30. package/dist/development/accessorPropagation/useEntity.js.map +1 -1
  31. package/dist/development/accessorPropagation/useEntityKey.js +3 -5
  32. package/dist/development/accessorPropagation/useEntityKey.js.map +1 -1
  33. package/dist/development/accessorPropagation/useEntityList.js +7 -9
  34. package/dist/development/accessorPropagation/useEntityList.js.map +1 -1
  35. package/dist/development/accessorPropagation/useEntityListSubTree.js +4 -6
  36. package/dist/development/accessorPropagation/useEntityListSubTree.js.map +1 -1
  37. package/dist/development/accessorPropagation/useEntityListSubTreeParameters.js +2 -4
  38. package/dist/development/accessorPropagation/useEntityListSubTreeParameters.js.map +1 -1
  39. package/dist/development/accessorPropagation/useEntitySubTree.js +4 -6
  40. package/dist/development/accessorPropagation/useEntitySubTree.js.map +1 -1
  41. package/dist/development/accessorPropagation/useEntitySubTreeParameters.js +2 -4
  42. package/dist/development/accessorPropagation/useEntitySubTreeParameters.js.map +1 -1
  43. package/dist/development/accessorPropagation/useEnvironment.js +2 -4
  44. package/dist/development/accessorPropagation/useEnvironment.js.map +1 -1
  45. package/dist/development/accessorPropagation/useField.js +7 -9
  46. package/dist/development/accessorPropagation/useField.js.map +1 -1
  47. package/dist/development/accessorPropagation/useGetEntityByKey.js +1 -3
  48. package/dist/development/accessorPropagation/useGetEntityByKey.js.map +1 -1
  49. package/dist/development/accessorPropagation/useGetEntityListSubTree.js +4 -6
  50. package/dist/development/accessorPropagation/useGetEntityListSubTree.js.map +1 -1
  51. package/dist/development/accessorPropagation/useGetEntitySubTree.js +4 -6
  52. package/dist/development/accessorPropagation/useGetEntitySubTree.js.map +1 -1
  53. package/dist/development/accessorPropagation/useGetTreeFilters.js +1 -3
  54. package/dist/development/accessorPropagation/useGetTreeFilters.js.map +1 -1
  55. package/dist/development/accessorPropagation/useHasEntity.js +2 -4
  56. package/dist/development/accessorPropagation/useHasEntity.js.map +1 -1
  57. package/dist/development/accessorPropagation/usePersist.js +1 -3
  58. package/dist/development/accessorPropagation/usePersist.js.map +1 -1
  59. package/dist/development/accessorPropagation/useTreeRootId.js +2 -4
  60. package/dist/development/accessorPropagation/useTreeRootId.js.map +1 -1
  61. package/dist/development/accessorSorting/SetOrderFieldOnCreate.js +9 -11
  62. package/dist/development/accessorSorting/SetOrderFieldOnCreate.js.map +1 -1
  63. package/dist/development/accessorSorting/addEntityAtIndex.js +2 -4
  64. package/dist/development/accessorSorting/addEntityAtIndex.js.map +1 -1
  65. package/dist/development/accessorSorting/errors.js +1 -3
  66. package/dist/development/accessorSorting/errors.js.map +1 -1
  67. package/dist/development/accessorSorting/moveEntity.js +1 -3
  68. package/dist/development/accessorSorting/moveEntity.js.map +1 -1
  69. package/dist/development/accessorSorting/repairEntitiesOrder.js +0 -1
  70. package/dist/development/accessorSorting/repairEntitiesOrder.js.map +1 -1
  71. package/dist/development/accessorSorting/sortEntities.js +0 -1
  72. package/dist/development/accessorSorting/sortEntities.js.map +1 -1
  73. package/dist/development/accessorSorting/useSortedEntities.js +6 -8
  74. package/dist/development/accessorSorting/useSortedEntities.js.map +1 -1
  75. package/dist/development/accessorTree/AccessorTree.js +4 -6
  76. package/dist/development/accessorTree/AccessorTree.js.map +1 -1
  77. package/dist/development/accessorTree/DirtinessContext.js +1 -3
  78. package/dist/development/accessorTree/DirtinessContext.js.map +1 -1
  79. package/dist/development/accessorTree/MutationStateContext.js +1 -3
  80. package/dist/development/accessorTree/MutationStateContext.js.map +1 -1
  81. package/dist/development/accessorTree/NormalizedPersistedData.js +0 -1
  82. package/dist/development/accessorTree/NormalizedPersistedData.js.map +1 -1
  83. package/dist/development/accessorTree/RuntimeId.js +22 -17
  84. package/dist/development/accessorTree/RuntimeId.js.map +1 -1
  85. package/dist/development/accessorTree/accessorTreeStateReducer.js +0 -1
  86. package/dist/development/accessorTree/accessorTreeStateReducer.js.map +1 -1
  87. package/dist/development/accessorTree/metadataToRequestError.js +0 -1
  88. package/dist/development/accessorTree/metadataToRequestError.js.map +1 -1
  89. package/dist/development/accessorTree/useDataBinding.js +5 -7
  90. package/dist/development/accessorTree/useDataBinding.js.map +1 -1
  91. package/dist/development/accessorTree/useDirtinessState.js +2 -4
  92. package/dist/development/accessorTree/useDirtinessState.js.map +1 -1
  93. package/dist/development/accessorTree/useMutationState.js +2 -4
  94. package/dist/development/accessorTree/useMutationState.js.map +1 -1
  95. package/dist/development/accessors/EntityAccessor.js +5 -7
  96. package/dist/development/accessors/EntityAccessor.js.map +1 -1
  97. package/dist/development/accessors/EntityListAccessor.js +1 -3
  98. package/dist/development/accessors/EntityListAccessor.js.map +1 -1
  99. package/dist/development/accessors/ErrorAccessor.js +0 -1
  100. package/dist/development/accessors/ErrorAccessor.js.map +1 -1
  101. package/dist/development/accessors/FieldAccessor.js +3 -5
  102. package/dist/development/accessors/FieldAccessor.js.map +1 -1
  103. package/dist/development/accessors/TreeRootAccessor.js +0 -1
  104. package/dist/development/accessors/TreeRootAccessor.js.map +1 -1
  105. package/dist/development/bindingTypes.js +0 -1
  106. package/dist/development/bindingTypes.js.map +1 -1
  107. package/dist/development/core/AccessorErrorManager.js +15 -11
  108. package/dist/development/core/AccessorErrorManager.js.map +1 -1
  109. package/dist/development/core/Config.js +18 -12
  110. package/dist/development/core/Config.js.map +1 -1
  111. package/dist/development/core/DataBinding.js +53 -48
  112. package/dist/development/core/DataBinding.js.map +1 -1
  113. package/dist/development/core/DirtinessTracker.js +10 -3
  114. package/dist/development/core/DirtinessTracker.js.map +1 -1
  115. package/dist/development/core/ErrorsPreprocessor.js +3 -5
  116. package/dist/development/core/ErrorsPreprocessor.js.map +1 -1
  117. package/dist/development/core/EventManager.js +20 -16
  118. package/dist/development/core/EventManager.js.map +1 -1
  119. package/dist/development/core/IncrementalMarkerBuilder.js +14 -8
  120. package/dist/development/core/IncrementalMarkerBuilder.js.map +1 -1
  121. package/dist/development/core/MarkerComparator.js +6 -8
  122. package/dist/development/core/MarkerComparator.js.map +1 -1
  123. package/dist/development/core/MarkerMerger.js +17 -15
  124. package/dist/development/core/MarkerMerger.js.map +1 -1
  125. package/dist/development/core/MarkerTreeGenerator.js +18 -13
  126. package/dist/development/core/MarkerTreeGenerator.js.map +1 -1
  127. package/dist/development/core/MutationGenerator.js +13 -15
  128. package/dist/development/core/MutationGenerator.js.map +1 -1
  129. package/dist/development/core/QueryGenerator.js +9 -11
  130. package/dist/development/core/QueryGenerator.js.map +1 -1
  131. package/dist/development/core/RealmKeyGenerator.js +10 -6
  132. package/dist/development/core/RealmKeyGenerator.js.map +1 -1
  133. package/dist/development/core/RequestResponseNormalizer.js +7 -9
  134. package/dist/development/core/RequestResponseNormalizer.js.map +1 -1
  135. package/dist/development/core/StateInitializer.js +27 -23
  136. package/dist/development/core/StateInitializer.js.map +1 -1
  137. package/dist/development/core/TreeAugmenter.js +6 -8
  138. package/dist/development/core/TreeAugmenter.js.map +1 -1
  139. package/dist/development/core/TreeFilterGenerator.js +8 -8
  140. package/dist/development/core/TreeFilterGenerator.js.map +1 -1
  141. package/dist/development/core/TreeParameterMerger.js +2 -4
  142. package/dist/development/core/TreeParameterMerger.js.map +1 -1
  143. package/dist/development/core/TreeStore.js +19 -15
  144. package/dist/development/core/TreeStore.js.map +1 -1
  145. package/dist/development/core/VariableInputTransformer.js +2 -4
  146. package/dist/development/core/VariableInputTransformer.js.map +1 -1
  147. package/dist/development/core/exceptions/ErrorLocator.js +10 -6
  148. package/dist/development/core/exceptions/ErrorLocator.js.map +1 -1
  149. package/dist/development/core/exceptions/LocalizedBindingError.js +1 -3
  150. package/dist/development/core/exceptions/LocalizedBindingError.js.map +1 -1
  151. package/dist/development/core/exceptions/MarkerSugarer.js +7 -9
  152. package/dist/development/core/exceptions/MarkerSugarer.js.map +1 -1
  153. package/dist/development/core/exceptions/TreeParameterSugarer.js +0 -1
  154. package/dist/development/core/exceptions/TreeParameterSugarer.js.map +1 -1
  155. package/dist/development/core/factories/createBatchUpdatesOptions.js +1 -3
  156. package/dist/development/core/factories/createBatchUpdatesOptions.js.map +1 -1
  157. package/dist/development/core/operations/EntityOperations.js +13 -14
  158. package/dist/development/core/operations/EntityOperations.js.map +1 -1
  159. package/dist/development/core/operations/FieldOperations.js +10 -12
  160. package/dist/development/core/operations/FieldOperations.js.map +1 -1
  161. package/dist/development/core/operations/ListOperations.js +12 -14
  162. package/dist/development/core/operations/ListOperations.js.map +1 -1
  163. package/dist/development/core/operations/OperationsHelpers.js +4 -6
  164. package/dist/development/core/operations/OperationsHelpers.js.map +1 -1
  165. package/dist/development/core/requestAliases/MutationAlias.js +2 -4
  166. package/dist/development/core/requestAliases/MutationAlias.js.map +1 -1
  167. package/dist/development/core/requestAliases/mutationOperationSubTreeType.js +0 -1
  168. package/dist/development/core/requestAliases/mutationOperationSubTreeType.js.map +1 -1
  169. package/dist/development/core/requestAliases/mutationOperationType.js +0 -1
  170. package/dist/development/core/requestAliases/mutationOperationType.js.map +1 -1
  171. package/dist/development/core/schema/Schema.js +0 -1
  172. package/dist/development/core/schema/Schema.js.map +1 -1
  173. package/dist/development/core/schema/SchemaLoader.js +9 -5
  174. package/dist/development/core/schema/SchemaLoader.js.map +1 -1
  175. package/dist/development/core/schema/SchemaPreprocessor.js +0 -1
  176. package/dist/development/core/schema/SchemaPreprocessor.js.map +1 -1
  177. package/dist/development/core/schema/SchemaValidator.js +9 -11
  178. package/dist/development/core/schema/SchemaValidator.js.map +1 -1
  179. package/dist/development/core/state/StateIterator.js +2 -4
  180. package/dist/development/core/state/StateIterator.js.map +1 -1
  181. package/dist/development/core/state/getEntityMarker.js +0 -1
  182. package/dist/development/core/state/getEntityMarker.js.map +1 -1
  183. package/dist/development/core/utils/getCombinedSignal.js +0 -1
  184. package/dist/development/core/utils/getCombinedSignal.js.map +1 -1
  185. package/dist/development/coreComponents/Component.js +2 -4
  186. package/dist/development/coreComponents/Component.js.map +1 -1
  187. package/dist/development/coreComponents/DataBindingProvider.js +3 -5
  188. package/dist/development/coreComponents/DataBindingProvider.js.map +1 -1
  189. package/dist/development/coreComponents/Entity.js +3 -5
  190. package/dist/development/coreComponents/Entity.js.map +1 -1
  191. package/dist/development/coreComponents/EntityList.js +3 -5
  192. package/dist/development/coreComponents/EntityList.js.map +1 -1
  193. package/dist/development/coreComponents/EntityListSubTree.js +12 -14
  194. package/dist/development/coreComponents/EntityListSubTree.js.map +1 -1
  195. package/dist/development/coreComponents/EntitySubTree.js +14 -16
  196. package/dist/development/coreComponents/EntitySubTree.js.map +1 -1
  197. package/dist/development/coreComponents/Field.js +5 -7
  198. package/dist/development/coreComponents/Field.js.map +1 -1
  199. package/dist/development/coreComponents/HasMany.js +8 -10
  200. package/dist/development/coreComponents/HasMany.js.map +1 -1
  201. package/dist/development/coreComponents/HasOne.js +8 -10
  202. package/dist/development/coreComponents/HasOne.js.map +1 -1
  203. package/dist/development/coreComponents/ParentEntity.js +3 -5
  204. package/dist/development/coreComponents/ParentEntity.js.map +1 -1
  205. package/dist/development/dao/Environment.js +7 -2
  206. package/dist/development/dao/Environment.js.map +1 -1
  207. package/dist/development/dao/VariableFieldValue.js +0 -1
  208. package/dist/development/dao/VariableFieldValue.js.map +1 -1
  209. package/dist/development/entityEvents/useDerivedField.js +4 -6
  210. package/dist/development/entityEvents/useDerivedField.js.map +1 -1
  211. package/dist/development/entityEvents/useEntityBeforePersist.js +1 -3
  212. package/dist/development/entityEvents/useEntityBeforePersist.js.map +1 -1
  213. package/dist/development/entityEvents/useEntityBeforeUpdate.js +1 -3
  214. package/dist/development/entityEvents/useEntityBeforeUpdate.js.map +1 -1
  215. package/dist/development/entityEvents/useEntityEvent.js +3 -5
  216. package/dist/development/entityEvents/useEntityEvent.js.map +1 -1
  217. package/dist/development/entityEvents/useEntityPersistError.js +1 -3
  218. package/dist/development/entityEvents/useEntityPersistError.js.map +1 -1
  219. package/dist/development/entityEvents/useEntityPersistSuccess.js +1 -3
  220. package/dist/development/entityEvents/useEntityPersistSuccess.js.map +1 -1
  221. package/dist/development/entityEvents/useOnConnectionUpdate.js +1 -3
  222. package/dist/development/entityEvents/useOnConnectionUpdate.js.map +1 -1
  223. package/dist/development/fieldHelpers/helpers/FieldHelper.js +8 -4
  224. package/dist/development/fieldHelpers/helpers/FieldHelper.js.map +1 -1
  225. package/dist/development/fieldHelpers/helpers/TemporalFieldHelper.js +1 -3
  226. package/dist/development/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  227. package/dist/development/fieldHelpers/helpers/UuidFieldHelper.js +2 -4
  228. package/dist/development/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  229. package/dist/development/helperComponents/DeferredSubTrees.js +9 -11
  230. package/dist/development/helperComponents/DeferredSubTrees.js.map +1 -1
  231. package/dist/development/helperComponents/EntityView.js +2 -4
  232. package/dist/development/helperComponents/EntityView.js.map +1 -1
  233. package/dist/development/helperComponents/FieldView.js +7 -9
  234. package/dist/development/helperComponents/FieldView.js.map +1 -1
  235. package/dist/development/helperComponents/StaticRender.js +2 -4
  236. package/dist/development/helperComponents/StaticRender.js.map +1 -1
  237. package/dist/development/helperComponents/SugaredField.js +3 -5
  238. package/dist/development/helperComponents/SugaredField.js.map +1 -1
  239. package/dist/development/index.js +101 -101
  240. package/dist/development/index.js.map +1 -1
  241. package/dist/development/markers/EntityFieldMarkersContainer.js +5 -7
  242. package/dist/development/markers/EntityFieldMarkersContainer.js.map +1 -1
  243. package/dist/development/markers/EntityFieldsWithHoistablesMarker.js +0 -1
  244. package/dist/development/markers/EntityFieldsWithHoistablesMarker.js.map +1 -1
  245. package/dist/development/markers/EntityListSubTreeMarker.js +8 -4
  246. package/dist/development/markers/EntityListSubTreeMarker.js.map +1 -1
  247. package/dist/development/markers/EntitySubTreeMarker.js +8 -4
  248. package/dist/development/markers/EntitySubTreeMarker.js.map +1 -1
  249. package/dist/development/markers/FieldMarker.js +8 -4
  250. package/dist/development/markers/FieldMarker.js.map +1 -1
  251. package/dist/development/markers/HasManyRelationMarker.js +9 -5
  252. package/dist/development/markers/HasManyRelationMarker.js.map +1 -1
  253. package/dist/development/markers/HasOneRelationMarker.js +8 -4
  254. package/dist/development/markers/HasOneRelationMarker.js.map +1 -1
  255. package/dist/development/markers/MarkerTreeRoot.js +0 -1
  256. package/dist/development/markers/MarkerTreeRoot.js.map +1 -1
  257. package/dist/development/markers/ParentEntityMarker.js +0 -1
  258. package/dist/development/markers/ParentEntityMarker.js.map +1 -1
  259. package/dist/development/markers/PlaceholderGenerator.js +1 -3
  260. package/dist/development/markers/PlaceholderGenerator.js.map +1 -1
  261. package/dist/development/queryLanguage/CacheStore.js +10 -4
  262. package/dist/development/queryLanguage/CacheStore.js.map +1 -1
  263. package/dist/development/queryLanguage/MarkerFactory.js +11 -13
  264. package/dist/development/queryLanguage/MarkerFactory.js.map +1 -1
  265. package/dist/development/queryLanguage/Parser.js +561 -556
  266. package/dist/development/queryLanguage/Parser.js.map +1 -1
  267. package/dist/development/queryLanguage/QueryLanguage.js +32 -27
  268. package/dist/development/queryLanguage/QueryLanguage.js.map +1 -1
  269. package/dist/development/queryLanguage/QueryLanguageError.js +0 -1
  270. package/dist/development/queryLanguage/QueryLanguageError.js.map +1 -1
  271. package/dist/development/queryLanguage/tokenList.js +1 -3
  272. package/dist/development/queryLanguage/tokenList.js.map +1 -1
  273. package/dist/development/structures/BijectiveIndexedMap.js +14 -9
  274. package/dist/development/structures/BijectiveIndexedMap.js.map +1 -1
  275. package/dist/development/structures/LRUCache.js +7 -2
  276. package/dist/development/structures/LRUCache.js.map +1 -1
  277. package/dist/development/structures/WeakIdCache.js +11 -4
  278. package/dist/development/structures/WeakIdCache.js.map +1 -1
  279. package/dist/development/treeParameterHelpers/wrapFilterInHasOnes.js +1 -3
  280. package/dist/development/treeParameterHelpers/wrapFilterInHasOnes.js.map +1 -1
  281. package/dist/development/treeParameters/EntityCreationParameters.js +0 -1
  282. package/dist/development/treeParameters/EntityCreationParameters.js.map +1 -1
  283. package/dist/development/treeParameters/EntityListPreferences.js +0 -1
  284. package/dist/development/treeParameters/EntityListPreferences.js.map +1 -1
  285. package/dist/development/treeParameters/EventListenersStore.js +0 -1
  286. package/dist/development/treeParameters/EventListenersStore.js.map +1 -1
  287. package/dist/development/treeParameters/LeafField.js +0 -1
  288. package/dist/development/treeParameters/LeafField.js.map +1 -1
  289. package/dist/development/treeParameters/QualifiedEntityParameters.js +0 -1
  290. package/dist/development/treeParameters/QualifiedEntityParameters.js.map +1 -1
  291. package/dist/development/treeParameters/Relation.js +0 -1
  292. package/dist/development/treeParameters/Relation.js.map +1 -1
  293. package/dist/development/utils/Hashing.js +0 -1
  294. package/dist/development/utils/Hashing.js.map +1 -1
  295. package/dist/development/utils/assert.js +1 -3
  296. package/dist/development/utils/assert.js.map +1 -1
  297. package/dist/development/utils/assertNever.js +1 -3
  298. package/dist/development/utils/assertNever.js.map +1 -1
  299. package/dist/development/utils/generateEnumerabilityPreventingEntropy.js +0 -1
  300. package/dist/development/utils/generateEnumerabilityPreventingEntropy.js.map +1 -1
  301. package/dist/development/utils/isEmptyObject.js +0 -1
  302. package/dist/development/utils/isEmptyObject.js.map +1 -1
  303. package/dist/development/utils/ucfirst.js +0 -1
  304. package/dist/development/utils/ucfirst.js.map +1 -1
  305. package/dist/production/BindingError.js.map +1 -1
  306. package/dist/production/accessorPropagation/AccessorProvider.js.map +1 -1
  307. package/dist/production/accessorPropagation/BindingOperationsContext.js.map +1 -1
  308. package/dist/production/accessorPropagation/BindingOperationsProvider.js.map +1 -1
  309. package/dist/production/accessorPropagation/EntityKeyContext.js.map +1 -1
  310. package/dist/production/accessorPropagation/EntityKeyProvider.js.map +1 -1
  311. package/dist/production/accessorPropagation/EnvironmentContext.js.map +1 -1
  312. package/dist/production/accessorPropagation/TreeRootIdContext.js.map +1 -1
  313. package/dist/production/accessorPropagation/TreeRootIdProvider.js.map +1 -1
  314. package/dist/production/accessorPropagation/useAccessorUpdateSubscription.js.map +1 -1
  315. package/dist/production/accessorPropagation/useBindingOperations.js.map +1 -1
  316. package/dist/production/accessorPropagation/useDesugaredRelativeEntityList.js.map +1 -1
  317. package/dist/production/accessorPropagation/useDesugaredRelativeSingleEntity.js.map +1 -1
  318. package/dist/production/accessorPropagation/useDesugaredRelativeSingleField.js.map +1 -1
  319. package/dist/production/accessorPropagation/useEntity.js.map +1 -1
  320. package/dist/production/accessorPropagation/useEntityKey.js.map +1 -1
  321. package/dist/production/accessorPropagation/useEntityList.js.map +1 -1
  322. package/dist/production/accessorPropagation/useEntityListSubTree.js.map +1 -1
  323. package/dist/production/accessorPropagation/useEntityListSubTreeParameters.js.map +1 -1
  324. package/dist/production/accessorPropagation/useEntitySubTree.js.map +1 -1
  325. package/dist/production/accessorPropagation/useEntitySubTreeParameters.js.map +1 -1
  326. package/dist/production/accessorPropagation/useEnvironment.js.map +1 -1
  327. package/dist/production/accessorPropagation/useField.js.map +1 -1
  328. package/dist/production/accessorPropagation/useGetEntityByKey.js.map +1 -1
  329. package/dist/production/accessorPropagation/useGetEntityListSubTree.js.map +1 -1
  330. package/dist/production/accessorPropagation/useGetEntitySubTree.js.map +1 -1
  331. package/dist/production/accessorPropagation/useGetTreeFilters.js.map +1 -1
  332. package/dist/production/accessorPropagation/useHasEntity.js.map +1 -1
  333. package/dist/production/accessorPropagation/usePersist.js.map +1 -1
  334. package/dist/production/accessorPropagation/useTreeRootId.js.map +1 -1
  335. package/dist/production/accessorSorting/SetOrderFieldOnCreate.js.map +1 -1
  336. package/dist/production/accessorSorting/addEntityAtIndex.js.map +1 -1
  337. package/dist/production/accessorSorting/errors.js.map +1 -1
  338. package/dist/production/accessorSorting/moveEntity.js.map +1 -1
  339. package/dist/production/accessorSorting/repairEntitiesOrder.js.map +1 -1
  340. package/dist/production/accessorSorting/sortEntities.js.map +1 -1
  341. package/dist/production/accessorSorting/useSortedEntities.js.map +1 -1
  342. package/dist/production/accessorTree/AccessorTree.js.map +1 -1
  343. package/dist/production/accessorTree/DirtinessContext.js.map +1 -1
  344. package/dist/production/accessorTree/MutationStateContext.js.map +1 -1
  345. package/dist/production/accessorTree/NormalizedPersistedData.js.map +1 -1
  346. package/dist/production/accessorTree/RuntimeId.js.map +1 -1
  347. package/dist/production/accessorTree/accessorTreeStateReducer.js.map +1 -1
  348. package/dist/production/accessorTree/metadataToRequestError.js.map +1 -1
  349. package/dist/production/accessorTree/useDataBinding.js.map +1 -1
  350. package/dist/production/accessorTree/useDirtinessState.js.map +1 -1
  351. package/dist/production/accessorTree/useMutationState.js.map +1 -1
  352. package/dist/production/accessors/EntityAccessor.js.map +1 -1
  353. package/dist/production/accessors/EntityListAccessor.js.map +1 -1
  354. package/dist/production/accessors/ErrorAccessor.js.map +1 -1
  355. package/dist/production/accessors/FieldAccessor.js.map +1 -1
  356. package/dist/production/accessors/TreeRootAccessor.js.map +1 -1
  357. package/dist/production/bindingTypes.js.map +1 -1
  358. package/dist/production/core/AccessorErrorManager.js.map +1 -1
  359. package/dist/production/core/Config.js.map +1 -1
  360. package/dist/production/core/DataBinding.js.map +1 -1
  361. package/dist/production/core/DirtinessTracker.js.map +1 -1
  362. package/dist/production/core/ErrorsPreprocessor.js.map +1 -1
  363. package/dist/production/core/EventManager.js.map +1 -1
  364. package/dist/production/core/IncrementalMarkerBuilder.js.map +1 -1
  365. package/dist/production/core/MarkerComparator.js.map +1 -1
  366. package/dist/production/core/MarkerMerger.js.map +1 -1
  367. package/dist/production/core/MarkerTreeGenerator.js.map +1 -1
  368. package/dist/production/core/MutationGenerator.js.map +1 -1
  369. package/dist/production/core/QueryGenerator.js.map +1 -1
  370. package/dist/production/core/RealmKeyGenerator.js.map +1 -1
  371. package/dist/production/core/RequestResponseNormalizer.js.map +1 -1
  372. package/dist/production/core/StateInitializer.js.map +1 -1
  373. package/dist/production/core/TreeAugmenter.js.map +1 -1
  374. package/dist/production/core/TreeFilterGenerator.js.map +1 -1
  375. package/dist/production/core/TreeParameterMerger.js.map +1 -1
  376. package/dist/production/core/TreeStore.js.map +1 -1
  377. package/dist/production/core/VariableInputTransformer.js.map +1 -1
  378. package/dist/production/core/exceptions/ErrorLocator.js.map +1 -1
  379. package/dist/production/core/exceptions/LocalizedBindingError.js.map +1 -1
  380. package/dist/production/core/exceptions/MarkerSugarer.js.map +1 -1
  381. package/dist/production/core/exceptions/TreeParameterSugarer.js.map +1 -1
  382. package/dist/production/core/factories/createBatchUpdatesOptions.js.map +1 -1
  383. package/dist/production/core/operations/EntityOperations.js.map +1 -1
  384. package/dist/production/core/operations/FieldOperations.js.map +1 -1
  385. package/dist/production/core/operations/ListOperations.js.map +1 -1
  386. package/dist/production/core/operations/OperationsHelpers.js.map +1 -1
  387. package/dist/production/core/requestAliases/MutationAlias.js.map +1 -1
  388. package/dist/production/core/requestAliases/mutationOperationSubTreeType.js.map +1 -1
  389. package/dist/production/core/requestAliases/mutationOperationType.js.map +1 -1
  390. package/dist/production/core/schema/Schema.js.map +1 -1
  391. package/dist/production/core/schema/SchemaLoader.js.map +1 -1
  392. package/dist/production/core/schema/SchemaPreprocessor.js.map +1 -1
  393. package/dist/production/core/state/StateIterator.js.map +1 -1
  394. package/dist/production/core/state/getEntityMarker.js.map +1 -1
  395. package/dist/production/core/utils/getCombinedSignal.js.map +1 -1
  396. package/dist/production/coreComponents/Component.js.map +1 -1
  397. package/dist/production/coreComponents/DataBindingProvider.js.map +1 -1
  398. package/dist/production/coreComponents/Entity.js.map +1 -1
  399. package/dist/production/coreComponents/EntityList.js.map +1 -1
  400. package/dist/production/coreComponents/EntityListSubTree.js.map +1 -1
  401. package/dist/production/coreComponents/EntitySubTree.js.map +1 -1
  402. package/dist/production/coreComponents/Field.js.map +1 -1
  403. package/dist/production/coreComponents/HasMany.js.map +1 -1
  404. package/dist/production/coreComponents/HasOne.js.map +1 -1
  405. package/dist/production/coreComponents/ParentEntity.js.map +1 -1
  406. package/dist/production/dao/Environment.js.map +1 -1
  407. package/dist/production/dao/VariableFieldValue.js.map +1 -1
  408. package/dist/production/entityEvents/useDerivedField.js.map +1 -1
  409. package/dist/production/entityEvents/useEntityBeforePersist.js.map +1 -1
  410. package/dist/production/entityEvents/useEntityBeforeUpdate.js.map +1 -1
  411. package/dist/production/entityEvents/useEntityEvent.js.map +1 -1
  412. package/dist/production/entityEvents/useEntityPersistError.js.map +1 -1
  413. package/dist/production/entityEvents/useEntityPersistSuccess.js.map +1 -1
  414. package/dist/production/entityEvents/useOnConnectionUpdate.js.map +1 -1
  415. package/dist/production/fieldHelpers/helpers/FieldHelper.js.map +1 -1
  416. package/dist/production/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  417. package/dist/production/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  418. package/dist/production/helperComponents/DeferredSubTrees.js.map +1 -1
  419. package/dist/production/helperComponents/EntityView.js.map +1 -1
  420. package/dist/production/helperComponents/FieldView.js.map +1 -1
  421. package/dist/production/helperComponents/StaticRender.js.map +1 -1
  422. package/dist/production/helperComponents/SugaredField.js.map +1 -1
  423. package/dist/production/markers/EntityFieldMarkersContainer.js.map +1 -1
  424. package/dist/production/markers/EntityFieldsWithHoistablesMarker.js.map +1 -1
  425. package/dist/production/markers/EntityListSubTreeMarker.js.map +1 -1
  426. package/dist/production/markers/EntitySubTreeMarker.js.map +1 -1
  427. package/dist/production/markers/FieldMarker.js.map +1 -1
  428. package/dist/production/markers/HasManyRelationMarker.js.map +1 -1
  429. package/dist/production/markers/HasOneRelationMarker.js.map +1 -1
  430. package/dist/production/markers/MarkerTreeRoot.js.map +1 -1
  431. package/dist/production/markers/ParentEntityMarker.js.map +1 -1
  432. package/dist/production/markers/PlaceholderGenerator.js.map +1 -1
  433. package/dist/production/queryLanguage/CacheStore.js.map +1 -1
  434. package/dist/production/queryLanguage/MarkerFactory.js.map +1 -1
  435. package/dist/production/queryLanguage/Parser.js.map +1 -1
  436. package/dist/production/queryLanguage/QueryLanguage.js.map +1 -1
  437. package/dist/production/queryLanguage/QueryLanguageError.js.map +1 -1
  438. package/dist/production/queryLanguage/tokenList.js.map +1 -1
  439. package/dist/production/structures/BijectiveIndexedMap.js.map +1 -1
  440. package/dist/production/structures/LRUCache.js.map +1 -1
  441. package/dist/production/structures/WeakIdCache.js.map +1 -1
  442. package/dist/production/treeParameterHelpers/wrapFilterInHasOnes.js.map +1 -1
  443. package/dist/production/treeParameters/EntityCreationParameters.js.map +1 -1
  444. package/dist/production/treeParameters/EntityListPreferences.js.map +1 -1
  445. package/dist/production/treeParameters/EventListenersStore.js.map +1 -1
  446. package/dist/production/treeParameters/LeafField.js.map +1 -1
  447. package/dist/production/treeParameters/QualifiedEntityParameters.js.map +1 -1
  448. package/dist/production/treeParameters/Relation.js.map +1 -1
  449. package/dist/production/utils/Hashing.js.map +1 -1
  450. package/dist/production/utils/assert.js.map +1 -1
  451. package/dist/production/utils/assertNever.js.map +1 -1
  452. package/dist/production/utils/generateEnumerabilityPreventingEntropy.js.map +1 -1
  453. package/dist/production/utils/isEmptyObject.js.map +1 -1
  454. package/dist/production/utils/ucfirst.js.map +1 -1
  455. package/dist/types/tsconfig.tsbuildinfo +1 -1
  456. package/package.json +8 -6
  457. package/src/tsconfig.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"MutationGenerator.js","sources":["../../../src/core/MutationGenerator.ts"],"sourcesContent":["// noinspection JSVoidFunctionReturnValueUsed // IntelliJ seems to be super confused througouht this file.\n\nimport { CrudQueryBuilder, GraphQlBuilder } from '@contember/client'\nimport { ClientGeneratedUuid, ServerGeneratedUuid } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport { PRIMARY_KEY_NAME, TYPENAME_KEY_NAME } from '../bindingTypes'\nimport { FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '../markers'\nimport type { EntityId, EntityName, FieldValue, PlaceholderName, TreeRootId } from '../treeParameters'\nimport { assertNever, isEmptyObject } from '../utils'\nimport { QueryGenerator } from './QueryGenerator'\nimport { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'\nimport {\n\tEntityListState,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n} from './state'\nimport type { TreeStore } from './TreeStore'\n\ntype QueryBuilder = Omit<CrudQueryBuilder.CrudQueryBuilder, CrudQueryBuilder.Queries>\n\ntype ProcessedPlaceholdersByEntity = Map<EntityState, Set<PlaceholderName>>\n\n// TODO enforce correct expected mutations in dev mode.\nexport class MutationGenerator {\n\tpublic constructor(private readonly treeStore: TreeStore) {}\n\n\tpublic getPersistMutation(): string | undefined {\n\t\ttry {\n\t\t\tlet builder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()\n\t\t\tconst processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()\n\n\t\t\tfor (const [treeRootId, rootStates] of this.treeStore.subTreeStatesByRoot) {\n\t\t\t\tfor (const [placeholderName, subTreeState] of rootStates) {\n\t\t\t\t\tif (subTreeState.type === 'entityRealm') {\n\t\t\t\t\t\tbuilder = this.addSubMutation(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\tmutationOperationSubTreeType.singleEntity,\n\t\t\t\t\t\t\tsubTreeState.entity.id.value,\n\t\t\t\t\t\t\tsubTreeState,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t} else if (subTreeState.type === 'entityList') {\n\t\t\t\t\t\tfor (const childState of subTreeState.children.values()) {\n\t\t\t\t\t\t\tif (childState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t// TODO there can be a forceCreate somewhere in there that we're hereby ignoring.\n\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbuilder = this.addSubMutation(\n\t\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\t\tmutationOperationSubTreeType.entityList,\n\t\t\t\t\t\t\t\tchildState.entity.id.value,\n\t\t\t\t\t\t\t\tchildState,\n\t\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (subTreeState.plannedRemovals) {\n\t\t\t\t\t\t\tfor (const [removedId, removalType] of subTreeState.plannedRemovals) {\n\t\t\t\t\t\t\t\tif (removalType === 'disconnect') {\n\t\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (removalType === 'delete') {\n\t\t\t\t\t\t\t\t\tbuilder = this.addDeleteMutation(\n\t\t\t\t\t\t\t\t\t\tsubTreeState.entityName,\n\t\t\t\t\t\t\t\t\t\tremovedId,\n\t\t\t\t\t\t\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\t\t\t\t\tsubTreePlaceholder: placeholderName,\n\t\t\t\t\t\t\t\t\t\t\ttype: mutationOperationType.delete,\n\t\t\t\t\t\t\t\t\t\t\tsubTreeType: mutationOperationSubTreeType.entityList,\n\t\t\t\t\t\t\t\t\t\t\tentityId: removedId,\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tassertNever(removalType)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tassertNever(subTreeState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn builder.inTransaction(undefined, { deferForeignKeyConstraints: true }).getGql()\n\t\t} catch (e) {\n\t\t\treturn undefined\n\t\t}\n\t}\n\n\tprivate addSubMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\ttreeRootId: TreeRootId | undefined,\n\t\tsubTreePlaceholder: PlaceholderName,\n\t\tsubTreeType: typeof mutationOperationSubTreeType[keyof typeof mutationOperationSubTreeType],\n\t\tentityId: EntityId,\n\t\trealmState: EntityRealmState,\n\t\tqueryBuilder: QueryBuilder,\n\t): QueryBuilder {\n\t\tif (realmState.unpersistedChangesCount === 0) {\n\t\t\t// Bail out early\n\t\t\treturn queryBuilder\n\t\t}\n\t\tif (realmState.entity.isScheduledForDeletion) {\n\t\t\treturn this.addDeleteMutation(\n\t\t\t\trealmState.entity.entityName,\n\t\t\t\tentityId,\n\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\ttreeRootId,\n\t\t\t\t\tsubTreePlaceholder,\n\t\t\t\t\ttype: mutationOperationType.delete,\n\t\t\t\t\tsubTreeType,\n\t\t\t\t\tentityId,\n\t\t\t\t}),\n\t\t\t\tqueryBuilder,\n\t\t\t)\n\t\t} else if (!realmState.entity.id.existsOnServer) {\n\t\t\treturn this.addCreateMutation(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\trealmState,\n\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\ttreeRootId,\n\t\t\t\t\tsubTreePlaceholder,\n\t\t\t\t\ttype: mutationOperationType.create,\n\t\t\t\t\tsubTreeType,\n\t\t\t\t\tentityId,\n\t\t\t\t}),\n\t\t\t\tthis.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),\n\t\t\t\tqueryBuilder,\n\t\t\t)\n\t\t}\n\t\treturn this.addUpdateMutation(\n\t\t\tprocessedPlaceholdersByEntity,\n\t\t\trealmState,\n\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\ttreeRootId,\n\t\t\t\tsubTreePlaceholder,\n\t\t\t\ttype: mutationOperationType.update,\n\t\t\t\tsubTreeType,\n\t\t\t\tentityId,\n\t\t\t}),\n\t\t\tthis.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),\n\t\t\tqueryBuilder,\n\t\t)\n\t}\n\n\tprivate getNodeFragmentName(\n\t\tsubTreePlaceholder: PlaceholderName,\n\t\tsubTreeType: typeof mutationOperationSubTreeType[keyof typeof mutationOperationSubTreeType],\n\t\trealmState: EntityRealmState,\n\t): string | undefined {\n\t\tif (subTreeType !== mutationOperationSubTreeType.entityList) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn `${realmState.entity.entityName}_${subTreePlaceholder}`\n\t}\n\n\tprivate addDeleteMutation(\n\t\tentityName: EntityName,\n\t\tdeletedId: EntityId,\n\t\talias: string,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\treturn queryBuilder.delete(\n\t\t\tentityName,\n\t\t\tbuilder =>\n\t\t\t\tbuilder\n\t\t\t\t\t.ok()\n\t\t\t\t\t.node(builder => builder.column(PRIMARY_KEY_NAME))\n\t\t\t\t\t.by({ [PRIMARY_KEY_NAME]: deletedId })\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage(),\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate addUpdateMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tentityRealm: EntityRealmState,\n\t\talias: string,\n\t\tnodeFragmentName: string | undefined,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\tconst updateBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update> =\n\t\t\tthis.registerUpdateMutationPart(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\tentityRealm,\n\t\t\t\tnew CrudQueryBuilder.WriteDataBuilder(),\n\t\t\t)\n\t\tif (updateBuilder.data === undefined || isEmptyObject(updateBuilder.data)) {\n\t\t\treturn queryBuilder\n\t\t}\n\n\t\tconst runtimeId = entityRealm.entity.id\n\n\t\tif (!runtimeId.existsOnServer) {\n\t\t\treturn queryBuilder\n\t\t}\n\n\t\tconst readBuilder = QueryGenerator.registerQueryPart(\n\t\t\tgetEntityMarker(entityRealm).fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(),\n\t\t)\n\n\t\tif (nodeFragmentName) {\n\t\t\tqueryBuilder = queryBuilder.fragment(nodeFragmentName, entityRealm.entity.entityName, readBuilder)\n\t\t}\n\n\t\treturn queryBuilder.update(\n\t\t\tentityRealm.entity.entityName,\n\t\t\tbuilder =>\n\t\t\t\tbuilder\n\t\t\t\t\t.node(nodeFragmentName ? builder => builder.applyFragment(nodeFragmentName) : readBuilder)\n\t\t\t\t\t.data(updateBuilder)\n\t\t\t\t\t.by({ [PRIMARY_KEY_NAME]: runtimeId.value })\n\t\t\t\t\t.ok()\n\t\t\t\t\t.validation()\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage(),\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate addCreateMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tentityRealm: EntityRealmState,\n\t\talias: string,\n\t\tnodeFragmentName: string | undefined,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\tconst createBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> =\n\t\t\tthis.registerCreateMutationPart(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\tentityRealm,\n\t\t\t\tnew CrudQueryBuilder.WriteDataBuilder(),\n\t\t\t)\n\t\tif (createBuilder.data === undefined || isEmptyObject(createBuilder.data)) {\n\t\t\treturn queryBuilder\n\t\t}\n\t\tconst readBuilder = QueryGenerator.registerQueryPart(\n\t\t\tgetEntityMarker(entityRealm).fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(),\n\t\t)\n\t\tif (nodeFragmentName) {\n\t\t\tqueryBuilder = queryBuilder.fragment(nodeFragmentName, entityRealm.entity.entityName, readBuilder)\n\t\t}\n\n\t\treturn queryBuilder.create(\n\t\t\tentityRealm.entity.entityName,\n\t\t\tbuilder => {\n\t\t\t\t// if (where && writeBuilder.data !== undefined && !isEmptyObject(writeBuilder.data)) {\n\t\t\t\t// \t// Shallow cloning the parameters like this IS too naïve but it will likely last surprisingly long before we\n\t\t\t\t// \t// run into issues.\n\t\t\t\t// \twriteBuilder = new CrudQueryBuilder.WriteDataBuilder({ ...writeBuilder.data, ...where })\n\t\t\t\t// }\n\t\t\t\treturn builder\n\t\t\t\t\t.node(nodeFragmentName ? builder => builder.applyFragment(nodeFragmentName) : readBuilder)\n\t\t\t\t\t.data(createBuilder)\n\t\t\t\t\t.ok()\n\t\t\t\t\t.validation()\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage()\n\t\t\t},\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate registerCreateMutationPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tcurrentState: EntityRealmState | EntityRealmStateStub,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> = new CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>(),\n\t): CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> {\n\t\tlet processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)\n\n\t\tif (processedPlaceholders === undefined) {\n\t\t\tprocessedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))\n\t\t}\n\n\t\tif (currentState.type === 'entityRealmStub') {\n\t\t\t// TODO If there's a forceCreate, this is wrong.\n\t\t\treturn builder\n\t\t}\n\n\t\tconst pathBack = this.treeStore.getPathBackToParent(currentState)\n\n\t\tconst nonbearingFields: Array<\n\t\t\t| { type: 'field'; marker: FieldMarker; fieldState: FieldState }\n\t\t\t| { type: 'hasOne'; marker: HasOneRelationMarker; fieldState: EntityRealmState | EntityRealmStateStub }\n\t\t\t| { type: 'hasMany'; marker: HasManyRelationMarker; fieldState: EntityListState }\n\t\t> = []\n\n\t\tfor (const [placeholderName, fieldState] of currentState.children) {\n\t\t\tif (processedPlaceholders.has(placeholderName)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tprocessedPlaceholders.add(placeholderName)\n\n\t\t\tswitch (fieldState.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tconst marker = fieldState.fieldMarker\n\t\t\t\t\tconst placeholderName = marker.placeholderName\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tplaceholderName === TYPENAME_KEY_NAME ||\n\t\t\t\t\t\t(placeholderName === PRIMARY_KEY_NAME && !(currentState.entity.id instanceof ClientGeneratedUuid))\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'field',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateFieldPart(fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealmStub':\n\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\tconst marker = getEntityMarker(fieldState)\n\t\t\t\t\tif (!(marker instanceof HasOneRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.parameters.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'hasOne',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateEntityPart(processedPlaceholdersByEntity, fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tconst marker = fieldState.blueprint.marker\n\t\t\t\t\tif (!(marker instanceof HasManyRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.parameters.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'hasMany',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateEntityListPart(processedPlaceholdersByEntity, fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\treturn assertNever(fieldState)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// if (\n\t\t// \tcurrentState.blueprint.creationParameters.forceCreation &&\n\t\t// \t(builder.data === undefined || isEmptyObject(builder.data))\n\t\t// ) {\n\t\t// \tbuilder = builder.set('_dummy_field_', true)\n\t\t// }\n\n\t\tif (\n\t\t\t(builder.data !== undefined && !isEmptyObject(builder.data)) ||\n\t\t\t!getEntityMarker(currentState).fields.hasAtLeastOneBearingField\n\t\t) {\n\t\t\tfor (const field of nonbearingFields) {\n\t\t\t\tswitch (field.type) {\n\t\t\t\t\tcase 'field': {\n\t\t\t\t\t\tbuilder = this.registerCreateFieldPart(field.fieldState, field.marker, builder)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tcase 'hasOne': {\n\t\t\t\t\t\tbuilder = this.registerCreateEntityPart(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\tfield.fieldState,\n\t\t\t\t\t\t\tfield.marker,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tcase 'hasMany': {\n\t\t\t\t\t\tbuilder = this.registerCreateEntityListPart(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\tfield.fieldState,\n\t\t\t\t\t\t\tfield.marker,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tassertNever(field)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst setOnCreate = getEntityMarker(currentState).parameters.setOnCreate\n\t\tif (setOnCreate && builder.data !== undefined && !isEmptyObject(builder.data)) {\n\t\t\tfor (const key in setOnCreate) {\n\t\t\t\tconst field = setOnCreate[key]\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof field === 'string' ||\n\t\t\t\t\ttypeof field === 'number' ||\n\t\t\t\t\tfield === null ||\n\t\t\t\t\tfield instanceof GraphQlBuilder.GraphQlLiteral\n\t\t\t\t) {\n\t\t\t\t\tbuilder = builder.set(key, field)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.one(key, builder => builder.connect(field))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n\n\tprivate registerCreateFieldPart(\n\t\tfieldState: FieldState,\n\t\tmarker: FieldMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\tconst resolvedValue = fieldState.getAccessor().resolvedValue\n\n\t\tif (resolvedValue !== undefined && resolvedValue !== null) {\n\t\t\treturn builder.set(marker.fieldName, this.transformFieldValue(fieldState, resolvedValue))\n\t\t}\n\t\treturn builder\n\t}\n\n\tprivate registerCreateEntityPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tfieldState: EntityRealmState | EntityRealmStateStub,\n\t\tmarker: HasOneRelationMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\tconst reducedBy = marker.parameters.reducedBy\n\t\tconst runtimeId = fieldState.entity.id\n\n\t\tif (reducedBy === undefined) {\n\t\t\treturn builder.one(marker.parameters.field, builder => {\n\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value })\n\t\t\t\t}\n\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t})\n\t\t}\n\t\treturn builder.many(marker.parameters.field, builder => {\n\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t// TODO also potentially update\n\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t}\n\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState), alias)\n\t\t})\n\t}\n\n\tprivate registerCreateEntityListPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tfieldState: EntityListState,\n\t\tmarker: HasManyRelationMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\treturn builder.many(marker.parameters.field, builder => {\n\t\t\tfor (const entityRealm of fieldState.children.values()) {\n\t\t\t\tconst alias = MutationAlias.encodeEntityId(entityRealm.entity.id)\n\t\t\t\tif (entityRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\tbuilder = builder.connect({ [PRIMARY_KEY_NAME]: entityRealm.entity.id.value }, alias)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, entityRealm), alias)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn builder\n\t\t})\n\t}\n\n\tprivate registerUpdateMutationPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tcurrentState: EntityRealmState,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update>,\n\t): CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update> {\n\t\tlet processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)\n\n\t\tif (processedPlaceholders === undefined) {\n\t\t\tprocessedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))\n\t\t}\n\n\t\tconst pathBack = this.treeStore.getPathBackToParent(currentState)\n\t\tconst entityData = this.treeStore.persistedEntityData.get(currentState.entity.id.value)\n\n\t\tfor (const [placeholderName, fieldState] of currentState.children) {\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME || placeholderName === TYPENAME_KEY_NAME) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (processedPlaceholders.has(placeholderName)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tprocessedPlaceholders.add(placeholderName)\n\n\t\t\tswitch (fieldState.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tif (fieldState.persistedValue !== undefined) {\n\t\t\t\t\t\tconst resolvedValue = fieldState.getAccessor().resolvedValue\n\t\t\t\t\t\tif (fieldState.persistedValue !== resolvedValue) {\n\t\t\t\t\t\t\tbuilder = builder.set(placeholderName, this.transformFieldValue(fieldState, resolvedValue))\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealmStub':\n\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\tconst marker = getEntityMarker(fieldState)\n\t\t\t\t\tif (!(marker instanceof HasOneRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst runtimeId = fieldState.entity.id\n\t\t\t\t\tconst reducedBy = marker.parameters.reducedBy\n\n\t\t\t\t\tif (reducedBy === undefined) {\n\t\t\t\t\t\tconst subBuilder = ((\n\t\t\t\t\t\t\tbuilder: CrudQueryBuilder.WriteOneRelationBuilder<CrudQueryBuilder.WriteOperation.Update>,\n\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\tconst persistedValue = entityData?.get?.(placeholderName)\n\n\t\t\t\t\t\t\tif (persistedValue instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\t\tif (persistedValue.value === runtimeId.value) {\n\t\t\t\t\t\t\t\t\t// The persisted and currently referenced ids match, and so this is an update.\n\t\t\t\t\t\t\t\t\tif (fieldState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\treturn builder // …unless we're dealing with a stub. There cannot be any updates there.\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn builder.update(builder =>\n\t\t\t\t\t\t\t\t\t\tthis.registerUpdateMutationPart(processedPlaceholdersByEntity, fieldState, builder),\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// There was a referenced entity but currently, there is a different one. Let's investigate:\n\n\t\t\t\t\t\t\t\tconst plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)\n\t\t\t\t\t\t\t\tif (plannedDeletion !== undefined) {\n\t\t\t\t\t\t\t\t\t// It's planned for deletion.\n\t\t\t\t\t\t\t\t\t// TODO also potentially do something about the current entity\n\t\t\t\t\t\t\t\t\treturn builder.delete()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t\t// This isn't the persisted entity but it does exist on the server. Thus this is a connect.\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\treturn builder.connect({\n\t\t\t\t\t\t\t\t\t\t[PRIMARY_KEY_NAME]: runtimeId.value,\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// The currently present entity doesn't exist on the server. Try if creating yields anything…\n\t\t\t\t\t\t\t\tconst subBuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tif (isEmptyObject(subBuilder.data)) {\n\t\t\t\t\t\t\t\t\t// …and if it doesn't, we just disconnect.\n\t\t\t\t\t\t\t\t\treturn builder.disconnect()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// …but if it does, we return the create, disconnecting implicitly.\n\t\t\t\t\t\t\t\treturn subBuilder\n\t\t\t\t\t\t\t} else if (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t// There isn't a linked entity on the server but we're seeing one that exists there.\n\t\t\t\t\t\t\t\t// Thus this is a connect.\n\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\treturn builder.connect({\n\t\t\t\t\t\t\t\t\t[PRIMARY_KEY_NAME]: runtimeId.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})(CrudQueryBuilder.WriteOneRelationBuilder.instantiate<CrudQueryBuilder.WriteOperation.Update>())\n\n\t\t\t\t\t\tif (subBuilder.data) {\n\t\t\t\t\t\t\tbuilder = builder.one(marker.parameters.field, subBuilder.data)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This is a reduced has many relation.\n\t\t\t\t\t\tbuilder = builder.many(marker.parameters.field, builder => {\n\t\t\t\t\t\t\tconst persistedValue = entityData?.get?.(placeholderName)\n\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\n\t\t\t\t\t\t\tif (persistedValue instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\t\tif (persistedValue.value === runtimeId.value) {\n\t\t\t\t\t\t\t\t\tif (fieldState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\treturn builder\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn builder.update(\n\t\t\t\t\t\t\t\t\t\treducedBy,\n\t\t\t\t\t\t\t\t\t\tbuilder => this.registerUpdateMutationPart(processedPlaceholdersByEntity, fieldState, builder),\n\t\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)\n\t\t\t\t\t\t\t\tif (plannedDeletion !== undefined) {\n\t\t\t\t\t\t\t\t\t// TODO also potentially do something about the current entity\n\t\t\t\t\t\t\t\t\treturn builder.delete(reducedBy, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t\t// TODO will re-using the alias like this work?\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\treturn builder.disconnect(reducedBy, alias).connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst subBuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tif (isEmptyObject(subBuilder.data)) {\n\t\t\t\t\t\t\t\t\treturn builder.disconnect(reducedBy, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn subBuilder\n\t\t\t\t\t\t\t} else if (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tconst marker = fieldState.blueprint.marker\n\t\t\t\t\tif (!(marker instanceof HasManyRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedEntityIds = entityData?.get?.(placeholderName) ?? new Set()\n\n\t\t\t\t\tif (!(persistedEntityIds instanceof Set)) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\tbuilder = builder.many(marker.parameters.field, builder => {\n\t\t\t\t\t\tfor (const childState of fieldState.children.values()) {\n\t\t\t\t\t\t\tconst runtimeId = childState.entity.id\n\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\n\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\tif (persistedEntityIds.has(runtimeId.value)) {\n\t\t\t\t\t\t\t\t\tif (childState.type !== 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\t// A stub cannot have any pending changes.\n\t\t\t\t\t\t\t\t\t\tbuilder = builder.update(\n\t\t\t\t\t\t\t\t\t\t\t{ [PRIMARY_KEY_NAME]: runtimeId.value },\n\t\t\t\t\t\t\t\t\t\t\tbuilder => this.registerUpdateMutationPart(processedPlaceholdersByEntity, childState, builder),\n\t\t\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\tbuilder = builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tbuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, childState),\n\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (fieldState.plannedRemovals) {\n\t\t\t\t\t\t\tfor (const [removedId, removalType] of fieldState.plannedRemovals) {\n\t\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(new ServerGeneratedUuid(removedId))\n\t\t\t\t\t\t\t\tif (removalType === 'delete') {\n\t\t\t\t\t\t\t\t\tbuilder = builder.delete({ [PRIMARY_KEY_NAME]: removedId }, alias)\n\t\t\t\t\t\t\t\t} else if (removalType === 'disconnect') {\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\tbuilder = builder.disconnect({ [PRIMARY_KEY_NAME]: removedId }, alias)\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tassertNever(removalType)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn builder\n\t\t\t\t\t})\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault:\n\t\t\t\t\treturn assertNever(fieldState)\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n\n\tprivate transformFieldValue(fieldState: FieldState, value: FieldValue): FieldValue | GraphQlBuilder.GraphQlLiteral {\n\t\tif (typeof value !== 'string') {\n\t\t\treturn value\n\t\t}\n\t\tconst fieldSchema = this.treeStore.schema.getEntityField(\n\t\t\tfieldState.parent.entity.entityName,\n\t\t\tfieldState.fieldMarker.fieldName,\n\t\t)\n\t\tif (fieldSchema === undefined || fieldSchema.__typename !== '_Column') {\n\t\t\tthrow new BindingError()\n\t\t}\n\t\treturn fieldSchema.enumName === null ? value : new GraphQlBuilder.GraphQlLiteral(value)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;wBA0B+B;AAAA,EACvB,YAA6B,WAAsB;;;EAEnD,qBAAyC;QAC3C;UACC,UAAwB,IAAI,iBAAiB;YAC3C,oDAAmE;iBAE9D,CAAC,YAAY,eAAe,KAAK,UAAU,qBAAqB;mBAC/D,CAAC,iBAAiB,iBAAiB,YAAY;cACrD,aAAa,SAAS,eAAe;sBAC9B,KAAK,eACd,+BACA,YACA,iBACA,6BAA6B,cAC7B,aAAa,OAAO,GAAG,OACvB,cACA;AAAA,qBAES,aAAa,SAAS,cAAc;uBACnC,cAAc,aAAa,SAAS,UAAU;kBACpD,WAAW,SAAS,mBAAmB;;;wBAIjC,KAAK,eACd,+BACA,YACA,iBACA,6BAA6B,YAC7B,WAAW,OAAO,GAAG,OACrB,YACA;AAAA;gBAGE,aAAa,iBAAiB;yBACtB,CAAC,WAAW,gBAAgB,aAAa,iBAAiB;oBAChE,gBAAgB,cAAc;;;oBAG9B,gBAAgB,UAAU;4BACnB,KAAK,kBACd,aAAa,YACb,WACA,cAAc,eAAe;AAAA,oBAC5B;AAAA,oBACA,oBAAoB;AAAA,oBACpB,MAAM,sBAAsB;AAAA,oBAC5B,aAAa,6BAA6B;AAAA,oBAC1C,UAAU;AAAA,sBAEX;AAAA,uBAEK;8BACM;AAAA;AAAA;AAAA;AAAA,iBAIT;wBACM;AAAA;AAAA;AAAA;aAIR,QAAQ,cAAc,QAAW,EAAE,4BAA4B,QAAQ;AAAA,aACtE;aACD;AAAA;AAAA;AAAA,EAID,eACP,+BACA,YACA,oBACA,aACA,UACA,YACA,cACe;QACX,WAAW,4BAA4B,GAAG;aAEtC;AAAA;QAEJ,WAAW,OAAO,wBAAwB;aACtC,KAAK,kBACX,WAAW,OAAO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B;sBAC5D,QAAQ,cAAc,eAAe,IAAI,oBAAoB;oBAC/D,gBAAgB,UAAU;6BACnB,SAAQ,OAAO,GAAG,mBAAmB,aAAa;AAAA,2BAClD,gBAAgB,cAAc;6BAE9B,SAAQ,WAAW,GAAG,mBAAmB,aAAa;AAAA,uBAC1D;;;;;mBAKF;AAAA;;;;iBAKD;AAAA;AAAA;WAIH;AAAA;AAAA,EAGA,oBAAoB,YAAwB,OAA+D;QAC9G,OAAO,UAAU,UAAU;aACvB;AAAA;UAEF,cAAc,KAAK,UAAU,OAAO,eACzC,WAAW,OAAO,OAAO,YACzB,WAAW,YAAY;QAEpB,gBAAgB,UAAa,YAAY,eAAe,WAAW;YAChE,IAAI;AAAA;WAEJ,YAAY,aAAa,OAAO,QAAQ,IAAI,eAAe,eAAe;AAAA;AAAA;;"}
1
+ {"version":3,"file":"MutationGenerator.js","sources":["../../../src/core/MutationGenerator.ts"],"sourcesContent":["// noinspection JSVoidFunctionReturnValueUsed // IntelliJ seems to be super confused througouht this file.\n\nimport { CrudQueryBuilder, GraphQlBuilder } from '@contember/client'\nimport { ClientGeneratedUuid, ServerGeneratedUuid } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport { PRIMARY_KEY_NAME, TYPENAME_KEY_NAME } from '../bindingTypes'\nimport { FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '../markers'\nimport type { EntityId, EntityName, FieldValue, PlaceholderName, TreeRootId } from '../treeParameters'\nimport { assertNever, isEmptyObject } from '../utils'\nimport { QueryGenerator } from './QueryGenerator'\nimport { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'\nimport {\n\tEntityListState,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n} from './state'\nimport type { TreeStore } from './TreeStore'\n\ntype QueryBuilder = Omit<CrudQueryBuilder.CrudQueryBuilder, CrudQueryBuilder.Queries>\n\ntype ProcessedPlaceholdersByEntity = Map<EntityState, Set<PlaceholderName>>\n\n// TODO enforce correct expected mutations in dev mode.\nexport class MutationGenerator {\n\tpublic constructor(private readonly treeStore: TreeStore) {}\n\n\tpublic getPersistMutation(): string | undefined {\n\t\ttry {\n\t\t\tlet builder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()\n\t\t\tconst processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()\n\n\t\t\tfor (const [treeRootId, rootStates] of this.treeStore.subTreeStatesByRoot) {\n\t\t\t\tfor (const [placeholderName, subTreeState] of rootStates) {\n\t\t\t\t\tif (subTreeState.type === 'entityRealm') {\n\t\t\t\t\t\tbuilder = this.addSubMutation(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\tmutationOperationSubTreeType.singleEntity,\n\t\t\t\t\t\t\tsubTreeState.entity.id.value,\n\t\t\t\t\t\t\tsubTreeState,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t} else if (subTreeState.type === 'entityList') {\n\t\t\t\t\t\tfor (const childState of subTreeState.children.values()) {\n\t\t\t\t\t\t\tif (childState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t// TODO there can be a forceCreate somewhere in there that we're hereby ignoring.\n\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbuilder = this.addSubMutation(\n\t\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\t\tmutationOperationSubTreeType.entityList,\n\t\t\t\t\t\t\t\tchildState.entity.id.value,\n\t\t\t\t\t\t\t\tchildState,\n\t\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (subTreeState.plannedRemovals) {\n\t\t\t\t\t\t\tfor (const [removedId, removalType] of subTreeState.plannedRemovals) {\n\t\t\t\t\t\t\t\tif (removalType === 'disconnect') {\n\t\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (removalType === 'delete') {\n\t\t\t\t\t\t\t\t\tbuilder = this.addDeleteMutation(\n\t\t\t\t\t\t\t\t\t\tsubTreeState.entityName,\n\t\t\t\t\t\t\t\t\t\tremovedId,\n\t\t\t\t\t\t\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\t\t\t\t\t\t\ttreeRootId,\n\t\t\t\t\t\t\t\t\t\t\tsubTreePlaceholder: placeholderName,\n\t\t\t\t\t\t\t\t\t\t\ttype: mutationOperationType.delete,\n\t\t\t\t\t\t\t\t\t\t\tsubTreeType: mutationOperationSubTreeType.entityList,\n\t\t\t\t\t\t\t\t\t\t\tentityId: removedId,\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tassertNever(removalType)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tassertNever(subTreeState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn builder.inTransaction(undefined, { deferForeignKeyConstraints: true }).getGql()\n\t\t} catch (e) {\n\t\t\treturn undefined\n\t\t}\n\t}\n\n\tprivate addSubMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\ttreeRootId: TreeRootId | undefined,\n\t\tsubTreePlaceholder: PlaceholderName,\n\t\tsubTreeType: typeof mutationOperationSubTreeType[keyof typeof mutationOperationSubTreeType],\n\t\tentityId: EntityId,\n\t\trealmState: EntityRealmState,\n\t\tqueryBuilder: QueryBuilder,\n\t): QueryBuilder {\n\t\tif (realmState.unpersistedChangesCount === 0) {\n\t\t\t// Bail out early\n\t\t\treturn queryBuilder\n\t\t}\n\t\tif (realmState.entity.isScheduledForDeletion) {\n\t\t\treturn this.addDeleteMutation(\n\t\t\t\trealmState.entity.entityName,\n\t\t\t\tentityId,\n\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\ttreeRootId,\n\t\t\t\t\tsubTreePlaceholder,\n\t\t\t\t\ttype: mutationOperationType.delete,\n\t\t\t\t\tsubTreeType,\n\t\t\t\t\tentityId,\n\t\t\t\t}),\n\t\t\t\tqueryBuilder,\n\t\t\t)\n\t\t} else if (!realmState.entity.id.existsOnServer) {\n\t\t\treturn this.addCreateMutation(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\trealmState,\n\t\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\t\ttreeRootId,\n\t\t\t\t\tsubTreePlaceholder,\n\t\t\t\t\ttype: mutationOperationType.create,\n\t\t\t\t\tsubTreeType,\n\t\t\t\t\tentityId,\n\t\t\t\t}),\n\t\t\t\tthis.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),\n\t\t\t\tqueryBuilder,\n\t\t\t)\n\t\t}\n\t\treturn this.addUpdateMutation(\n\t\t\tprocessedPlaceholdersByEntity,\n\t\t\trealmState,\n\t\t\tMutationAlias.encodeTopLevel({\n\t\t\t\ttreeRootId,\n\t\t\t\tsubTreePlaceholder,\n\t\t\t\ttype: mutationOperationType.update,\n\t\t\t\tsubTreeType,\n\t\t\t\tentityId,\n\t\t\t}),\n\t\t\tthis.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),\n\t\t\tqueryBuilder,\n\t\t)\n\t}\n\n\tprivate getNodeFragmentName(\n\t\tsubTreePlaceholder: PlaceholderName,\n\t\tsubTreeType: typeof mutationOperationSubTreeType[keyof typeof mutationOperationSubTreeType],\n\t\trealmState: EntityRealmState,\n\t): string | undefined {\n\t\tif (subTreeType !== mutationOperationSubTreeType.entityList) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn `${realmState.entity.entityName}_${subTreePlaceholder}`\n\t}\n\n\tprivate addDeleteMutation(\n\t\tentityName: EntityName,\n\t\tdeletedId: EntityId,\n\t\talias: string,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\treturn queryBuilder.delete(\n\t\t\tentityName,\n\t\t\tbuilder =>\n\t\t\t\tbuilder\n\t\t\t\t\t.ok()\n\t\t\t\t\t.node(builder => builder.column(PRIMARY_KEY_NAME))\n\t\t\t\t\t.by({ [PRIMARY_KEY_NAME]: deletedId })\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage(),\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate addUpdateMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tentityRealm: EntityRealmState,\n\t\talias: string,\n\t\tnodeFragmentName: string | undefined,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\tconst updateBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update> =\n\t\t\tthis.registerUpdateMutationPart(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\tentityRealm,\n\t\t\t\tnew CrudQueryBuilder.WriteDataBuilder(),\n\t\t\t)\n\t\tif (updateBuilder.data === undefined || isEmptyObject(updateBuilder.data)) {\n\t\t\treturn queryBuilder\n\t\t}\n\n\t\tconst runtimeId = entityRealm.entity.id\n\n\t\tif (!runtimeId.existsOnServer) {\n\t\t\treturn queryBuilder\n\t\t}\n\n\t\tconst readBuilder = QueryGenerator.registerQueryPart(\n\t\t\tgetEntityMarker(entityRealm).fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(),\n\t\t)\n\n\t\tif (nodeFragmentName) {\n\t\t\tqueryBuilder = queryBuilder.fragment(nodeFragmentName, entityRealm.entity.entityName, readBuilder)\n\t\t}\n\n\t\treturn queryBuilder.update(\n\t\t\tentityRealm.entity.entityName,\n\t\t\tbuilder =>\n\t\t\t\tbuilder\n\t\t\t\t\t.node(nodeFragmentName ? builder => builder.applyFragment(nodeFragmentName) : readBuilder)\n\t\t\t\t\t.data(updateBuilder)\n\t\t\t\t\t.by({ [PRIMARY_KEY_NAME]: runtimeId.value })\n\t\t\t\t\t.ok()\n\t\t\t\t\t.validation()\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage(),\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate addCreateMutation(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tentityRealm: EntityRealmState,\n\t\talias: string,\n\t\tnodeFragmentName: string | undefined,\n\t\tqueryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),\n\t): QueryBuilder {\n\t\tconst createBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> =\n\t\t\tthis.registerCreateMutationPart(\n\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\tentityRealm,\n\t\t\t\tnew CrudQueryBuilder.WriteDataBuilder(),\n\t\t\t)\n\t\tif (createBuilder.data === undefined || isEmptyObject(createBuilder.data)) {\n\t\t\treturn queryBuilder\n\t\t}\n\t\tconst readBuilder = QueryGenerator.registerQueryPart(\n\t\t\tgetEntityMarker(entityRealm).fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(),\n\t\t)\n\t\tif (nodeFragmentName) {\n\t\t\tqueryBuilder = queryBuilder.fragment(nodeFragmentName, entityRealm.entity.entityName, readBuilder)\n\t\t}\n\n\t\treturn queryBuilder.create(\n\t\t\tentityRealm.entity.entityName,\n\t\t\tbuilder => {\n\t\t\t\t// if (where && writeBuilder.data !== undefined && !isEmptyObject(writeBuilder.data)) {\n\t\t\t\t// \t// Shallow cloning the parameters like this IS too naïve but it will likely last surprisingly long before we\n\t\t\t\t// \t// run into issues.\n\t\t\t\t// \twriteBuilder = new CrudQueryBuilder.WriteDataBuilder({ ...writeBuilder.data, ...where })\n\t\t\t\t// }\n\t\t\t\treturn builder\n\t\t\t\t\t.node(nodeFragmentName ? builder => builder.applyFragment(nodeFragmentName) : readBuilder)\n\t\t\t\t\t.data(createBuilder)\n\t\t\t\t\t.ok()\n\t\t\t\t\t.validation()\n\t\t\t\t\t.errors()\n\t\t\t\t\t.errorMessage()\n\t\t\t},\n\t\t\talias,\n\t\t)\n\t}\n\n\tprivate registerCreateMutationPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tcurrentState: EntityRealmState | EntityRealmStateStub,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> = new CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>(),\n\t): CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> {\n\t\tlet processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)\n\n\t\tif (processedPlaceholders === undefined) {\n\t\t\tprocessedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))\n\t\t}\n\n\t\tif (currentState.type === 'entityRealmStub') {\n\t\t\t// TODO If there's a forceCreate, this is wrong.\n\t\t\treturn builder\n\t\t}\n\n\t\tconst pathBack = this.treeStore.getPathBackToParent(currentState)\n\n\t\tconst nonbearingFields: Array<\n\t\t\t| { type: 'field'; marker: FieldMarker; fieldState: FieldState }\n\t\t\t| { type: 'hasOne'; marker: HasOneRelationMarker; fieldState: EntityRealmState | EntityRealmStateStub }\n\t\t\t| { type: 'hasMany'; marker: HasManyRelationMarker; fieldState: EntityListState }\n\t\t> = []\n\n\t\tfor (const [placeholderName, fieldState] of currentState.children) {\n\t\t\tif (processedPlaceholders.has(placeholderName)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tprocessedPlaceholders.add(placeholderName)\n\n\t\t\tswitch (fieldState.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tconst marker = fieldState.fieldMarker\n\t\t\t\t\tconst placeholderName = marker.placeholderName\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tplaceholderName === TYPENAME_KEY_NAME ||\n\t\t\t\t\t\t(placeholderName === PRIMARY_KEY_NAME && !(currentState.entity.id instanceof ClientGeneratedUuid))\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'field',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateFieldPart(fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealmStub':\n\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\tconst marker = getEntityMarker(fieldState)\n\t\t\t\t\tif (!(marker instanceof HasOneRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.parameters.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'hasOne',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateEntityPart(processedPlaceholdersByEntity, fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tconst marker = fieldState.blueprint.marker\n\t\t\t\t\tif (!(marker instanceof HasManyRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.parameters.isNonbearing) {\n\t\t\t\t\t\tnonbearingFields.push({\n\t\t\t\t\t\t\ttype: 'hasMany',\n\t\t\t\t\t\t\tmarker,\n\t\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\t})\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tbuilder = this.registerCreateEntityListPart(processedPlaceholdersByEntity, fieldState, marker, builder)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\treturn assertNever(fieldState)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// if (\n\t\t// \tcurrentState.blueprint.creationParameters.forceCreation &&\n\t\t// \t(builder.data === undefined || isEmptyObject(builder.data))\n\t\t// ) {\n\t\t// \tbuilder = builder.set('_dummy_field_', true)\n\t\t// }\n\n\t\tif (\n\t\t\t(builder.data !== undefined && !isEmptyObject(builder.data)) ||\n\t\t\t!getEntityMarker(currentState).fields.hasAtLeastOneBearingField\n\t\t) {\n\t\t\tfor (const field of nonbearingFields) {\n\t\t\t\tswitch (field.type) {\n\t\t\t\t\tcase 'field': {\n\t\t\t\t\t\tbuilder = this.registerCreateFieldPart(field.fieldState, field.marker, builder)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tcase 'hasOne': {\n\t\t\t\t\t\tbuilder = this.registerCreateEntityPart(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\tfield.fieldState,\n\t\t\t\t\t\t\tfield.marker,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tcase 'hasMany': {\n\t\t\t\t\t\tbuilder = this.registerCreateEntityListPart(\n\t\t\t\t\t\t\tprocessedPlaceholdersByEntity,\n\t\t\t\t\t\t\tfield.fieldState,\n\t\t\t\t\t\t\tfield.marker,\n\t\t\t\t\t\t\tbuilder,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tassertNever(field)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst setOnCreate = getEntityMarker(currentState).parameters.setOnCreate\n\t\tif (setOnCreate && builder.data !== undefined && !isEmptyObject(builder.data)) {\n\t\t\tfor (const key in setOnCreate) {\n\t\t\t\tconst field = setOnCreate[key]\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof field === 'string' ||\n\t\t\t\t\ttypeof field === 'number' ||\n\t\t\t\t\tfield === null ||\n\t\t\t\t\tfield instanceof GraphQlBuilder.GraphQlLiteral\n\t\t\t\t) {\n\t\t\t\t\tbuilder = builder.set(key, field)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.one(key, builder => builder.connect(field))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n\n\tprivate registerCreateFieldPart(\n\t\tfieldState: FieldState,\n\t\tmarker: FieldMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\tconst resolvedValue = fieldState.getAccessor().resolvedValue\n\n\t\tif (resolvedValue !== undefined && resolvedValue !== null) {\n\t\t\treturn builder.set(marker.fieldName, this.transformFieldValue(fieldState, resolvedValue))\n\t\t}\n\t\treturn builder\n\t}\n\n\tprivate registerCreateEntityPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tfieldState: EntityRealmState | EntityRealmStateStub,\n\t\tmarker: HasOneRelationMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\tconst reducedBy = marker.parameters.reducedBy\n\t\tconst runtimeId = fieldState.entity.id\n\n\t\tif (reducedBy === undefined) {\n\t\t\treturn builder.one(marker.parameters.field, builder => {\n\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value })\n\t\t\t\t}\n\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t})\n\t\t}\n\t\treturn builder.many(marker.parameters.field, builder => {\n\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t// TODO also potentially update\n\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t}\n\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState), alias)\n\t\t})\n\t}\n\n\tprivate registerCreateEntityListPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tfieldState: EntityListState,\n\t\tmarker: HasManyRelationMarker,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create>,\n\t) {\n\t\treturn builder.many(marker.parameters.field, builder => {\n\t\t\tfor (const entityRealm of fieldState.children.values()) {\n\t\t\t\tconst alias = MutationAlias.encodeEntityId(entityRealm.entity.id)\n\t\t\t\tif (entityRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\tbuilder = builder.connect({ [PRIMARY_KEY_NAME]: entityRealm.entity.id.value }, alias)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, entityRealm), alias)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn builder\n\t\t})\n\t}\n\n\tprivate registerUpdateMutationPart(\n\t\tprocessedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,\n\t\tcurrentState: EntityRealmState,\n\t\tbuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update>,\n\t): CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update> {\n\t\tlet processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)\n\n\t\tif (processedPlaceholders === undefined) {\n\t\t\tprocessedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))\n\t\t}\n\n\t\tconst pathBack = this.treeStore.getPathBackToParent(currentState)\n\t\tconst entityData = this.treeStore.persistedEntityData.get(currentState.entity.id.value)\n\n\t\tfor (const [placeholderName, fieldState] of currentState.children) {\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME || placeholderName === TYPENAME_KEY_NAME) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (processedPlaceholders.has(placeholderName)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tprocessedPlaceholders.add(placeholderName)\n\n\t\t\tswitch (fieldState.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tif (fieldState.persistedValue !== undefined) {\n\t\t\t\t\t\tconst resolvedValue = fieldState.getAccessor().resolvedValue\n\t\t\t\t\t\tif (fieldState.persistedValue !== resolvedValue) {\n\t\t\t\t\t\t\tbuilder = builder.set(placeholderName, this.transformFieldValue(fieldState, resolvedValue))\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealmStub':\n\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\tconst marker = getEntityMarker(fieldState)\n\t\t\t\t\tif (!(marker instanceof HasOneRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst runtimeId = fieldState.entity.id\n\t\t\t\t\tconst reducedBy = marker.parameters.reducedBy\n\n\t\t\t\t\tif (reducedBy === undefined) {\n\t\t\t\t\t\tconst subBuilder = ((\n\t\t\t\t\t\t\tbuilder: CrudQueryBuilder.WriteOneRelationBuilder<CrudQueryBuilder.WriteOperation.Update>,\n\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\tconst persistedValue = entityData?.get?.(placeholderName)\n\n\t\t\t\t\t\t\tif (persistedValue instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\t\tif (persistedValue.value === runtimeId.value) {\n\t\t\t\t\t\t\t\t\t// The persisted and currently referenced ids match, and so this is an update.\n\t\t\t\t\t\t\t\t\tif (fieldState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\treturn builder // …unless we're dealing with a stub. There cannot be any updates there.\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn builder.update(builder =>\n\t\t\t\t\t\t\t\t\t\tthis.registerUpdateMutationPart(processedPlaceholdersByEntity, fieldState, builder),\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// There was a referenced entity but currently, there is a different one. Let's investigate:\n\n\t\t\t\t\t\t\t\tconst plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)\n\t\t\t\t\t\t\t\tif (plannedDeletion !== undefined) {\n\t\t\t\t\t\t\t\t\t// It's planned for deletion.\n\t\t\t\t\t\t\t\t\t// TODO also potentially do something about the current entity\n\t\t\t\t\t\t\t\t\treturn builder.delete()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t\t// This isn't the persisted entity but it does exist on the server. Thus this is a connect.\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\treturn builder.connect({\n\t\t\t\t\t\t\t\t\t\t[PRIMARY_KEY_NAME]: runtimeId.value,\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// The currently present entity doesn't exist on the server. Try if creating yields anything…\n\t\t\t\t\t\t\t\tconst subBuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tif (isEmptyObject(subBuilder.data)) {\n\t\t\t\t\t\t\t\t\t// …and if it doesn't, we just disconnect.\n\t\t\t\t\t\t\t\t\treturn builder.disconnect()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// …but if it does, we return the create, disconnecting implicitly.\n\t\t\t\t\t\t\t\treturn subBuilder\n\t\t\t\t\t\t\t} else if (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t// There isn't a linked entity on the server but we're seeing one that exists there.\n\t\t\t\t\t\t\t\t// Thus this is a connect.\n\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\treturn builder.connect({\n\t\t\t\t\t\t\t\t\t[PRIMARY_KEY_NAME]: runtimeId.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})(CrudQueryBuilder.WriteOneRelationBuilder.instantiate<CrudQueryBuilder.WriteOperation.Update>())\n\n\t\t\t\t\t\tif (subBuilder.data) {\n\t\t\t\t\t\t\tbuilder = builder.one(marker.parameters.field, subBuilder.data)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This is a reduced has many relation.\n\t\t\t\t\t\tbuilder = builder.many(marker.parameters.field, builder => {\n\t\t\t\t\t\t\tconst persistedValue = entityData?.get?.(placeholderName)\n\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\n\t\t\t\t\t\t\tif (persistedValue instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\t\tif (persistedValue.value === runtimeId.value) {\n\t\t\t\t\t\t\t\t\tif (fieldState.type === 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\treturn builder\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn builder.update(\n\t\t\t\t\t\t\t\t\t\treducedBy,\n\t\t\t\t\t\t\t\t\t\tbuilder => this.registerUpdateMutationPart(processedPlaceholdersByEntity, fieldState, builder),\n\t\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)\n\t\t\t\t\t\t\t\tif (plannedDeletion !== undefined) {\n\t\t\t\t\t\t\t\t\t// TODO also potentially do something about the current entity\n\t\t\t\t\t\t\t\t\treturn builder.delete(reducedBy, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\t\t// TODO will re-using the alias like this work?\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\treturn builder.disconnect(reducedBy, alias).connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst subBuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tif (isEmptyObject(subBuilder.data)) {\n\t\t\t\t\t\t\t\t\treturn builder.disconnect(reducedBy, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn subBuilder\n\t\t\t\t\t\t\t} else if (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\treturn builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\treturn builder.create(this.registerCreateMutationPart(processedPlaceholdersByEntity, fieldState))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tconst marker = fieldState.blueprint.marker\n\t\t\t\t\tif (!(marker instanceof HasManyRelationMarker)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (pathBack?.fieldBackToParent === marker.parameters.field) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedEntityIds = entityData?.get?.(placeholderName) ?? new Set()\n\n\t\t\t\t\tif (!(persistedEntityIds instanceof Set)) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\tbuilder = builder.many(marker.parameters.field, builder => {\n\t\t\t\t\t\tfor (const childState of fieldState.children.values()) {\n\t\t\t\t\t\t\tconst runtimeId = childState.entity.id\n\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(runtimeId)\n\n\t\t\t\t\t\t\tif (runtimeId.existsOnServer) {\n\t\t\t\t\t\t\t\tif (persistedEntityIds.has(runtimeId.value)) {\n\t\t\t\t\t\t\t\t\tif (childState.type !== 'entityRealmStub') {\n\t\t\t\t\t\t\t\t\t\t// A stub cannot have any pending changes.\n\t\t\t\t\t\t\t\t\t\tbuilder = builder.update(\n\t\t\t\t\t\t\t\t\t\t\t{ [PRIMARY_KEY_NAME]: runtimeId.value },\n\t\t\t\t\t\t\t\t\t\t\tbuilder => this.registerUpdateMutationPart(processedPlaceholdersByEntity, childState, builder),\n\t\t\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\tbuilder = builder.connect({ [PRIMARY_KEY_NAME]: runtimeId.value }, alias)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tbuilder = builder.create(\n\t\t\t\t\t\t\t\t\tthis.registerCreateMutationPart(processedPlaceholdersByEntity, childState),\n\t\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (fieldState.plannedRemovals) {\n\t\t\t\t\t\t\tfor (const [removedId, removalType] of fieldState.plannedRemovals) {\n\t\t\t\t\t\t\t\tconst alias = MutationAlias.encodeEntityId(new ServerGeneratedUuid(removedId))\n\t\t\t\t\t\t\t\tif (removalType === 'delete') {\n\t\t\t\t\t\t\t\t\tbuilder = builder.delete({ [PRIMARY_KEY_NAME]: removedId }, alias)\n\t\t\t\t\t\t\t\t} else if (removalType === 'disconnect') {\n\t\t\t\t\t\t\t\t\t// TODO also potentially update\n\t\t\t\t\t\t\t\t\tbuilder = builder.disconnect({ [PRIMARY_KEY_NAME]: removedId }, alias)\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tassertNever(removalType)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn builder\n\t\t\t\t\t})\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault:\n\t\t\t\t\treturn assertNever(fieldState)\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n\n\tprivate transformFieldValue(fieldState: FieldState, value: FieldValue): FieldValue | GraphQlBuilder.GraphQlLiteral {\n\t\tif (typeof value !== 'string') {\n\t\t\treturn value\n\t\t}\n\t\tconst fieldSchema = this.treeStore.schema.getEntityField(\n\t\t\tfieldState.parent.entity.entityName,\n\t\t\tfieldState.fieldMarker.fieldName,\n\t\t)\n\t\tif (fieldSchema === undefined || fieldSchema.__typename !== '_Column') {\n\t\t\tthrow new BindingError()\n\t\t}\n\t\treturn fieldSchema.enumName === null ? value : new GraphQlBuilder.GraphQlLiteral(value)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AA0BO,MAAM,kBAAkB;A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AAM,GACtC,CAAW,aAAA,KAAK,2BAA2B,+BAA+B,YAAY,QAAO,GAC7F,KACD;AAAA,kBACD;AAAA,gBAAA,OACM;AAEN,6BAAU,SAAQ,QAAQ,GAAG,mBAAmB,UAAU,SAAS,KAAK;AAAA,gBACzE;AAAA,cAAA,OACM;AACN,2BAAU,SAAQ,OACjB,KAAK,2BAA2B,+BAA+B,UAAU,GACzE,KACD;AAAA,cACD;AAAA,YACD;AACA,gBAAI,WAAW,iBAAiB;AAC/B,yBAAW,CAAC,WAAW,gBAAgB,WAAW,iBAAiB;AAClE,sBAAM,QAAQ,cAAc,eAAe,IAAI,oBAAoB,SAAS,CAAC;AAC7E,oBAAI,gBAAgB,UAAU;AAC7B,6BAAU,SAAQ,OAAO,GAAG,mBAAmB,UAAA,GAAa,KAAK;AAAA,gBAAA,WACvD,gBAAgB,cAAc;AAExC,6BAAU,SAAQ,WAAW,GAAG,mBAAmB,UAAA,GAAa,KAAK;AAAA,gBAAA,OAC/D;AACN,8BAAuB;AAAA,gBACxB;AAAA,cACD;AAAA,YACD;AACO,mBAAA;AAAA,UAAA,CACP;AACD;AAAA,QACD;AAAA;AAEC,iBAAO,YAAsB;AAAA;AAAA,IAEhC;AAEO,WAAA;AAAA,EACR;AAAA,EAEQ,oBAAoB,YAAwB,OAA+D;AAC9G,QAAA,OAAO,UAAU,UAAU;AACvB,aAAA;AAAA,IACR;AACM,UAAA,cAAc,KAAK,UAAU,OAAO,eACzC,WAAW,OAAO,OAAO,YACzB,WAAW,YAAY,SACxB;AACA,QAAI,gBAAgB,UAAa,YAAY,eAAe,WAAW;AACtE,YAAM,IAAI,aAAa;AAAA,IACxB;AACA,WAAO,YAAY,aAAa,OAAO,QAAQ,IAAI,eAAe,eAAe,KAAK;AAAA,EACvF;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"QueryGenerator.js","sources":["../../../src/core/QueryGenerator.ts"],"sourcesContent":["import { CrudQueryBuilder } from '@contember/client'\nimport { PRIMARY_KEY_NAME } from '../bindingTypes'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '../markers'\nimport { assertNever, ucfirst } from '../utils'\n\ntype BaseQueryBuilder = Omit<CrudQueryBuilder.CrudQueryBuilder, CrudQueryBuilder.Mutations>\n\ntype ReadBuilder = CrudQueryBuilder.ReadBuilder.Builder<never>\n\nexport class QueryGenerator {\n\tconstructor(private tree: MarkerTreeRoot) {}\n\n\tpublic getReadQuery(): string | undefined {\n\t\ttry {\n\t\t\tlet baseQueryBuilder: BaseQueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()\n\n\t\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\t\tbaseQueryBuilder = this.addSubQuery(subTreeMarker, baseQueryBuilder)\n\t\t\t}\n\t\t\treturn baseQueryBuilder.getGql()\n\t\t} catch (e) {\n\t\t\treturn undefined\n\t\t}\n\t}\n\n\tprivate addSubQuery(\n\t\tsubTree: EntitySubTreeMarker | EntityListSubTreeMarker,\n\t\tbaseQueryBuilder: BaseQueryBuilder,\n\t): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\t// Unconstrained trees, by definition, don't have any queries.\n\t\t\treturn baseQueryBuilder\n\t\t}\n\n\t\tif (subTree instanceof EntityListSubTreeMarker) {\n\t\t\treturn this.addListQuery(baseQueryBuilder, subTree)\n\t\t} else if (subTree instanceof EntitySubTreeMarker) {\n\t\t\treturn this.addGetQuery(baseQueryBuilder, subTree)\n\t\t}\n\n\t\tassertNever(subTree)\n\t}\n\n\tprivate addGetQuery(baseQueryBuilder: BaseQueryBuilder, subTree: EntitySubTreeMarker): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn baseQueryBuilder\n\t\t}\n\t\tconst populatedListQueryBuilder = QueryGenerator.registerQueryPart(\n\t\t\tsubTree.fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate<CrudQueryBuilder.GetQueryArguments>().by(subTree.parameters.where),\n\t\t)\n\t\treturn baseQueryBuilder.get(\n\t\t\tsubTree.entityName,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\tpopulatedListQueryBuilder ? populatedListQueryBuilder.objectBuilder : undefined,\n\t\t\t),\n\t\t\tsubTree.placeholderName,\n\t\t)\n\t}\n\n\tprivate addListQuery(baseQueryBuilder: BaseQueryBuilder, subTree: EntityListSubTreeMarker): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn baseQueryBuilder\n\t\t}\n\n\t\tlet finalBuilder: ReadBuilder\n\n\t\tif (subTree.parameters) {\n\t\t\tconst parameters = subTree.parameters\n\t\t\tconst withFilter: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\tparameters.filter\n\t\t\t\t\t? CrudQueryBuilder.ReadBuilder.instantiate().filter(parameters.filter)\n\t\t\t\t\t: CrudQueryBuilder.ReadBuilder.instantiate()\n\n\t\t\tconst withOrderBy: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy'>\n\t\t\t> = parameters.orderBy ? withFilter.orderBy(parameters.orderBy) : withFilter\n\n\t\t\tconst withOffset: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy' | 'offset'>\n\t\t\t> = parameters.offset === undefined ? withOrderBy : withOrderBy.offset(parameters.offset)\n\n\t\t\tfinalBuilder = parameters.limit === undefined ? withOffset : withOffset.limit(parameters.limit)\n\t\t} else {\n\t\t\tfinalBuilder = CrudQueryBuilder.ReadBuilder.instantiate()\n\t\t}\n\n\t\t// const fullyPopulated = withAllParams\n\t\t// \t.anyRelation('pageInfo', builder => builder.column('totalCount'))\n\t\t// \t.anyRelation('edges', builder =>\n\t\t// \t\tbuilder.anyRelation('node', builder => QueryGenerator.registerQueryPart(subTree.fields.markers, builder)),\n\t\t// \t)\n\n\t\treturn baseQueryBuilder.list(\n\t\t\tsubTree.entityName,\n\t\t\tQueryGenerator.registerQueryPart(subTree.fields.markers, finalBuilder),\n\t\t\tsubTree.placeholderName,\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, builder: ReadBuilder): ReadBuilder {\n\t\tbuilder = builder.column(PRIMARY_KEY_NAME)\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tbuilder = builder.column(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\t\t\t\tconst builderWithBody = CrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\t\tthis.registerQueryPart(fieldValue.fields.markers, CrudQueryBuilder.ReadBuilder.instantiate()).objectBuilder,\n\t\t\t\t)\n\n\t\t\t\tconst filteredBuilder: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\t\trelation.filter ? builderWithBody.filter(relation.filter) : builderWithBody\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tbuilder = builder.reductionRelation(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\tfilteredBuilder.by(relation.reducedBy),\n\t\t\t\t\t\tfieldValue.placeholderName,\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.hasOneRelation(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\tfilteredBuilder,\n\t\t\t\t\t\t// TODO this will currently always go to the latter condition, resulting in less than ideal queries.\n\t\t\t\t\t\tfieldValue.placeholderName === relation.field ? undefined : fieldValue.placeholderName,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\t\t\t\tconst builderWithBody = CrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\t\tthis.registerQueryPart(fieldValue.fields.markers, CrudQueryBuilder.ReadBuilder.instantiate()).objectBuilder,\n\t\t\t\t)\n\n\t\t\t\tconst withFilter: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\t\trelation.filter ? builderWithBody.filter(relation.filter) : builderWithBody\n\n\t\t\t\tconst withOrderBy: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy'>\n\t\t\t\t> = relation.orderBy ? withFilter.orderBy(relation.orderBy) : withFilter\n\n\t\t\t\tconst withOffset: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy' | 'offset'>\n\t\t\t\t> = relation.offset === undefined ? withOrderBy : withOrderBy.offset(relation.offset)\n\n\t\t\t\tconst withLimit = relation.limit === undefined ? withOffset : withOffset.limit(relation.limit)\n\n\t\t\t\tbuilder = builder.anyRelation(\n\t\t\t\t\trelation.field,\n\t\t\t\t\twithLimit,\n\t\t\t\t\tfieldValue.placeholderName === relation.field ? undefined : fieldValue.placeholderName,\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;qBAiB4B;AAAA,EAC3B,YAAoB,MAAsB;;;EAEnC,eAAmC;QACrC;UACC,mBAAqC,IAAI,iBAAiB;iBAE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B,kBAAkB,iBAAiB,YAAY,YACpD,KAAK,kBAAkB,WAAW,OAAO,SAAS,iBAAiB,YAAY,eAAe;cAGzF,kBACL,SAAS,SAAS,gBAAgB,OAAO,SAAS,UAAU;YAEzD,SAAS,WAAW;gBAEjB,gBAAgB,GAAG,SAAS,UAAU,QAAQ,OAAO,KAAK,SAAS,WAAW;oBAC1E,QAAQ,kBACjB,eACA,gBAAgB,GAAG,SAAS,YAC5B,WAAW;AAAA,eAEN;oBACI,QAAQ,eACjB,SAAS,OACT,iBAEA,WAAW,oBAAoB,SAAS,QAAQ,SAAY,WAAW;AAAA;AAAA,iBAG/D,sBAAsB,uBAAuB;cACjD,WAAW,WAAW;cACtB,kBAAkB,iBAAiB,YAAY,YACpD,KAAK,kBAAkB,WAAW,OAAO,SAAS,iBAAiB,YAAY,eAAe;cAGzF,aACL,SAAS,SAAS,gBAAgB,OAAO,SAAS,UAAU;cAEvD,cAEF,SAAS,UAAU,WAAW,QAAQ,SAAS,WAAW;cAExD,aAEF,SAAS,WAAW,SAAY,cAAc,YAAY,OAAO,SAAS;cAExE,YAAY,SAAS,UAAU,SAAY,aAAa,WAAW,MAAM,SAAS;kBAE9E,QAAQ,YACjB,SAAS,OACT,WACA,WAAW,oBAAoB,SAAS,QAAQ,SAAY,WAAW;AAAA,aAElE;;;;WAKD;AAAA;AAAA;;"}
1
+ {"version":3,"file":"QueryGenerator.js","sources":["../../../src/core/QueryGenerator.ts"],"sourcesContent":["import { CrudQueryBuilder } from '@contember/client'\nimport { PRIMARY_KEY_NAME } from '../bindingTypes'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '../markers'\nimport { assertNever, ucfirst } from '../utils'\n\ntype BaseQueryBuilder = Omit<CrudQueryBuilder.CrudQueryBuilder, CrudQueryBuilder.Mutations>\n\ntype ReadBuilder = CrudQueryBuilder.ReadBuilder.Builder<never>\n\nexport class QueryGenerator {\n\tconstructor(private tree: MarkerTreeRoot) {}\n\n\tpublic getReadQuery(): string | undefined {\n\t\ttry {\n\t\t\tlet baseQueryBuilder: BaseQueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()\n\n\t\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\t\tbaseQueryBuilder = this.addSubQuery(subTreeMarker, baseQueryBuilder)\n\t\t\t}\n\t\t\treturn baseQueryBuilder.getGql()\n\t\t} catch (e) {\n\t\t\treturn undefined\n\t\t}\n\t}\n\n\tprivate addSubQuery(\n\t\tsubTree: EntitySubTreeMarker | EntityListSubTreeMarker,\n\t\tbaseQueryBuilder: BaseQueryBuilder,\n\t): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\t// Unconstrained trees, by definition, don't have any queries.\n\t\t\treturn baseQueryBuilder\n\t\t}\n\n\t\tif (subTree instanceof EntityListSubTreeMarker) {\n\t\t\treturn this.addListQuery(baseQueryBuilder, subTree)\n\t\t} else if (subTree instanceof EntitySubTreeMarker) {\n\t\t\treturn this.addGetQuery(baseQueryBuilder, subTree)\n\t\t}\n\n\t\tassertNever(subTree)\n\t}\n\n\tprivate addGetQuery(baseQueryBuilder: BaseQueryBuilder, subTree: EntitySubTreeMarker): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn baseQueryBuilder\n\t\t}\n\t\tconst populatedListQueryBuilder = QueryGenerator.registerQueryPart(\n\t\t\tsubTree.fields.markers,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate<CrudQueryBuilder.GetQueryArguments>().by(subTree.parameters.where),\n\t\t)\n\t\treturn baseQueryBuilder.get(\n\t\t\tsubTree.entityName,\n\t\t\tCrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\tpopulatedListQueryBuilder ? populatedListQueryBuilder.objectBuilder : undefined,\n\t\t\t),\n\t\t\tsubTree.placeholderName,\n\t\t)\n\t}\n\n\tprivate addListQuery(baseQueryBuilder: BaseQueryBuilder, subTree: EntityListSubTreeMarker): BaseQueryBuilder {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn baseQueryBuilder\n\t\t}\n\n\t\tlet finalBuilder: ReadBuilder\n\n\t\tif (subTree.parameters) {\n\t\t\tconst parameters = subTree.parameters\n\t\t\tconst withFilter: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\tparameters.filter\n\t\t\t\t\t? CrudQueryBuilder.ReadBuilder.instantiate().filter(parameters.filter)\n\t\t\t\t\t: CrudQueryBuilder.ReadBuilder.instantiate()\n\n\t\t\tconst withOrderBy: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy'>\n\t\t\t> = parameters.orderBy ? withFilter.orderBy(parameters.orderBy) : withFilter\n\n\t\t\tconst withOffset: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy' | 'offset'>\n\t\t\t> = parameters.offset === undefined ? withOrderBy : withOrderBy.offset(parameters.offset)\n\n\t\t\tfinalBuilder = parameters.limit === undefined ? withOffset : withOffset.limit(parameters.limit)\n\t\t} else {\n\t\t\tfinalBuilder = CrudQueryBuilder.ReadBuilder.instantiate()\n\t\t}\n\n\t\t// const fullyPopulated = withAllParams\n\t\t// \t.anyRelation('pageInfo', builder => builder.column('totalCount'))\n\t\t// \t.anyRelation('edges', builder =>\n\t\t// \t\tbuilder.anyRelation('node', builder => QueryGenerator.registerQueryPart(subTree.fields.markers, builder)),\n\t\t// \t)\n\n\t\treturn baseQueryBuilder.list(\n\t\t\tsubTree.entityName,\n\t\t\tQueryGenerator.registerQueryPart(subTree.fields.markers, finalBuilder),\n\t\t\tsubTree.placeholderName,\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, builder: ReadBuilder): ReadBuilder {\n\t\tbuilder = builder.column(PRIMARY_KEY_NAME)\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tbuilder = builder.column(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\t\t\t\tconst builderWithBody = CrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\t\tthis.registerQueryPart(fieldValue.fields.markers, CrudQueryBuilder.ReadBuilder.instantiate()).objectBuilder,\n\t\t\t\t)\n\n\t\t\t\tconst filteredBuilder: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\t\trelation.filter ? builderWithBody.filter(relation.filter) : builderWithBody\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tbuilder = builder.reductionRelation(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\tfilteredBuilder.by(relation.reducedBy),\n\t\t\t\t\t\tfieldValue.placeholderName,\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tbuilder = builder.hasOneRelation(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\tfilteredBuilder,\n\t\t\t\t\t\t// TODO this will currently always go to the latter condition, resulting in less than ideal queries.\n\t\t\t\t\t\tfieldValue.placeholderName === relation.field ? undefined : fieldValue.placeholderName,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\t\t\t\tconst builderWithBody = CrudQueryBuilder.ReadBuilder.instantiate(\n\t\t\t\t\tthis.registerQueryPart(fieldValue.fields.markers, CrudQueryBuilder.ReadBuilder.instantiate()).objectBuilder,\n\t\t\t\t)\n\n\t\t\t\tconst withFilter: CrudQueryBuilder.ReadBuilder.Builder<Exclude<CrudQueryBuilder.ReadArguments, 'filter'>> =\n\t\t\t\t\trelation.filter ? builderWithBody.filter(relation.filter) : builderWithBody\n\n\t\t\t\tconst withOrderBy: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy'>\n\t\t\t\t> = relation.orderBy ? withFilter.orderBy(relation.orderBy) : withFilter\n\n\t\t\t\tconst withOffset: CrudQueryBuilder.ReadBuilder.Builder<\n\t\t\t\t\tExclude<CrudQueryBuilder.ReadArguments, 'filter' | 'orderBy' | 'offset'>\n\t\t\t\t> = relation.offset === undefined ? withOrderBy : withOrderBy.offset(relation.offset)\n\n\t\t\t\tconst withLimit = relation.limit === undefined ? withOffset : withOffset.limit(relation.limit)\n\n\t\t\t\tbuilder = builder.anyRelation(\n\t\t\t\t\trelation.field,\n\t\t\t\t\twithLimit,\n\t\t\t\t\tfieldValue.placeholderName === relation.field ? undefined : fieldValue.placeholderName,\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t}\n\n\t\treturn builder\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;AAiBO,MAAM,eAAe;AAAA,EAC3B,YAAoB,MAAsB;AAAtB,SAAA,OAAA;AAAA,EAAuB;AAAA,EAEpC,eAAmC;AACrC,QAAA;AACC,UAAA,mBAAqC,IAAI,iBAAiB;AAE9D,iBAAW,CAAA,EAAG,kBAAkB,KAAK,KAAK,UAAU;AAChC,2BAAA,KAAK,YAAY,eAAe,gBAAgB;AAAA,MACpE;AACA,aAAO,iBAAiB;aAChB;AACD,aAAA;AAAA,IACR;AAAA,EACD;AAAA,EAEQ,YACP,SACA,kBACmB;AACf,QAAA,QAAQ,WAAW,YAAY;AAE3B,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A,EACR;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RealmKeyGenerator.js","sources":["../../../src/core/RealmKeyGenerator.ts"],"sourcesContent":["import type { RuntimeId } from '../accessorTree'\nimport type { EntityFieldMarkersContainer } from '../markers'\nimport type { EntityId, EntityRealmKey, PlaceholderName } from '../treeParameters'\nimport { assertNever } from '../utils'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '../structures'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: EntityId,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${this.parentStateIdCache.getOptionalKeyId(\n\t\t\tparent,\n\t\t)}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\tid.value,\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;AAOA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG;wBAEH;AAAA,SAMhB,uBAAuB,WAA4B;WAEzD,UAAU,WAAW,MAAM,WAAW,YAAY,KAAK;AAAA;AAAA,SAGhD,YACd,QACA,iBACA,IACA,WACiB;WAEV,MAAM,OAAO,KAAK,mBAAmB,iBAC3C,UACG,OAAO,kBAAkB,KAAK,OAAO,KAAK,cAAc,MAAM;AAAA;AAAA,SAGrD,YAAY,IAAe,WAAiD;UACnF,SAAS,UAAU;QACrB;QAEA,UAAU,SAAS,WAAW;wBACf,UAAU,OAAO;AAAA,eACzB,UAAU,SAAS,cAAc;wBACzB,UAAU,OAAO,UAAU,OAAO;AAAA,eAC1C,UAAU,SAAS,UAAU;wBACrB,UAAU,OAAO;AAAA,WAC7B;aACC;AAAA;WAED,KAAK,YACX,QACA,iBACA,GAAG,OACH,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO;AAAA;AAAA;AArCjF,iCAAA,sBAAqB,IAAI;AACzB,iCAAA,iBAAgB,IAAI;;"}
1
+ {"version":3,"file":"RealmKeyGenerator.js","sources":["../../../src/core/RealmKeyGenerator.ts"],"sourcesContent":["import type { RuntimeId } from '../accessorTree'\nimport type { EntityFieldMarkersContainer } from '../markers'\nimport type { EntityId, EntityRealmKey, PlaceholderName } from '../treeParameters'\nimport { assertNever } from '../utils'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '../structures'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: EntityId,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${this.parentStateIdCache.getOptionalKeyId(\n\t\t\tparent,\n\t\t)}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\tid.value,\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;AAOA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG,WAAW;AAEtC,MAAM,kBAAkB;AAAA,SAMhB,uBAAuB,WAA4B;AAEhE,WAAO,UAAU,WAAW,MAAM,MAAM,KAAK,YAAY,KAAK,SAAS;AAAA,EACxE;AAAA,SAEe,YACd,QACA,iBACA,IACA,WACiB;AAEjB,WAAO,MAAM,OAAO,KAAK,mBAAmB,iBAC3C,MACD,IAAI,OAAO,kBAAkB,KAAK,OAAO,KAAK,cAAc,MAAM,SAAS;AAAA,EAC5E;AAAA,SAEc,YAAY,IAAe,WAAiD;AACzF,UAAM,SAAS,UAAU;AACrB,QAAA;AAEA,QAAA,UAAU,SAAS,WAAW;AACjC,wBAAkB,UAAU,OAAO;AAAA,IAAA,WACzB,UAAU,SAAS,cAAc;AACzB,wBAAA,UAAU,OAAO,UAAU,OAAO;AAAA,IAAA,WAC1C,UAAU,SAAS,UAAU;AACvC,wBAAkB,UAAU,OAAO;AAAA,IAAA,OAC7B;AACN,aAAO,YAAqB;AAAA,IAC7B;AACA,WAAO,KAAK,YACX,QACA,iBACA,GAAG,OACH,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO,MAC/F;AAAA,EACD;AACD;AAxCgB,cAHT,mBAGS,sBAAqB,IAAI;AACzB,cAJT,mBAIS,iBAAgB,IAAI;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RequestResponseNormalizer.js","sources":["../../../src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import {\n\tEntityFieldPersistedData,\n\tNormalizedPersistedData,\n\tPersistedEntityDataStore,\n\tReceivedDataTree,\n\tReceivedEntityData,\n\tReceivedFieldData,\n\tServerGeneratedUuid,\n\tSingleEntityPersistedData,\n} from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport { PRIMARY_KEY_NAME } from '../bindingTypes'\nimport { assertNever } from '../utils'\nimport { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t): Set<string> {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\t\tconst presentIds: Set<string> = new Set()\n\n\t\tfor (const placeholderName in newPersistedData) {\n\t\t\tconst treeDatum = newPersistedData[placeholderName]\n\n\t\t\tconst fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)\n\n\t\t\tif (fieldData === null) {\n\t\t\t\tsubTreeDataStore.delete(placeholderName)\n\t\t\t} else if (fieldData instanceof Set || typeof fieldData === 'object') {\n\t\t\t\tsubTreeDataStore.set(placeholderName, fieldData)\n\t\t\t} else {\n\t\t\t\tthis.rejectData()\n\t\t\t}\n\t\t}\n\t\treturn presentIds\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t): Set<string> {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\t\tconst presentIds: Set<string> = new Set()\n\n\t\tfor (const operationAlias in newPersistedData) {\n\t\t\tconst operation = MutationAlias.decodeTopLevel(operationAlias)\n\n\t\t\tif (operation === undefined) {\n\t\t\t\treturn this.rejectData()\n\t\t\t}\n\t\t\tconst { type, entityId, subTreeType, subTreePlaceholder } = operation\n\n\t\t\tswitch (type) {\n\t\t\t\tcase mutationOperationType.update:\n\t\t\t\tcase mutationOperationType.create: {\n\t\t\t\t\tconst treeDatum = newPersistedData[operationAlias]\n\t\t\t\t\tconst fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)\n\n\t\t\t\t\tif (subTreeType === mutationOperationSubTreeType.singleEntity) {\n\t\t\t\t\t\tif (fieldData instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, fieldData)\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (subTreeType === mutationOperationSubTreeType.entityList) {\n\t\t\t\t\t\tif (fieldData instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\tif (type === mutationOperationType.create) {\n\t\t\t\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t\t\t\t//\t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\t\t\t\tlist.add(fieldData.value)\n\t\t\t\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([fieldData.value]))\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase mutationOperationType.delete: {\n\t\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t\t//\t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\t\tpersistedEntityDataStore.delete(entityId)\n\t\t\t\t\tif (subTreeType === mutationOperationSubTreeType.singleEntity) {\n\t\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t\t} else if (subTreeType === mutationOperationSubTreeType.entityList) {\n\t\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlist.delete(entityId)\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault:\n\t\t\t\t\treturn assertNever(type)\n\t\t\t}\n\t\t}\n\t\treturn presentIds\n\t}\n\n\tprivate static createFieldData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\tfieldData: ReceivedFieldData,\n\t): EntityFieldPersistedData {\n\t\tif (Array.isArray(fieldData)) {\n\t\t\tconst subTreeListIds = new Set<string>()\n\t\t\tfor (const entityDatum of fieldData) {\n\t\t\t\tsubTreeListIds.add(this.createEntityData(entityMap, entityDatum))\n\t\t\t}\n\t\t\treturn subTreeListIds\n\t\t} else if (fieldData !== null && typeof fieldData === 'object') {\n\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, fieldData))\n\t\t} else {\n\t\t\treturn fieldData\n\t\t}\n\t}\n\n\tprivate static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData): string {\n\t\tconst primaryKey: string | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\tconst presentEntityData = entityMap.get(primaryKey)\n\n\t\tif (presentEntityData === undefined) {\n\t\t\tconst fieldsMap: SingleEntityPersistedData = new Map()\n\t\t\tentityMap.set(primaryKey, fieldsMap)\n\t\t\tfor (const field in entityData) {\n\t\t\t\tconst fieldDatum = entityData[field]\n\n\t\t\t\tfieldsMap.set(field, this.createFieldData(entityMap, fieldDatum))\n\t\t\t}\n\t\t} else {\n\t\t\tthis.mergeInEntityData(entityMap, presentEntityData, entityData)\n\t\t}\n\t\treturn primaryKey\n\t}\n\n\tprivate static mergeInFieldData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\tfromTarget: EntityFieldPersistedData | undefined,\n\t\tnewDatum: ReceivedFieldData,\n\t): EntityFieldPersistedData {\n\t\tif (fromTarget === undefined) {\n\t\t\treturn this.createFieldData(entityMap, newDatum)\n\t\t}\n\t\tif (fromTarget instanceof ServerGeneratedUuid || (fromTarget === null && typeof newDatum === 'object')) {\n\t\t\tif (newDatum === null) {\n\t\t\t\treturn null\n\t\t\t} else if (typeof newDatum === 'object' && !Array.isArray(newDatum)) {\n\t\t\t\tconst newId = newDatum[PRIMARY_KEY_NAME]\n\t\t\t\tconst target = entityMap.get(newId)\n\t\t\t\tif (target === undefined) {\n\t\t\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, newDatum))\n\t\t\t\t}\n\n\t\t\t\tif (fromTarget?.value === newId) {\n\t\t\t\t\tthis.mergeInEntityData(entityMap, target, newDatum)\n\t\t\t\t\treturn fromTarget\n\t\t\t\t} else {\n\t\t\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, newDatum))\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (fromTarget instanceof Set) {\n\t\t\tif (Array.isArray(newDatum)) {\n\t\t\t\tfromTarget.clear()\n\t\t\t\tfor (const entityData of newDatum) {\n\t\t\t\t\tfromTarget.add(this.createEntityData(entityMap, entityData))\n\t\t\t\t}\n\t\t\t\treturn fromTarget\n\t\t\t}\n\t\t} else if (!Array.isArray(newDatum) && (newDatum === null || typeof newDatum !== 'object')) {\n\t\t\treturn newDatum\n\t\t}\n\t\treturn this.rejectData()\n\t}\n\n\tprivate static mergeInEntityData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\ttarget: SingleEntityPersistedData,\n\t\tnewData: ReceivedEntityData,\n\t): SingleEntityPersistedData {\n\t\tfor (const field in newData) {\n\t\t\tconst fromTarget = target.get(field)\n\t\t\tconst newDatum = newData[field]\n\n\t\t\ttarget.set(field, this.mergeInFieldData(entityMap, fromTarget, newDatum))\n\t\t}\n\t\treturn target\n\t}\n\n\tprivate static rejectData(extraMessage?: string): never {\n\t\tthrow new BindingError(`Failed to process data received from the API.${extraMessage ? `\\n${extraMessage}` : ''}`)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;gCAeuC;AAAA,SACxB,qBACb,UACA,kBACc;UACR,EA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aAAa,WAAW;aACpF;AAAA;WAED,KAAK;AAAA;AAAA,SAGE,kBACd,WACA,QACA,SAC4B;eACjB,SAAS,SAAS;YACtB,aAAa,OAAO,IAAI;YACxB,WAAW,QAAQ;aAElB,IAAI,OAAO,KAAK,iBAAiB,WAAW,YAAY;AAAA;WAEzD;AAAA;AAAA,SAGO,WAAW,cAA8B;UACjD,IAAI,aAAa,gDAAgD,eAAe;AAAA,EAAK,iBAAiB;AAAA;AAAA;;"}
1
+ {"version":3,"file":"RequestResponseNormalizer.js","sources":["../../../src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import {\n\tEntityFieldPersistedData,\n\tNormalizedPersistedData,\n\tPersistedEntityDataStore,\n\tReceivedDataTree,\n\tReceivedEntityData,\n\tReceivedFieldData,\n\tServerGeneratedUuid,\n\tSingleEntityPersistedData,\n} from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport { PRIMARY_KEY_NAME } from '../bindingTypes'\nimport { assertNever } from '../utils'\nimport { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t): Set<string> {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\t\tconst presentIds: Set<string> = new Set()\n\n\t\tfor (const placeholderName in newPersistedData) {\n\t\t\tconst treeDatum = newPersistedData[placeholderName]\n\n\t\t\tconst fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)\n\n\t\t\tif (fieldData === null) {\n\t\t\t\tsubTreeDataStore.delete(placeholderName)\n\t\t\t} else if (fieldData instanceof Set || typeof fieldData === 'object') {\n\t\t\t\tsubTreeDataStore.set(placeholderName, fieldData)\n\t\t\t} else {\n\t\t\t\tthis.rejectData()\n\t\t\t}\n\t\t}\n\t\treturn presentIds\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t): Set<string> {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\t\tconst presentIds: Set<string> = new Set()\n\n\t\tfor (const operationAlias in newPersistedData) {\n\t\t\tconst operation = MutationAlias.decodeTopLevel(operationAlias)\n\n\t\t\tif (operation === undefined) {\n\t\t\t\treturn this.rejectData()\n\t\t\t}\n\t\t\tconst { type, entityId, subTreeType, subTreePlaceholder } = operation\n\n\t\t\tswitch (type) {\n\t\t\t\tcase mutationOperationType.update:\n\t\t\t\tcase mutationOperationType.create: {\n\t\t\t\t\tconst treeDatum = newPersistedData[operationAlias]\n\t\t\t\t\tconst fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)\n\n\t\t\t\t\tif (subTreeType === mutationOperationSubTreeType.singleEntity) {\n\t\t\t\t\t\tif (fieldData instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, fieldData)\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (subTreeType === mutationOperationSubTreeType.entityList) {\n\t\t\t\t\t\tif (fieldData instanceof ServerGeneratedUuid) {\n\t\t\t\t\t\t\tif (type === mutationOperationType.create) {\n\t\t\t\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t\t\t\t//\t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\t\t\t\tlist.add(fieldData.value)\n\t\t\t\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([fieldData.value]))\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase mutationOperationType.delete: {\n\t\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t\t//\t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\t\tpersistedEntityDataStore.delete(entityId)\n\t\t\t\t\tif (subTreeType === mutationOperationSubTreeType.singleEntity) {\n\t\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t\t} else if (subTreeType === mutationOperationSubTreeType.entityList) {\n\t\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\t\treturn this.rejectData()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlist.delete(entityId)\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault:\n\t\t\t\t\treturn assertNever(type)\n\t\t\t}\n\t\t}\n\t\treturn presentIds\n\t}\n\n\tprivate static createFieldData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\tfieldData: ReceivedFieldData,\n\t): EntityFieldPersistedData {\n\t\tif (Array.isArray(fieldData)) {\n\t\t\tconst subTreeListIds = new Set<string>()\n\t\t\tfor (const entityDatum of fieldData) {\n\t\t\t\tsubTreeListIds.add(this.createEntityData(entityMap, entityDatum))\n\t\t\t}\n\t\t\treturn subTreeListIds\n\t\t} else if (fieldData !== null && typeof fieldData === 'object') {\n\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, fieldData))\n\t\t} else {\n\t\t\treturn fieldData\n\t\t}\n\t}\n\n\tprivate static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData): string {\n\t\tconst primaryKey: string | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\tconst presentEntityData = entityMap.get(primaryKey)\n\n\t\tif (presentEntityData === undefined) {\n\t\t\tconst fieldsMap: SingleEntityPersistedData = new Map()\n\t\t\tentityMap.set(primaryKey, fieldsMap)\n\t\t\tfor (const field in entityData) {\n\t\t\t\tconst fieldDatum = entityData[field]\n\n\t\t\t\tfieldsMap.set(field, this.createFieldData(entityMap, fieldDatum))\n\t\t\t}\n\t\t} else {\n\t\t\tthis.mergeInEntityData(entityMap, presentEntityData, entityData)\n\t\t}\n\t\treturn primaryKey\n\t}\n\n\tprivate static mergeInFieldData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\tfromTarget: EntityFieldPersistedData | undefined,\n\t\tnewDatum: ReceivedFieldData,\n\t): EntityFieldPersistedData {\n\t\tif (fromTarget === undefined) {\n\t\t\treturn this.createFieldData(entityMap, newDatum)\n\t\t}\n\t\tif (fromTarget instanceof ServerGeneratedUuid || (fromTarget === null && typeof newDatum === 'object')) {\n\t\t\tif (newDatum === null) {\n\t\t\t\treturn null\n\t\t\t} else if (typeof newDatum === 'object' && !Array.isArray(newDatum)) {\n\t\t\t\tconst newId = newDatum[PRIMARY_KEY_NAME]\n\t\t\t\tconst target = entityMap.get(newId)\n\t\t\t\tif (target === undefined) {\n\t\t\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, newDatum))\n\t\t\t\t}\n\n\t\t\t\tif (fromTarget?.value === newId) {\n\t\t\t\t\tthis.mergeInEntityData(entityMap, target, newDatum)\n\t\t\t\t\treturn fromTarget\n\t\t\t\t} else {\n\t\t\t\t\treturn new ServerGeneratedUuid(this.createEntityData(entityMap, newDatum))\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (fromTarget instanceof Set) {\n\t\t\tif (Array.isArray(newDatum)) {\n\t\t\t\tfromTarget.clear()\n\t\t\t\tfor (const entityData of newDatum) {\n\t\t\t\t\tfromTarget.add(this.createEntityData(entityMap, entityData))\n\t\t\t\t}\n\t\t\t\treturn fromTarget\n\t\t\t}\n\t\t} else if (!Array.isArray(newDatum) && (newDatum === null || typeof newDatum !== 'object')) {\n\t\t\treturn newDatum\n\t\t}\n\t\treturn this.rejectData()\n\t}\n\n\tprivate static mergeInEntityData(\n\t\tentityMap: PersistedEntityDataStore,\n\t\ttarget: SingleEntityPersistedData,\n\t\tnewData: ReceivedEntityData,\n\t): SingleEntityPersistedData {\n\t\tfor (const field in newData) {\n\t\t\tconst fromTarget = target.get(field)\n\t\t\tconst newDatum = newData[field]\n\n\t\t\ttarget.set(field, this.mergeInFieldData(entityMap, fromTarget, newDatum))\n\t\t}\n\t\treturn target\n\t}\n\n\tprivate static rejectData(extraMessage?: string): never {\n\t\tthrow new BindingError(`Failed to process data received from the API.${extraMessage ? `\\n${extraMessage}` : ''}`)\n\t}\n}\n"],"names":[],"mappings":";;;;;;;AAeO,MAAM,0BAA0B;AAAA,SACxB,qBACb,UACA,kBACc;AACR,UAAA,EAAE,kBAAkB,6BAA6B;AACjD,UAAA,iCAA8B;AAEpC,eAAW,mBAAmB,kBAAkB;AAC/C,YAAM,YAAY,iBAAiB;AAEnC,YAAM,YAAY,KAAK,gBAAgB,0BAA0B,SAAS;AAE1E,UAAI,cAAc,MAAM;AACvB,yBAAiB,OAAO,eAAe;AAAA,MAC7B,WAAA,qBAAqB,OAAO,OAAO,cAAc,UAAU;AACpD,yBAAA,IAAI,iBAAiB,SAAS;AAAA,MAAA,OACzC;AACN,aAAK,WAAW;AAAA,MACjB;AAAA,IACD;AACO,WAAA;AAAA,EACR;AAAA,SAEc,wBACb,UACA,kBACc;AACR,UAAA,EAAE,kBAAkB,6BAA6B;AACjD,UAAA,iCAA8B;AAEpC,eAAW,kBAAkB,kBAAkB;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ACzB,iBAAO,IAAI,oBAAoB,KAAK,iBAAiB,WAAW,QAAQ,CAAC;AAAA,QAC1E;AAEI,YAAA,YAAY,UAAU,OAAO;AAC3B,eAAA,kBAAkB,WAAW,QAAQ,QAAQ;AAC3C,iBAAA;AAAA,QAAA,OACD;AACN,iBAAO,IAAI,oBAAoB,KAAK,iBAAiB,WAAW,QAAQ,CAAC;AAAA,QAC1E;AAAA,MACD;AAAA,IAAA,WACU,sBAAsB,KAAK;AACjC,UAAA,MAAM,QAAQ,QAAQ,GAAG;AAC5B,mBAAW,MAAM;AACjB,mBAAW,cAAc,UAAU;AAClC,qBAAW,IAAI,KAAK,iBAAiB,WAAW,UAAU,CAAC;AAAA,QAC5D;AACO,eAAA;AAAA,MACR;AAAA,IAAA,WACU,CAAC,MAAM,QAAQ,QAAQ,KAAmB,cAAA,QAAQ,OAAO,aAAa,WAAW;AACpF,aAAA;AAAA,IACR;AACA,WAAO,KAAK;EACb;AAAA,SAEe,kBACd,WACA,QACA,SAC4B;AAC5B,eAAW,SAAS,SAAS;AACtB,YAAA,aAAa,OAAO,IAAI,KAAK;AACnC,YAAM,WAAW,QAAQ;AAEzB,aAAO,IAAI,OAAO,KAAK,iBAAiB,WAAW,YAAY,QAAQ,CAAC;AAAA,IACzE;AACO,WAAA;AAAA,EACR;AAAA,SAEe,WAAW,cAA8B;AACjD,UAAA,IAAI,aAAa,gDAAgD,eAAe;AAAA,EAAK,iBAAiB,IAAI;AAAA,EACjH;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"StateInitializer.js","sources":["../../../src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, FieldAccessor } from '../accessors'\nimport { EntityFieldPersistedData, RuntimeId, ServerGeneratedUuid, UnpersistedEntityDummyId } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '../markers'\nimport { BijectiveIndexedMap } from '../structures'\nimport type {\n\tEntityEventListenerStore,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tScalar,\n} from '../treeParameters'\nimport { assertNever } from '../utils'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { Config } from './Config'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from './TreeParameterMerger'\nimport type { TreeStore } from './TreeStore'\nimport { EventListenersStore } from '../treeParameters'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly config: Config,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(this.accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis,\n\t\t\tthis.treeStore,\n\t\t)\n\t\tthis.listOperations = new ListOperations(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis,\n\t\t\tthis.treeStore,\n\t\t)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<string> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerGeneratedUuid ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, value.value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// todo: get from copyFrom\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined = undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t\truntimeId = value.entity.id\n\t\t\t\t\t} else {\n\t\t\t\t\t\truntimeId = new UnpersistedEntityDummyId()\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(blueprint: EntityRealmBlueprint, realmKey: string, entity: EntityState): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessor(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.value),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.value)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.value\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerGeneratedUuid,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<string>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessor(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<string | undefined> =\n\t\t\tinitialEntityIds.size === 0\n\t\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerGeneratedUuid(entityId) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: Scalar | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = persistedValue ?? fieldMarker.defaultValue ?? null\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityField(parent.entity.entityName, fieldMarker.fieldName)\n\t\t\t\t\tif (!fieldSchema) {\n\t\t\t\t\t\tthrow new BindingError(`Unknown field ${parent.entity.entityName}.${fieldMarker.fieldName}.`)\n\t\t\t\t\t}\n\t\t\t\t\tif (fieldSchema.__typename !== '_Column') {\n\t\t\t\t\t\tthrow new BindingError(`Invalid type of field ${parent.entity.entityName}.${fieldMarker.fieldName}. Expected '_Column', '${fieldSchema.__typename}' found.`)\n\t\t\t\t\t}\n\t\t\t\t\tfieldState.accessor = new FieldAccessor(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tfieldDatum: EntityFieldPersistedData | undefined,\n\t) {\n\t\tif (fieldDatum instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (fieldDatum instanceof ServerGeneratedUuid) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, fieldDatum),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<string>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityField(entityRealm.entity.entityName, field.parameters.field)\n\n\t\tif (targetField?.__typename !== '_Relation') {\n\t\t\tthrow new BindingError() // This should have been validated elsewhere.\n\t\t}\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneSingleEntityEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneEntityListEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneSingleEntityEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\t// TODO !!!\n\t\treturn undefined\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;uBAwC8B;AAAA,EAKtB,YACW,sBACA,qBACA,QACA,cACA,WAChB;AAVe;AACA;AACA;;;;;;SASX,kBAAkB,IAAI,gBAAgB,KAAK,sBAAsB,KAAK,cAAc,MAAM,KAAK;SAC/F,mBAAmB,IAAI,iBAC3B,KAAK,sBACL,KAAK,qBACL,KAAK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WAAW;YAClE,kBAAkB,qBAAoB,IAAI,EAAE,MAAM;YAClD,QAAkC,IAAI,oBAAoB,MAAM,UAAU,OAAO;UAEnF,oBAAoB,QAAW;cAC5B,IAAI,EAAE,MAAM,gBAAgB;AAAA;aAE5B;AAAA;UAGF,qBAAqB,UAAU,OAAO,WAAW;QACnD,uBAAuB,QAAW;aAC9B;AAAA;WAED,oBAAoB,gCAAgC;AAAA;AAAA,EAGpD,uCACP,WAC2C;UACrC,qBAAqB,UAAU,OAAO,WAAW;QAEnD,uBAAuB,QAAW;aAC9B;AAAA;WAED,oBAAoB,8BAA8B;AAAA;AAAA,EAGlD,4CACP,WACuC;UACjC,qBAAqB,UAAU,OAAO,WAAW;QAEnD,uBAAuB,QAAW;aAC9B;AAAA;WAED,oBAAoB,gCAAgC;AAAA;AAAA,EAGpD,kCAAkC,QAA0D;WAE5F;AAAA;AAAA;;"}
1
+ {"version":3,"file":"StateInitializer.js","sources":["../../../src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, FieldAccessor } from '../accessors'\nimport { EntityFieldPersistedData, RuntimeId, ServerGeneratedUuid, UnpersistedEntityDummyId } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '../markers'\nimport { BijectiveIndexedMap } from '../structures'\nimport type {\n\tEntityEventListenerStore,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tScalar,\n} from '../treeParameters'\nimport { assertNever } from '../utils'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { Config } from './Config'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from './TreeParameterMerger'\nimport type { TreeStore } from './TreeStore'\nimport { EventListenersStore } from '../treeParameters'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly config: Config,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(this.accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis,\n\t\t\tthis.treeStore,\n\t\t)\n\t\tthis.listOperations = new ListOperations(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis,\n\t\t\tthis.treeStore,\n\t\t)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<string> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerGeneratedUuid ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, value.value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// todo: get from copyFrom\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined = undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t\truntimeId = value.entity.id\n\t\t\t\t\t} else {\n\t\t\t\t\t\truntimeId = new UnpersistedEntityDummyId()\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(blueprint: EntityRealmBlueprint, realmKey: string, entity: EntityState): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessor(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.value),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.value)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.value\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerGeneratedUuid,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<string>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessor(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<string | undefined> =\n\t\t\tinitialEntityIds.size === 0\n\t\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerGeneratedUuid(entityId) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: Scalar | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = persistedValue ?? fieldMarker.defaultValue ?? null\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityField(parent.entity.entityName, fieldMarker.fieldName)\n\t\t\t\t\tif (!fieldSchema) {\n\t\t\t\t\t\tthrow new BindingError(`Unknown field ${parent.entity.entityName}.${fieldMarker.fieldName}.`)\n\t\t\t\t\t}\n\t\t\t\t\tif (fieldSchema.__typename !== '_Column') {\n\t\t\t\t\t\tthrow new BindingError(`Invalid type of field ${parent.entity.entityName}.${fieldMarker.fieldName}. Expected '_Column', '${fieldSchema.__typename}' found.`)\n\t\t\t\t\t}\n\t\t\t\t\tfieldState.accessor = new FieldAccessor(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tfieldDatum: EntityFieldPersistedData | undefined,\n\t) {\n\t\tif (fieldDatum instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (fieldDatum instanceof ServerGeneratedUuid) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, fieldDatum),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<string>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityField(entityRealm.entity.entityName, field.parameters.field)\n\n\t\tif (targetField?.__typename !== '_Relation') {\n\t\t\tthrow new BindingError() // This should have been validated elsewhere.\n\t\t}\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneSingleEntityEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneEntityListEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn TreeParameterMerger.cloneSingleEntityEventListeners(blueprintListeners)\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\t// TODO !!!\n\t\treturn undefined\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwCO,MAAM,iBAAiB;AAAA,EAKtB,YACW,sBACA,qBACA,QACA,cACA,WAChB;AAVe;AACA;AACA;AAGC,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,SAAA;AACA,SAAA,eAAA;AACA,SAAA,YAAA;AAEZ,SAAA,kBAAkB,IAAI,gBAAgB,KAAK,sBAAsB,KAAK,cAAc,MAAM,KAAK,SAAS;AACxG,SAAA,mBAAmB,IAAI,iBAC3B,KAAK,sBACL,KAAK,qBACL,KAAK,cACL,MACA,KAAK,SACN;AACK,SAAA,iBAAiB,IAAI,eACzB,KAAK,sBACL,KAAK,qBACL,KAAK,cACL,MACA,KAAK,SACN;AAAA,EACD;AAAA,EAEO,kBAAkB,MAAoE;AACxF,QAAA;AACJ,UAAM,oBAAoB,KAAK,UAAU,qBAAqB,IAAI,KAAK,eAAe;AAEtF,QAAI,gBAAgB,yBAAyB;AAC5C,YAAM,qBAAkC,6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oB,SAAS,GAAG;AACxE,YAAM,OAAO,kBAAkB;AAAA,IAChC;AAAA,EACD;AAAA,EAEO,mCAAmC,WAAuE;AAC5G,QAAA,UAAU,SAAS,cAAc;AACpC,YAAM,sBAAqB,UAAU,OAAO,UAAU,OAAO,WAAW;AACxE,YAAM,kBAAkB,qBAAoB,IAAI,EAAE,MAAM,cAAc;AACtE,YAAM,QAAkC,IAAI,oBAAoB,MAAM,UAAU,OAAO,mBAAmB;AAE1G,UAAI,oBAAoB,QAAW;AAClC,cAAM,IAAI,EAAE,MAAM,gBAAgB,eAAe;AAAA,MAClD;AACO,aAAA;AAAA,IACR;AAEM,UAAA,qBAAqB,UAAU,OAAO,WAAW;AACvD,QAAI,uBAAuB,QAAW;AAC9B,aAAA;AAAA,IACR;AACO,WAAA,oBAAoB,gCAAgC,kBAAkB;AAAA,EAC9E;AAAA,EAEQ,uCACP,WAC2C;AACrC,UAAA,qBAAqB,UAAU,OAAO,WAAW;AAEvD,QAAI,uBAAuB,QAAW;AAC9B,aAAA;AAAA,IACR;AACO,WAAA,oBAAoB,8BAA8B,kBAAkB;AAAA,EAC5E;AAAA,EAEQ,4CACP,WACuC;AACjC,UAAA,qBAAqB,UAAU,OAAO,WAAW;AAEvD,QAAI,uBAAuB,QAAW;AAC9B,aAAA;AAAA,IACR;AACO,WAAA,oBAAoB,gCAAgC,kBAAkB;AAAA,EAC9E;AAAA,EAEQ,kCAAkC,QAA0D;AAE5F,WAAA;AAAA,EACR;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"TreeAugmenter.js","sources":["../../../src/core/TreeAugmenter.ts"],"sourcesContent":["import {\n\tClientGeneratedUuid,\n\tEntityListPersistedData,\n\tReceivedDataTree,\n\tRuntimeId,\n\tServerGeneratedUuid,\n\tUnpersistedEntityDummyId,\n} from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport type { MarkerTreeRoot } from '../markers'\nimport type { EntityId, PlaceholderName, TreeRootId } from '../treeParameters'\nimport { assert, assertNever } from '../utils'\nimport { EventManager } from './EventManager'\nimport { OperationsHelpers } from './operations/OperationsHelpers'\nimport type { EntityListState, EntityRealmState, EntityRealmStateStub, RootStateNode } from './state'\nimport type { StateInitializer } from './StateInitializer'\nimport type { TreeStore } from './TreeStore'\n\nexport class TreeAugmenter {\n\tpublic constructor(\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic extendPersistedData(newPersistedData: ReceivedDataTree) {\n\t\t// TODO this doesn't yet handle updates for entities whose persisted data just gets magically changed without notice.\n\t\tthis.treeStore.mergeInQueryResponse(newPersistedData)\n\t}\n\n\tpublic extendTreeStates(newTreeId: TreeRootId | undefined, newMarkerTree: MarkerTreeRoot): void {\n\t\tconst subTreeStates: Map<PlaceholderName, RootStateNode> = new Map()\n\n\t\tthis.treeStore.markerTrees.set(newTreeId, newMarkerTree)\n\t\tthis.treeStore.subTreeStatesByRoot.set(newTreeId, subTreeStates)\n\n\t\tfor (const [placeholderName, newSubTreeMarker] of newMarkerTree.subTrees) {\n\t\t\tconst newState = this.stateInitializer.initializeSubTree(newSubTreeMarker)\n\t\t\tsubTreeStates.set(placeholderName, newState)\n\t\t}\n\t}\n\n\tpublic updatePersistedData(response: ReceivedDataTree) {\n\t\tthis.treeStore.mergeInMutationResponse(response)\n\n\t\tthis.eventManager.syncTransaction(() => {\n\t\t\tfor (const rootStates of this.treeStore.subTreeStatesByRoot.values()) {\n\t\t\t\t// for (const rootPlaceholder in response) {\n\t\t\t\t// \tconst rootState = rootStates.get(rootPlaceholder)\n\t\t\t\t//\n\t\t\t\t// \tif (rootState === undefined) {\n\t\t\t\t// \t\tcontinue\n\t\t\t\t// \t}\n\t\t\t\t// TODO the above would be better, only refreshing roots which participated in the mutation. However, due to\n\t\t\t\t// \thow mutations are currently generated, we cannot assume that only roots present in the response need\n\t\t\t\t// \trefreshed. So we just refresh all.\n\t\t\t\tfor (const [rootPlaceholder, rootState] of rootStates) {\n\t\t\t\t\tconst rootData = this.treeStore.subTreePersistedData.get(rootPlaceholder)\n\n\t\t\t\t\tif (rootState.type === 'entityList') {\n\t\t\t\t\t\tif (!(rootData instanceof Set)) {\n\t\t\t\t\t\t\tcontinue // This should never happen.\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.updateEntityListPersistedData(rootState, rootData)\n\t\t\t\t\t} else if (rootState.type === 'entityRealm') {\n\t\t\t\t\t\tif (rootData !== undefined && !(rootData instanceof ServerGeneratedUuid)) {\n\t\t\t\t\t\t\tcontinue // This should never happen.\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.updateRealmIdIfNecessary(rootState, rootData)\n\t\t\t\t\t\tthis.updateEntityRealmPersistedData(rootState)\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(rootState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic resetCreatingSubTrees() {\n\t\tfor (const rootStates of this.treeStore.subTreeStatesByRoot.values()) {\n\t\t\tfor (const [rootPlaceholder, rootState] of rootStates) {\n\t\t\t\tif (rootState.type === 'entityList') {\n\t\t\t\t\tassert(rootState.blueprint.parent === undefined)\n\t\t\t\t\tif (!rootState.blueprint.marker.parameters.isCreating) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.subTreePersistedData.delete(rootPlaceholder)\n\t\t\t\t\tconst changesCount = rootState.unpersistedChangesCount\n\n\t\t\t\t\t// Just emptying the list. But the list itself can stay.\n\t\t\t\t\tfor (const childState of rootState.children.values()) {\n\t\t\t\t\t\tthis.treeStore.disposeOfRealm(childState)\n\t\t\t\t\t}\n\t\t\t\t\trootState.accessor = undefined\n\t\t\t\t\trootState.children.clear()\n\t\t\t\t\trootState.childrenWithPendingUpdates?.clear()\n\t\t\t\t\trootState.errors = undefined\n\t\t\t\t\trootState.plannedRemovals?.clear()\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(\n\t\t\t\t\t\trootState,\n\t\t\t\t\t\tchangesCount ? -1 * changesCount : EventManager.NO_CHANGES_DIFFERENCE,\n\t\t\t\t\t)\n\t\t\t\t} else if (rootState.type === 'entityRealm') {\n\t\t\t\t\tassert(rootState.blueprint.parent === undefined)\n\t\t\t\t\tif (!rootState.blueprint.marker.parameters.isCreating) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst presentListeners = rootState.eventListeners\n\t\t\t\t\tthis.treeStore.disposeOfRealm(rootState)\n\n\t\t\t\t\t// It's crucial to delete the persisted data before we re-initialize so that we get a fresh, new entity.\n\t\t\t\t\tthis.treeStore.subTreePersistedData.delete(rootPlaceholder)\n\t\t\t\t\tconst newRootState = this.stateInitializer.initializeSubTree(rootState.blueprint.marker)\n\n\t\t\t\t\t// We preserve the previous listeners but purge some of them.\n\t\t\t\t\tnewRootState.eventListeners = presentListeners\n\t\t\t\t\tOperationsHelpers.purgeStaleListenersAfterIdChange(newRootState as EntityRealmState)\n\n\t\t\t\t\trootStates.set(rootPlaceholder, newRootState)\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(newRootState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t} else {\n\t\t\t\t\tassertNever(rootState)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateRealmIdIfNecessary(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: ServerGeneratedUuid | undefined | null,\n\t) {\n\t\tconst currentId = realm.entity.id\n\t\tlet idToChangeTo: RuntimeId | undefined = undefined\n\n\t\tif (currentId instanceof ServerGeneratedUuid) {\n\t\t\tif (newId === undefined || newId === null) {\n\t\t\t\tidToChangeTo = new UnpersistedEntityDummyId()\n\t\t\t} else if (currentId.value !== newId.value) {\n\t\t\t\tidToChangeTo = newId\n\t\t\t}\n\t\t} else if (currentId instanceof ClientGeneratedUuid) {\n\t\t\tidToChangeTo = newId ?? new UnpersistedEntityDummyId()\n\t\t} else if (currentId instanceof UnpersistedEntityDummyId) {\n\t\t\tif (newId) {\n\t\t\t\tidToChangeTo = newId\n\t\t\t}\n\t\t} else {\n\t\t\treturn assertNever(currentId)\n\t\t}\n\t\tif (idToChangeTo) {\n\t\t\tOperationsHelpers.changeRealmId(this.treeStore, this.eventManager, this.stateInitializer, realm, idToChangeTo)\n\t\t}\n\t}\n\n\tprivate updateEntityRealmPersistedData(realm: EntityRealmState) {\n\t\tconst realmId = realm.entity.id\n\t\tconst persistedData = realmId.existsOnServer ? this.treeStore.persistedEntityData.get(realmId.value) : undefined\n\n\t\tfor (const [placeholderName, child] of realm.children) {\n\t\t\tconst childData = persistedData?.get(placeholderName)\n\n\t\t\tswitch (child.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tif (childData instanceof ServerGeneratedUuid || childData instanceof Set) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (child.persistedValue !== childData) {\n\t\t\t\t\t\tconst shouldChangeBothValues = child.persistedValue === child.value\n\n\t\t\t\t\t\tchild.persistedValue = childData\n\t\t\t\t\t\tif (shouldChangeBothValues) {\n\t\t\t\t\t\t\tchild.value = childData ?? null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(child, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealm':\n\t\t\t\tcase 'entityRealmStub': {\n\t\t\t\t\tif (!(childData instanceof ServerGeneratedUuid) && childData !== undefined && childData !== null) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tthis.updateRealmIdIfNecessary(child, childData)\n\t\t\t\t\tif (child.type === 'entityRealm') {\n\t\t\t\t\t\tthis.updateEntityRealmPersistedData(child)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tif (childData !== undefined && !(childData instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tthis.updateEntityListPersistedData(child, childData ?? new Set()) // TODO!!\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\treturn assertNever(child)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateEntityListPersistedData(state: EntityListState, newPersistedIds: EntityListPersistedData) {\n\t\tconst persistedWithoutState: EntityId[] = []\n\n\t\t// TODO This isn't particularly efficient. We should probably use something like diff-sequence here.\n\n\t\tfor (const persistedId of newPersistedIds) {\n\t\t\tif (!state.children.has(persistedId)) {\n\t\t\t\tpersistedWithoutState.push(persistedId)\n\t\t\t}\n\t\t}\n\t\tfor (const [stateId, child] of state.children) {\n\t\t\tconst childRuntimeId = child.entity.id\n\t\t\tif (!newPersistedIds.has(stateId)) {\n\t\t\t\tif (\n\t\t\t\t\t// It's a uuid but it's not among the persisted so either it got deleted/disconnected or the client-side\n\t\t\t\t\t// id generation didn't quite pan out.\n\t\t\t\t\tchildRuntimeId instanceof ServerGeneratedUuid ||\n\t\t\t\t\tchildRuntimeId instanceof ClientGeneratedUuid ||\n\t\t\t\t\t// No persisted entity id to allocate to this so that's a goodbye.\n\t\t\t\t\tpersistedWithoutState.length === 0\n\t\t\t\t) {\n\t\t\t\t\tstate.getAccessor().disconnectEntity(child.getAccessor())\n\t\t\t\t\tcontinue\n\t\t\t\t} else {\n\t\t\t\t\tconst drawnId = persistedWithoutState.shift()!\n\t\t\t\t\tOperationsHelpers.changeRealmId(\n\t\t\t\t\t\tthis.treeStore,\n\t\t\t\t\t\tthis.eventManager,\n\t\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\t\tchild,\n\t\t\t\t\t\tnew ServerGeneratedUuid(drawnId),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (childRuntimeId instanceof ClientGeneratedUuid) {\n\t\t\t\tOperationsHelpers.changeRealmId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tchild,\n\t\t\t\t\tnew ServerGeneratedUuid(childRuntimeId.value),\n\t\t\t\t)\n\t\t\t}\n\t\t\tif (child.type === 'entityRealm') {\n\t\t\t\tthis.updateEntityRealmPersistedData(child)\n\t\t\t}\n\t\t}\n\t\tfor (const unclaimedPersistedId of persistedWithoutState) {\n\t\t\tthis.stateInitializer.initializeEntityRealm(new ServerGeneratedUuid(unclaimedPersistedId), state.entityName, {\n\t\t\t\ttype: 'listEntity',\n\t\t\t\tparent: state,\n\t\t\t})\n\t\t}\n\t\t// All that remains now is the order.\n\t\tstate.children.changeKeyOrder(newPersistedIds)\n\t}\n}\n"],"names":[],"mappings":";;;;;;oBAkB2B;AAAA,EACnB,YACW,cACA,kBACA,WAChB;;;;;EAEK,oBAAoB,kBAAoC;SAEzD,UAAU,qBAAqB;AAAA;AAAA,EAG9B,iBAAiB,WAAmC,eAAqC;UACzF,oCAAyD;SAE1D,UAAU,YAAY,IAAI,WAAW;SACrC,UAAU,oBAAoB,IAAI,WAAW;eAEvC,CAAC,iBAAiB,qBAAqB,cAAc,UAAU;YACnE,WAAW,KAAK,iBAAiB,kBAAkB;oBAC3C,IAAI,iBAAiB;AAAA;AAAA;AAAA,EAI9B,oBAAoB,UAA4B;SACjD,UAAU,wBAAwB;SAElC,aAAa,gBAAgB,MAAM;iBAC5B,cAAc,KAAK,UAAU,oBAAoB,UAAU;mBAU1D,CAAC,iBAAiB,cAAc,YAAY;gBAChD,WAAW,KAAK,UAAU,qBAAqB,IAAI;cAErD,UAAU,SAAS,cAAc;gBAChC,sBAAsB,MAAM;;;iBAG3B,8BAA8B,WAAW;AAAA,qBACpC,UAAU,SAAS,eAAe;gBACxC,aAAa,UAAa,sBAAsB,sBAAsB;;;iBAGrE,yBAAyB,WAAW;iBACpC,+BAA+B;AAAA,iBAC9B;m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AAA;eAG3B,wBAAwB,uBAAuB;WACpD,iBAAiB,sBAAsB,IAAI,oBAAoB,uBAAuB,MAAM,YAAY;AAAA,QAC5G,MAAM;AAAA,QACN,QAAQ;AAAA;AAAA;UAIJ,SAAS,eAAe;AAAA;AAAA;;"}
1
+ {"version":3,"file":"TreeAugmenter.js","sources":["../../../src/core/TreeAugmenter.ts"],"sourcesContent":["import {\n\tClientGeneratedUuid,\n\tEntityListPersistedData,\n\tReceivedDataTree,\n\tRuntimeId,\n\tServerGeneratedUuid,\n\tUnpersistedEntityDummyId,\n} from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport type { MarkerTreeRoot } from '../markers'\nimport type { EntityId, PlaceholderName, TreeRootId } from '../treeParameters'\nimport { assert, assertNever } from '../utils'\nimport { EventManager } from './EventManager'\nimport { OperationsHelpers } from './operations/OperationsHelpers'\nimport type { EntityListState, EntityRealmState, EntityRealmStateStub, RootStateNode } from './state'\nimport type { StateInitializer } from './StateInitializer'\nimport type { TreeStore } from './TreeStore'\n\nexport class TreeAugmenter {\n\tpublic constructor(\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic extendPersistedData(newPersistedData: ReceivedDataTree) {\n\t\t// TODO this doesn't yet handle updates for entities whose persisted data just gets magically changed without notice.\n\t\tthis.treeStore.mergeInQueryResponse(newPersistedData)\n\t}\n\n\tpublic extendTreeStates(newTreeId: TreeRootId | undefined, newMarkerTree: MarkerTreeRoot): void {\n\t\tconst subTreeStates: Map<PlaceholderName, RootStateNode> = new Map()\n\n\t\tthis.treeStore.markerTrees.set(newTreeId, newMarkerTree)\n\t\tthis.treeStore.subTreeStatesByRoot.set(newTreeId, subTreeStates)\n\n\t\tfor (const [placeholderName, newSubTreeMarker] of newMarkerTree.subTrees) {\n\t\t\tconst newState = this.stateInitializer.initializeSubTree(newSubTreeMarker)\n\t\t\tsubTreeStates.set(placeholderName, newState)\n\t\t}\n\t}\n\n\tpublic updatePersistedData(response: ReceivedDataTree) {\n\t\tthis.treeStore.mergeInMutationResponse(response)\n\n\t\tthis.eventManager.syncTransaction(() => {\n\t\t\tfor (const rootStates of this.treeStore.subTreeStatesByRoot.values()) {\n\t\t\t\t// for (const rootPlaceholder in response) {\n\t\t\t\t// \tconst rootState = rootStates.get(rootPlaceholder)\n\t\t\t\t//\n\t\t\t\t// \tif (rootState === undefined) {\n\t\t\t\t// \t\tcontinue\n\t\t\t\t// \t}\n\t\t\t\t// TODO the above would be better, only refreshing roots which participated in the mutation. However, due to\n\t\t\t\t// \thow mutations are currently generated, we cannot assume that only roots present in the response need\n\t\t\t\t// \trefreshed. So we just refresh all.\n\t\t\t\tfor (const [rootPlaceholder, rootState] of rootStates) {\n\t\t\t\t\tconst rootData = this.treeStore.subTreePersistedData.get(rootPlaceholder)\n\n\t\t\t\t\tif (rootState.type === 'entityList') {\n\t\t\t\t\t\tif (!(rootData instanceof Set)) {\n\t\t\t\t\t\t\tcontinue // This should never happen.\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.updateEntityListPersistedData(rootState, rootData)\n\t\t\t\t\t} else if (rootState.type === 'entityRealm') {\n\t\t\t\t\t\tif (rootData !== undefined && !(rootData instanceof ServerGeneratedUuid)) {\n\t\t\t\t\t\t\tcontinue // This should never happen.\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.updateRealmIdIfNecessary(rootState, rootData)\n\t\t\t\t\t\tthis.updateEntityRealmPersistedData(rootState)\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn assertNever(rootState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic resetCreatingSubTrees() {\n\t\tfor (const rootStates of this.treeStore.subTreeStatesByRoot.values()) {\n\t\t\tfor (const [rootPlaceholder, rootState] of rootStates) {\n\t\t\t\tif (rootState.type === 'entityList') {\n\t\t\t\t\tassert(rootState.blueprint.parent === undefined)\n\t\t\t\t\tif (!rootState.blueprint.marker.parameters.isCreating) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.subTreePersistedData.delete(rootPlaceholder)\n\t\t\t\t\tconst changesCount = rootState.unpersistedChangesCount\n\n\t\t\t\t\t// Just emptying the list. But the list itself can stay.\n\t\t\t\t\tfor (const childState of rootState.children.values()) {\n\t\t\t\t\t\tthis.treeStore.disposeOfRealm(childState)\n\t\t\t\t\t}\n\t\t\t\t\trootState.accessor = undefined\n\t\t\t\t\trootState.children.clear()\n\t\t\t\t\trootState.childrenWithPendingUpdates?.clear()\n\t\t\t\t\trootState.errors = undefined\n\t\t\t\t\trootState.plannedRemovals?.clear()\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(\n\t\t\t\t\t\trootState,\n\t\t\t\t\t\tchangesCount ? -1 * changesCount : EventManager.NO_CHANGES_DIFFERENCE,\n\t\t\t\t\t)\n\t\t\t\t} else if (rootState.type === 'entityRealm') {\n\t\t\t\t\tassert(rootState.blueprint.parent === undefined)\n\t\t\t\t\tif (!rootState.blueprint.marker.parameters.isCreating) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tconst presentListeners = rootState.eventListeners\n\t\t\t\t\tthis.treeStore.disposeOfRealm(rootState)\n\n\t\t\t\t\t// It's crucial to delete the persisted data before we re-initialize so that we get a fresh, new entity.\n\t\t\t\t\tthis.treeStore.subTreePersistedData.delete(rootPlaceholder)\n\t\t\t\t\tconst newRootState = this.stateInitializer.initializeSubTree(rootState.blueprint.marker)\n\n\t\t\t\t\t// We preserve the previous listeners but purge some of them.\n\t\t\t\t\tnewRootState.eventListeners = presentListeners\n\t\t\t\t\tOperationsHelpers.purgeStaleListenersAfterIdChange(newRootState as EntityRealmState)\n\n\t\t\t\t\trootStates.set(rootPlaceholder, newRootState)\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(newRootState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t} else {\n\t\t\t\t\tassertNever(rootState)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateRealmIdIfNecessary(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: ServerGeneratedUuid | undefined | null,\n\t) {\n\t\tconst currentId = realm.entity.id\n\t\tlet idToChangeTo: RuntimeId | undefined = undefined\n\n\t\tif (currentId instanceof ServerGeneratedUuid) {\n\t\t\tif (newId === undefined || newId === null) {\n\t\t\t\tidToChangeTo = new UnpersistedEntityDummyId()\n\t\t\t} else if (currentId.value !== newId.value) {\n\t\t\t\tidToChangeTo = newId\n\t\t\t}\n\t\t} else if (currentId instanceof ClientGeneratedUuid) {\n\t\t\tidToChangeTo = newId ?? new UnpersistedEntityDummyId()\n\t\t} else if (currentId instanceof UnpersistedEntityDummyId) {\n\t\t\tif (newId) {\n\t\t\t\tidToChangeTo = newId\n\t\t\t}\n\t\t} else {\n\t\t\treturn assertNever(currentId)\n\t\t}\n\t\tif (idToChangeTo) {\n\t\t\tOperationsHelpers.changeRealmId(this.treeStore, this.eventManager, this.stateInitializer, realm, idToChangeTo)\n\t\t}\n\t}\n\n\tprivate updateEntityRealmPersistedData(realm: EntityRealmState) {\n\t\tconst realmId = realm.entity.id\n\t\tconst persistedData = realmId.existsOnServer ? this.treeStore.persistedEntityData.get(realmId.value) : undefined\n\n\t\tfor (const [placeholderName, child] of realm.children) {\n\t\t\tconst childData = persistedData?.get(placeholderName)\n\n\t\t\tswitch (child.type) {\n\t\t\t\tcase 'field': {\n\t\t\t\t\tif (childData instanceof ServerGeneratedUuid || childData instanceof Set) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (child.persistedValue !== childData) {\n\t\t\t\t\t\tconst shouldChangeBothValues = child.persistedValue === child.value\n\n\t\t\t\t\t\tchild.persistedValue = childData\n\t\t\t\t\t\tif (shouldChangeBothValues) {\n\t\t\t\t\t\t\tchild.value = childData ?? null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(child, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityRealm':\n\t\t\t\tcase 'entityRealmStub': {\n\t\t\t\t\tif (!(childData instanceof ServerGeneratedUuid) && childData !== undefined && childData !== null) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tthis.updateRealmIdIfNecessary(child, childData)\n\t\t\t\t\tif (child.type === 'entityRealm') {\n\t\t\t\t\t\tthis.updateEntityRealmPersistedData(child)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcase 'entityList': {\n\t\t\t\t\tif (childData !== undefined && !(childData instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tthis.updateEntityListPersistedData(child, childData ?? new Set()) // TODO!!\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\treturn assertNever(child)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateEntityListPersistedData(state: EntityListState, newPersistedIds: EntityListPersistedData) {\n\t\tconst persistedWithoutState: EntityId[] = []\n\n\t\t// TODO This isn't particularly efficient. We should probably use something like diff-sequence here.\n\n\t\tfor (const persistedId of newPersistedIds) {\n\t\t\tif (!state.children.has(persistedId)) {\n\t\t\t\tpersistedWithoutState.push(persistedId)\n\t\t\t}\n\t\t}\n\t\tfor (const [stateId, child] of state.children) {\n\t\t\tconst childRuntimeId = child.entity.id\n\t\t\tif (!newPersistedIds.has(stateId)) {\n\t\t\t\tif (\n\t\t\t\t\t// It's a uuid but it's not among the persisted so either it got deleted/disconnected or the client-side\n\t\t\t\t\t// id generation didn't quite pan out.\n\t\t\t\t\tchildRuntimeId instanceof ServerGeneratedUuid ||\n\t\t\t\t\tchildRuntimeId instanceof ClientGeneratedUuid ||\n\t\t\t\t\t// No persisted entity id to allocate to this so that's a goodbye.\n\t\t\t\t\tpersistedWithoutState.length === 0\n\t\t\t\t) {\n\t\t\t\t\tstate.getAccessor().disconnectEntity(child.getAccessor())\n\t\t\t\t\tcontinue\n\t\t\t\t} else {\n\t\t\t\t\tconst drawnId = persistedWithoutState.shift()!\n\t\t\t\t\tOperationsHelpers.changeRealmId(\n\t\t\t\t\t\tthis.treeStore,\n\t\t\t\t\t\tthis.eventManager,\n\t\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\t\tchild,\n\t\t\t\t\t\tnew ServerGeneratedUuid(drawnId),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (childRuntimeId instanceof ClientGeneratedUuid) {\n\t\t\t\tOperationsHelpers.changeRealmId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tchild,\n\t\t\t\t\tnew ServerGeneratedUuid(childRuntimeId.value),\n\t\t\t\t)\n\t\t\t}\n\t\t\tif (child.type === 'entityRealm') {\n\t\t\t\tthis.updateEntityRealmPersistedData(child)\n\t\t\t}\n\t\t}\n\t\tfor (const unclaimedPersistedId of persistedWithoutState) {\n\t\t\tthis.stateInitializer.initializeEntityRealm(new ServerGeneratedUuid(unclaimedPersistedId), state.entityName, {\n\t\t\t\ttype: 'listEntity',\n\t\t\t\tparent: state,\n\t\t\t})\n\t\t}\n\t\t// All that remains now is the order.\n\t\tstate.children.changeKeyOrder(newPersistedIds)\n\t}\n}\n"],"names":[],"mappings":";;;;;;AAkBO,MAAM,cAAc;AAAA,EACnB,YACW,cACA,kBACA,WAChB;AAHgB,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,oBAAoB,kBAAoC;AAEzD,SAAA,UAAU,qBAAqB,gBAAgB;AAAA,EACrD;AAAA,EAEO,iBAAiB,WAAmC,eAAqC;AACzF,UAAA,oCA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AAW,CAAC,SAAS,UAAU,MAAM,UAAU;AACxC,YAAA,iBAAiB,MAAM,OAAO;AACpC,UAAI,CAAC,gBAAgB,IAAI,OAAO,GAAG;AAClC,YAGC,0BAA0B,uBAC1B,0BAA0B,uBAE1B,sBAAsB,WAAW,GAChC;AACD,gBAAM,YAAY,EAAE,iBAAiB,MAAM,YAAa,CAAA;AACxD;AAAA,QAAA,OACM;AACA,gBAAA,UAAU,sBAAsB;AACpB,4BAAA,cACjB,KAAK,WACL,KAAK,cACL,KAAK,kBACL,OACA,IAAI,oBAAoB,OAAO,CAChC;AAAA,QACD;AAAA,MAAA,WACU,0BAA0B,qBAAqB;AACzD,0BAAkB,cACjB,KAAK,WACL,KAAK,cACL,KAAK,kBACL,OACA,IAAI,oBAAoB,eAAe,KAAK,CAC7C;AAAA,MACD;AACI,UAAA,MAAM,SAAS,eAAe;AACjC,aAAK,+BAA+B,KAAK;AAAA,MAC1C;AAAA,IACD;AACA,eAAW,wBAAwB,uBAAuB;AACzD,WAAK,iBAAiB,sBAAsB,IAAI,oBAAoB,oBAAoB,GAAG,MAAM,YAAY;AAAA,QAC5G,MAAM;AAAA,QACN,QAAQ;AAAA,MAAA,CACR;AAAA,IACF;AAEM,UAAA,SAAS,eAAe,eAAe;AAAA,EAC9C;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"TreeFilterGenerator.js","sources":["../../../src/core/TreeFilterGenerator.ts"],"sourcesContent":["import type { RelationFilter, TreeFilter } from '@contember/client'\nimport { EntityListPersistedData, ServerGeneratedUuid } from '../accessorTree'\nimport {\n\tEntityFieldMarkersContainer,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '../markers'\nimport type { EntityName } from '../treeParameters'\nimport { assertNever } from '../utils'\nimport { StateIterator } from './state'\nimport type { TreeStore } from './TreeStore'\n\ntype RawRelationFilters = Map<string, RawRelationFilters>\n\nexport class TreeFilterGenerator {\n\tpublic constructor(private readonly treeStore: TreeStore) {}\n\n\tpublic generateTreeFilter(): TreeFilter[] {\n\t\tconst filters: TreeFilter[][] = []\n\n\t\tfor (const [placeholderName, state] of StateIterator.eachRootState(this.treeStore)) {\n\t\t\tconst blueprint = state.blueprint\n\n\t\t\tif (blueprint.parent || blueprint.marker.parameters.expectedMutation === 'none') {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst marker = blueprint.marker\n\t\t\tfilters.push(this.generateSubTreeFilter(marker, this.treeStore.subTreePersistedData.get(placeholderName)))\n\t\t}\n\t\treturn filters.flat()\n\t}\n\n\tprivate generateSubTreeFilter(\n\t\tsubTree: EntitySubTreeMarker | EntityListSubTreeMarker,\n\t\tpersistedData: ServerGeneratedUuid | EntityListPersistedData | undefined,\n\t): TreeFilter[] {\n\t\tconst filters: TreeFilter[] = []\n\n\t\tif (persistedData === undefined) {\n\t\t\treturn filters // Do nothing\n\t\t}\n\n\t\tif (persistedData instanceof ServerGeneratedUuid) {\n\t\t\tconst filter = this.generateTopLevelEntityFilter(\n\t\t\t\tpersistedData.value,\n\t\t\t\tsubTree.parameters.entityName,\n\t\t\t\tsubTree.fields,\n\t\t\t)\n\t\t\tfilter && filters.push(filter)\n\t\t} else {\n\t\t\tfor (const id of persistedData) {\n\t\t\t\tconst filter = this.generateTopLevelEntityFilter(id, subTree.parameters.entityName, subTree.fields)\n\t\t\t\tfilter && filters.push(filter)\n\t\t\t}\n\t\t}\n\n\t\treturn filters\n\t}\n\n\tprivate generateTopLevelEntityFilter(\n\t\tid: string,\n\t\tentityName: EntityName,\n\t\tfields: EntityFieldMarkersContainer,\n\t): TreeFilter | undefined {\n\t\treturn {\n\t\t\tentity: entityName,\n\t\t\tid: id,\n\t\t\trelations: this.generateEntityRelations(fields),\n\t\t}\n\t}\n\n\tprivate generateEntityRelations(markers: EntityFieldMarkersContainer): RelationFilter[] {\n\t\tconst rawFilter = this.populateRawRelationFilters(markers)\n\t\treturn this.normalizeRawRelationFilters(rawFilter)\n\t}\n\n\tprivate normalizeRawRelationFilters(raw: RawRelationFilters): RelationFilter[] {\n\t\treturn Array.from(raw, ([fieldName, relations]) => ({\n\t\t\tname: fieldName,\n\t\t\trelations: this.normalizeRawRelationFilters(relations),\n\t\t}))\n\t}\n\n\tprivate populateRawRelationFilters(\n\t\tmarkers: EntityFieldMarkersContainer,\n\t\trelations: RawRelationFilters = new Map(),\n\t): RawRelationFilters {\n\t\tfor (const [, marker] of markers.markers) {\n\t\t\tif (!(marker instanceof HasOneRelationMarker) && !(marker instanceof HasManyRelationMarker)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (\n\t\t\t\tmarker.parameters.expectedMutation === 'none' ||\n\t\t\t\tmarker.parameters.expectedMutation === 'connectOrDisconnect'\n\t\t\t) {\n\t\t\t\tcontinue\n\t\t\t} else if (\n\t\t\t\tmarker.parameters.expectedMutation === 'anyMutation' ||\n\t\t\t\tmarker.parameters.expectedMutation === 'createOrDelete'\n\t\t\t) {\n\t\t\t} else {\n\t\t\t\treturn assertNever(marker.parameters.expectedMutation)\n\t\t\t}\n\t\t\tlet existingRelation = relations.get(marker.parameters.field)\n\n\t\t\tif (existingRelation === undefined) {\n\t\t\t\trelations.set(marker.parameters.field, (existingRelation = new Map()))\n\t\t\t}\n\t\t\tthis.populateRawRelationFilters(marker.fields, existingRelation)\n\t\t}\n\n\t\treturn relations\n\t}\n}\n"],"names":[],"mappings":";;;;;0BAgBiC;AAAA,EACzB,YAA6B,WAAsB;;;EAEnD,qBAAmC;UACnC,UAA0B;eAErB,CAAC,iBAAiB,UAAU,cAAc,cAAc,KAAK,YAAY;YAC7E,YAAY,MAAM;UAEpB,UAAU,UAAU,UAAU,OAAO,WAAW,qBAAqB,QAAQ;;;YAG3E,SAAS,UAAU;cACjB,KAAK,KAAK,sBAAsB,QAAQ,KAAK,UAAU,qBAAqB,IAAI;AAAA;WAElF,QAAQ;AAAA;AAAA,EAGR,sBACP,SACA,eACe;UACT,UAAwB;QAE1B,kBAAkB,QAAW;aACzB;AAAA;QAGJ,yBAAyB,qBAAqB;YAC3C,SAAS,KAAK,6BACnB,cAAc,OACd,QAAQ,WAAW,YACnB,QAAQ;gBAEC,QAAQ,KAAK;AAAA,WACjB;iBACK,MAAM,eAAe;cACzB,SAAS,KAAK,6BAA6B,IAAI,QAAQ,WAAW,YAAY,QAAQ;kBAClF,QAAQ,KAAK;AAAA;AAAA;WAIlB;AAAA;AAAA,EAGA,6BACP,IACA,YACA,QACyB;WAClB;AAAA,MACN,QAAQ;AAAA,MACR;AAAA,MACA,WAAW,KAAK,wBAAwB;AAAA;AAAA;AAAA,EAIlC,wBAAwB,SAAwD;UACjF,YAAY,KAAK,2BAA2B;WAC3C,KAAK,4BAA4B;AAAA;AAAA,EAGjC,4BAA4B,KAA2C;WACvE,MAAM,KAAK,KAAK,CAAC,CAAC,WAAW;MACnC,MAAM;AAAA,MACN,WAAW,KAAK,4BAA4B;AAAA;AAAA;AAAA,EAItC,2BACP,SACA,gCAAoC,OACf;eACV,GAAG,WAAW,QAAQ,SAAS;UACrC,oBAAoB,yBAAyB,oBAAoB,wBAAwB;;;UAI5F,OAAO,WAAW,qBAAqB,UACvC,OAAO,WAAW,qBAAqB,uBACtC;;iBAGD,OAAO,WAAW,qBAAqB,iBACvC,OAAO,WAAW,qBAAqB;AACtC;AAAA,WACK;eACC,YAAY,OAAO,WAAW;AAAA;UAElC,mBAAmB,UAAU,IAAI,OAAO,WAAW;UAEnD,qBAAqB,QAAW;kBACzB,IAAI,OAAO,WAAW,OAAQ,uCAAuB;AAAA;WAE3D,2BAA2B,OAAO,QAAQ;AAAA;WAGzC;AAAA;AAAA;;"}
1
+ {"version":3,"file":"TreeFilterGenerator.js","sources":["../../../src/core/TreeFilterGenerator.ts"],"sourcesContent":["import type { RelationFilter, TreeFilter } from '@contember/client'\nimport { EntityListPersistedData, ServerGeneratedUuid } from '../accessorTree'\nimport {\n\tEntityFieldMarkersContainer,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '../markers'\nimport type { EntityName } from '../treeParameters'\nimport { assertNever } from '../utils'\nimport { StateIterator } from './state'\nimport type { TreeStore } from './TreeStore'\n\ntype RawRelationFilters = Map<string, RawRelationFilters>\n\nexport class TreeFilterGenerator {\n\tpublic constructor(private readonly treeStore: TreeStore) {}\n\n\tpublic generateTreeFilter(): TreeFilter[] {\n\t\tconst filters: TreeFilter[][] = []\n\n\t\tfor (const [placeholderName, state] of StateIterator.eachRootState(this.treeStore)) {\n\t\t\tconst blueprint = state.blueprint\n\n\t\t\tif (blueprint.parent || blueprint.marker.parameters.expectedMutation === 'none') {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst marker = blueprint.marker\n\t\t\tfilters.push(this.generateSubTreeFilter(marker, this.treeStore.subTreePersistedData.get(placeholderName)))\n\t\t}\n\t\treturn filters.flat()\n\t}\n\n\tprivate generateSubTreeFilter(\n\t\tsubTree: EntitySubTreeMarker | EntityListSubTreeMarker,\n\t\tpersistedData: ServerGeneratedUuid | EntityListPersistedData | undefined,\n\t): TreeFilter[] {\n\t\tconst filters: TreeFilter[] = []\n\n\t\tif (persistedData === undefined) {\n\t\t\treturn filters // Do nothing\n\t\t}\n\n\t\tif (persistedData instanceof ServerGeneratedUuid) {\n\t\t\tconst filter = this.generateTopLevelEntityFilter(\n\t\t\t\tpersistedData.value,\n\t\t\t\tsubTree.parameters.entityName,\n\t\t\t\tsubTree.fields,\n\t\t\t)\n\t\t\tfilter && filters.push(filter)\n\t\t} else {\n\t\t\tfor (const id of persistedData) {\n\t\t\t\tconst filter = this.generateTopLevelEntityFilter(id, subTree.parameters.entityName, subTree.fields)\n\t\t\t\tfilter && filters.push(filter)\n\t\t\t}\n\t\t}\n\n\t\treturn filters\n\t}\n\n\tprivate generateTopLevelEntityFilter(\n\t\tid: string,\n\t\tentityName: EntityName,\n\t\tfields: EntityFieldMarkersContainer,\n\t): TreeFilter | undefined {\n\t\treturn {\n\t\t\tentity: entityName,\n\t\t\tid: id,\n\t\t\trelations: this.generateEntityRelations(fields),\n\t\t}\n\t}\n\n\tprivate generateEntityRelations(markers: EntityFieldMarkersContainer): RelationFilter[] {\n\t\tconst rawFilter = this.populateRawRelationFilters(markers)\n\t\treturn this.normalizeRawRelationFilters(rawFilter)\n\t}\n\n\tprivate normalizeRawRelationFilters(raw: RawRelationFilters): RelationFilter[] {\n\t\treturn Array.from(raw, ([fieldName, relations]) => ({\n\t\t\tname: fieldName,\n\t\t\trelations: this.normalizeRawRelationFilters(relations),\n\t\t}))\n\t}\n\n\tprivate populateRawRelationFilters(\n\t\tmarkers: EntityFieldMarkersContainer,\n\t\trelations: RawRelationFilters = new Map(),\n\t): RawRelationFilters {\n\t\tfor (const [, marker] of markers.markers) {\n\t\t\tif (!(marker instanceof HasOneRelationMarker) && !(marker instanceof HasManyRelationMarker)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (\n\t\t\t\tmarker.parameters.expectedMutation === 'none' ||\n\t\t\t\tmarker.parameters.expectedMutation === 'connectOrDisconnect'\n\t\t\t) {\n\t\t\t\tcontinue\n\t\t\t} else if (\n\t\t\t\tmarker.parameters.expectedMutation === 'anyMutation' ||\n\t\t\t\tmarker.parameters.expectedMutation === 'createOrDelete'\n\t\t\t) {\n\t\t\t} else {\n\t\t\t\treturn assertNever(marker.parameters.expectedMutation)\n\t\t\t}\n\t\t\tlet existingRelation = relations.get(marker.parameters.field)\n\n\t\t\tif (existingRelation === undefined) {\n\t\t\t\trelations.set(marker.parameters.field, (existingRelation = new Map()))\n\t\t\t}\n\t\t\tthis.populateRawRelationFilters(marker.fields, existingRelation)\n\t\t}\n\n\t\treturn relations\n\t}\n}\n"],"names":[],"mappings":";;;;;AAgBO,MAAM,oBAAoB;AAAA,EACzB,YAA6B,WAAsB;AAAtB,SAAA,YAAA;AAAA,EAAuB;AAAA,EAEpD,qBAAmC;AACzC,UAAM,UAA0B,CAAA;AAEhC,eAAW,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