@contember/binding 1.2.0-alpha.0 → 1.2.0-alpha.10

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 (179) hide show
  1. package/dist/development/accessorPropagation/AccessorProvider.js +10 -2
  2. package/dist/development/accessorPropagation/AccessorProvider.js.map +1 -1
  3. package/dist/development/accessorPropagation/BindingOperationsProvider.js +6 -2
  4. package/dist/development/accessorPropagation/BindingOperationsProvider.js.map +1 -1
  5. package/dist/development/accessorPropagation/EntityKeyProvider.js +6 -2
  6. package/dist/development/accessorPropagation/EntityKeyProvider.js.map +1 -1
  7. package/dist/development/accessorPropagation/EnvironmentContext.js +21 -5
  8. package/dist/development/accessorPropagation/EnvironmentContext.js.map +1 -1
  9. package/dist/development/accessorPropagation/TreeRootIdProvider.js +6 -2
  10. package/dist/development/accessorPropagation/TreeRootIdProvider.js.map +1 -1
  11. package/dist/development/accessorSorting/SetOrderFieldOnCreate.js +57 -13
  12. package/dist/development/accessorSorting/SetOrderFieldOnCreate.js.map +1 -1
  13. package/dist/development/accessorTree/AccessorTree.js +24 -4
  14. package/dist/development/accessorTree/AccessorTree.js.map +1 -1
  15. package/dist/development/accessors/EntityAccessor.js +66 -22
  16. package/dist/development/accessors/EntityAccessor.js.map +1 -1
  17. package/dist/development/core/EventManager.js +27 -1
  18. package/dist/development/core/EventManager.js.map +1 -1
  19. package/dist/development/core/operations/FieldOperations.js +21 -1
  20. package/dist/development/core/operations/FieldOperations.js.map +1 -1
  21. package/dist/development/coreComponents/DataBindingProvider.js +6 -2
  22. package/dist/development/coreComponents/DataBindingProvider.js.map +1 -1
  23. package/dist/development/coreComponents/Entity.js +6 -2
  24. package/dist/development/coreComponents/Entity.js.map +1 -1
  25. package/dist/development/coreComponents/EntityList.js +17 -5
  26. package/dist/development/coreComponents/EntityList.js.map +1 -1
  27. package/dist/development/coreComponents/EntityListSubTree.js +17 -5
  28. package/dist/development/coreComponents/EntityListSubTree.js.map +1 -1
  29. package/dist/development/coreComponents/EntitySubTree.js +33 -9
  30. package/dist/development/coreComponents/EntitySubTree.js.map +1 -1
  31. package/dist/development/coreComponents/Field.js +11 -3
  32. package/dist/development/coreComponents/Field.js.map +1 -1
  33. package/dist/development/coreComponents/HasMany.js +17 -5
  34. package/dist/development/coreComponents/HasMany.js.map +1 -1
  35. package/dist/development/coreComponents/HasOne.js +17 -5
  36. package/dist/development/coreComponents/HasOne.js.map +1 -1
  37. package/dist/development/coreComponents/ParentEntity.js +6 -2
  38. package/dist/development/coreComponents/ParentEntity.js.map +1 -1
  39. package/dist/development/dao/TreeNodeEnvironmentFactory.js +6 -76
  40. package/dist/development/dao/TreeNodeEnvironmentFactory.js.map +1 -1
  41. package/dist/development/helperComponents/DeferredSubTrees.js +22 -6
  42. package/dist/development/helperComponents/DeferredSubTrees.js.map +1 -1
  43. package/dist/development/helperComponents/EntityView.js +6 -2
  44. package/dist/development/helperComponents/EntityView.js.map +1 -1
  45. package/dist/development/helperComponents/FieldView.js +17 -5
  46. package/dist/development/helperComponents/FieldView.js.map +1 -1
  47. package/dist/development/helperComponents/If.js +40 -8
  48. package/dist/development/helperComponents/If.js.map +1 -1
  49. package/dist/development/helperComponents/StaticRender.js +6 -2
  50. package/dist/development/helperComponents/StaticRender.js.map +1 -1
  51. package/dist/development/helperComponents/SugaredField.js +11 -3
  52. package/dist/development/helperComponents/SugaredField.js.map +1 -1
  53. package/dist/development/helperComponents/helpers/evaluateCondition.js.map +1 -1
  54. package/dist/development/markers/HasManyRelationMarker.js +6 -0
  55. package/dist/development/markers/HasManyRelationMarker.js.map +1 -1
  56. package/dist/development/markers/PlaceholderGenerator.js +5 -4
  57. package/dist/development/markers/PlaceholderGenerator.js.map +1 -1
  58. package/dist/development/markers/PlaceholderParametersGenerator.js +62 -0
  59. package/dist/development/markers/PlaceholderParametersGenerator.js.map +1 -0
  60. package/dist/development/queryLanguage/Parser.js +3 -0
  61. package/dist/development/queryLanguage/Parser.js.map +1 -1
  62. package/dist/development/queryLanguage/QueryLanguage.js +3 -0
  63. package/dist/development/queryLanguage/QueryLanguage.js.map +1 -1
  64. package/dist/development/treeParameters/LeafField.js.map +1 -1
  65. package/dist/development/utils/Hashing.js +2 -56
  66. package/dist/development/utils/Hashing.js.map +1 -1
  67. package/dist/development/utils/TreeNodeUtils.js +77 -0
  68. package/dist/development/utils/TreeNodeUtils.js.map +1 -0
  69. package/dist/production/accessorTree/AccessorTree.js +2 -2
  70. package/dist/production/accessorTree/AccessorTree.js.map +1 -1
  71. package/dist/production/accessors/EntityAccessor.js +66 -22
  72. package/dist/production/accessors/EntityAccessor.js.map +1 -1
  73. package/dist/production/coreComponents/DataBindingProvider.js.map +1 -1
  74. package/dist/production/coreComponents/Entity.js.map +1 -1
  75. package/dist/production/coreComponents/EntityList.js.map +1 -1
  76. package/dist/production/coreComponents/EntityListSubTree.js.map +1 -1
  77. package/dist/production/coreComponents/EntitySubTree.js.map +1 -1
  78. package/dist/production/coreComponents/Field.js.map +1 -1
  79. package/dist/production/coreComponents/HasMany.js.map +1 -1
  80. package/dist/production/coreComponents/HasOne.js.map +1 -1
  81. package/dist/production/coreComponents/ParentEntity.js.map +1 -1
  82. package/dist/production/dao/TreeNodeEnvironmentFactory.js +6 -76
  83. package/dist/production/dao/TreeNodeEnvironmentFactory.js.map +1 -1
  84. package/dist/production/helperComponents/DeferredSubTrees.js.map +1 -1
  85. package/dist/production/helperComponents/EntityView.js.map +1 -1
  86. package/dist/production/helperComponents/FieldView.js.map +1 -1
  87. package/dist/production/helperComponents/If.js.map +1 -1
  88. package/dist/production/helperComponents/StaticRender.js.map +1 -1
  89. package/dist/production/helperComponents/SugaredField.js.map +1 -1
  90. package/dist/production/helperComponents/helpers/evaluateCondition.js.map +1 -1
  91. package/dist/production/markers/PlaceholderGenerator.js +5 -4
  92. package/dist/production/markers/PlaceholderGenerator.js.map +1 -1
  93. package/dist/production/markers/PlaceholderParametersGenerator.js +62 -0
  94. package/dist/production/markers/PlaceholderParametersGenerator.js.map +1 -0
  95. package/dist/production/treeParameters/LeafField.js.map +1 -1
  96. package/dist/production/utils/Hashing.js +2 -56
  97. package/dist/production/utils/Hashing.js.map +1 -1
  98. package/dist/production/utils/TreeNodeUtils.js +77 -0
  99. package/dist/production/utils/TreeNodeUtils.js.map +1 -0
  100. package/dist/types/accessorTree/AccessorTree.d.ts +7 -4
  101. package/dist/types/accessorTree/AccessorTree.d.ts.map +1 -1
  102. package/dist/types/accessors/EntityAccessor.d.ts +9 -4
  103. package/dist/types/accessors/EntityAccessor.d.ts.map +1 -1
  104. package/dist/types/coreComponents/DataBindingProvider.d.ts +10 -0
  105. package/dist/types/coreComponents/DataBindingProvider.d.ts.map +1 -1
  106. package/dist/types/coreComponents/Entity.d.ts +3 -0
  107. package/dist/types/coreComponents/Entity.d.ts.map +1 -1
  108. package/dist/types/coreComponents/EntityList.d.ts +3 -0
  109. package/dist/types/coreComponents/EntityList.d.ts.map +1 -1
  110. package/dist/types/coreComponents/EntityListSubTree.d.ts +10 -0
  111. package/dist/types/coreComponents/EntityListSubTree.d.ts.map +1 -1
  112. package/dist/types/coreComponents/EntitySubTree.d.ts +10 -0
  113. package/dist/types/coreComponents/EntitySubTree.d.ts.map +1 -1
  114. package/dist/types/coreComponents/Field.d.ts +3 -0
  115. package/dist/types/coreComponents/Field.d.ts.map +1 -1
  116. package/dist/types/coreComponents/HasMany.d.ts +3 -0
  117. package/dist/types/coreComponents/HasMany.d.ts.map +1 -1
  118. package/dist/types/coreComponents/HasOne.d.ts +3 -0
  119. package/dist/types/coreComponents/HasOne.d.ts.map +1 -1
  120. package/dist/types/coreComponents/ParentEntity.d.ts +3 -0
  121. package/dist/types/coreComponents/ParentEntity.d.ts.map +1 -1
  122. package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts +0 -8
  123. package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts.map +1 -1
  124. package/dist/types/helperComponents/DeferredSubTrees.d.ts +3 -0
  125. package/dist/types/helperComponents/DeferredSubTrees.d.ts.map +1 -1
  126. package/dist/types/helperComponents/EntityView.d.ts +3 -0
  127. package/dist/types/helperComponents/EntityView.d.ts.map +1 -1
  128. package/dist/types/helperComponents/FieldView.d.ts +13 -0
  129. package/dist/types/helperComponents/FieldView.d.ts.map +1 -1
  130. package/dist/types/helperComponents/If.d.ts +3 -0
  131. package/dist/types/helperComponents/If.d.ts.map +1 -1
  132. package/dist/types/helperComponents/StaticRender.d.ts +3 -0
  133. package/dist/types/helperComponents/StaticRender.d.ts.map +1 -1
  134. package/dist/types/helperComponents/SugaredField.d.ts +3 -0
  135. package/dist/types/helperComponents/SugaredField.d.ts.map +1 -1
  136. package/dist/types/markers/PlaceholderGenerator.d.ts.map +1 -1
  137. package/dist/types/markers/PlaceholderParametersGenerator.d.ts +9 -0
  138. package/dist/types/markers/PlaceholderParametersGenerator.d.ts.map +1 -0
  139. package/dist/types/treeParameters/FieldEventListeners.d.ts +5 -2
  140. package/dist/types/treeParameters/FieldEventListeners.d.ts.map +1 -1
  141. package/dist/types/treeParameters/LeafField.d.ts +2 -2
  142. package/dist/types/treeParameters/LeafField.d.ts.map +1 -1
  143. package/dist/types/treeParameters/QualifiedFieldList.d.ts +7 -8
  144. package/dist/types/treeParameters/QualifiedFieldList.d.ts.map +1 -1
  145. package/dist/types/treeParameters/RelativeSingleField.d.ts +8 -8
  146. package/dist/types/treeParameters/RelativeSingleField.d.ts.map +1 -1
  147. package/dist/types/tsconfig.tsbuildinfo +1 -1
  148. package/dist/types/utils/Hashing.d.ts +1 -7
  149. package/dist/types/utils/Hashing.d.ts.map +1 -1
  150. package/dist/types/utils/TreeNodeUtils.d.ts +13 -0
  151. package/dist/types/utils/TreeNodeUtils.d.ts.map +1 -0
  152. package/package.json +6 -6
  153. package/src/accessorTree/AccessorTree.tsx +4 -1
  154. package/src/accessors/EntityAccessor.ts +96 -28
  155. package/src/coreComponents/DataBindingProvider.tsx +10 -0
  156. package/src/coreComponents/Entity.tsx +3 -0
  157. package/src/coreComponents/EntityList.tsx +3 -0
  158. package/src/coreComponents/EntityListSubTree.tsx +10 -0
  159. package/src/coreComponents/EntitySubTree.tsx +10 -0
  160. package/src/coreComponents/Field.tsx +3 -0
  161. package/src/coreComponents/HasMany.tsx +3 -0
  162. package/src/coreComponents/HasOne.tsx +3 -0
  163. package/src/coreComponents/ParentEntity.tsx +3 -0
  164. package/src/dao/TreeNodeEnvironmentFactory.ts +6 -92
  165. package/src/helperComponents/DeferredSubTrees.tsx +3 -0
  166. package/src/helperComponents/EntityView.tsx +3 -0
  167. package/src/helperComponents/FieldView.tsx +13 -0
  168. package/src/helperComponents/If.tsx +3 -0
  169. package/src/helperComponents/StaticRender.tsx +3 -0
  170. package/src/helperComponents/SugaredField.tsx +3 -0
  171. package/src/helperComponents/helpers/evaluateCondition.ts +1 -1
  172. package/src/markers/PlaceholderGenerator.ts +6 -5
  173. package/src/markers/PlaceholderParametersGenerator.ts +79 -0
  174. package/src/treeParameters/FieldEventListeners.ts +15 -5
  175. package/src/treeParameters/LeafField.ts +6 -4
  176. package/src/treeParameters/QualifiedFieldList.ts +24 -17
  177. package/src/treeParameters/RelativeSingleField.ts +19 -12
  178. package/src/utils/Hashing.ts +2 -68
  179. package/src/utils/TreeNodeUtils.ts +92 -0
@@ -1,11 +1,5 @@
1
- import type { HasManyRelation, HasOneRelation, QualifiedEntityList, QualifiedSingleEntity, UnconstrainedQualifiedEntityList, UnconstrainedQualifiedSingleEntity } from '../treeParameters';
2
- import { Environment } from '../dao';
3
1
  export declare class Hashing {
4
- static hashHasOneRelation(relation: HasOneRelation): number;
5
- static hashHasManyRelation(relation: HasManyRelation): number;
6
- static hashEntityListSubTreeParameters(parameters: QualifiedEntityList | UnconstrainedQualifiedEntityList, environment: Environment): number;
7
- static hashEntitySubTreeParameters(parameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity, environment: Environment): number;
8
- private static hashArray;
2
+ static hashAny(array: any[]): number;
9
3
  static hash(str: string): number;
10
4
  }
11
5
  //# sourceMappingURL=Hashing.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Hashing.d.ts","sourceRoot":"","sources":["../../../src/utils/Hashing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,qBAAqB,EACrB,gCAAgC,EAChC,kCAAkC,EAClC,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAGpC,qBAAa,OAAO;WACL,kBAAkB,CAAC,QAAQ,EAAE,cAAc,GAAG,MAAM;WAWpD,mBAAmB,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM;WAatD,+BAA+B,CAC5C,UAAU,EAAE,mBAAmB,GAAG,gCAAgC,EAClE,WAAW,EAAE,WAAW,GACtB,MAAM;WAmBK,2BAA2B,CACxC,UAAU,EAAE,qBAAqB,GAAG,kCAAkC,EACtE,WAAW,EAAE,WAAW,GACtB,MAAM;IAiBT,OAAO,CAAC,MAAM,CAAC,SAAS;WAMV,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;CAWvC"}
1
+ {"version":3,"file":"Hashing.d.ts","sourceRoot":"","sources":["../../../src/utils/Hashing.ts"],"names":[],"mappings":"AAWA,qBAAa,OAAO;WACL,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,MAAM;WAM7B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;CAWvC"}
@@ -0,0 +1,13 @@
1
+ import { Environment } from '../dao';
2
+ import { Schema, SchemaColumn, SchemaEntity, SchemaRelation } from '../core';
3
+ export declare class TreeNodeUtils {
4
+ static resolveEntity(schema: Schema, entityName: string, type: 'entity' | 'entity list'): SchemaEntity;
5
+ static resolveHasOneRelation(environment: Environment, field: string, isReduced: boolean): SchemaRelation;
6
+ static resolveHasManyRelation(environment: Environment, field: string): SchemaRelation;
7
+ private static resolveRelation;
8
+ static resolveColumn(environment: Environment, fieldName: string): SchemaColumn;
9
+ private static resolveField;
10
+ static describeLocation(environment: Environment): string;
11
+ static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined;
12
+ }
13
+ //# sourceMappingURL=TreeNodeUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TreeNodeUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/TreeNodeUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AACpC,OAAO,EAAgB,MAAM,EAAE,YAAY,EAAE,YAAY,EAAe,cAAc,EAAE,MAAM,SAAS,CAAA;AAIvG,qBAAa,aAAa;WAEX,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,aAAa,GAAG,YAAY;WAU/F,qBAAqB,CAAC,WAAW,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,GAAG,cAAc;WAQlG,sBAAsB,CAAC,WAAW,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc;IAI7F,OAAO,CAAC,MAAM,CAAC,eAAe;WAMhB,aAAa,CAAC,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,YAAY;IAMtF,OAAO,CAAC,MAAM,CAAC,YAAY;WAwBb,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM;WAYlD,oBAAoB,CAAC,QAAQ,EAAE,MAAM,EAAE,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,SAAS;CAchH"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contember/binding",
3
3
  "license": "Apache-2.0",
4
- "version": "1.2.0-alpha.0",
4
+ "version": "1.2.0-alpha.10",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "./dist/production/index.js",
@@ -21,17 +21,17 @@
21
21
  "typings": "./dist/types/index.d.ts",
22
22
  "scripts": {
23
23
  "build": "yarn build:js:dev && yarn build:js:prod",
24
- "build:js:dev": "vite build --mode development",
24
+ "build:js:dev": "NODE_ENV=development vite build --mode development",
25
25
  "build:js:prod": "vite build --mode production",
26
26
  "ae:build": "api-extractor run --local",
27
27
  "ae:test": "api-extractor run",
28
28
  "test": "vitest"
29
29
  },
30
30
  "dependencies": {
31
- "@contember/client": "1.2.0-alpha.0",
32
- "@contember/react-client": "1.2.0-alpha.0",
33
- "@contember/react-multipass-rendering": "1.2.0-alpha.0",
34
- "@contember/react-utils": "1.2.0-alpha.0",
31
+ "@contember/client": "1.2.0-alpha.10",
32
+ "@contember/react-client": "1.2.0-alpha.10",
33
+ "@contember/react-multipass-rendering": "1.2.0-alpha.10",
34
+ "@contember/react-utils": "1.2.0-alpha.10",
35
35
  "chevrotain": "6.5.0",
36
36
  "fast-deep-equal": "^3.1.3",
37
37
  "js-levenshtein": "1.1.6",
@@ -10,7 +10,10 @@ export interface AccessorTreeProps {
10
10
  children: ReactNode
11
11
  }
12
12
 
13
- export function AccessorTree({ state, children }: AccessorTreeProps) {
13
+ /**
14
+ * @group Data binding
15
+ */
16
+ export const AccessorTree = ({ state, children }: AccessorTreeProps) => {
14
17
  const stateData = state.name === 'initialized' ? state.data : {
15
18
  hasUnpersistedChanges: false,
16
19
  isMutating: false,
@@ -2,7 +2,7 @@ import { RuntimeId, SingleEntityPersistedData } from '../accessorTree'
2
2
  import { BindingError } from '../BindingError'
3
3
  import type { EntityOperations } from '../core/operations'
4
4
  import type { Environment } from '../dao'
5
- import { PlaceholderGenerator } from '../markers'
5
+ import { EntityFieldMarkers, HasManyRelationMarker, HasOneRelationMarker, PlaceholderGenerator } from '../markers'
6
6
  import { QueryLanguage } from '../queryLanguage'
7
7
  import type {
8
8
  EntityId,
@@ -10,6 +10,8 @@ import type {
10
10
  EntityRealmKey,
11
11
  FieldName,
12
12
  FieldValue,
13
+ HasManyRelation,
14
+ HasOneRelation,
13
15
  RelativeEntityList,
14
16
  RelativeSingleEntity,
15
17
  RelativeSingleField,
@@ -19,7 +21,7 @@ import type {
19
21
  } from '../treeParameters'
20
22
  import type { AsyncBatchUpdatesOptions } from './AsyncBatchUpdatesOptions'
21
23
  import type { BatchUpdatesOptions } from './BatchUpdatesOptions'
22
- import type { EntityListAccessor } from './EntityListAccessor'
24
+ import { EntityListAccessor } from './EntityListAccessor'
23
25
  import type { Errorable } from './Errorable'
24
26
  import { ErrorAccessor } from './ErrorAccessor'
25
27
  import type { FieldAccessor } from './FieldAccessor'
@@ -27,6 +29,8 @@ import type { PersistErrorOptions } from './PersistErrorOptions'
27
29
  import type { PersistSuccessOptions } from './PersistSuccessOptions'
28
30
  import type { EntityRealmState } from '../core/state'
29
31
  import { getEntityMarker } from '../core/state'
32
+ import { TreeNodeUtils } from '../utils/TreeNodeUtils'
33
+ import { PlaceholderParametersGenerator } from '../markers/PlaceholderParametersGenerator'
30
34
 
31
35
  class EntityAccessor implements Errorable {
32
36
  public constructor(
@@ -41,7 +45,8 @@ class EntityAccessor implements Errorable {
41
45
  public readonly errors: ErrorAccessor | undefined,
42
46
  public readonly environment: Environment,
43
47
  public readonly getAccessor: EntityAccessor.GetEntityAccessor,
44
- ) {}
48
+ ) {
49
+ }
45
50
 
46
51
  public get idOnServer(): EntityId | undefined {
47
52
  return this.runtimeId.existsOnServer ? this.runtimeId.value : undefined
@@ -123,26 +128,104 @@ class EntityAccessor implements Errorable {
123
128
  * @see EntityAccessor.getField
124
129
  */
125
130
  public getRelativeSingleField<Value extends FieldValue = FieldValue>(field: RelativeSingleField): FieldAccessor<Value> {
126
- return this.getRelativeSingleEntity(field).getAccessorByPlaceholder(
127
- PlaceholderGenerator.getFieldPlaceholder(field.field),
128
- ) as unknown as FieldAccessor<Value>
131
+ const entity = this.getRelativeSingleEntity(field)
132
+ const fieldPlaceholder = PlaceholderGenerator.getFieldPlaceholder(field.field)
133
+ const accessor = entity.fieldData.get(fieldPlaceholder)
134
+ if (accessor) {
135
+ return accessor.getAccessor() as FieldAccessor<Value>
136
+ }
137
+
138
+ TreeNodeUtils.resolveColumn(entity.environment, field.field)
139
+
140
+ throw new BindingError(''
141
+ + 'EntityAccessor: cannot access field '
142
+ + `'${field.field}' on ${TreeNodeUtils.describeLocation(entity.environment)}.\n`
143
+ + 'The cause of the error is that this relation has not been registered during the static rendering process. As a result, it lacks a required marker and accessor.',
144
+ )
129
145
  }
130
146
 
131
147
  public getRelativeSingleEntity(relativeSingleEntity: RelativeSingleEntity): EntityAccessor {
132
148
  let relativeTo: EntityAccessor = this
133
149
 
134
150
  for (const hasOneRelation of relativeSingleEntity.hasOneRelationPath) {
135
- relativeTo = relativeTo.getAccessorByPlaceholder(
136
- PlaceholderGenerator.getHasOneRelationPlaceholder(hasOneRelation),
137
- ) as EntityAccessor
151
+ const fieldPlaceholder = PlaceholderGenerator.getHasOneRelationPlaceholder(hasOneRelation)
152
+ const accessor = relativeTo.fieldData.get(fieldPlaceholder)
153
+ if (!accessor) {
154
+ return this.raiseInvalidHasOneRelationError(relativeTo, hasOneRelation)
155
+ }
156
+
157
+ relativeTo = accessor.getAccessor() as EntityAccessor
138
158
  }
139
159
  return relativeTo
140
160
  }
141
161
 
142
162
  public getRelativeEntityList(entityList: RelativeEntityList): EntityListAccessor {
143
- return this.getRelativeSingleEntity(entityList).getAccessorByPlaceholder(
144
- PlaceholderGenerator.getHasManyRelationPlaceholder(entityList.hasManyRelation),
145
- ) as EntityListAccessor
163
+ const entity = this.getRelativeSingleEntity(entityList)
164
+ const fieldPlaceholder = PlaceholderGenerator.getHasManyRelationPlaceholder(entityList.hasManyRelation)
165
+ const accessor = entity.fieldData.get(fieldPlaceholder)
166
+ if (!accessor) {
167
+ return this.raiseInvalidHasManyRelationError(entity, entityList.hasManyRelation)
168
+ }
169
+
170
+ return accessor.getAccessor() as EntityListAccessor
171
+ }
172
+
173
+ private raiseInvalidHasOneRelationError(relativeTo: EntityAccessor, hasOneRelation: HasOneRelation): never {
174
+ TreeNodeUtils.resolveHasOneRelation(relativeTo.environment, hasOneRelation.field, !!hasOneRelation.reducedBy)
175
+
176
+ const entityMarker = relativeTo.getMarker()
177
+ const possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasOneRelationMarker, hasOneRelation.field)
178
+ const markerParams = possibleMarkers.map(it => PlaceholderParametersGenerator.createHasOneRelationParameters(it.parameters))
179
+
180
+ const relationParams = PlaceholderParametersGenerator.createHasOneRelationParameters(hasOneRelation)
181
+
182
+ this.raiseUndefinedRelationError(hasOneRelation.field, relativeTo.environment, relationParams, markerParams)
183
+ }
184
+
185
+ private raiseInvalidHasManyRelationError(entity: EntityAccessor, hasManyRelation: HasManyRelation): never {
186
+ TreeNodeUtils.resolveHasManyRelation(entity.environment, hasManyRelation.field)
187
+
188
+ const entityMarker = entity.getMarker()
189
+ const possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasManyRelationMarker, hasManyRelation.field)
190
+ const markerParams = possibleMarkers.map(it => PlaceholderParametersGenerator.createHasManyRelationParameters(it.parameters))
191
+
192
+ const relationParams = PlaceholderParametersGenerator.createHasManyRelationParameters(hasManyRelation)
193
+
194
+ this.raiseUndefinedRelationError(hasManyRelation.field, entity.environment, relationParams, markerParams)
195
+ }
196
+
197
+ private raiseUndefinedRelationError(field: string, environment: Environment, relationParams: any, otherMarkersParams: any[]): never {
198
+ const hint = this.createHintMessage(otherMarkersParams)
199
+ throw new BindingError(''
200
+ + `EntityAccessor: cannot access relation `
201
+ + `'${field}' on ${TreeNodeUtils.describeLocation(environment)}.\n`
202
+ + 'The cause of the error is that this relation has not been registered during the static rendering process. As a result, it lacks a required marker and accessor.\n\n'
203
+ + 'Provided parameters:\n'
204
+ + JSON.stringify(relationParams)
205
+ + hint,
206
+ )
207
+ }
208
+
209
+ private createHintMessage(params: any[]): string {
210
+ if (params.length === 0) {
211
+ return ''
212
+ }
213
+
214
+ const serializedParams = params.map(it => JSON.stringify(it)).join('\n')
215
+ const message = params.length === 1
216
+ ? 'However, there is existing marker for the same field, but it have different parameters:'
217
+ : 'However, there are existing markers for the same field, but they have different parameters:'
218
+
219
+ return `\n\n${message}\n${serializedParams}`
220
+ }
221
+
222
+ private getMarkersOfType<T extends { new(...args: any[]): HasManyRelationMarker | HasOneRelationMarker }>(
223
+ markers: EntityFieldMarkers,
224
+ markerConstructor: T,
225
+ fieldName: string,
226
+ ): InstanceType<T>[] {
227
+ return Array.from(markers.values())
228
+ .filter((it): it is InstanceType<T> => (it instanceof markerConstructor) && it.parameters.field === fieldName)
146
229
  }
147
230
 
148
231
  public getParent(): EntityAccessor | EntityListAccessor | undefined {
@@ -156,28 +239,13 @@ class EntityAccessor implements Errorable {
156
239
  public getMarker() {
157
240
  return getEntityMarker(this.state)
158
241
  }
159
-
160
- private getAccessorByPlaceholder(placeholderName: FieldName): EntityAccessor.NestedAccessor {
161
- const record = this.fieldData.get(placeholderName)
162
- if (record === undefined) {
163
- throw new BindingError(
164
- `EntityAccessor: unknown field placeholder '${placeholderName} on ${this.name}'. Unless this is just a typo, this is ` +
165
- `typically caused by one of the following:\n` +
166
- `\t• Trying to access a field that has not been registered during static render, and thus lacks a marker and an accessor.\n` +
167
- `\t• Misusing an EntityAccessor getter. If you used one of the getRelative[…] family, please make sure all ` +
168
- `parameters match the marker tree exactly.` +
169
- `\n\nFor more information, please consult the documentation.\n\n`,
170
- )
171
- }
172
- return record.getAccessor()
173
- }
174
242
  }
175
243
 
176
244
  namespace EntityAccessor {
177
245
  export interface FieldDatum {
178
246
  getAccessor(): NestedAccessor
179
247
  }
180
- export type NestedAccessor = EntityAccessor | EntityListAccessor | FieldAccessor
248
+ export type NestedAccessor = EntityAccessor | EntityListAccessor | FieldAccessor<any>
181
249
 
182
250
  export type FieldValuePairs =
183
251
  | {
@@ -18,6 +18,16 @@ export type DataBindingProviderProps<StateProps> = DataBindingProviderBaseProps
18
18
  }
19
19
  )
20
20
 
21
+ /**
22
+ * The `DataBindingProvider` is a root component for all other data binding related components.
23
+ *
24
+ * @example
25
+ * ```
26
+ * <DataBindingProvider stateComponent={FeedbackRenderer} />
27
+ * ```
28
+ *
29
+ * @group Data binding
30
+ */
21
31
  export const DataBindingProvider = memo(function DataBindingProvider<StateProps extends DataBindingStateComponentProps>(
22
32
  props: DataBindingProviderProps<StateProps>,
23
33
  ) {
@@ -10,6 +10,9 @@ export interface EntityBaseProps {
10
10
 
11
11
  export type EntityProps = EntityBaseProps
12
12
 
13
+ /**
14
+ * @group Data binding
15
+ */
13
16
  export const Entity = Component(
14
17
  ({ children, accessor }: EntityProps) => {
15
18
  return (
@@ -17,6 +17,9 @@ export type EntityListProps<ListProps> = EntityListBaseProps &
17
17
  }
18
18
  )
19
19
 
20
+ /**
21
+ * @group Data binding
22
+ */
20
23
  export const EntityList = Component(
21
24
  <ListProps extends {}>(props: EntityListProps<ListProps>) => {
22
25
  if ('listComponent' in props && props.listComponent) {
@@ -31,6 +31,16 @@ export type EntityListSubTreeProps<ListProps, EntityProps> = {
31
31
  }
32
32
  )
33
33
 
34
+ /**
35
+ * Creates a subtree with list of entities in current data binding context.
36
+ *
37
+ * @example
38
+ * ```
39
+ * <EntityListSubTree entities="Post" />
40
+ * ```
41
+ *
42
+ * @group Data binding
43
+ */
34
44
  export const EntityListSubTree = Component(
35
45
  <ListProps, EntityProps>(props: EntityListSubTreeProps<ListProps, EntityProps>) => {
36
46
  useConstantValueInvariant(props.isCreating, 'EntityListSubTree: cannot update isCreating')
@@ -32,6 +32,16 @@ export type EntitySubTreeProps<EntityProps> =
32
32
  | (SugaredUnconstrainedQualifiedSingleEntity & EntitySubTreeAdditionalCreationProps)
33
33
  )
34
34
 
35
+ /**
36
+ * Creates a single entity subtree in current data binding context.
37
+ *
38
+ * @example
39
+ * ```
40
+ * <EntitySubTree entity="Post(id = $id)" />
41
+ * ```
42
+ *
43
+ * @group Data binding
44
+ */
35
45
  export const EntitySubTree = Component(
36
46
  <EntityProps extends {}>(props: EntitySubTreeProps<EntityProps>) => {
37
47
  useConstantValueInvariant(props.isCreating, 'EntitySubTree: cannot update isCreating')
@@ -16,6 +16,9 @@ export interface FieldProps<Persisted extends FieldValue = FieldValue>
16
16
  extends FieldBasicProps,
17
17
  FieldRuntimeProps<Persisted> {}
18
18
 
19
+ /**
20
+ * @group Data binding
21
+ */
19
22
  export const Field = Component(
20
23
  <Persisted extends FieldValue = FieldValue>(props: FieldProps<Persisted>) => {
21
24
  const field = useField<Persisted>(props)
@@ -20,6 +20,9 @@ export type HasManyProps<ListProps = never, EntityProps = never> = SugaredRelati
20
20
  }
21
21
  )
22
22
 
23
+ /**
24
+ * @group Data binding
25
+ */
23
26
  export const HasMany = Component(
24
27
  <ListProps, EntityProps>(props: HasManyProps<ListProps, EntityProps>) => {
25
28
  const entityList = useEntityList(props)
@@ -14,6 +14,9 @@ export type HasOneProps<EntityProps = never> = SugaredRelativeSingleEntity & {
14
14
  variables?: Environment.ValuesMapWithFactory
15
15
  }
16
16
 
17
+ /**
18
+ * @group Data binding
19
+ */
17
20
  export const HasOne = Component(
18
21
  <EntityProps extends {}>(props: HasOneProps<EntityProps>) => {
19
22
  const entity = useEntity(props)
@@ -7,6 +7,9 @@ export interface ParentEntityProps extends SugaredParentEntityParameters {
7
7
  children?: ReactNode
8
8
  }
9
9
 
10
+ /**
11
+ * @group Data binding
12
+ */
10
13
  export const ParentEntity = Component<ParentEntityProps>(
11
14
  props => <>{props.children}</>,
12
15
  {
@@ -10,10 +10,8 @@ import {
10
10
  SugaredUnconstrainedQualifiedSingleEntity,
11
11
  } from '../treeParameters'
12
12
  import { QueryLanguage } from '../queryLanguage'
13
- import { BindingError } from '../BindingError'
14
13
  import { whereToFilter } from '@contember/client'
15
- import { BaseRelation, Schema, SchemaColumn, SchemaEntity, SchemaField, SchemaRelation } from '../core/schema'
16
- import levenshtein from 'js-levenshtein'
14
+ import { TreeNodeUtils } from '../utils/TreeNodeUtils'
17
15
 
18
16
  export class TreeNodeEnvironmentFactory {
19
17
 
@@ -36,7 +34,7 @@ export class TreeNodeEnvironmentFactory {
36
34
  expectedCardinality = 'zero-to-many'
37
35
  }
38
36
 
39
- const entitySchema = this.resolveEntity(environment.getSchema(), entityList.entityName, 'entity list')
37
+ const entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entityList.entityName, 'entity list')
40
38
 
41
39
  return environment.withSubTree({
42
40
  type: 'subtree-entity-list',
@@ -65,7 +63,7 @@ export class TreeNodeEnvironmentFactory {
65
63
  expectedCardinality = entity.setOnCreate ? 'zero-or-one' : 'one'
66
64
  }
67
65
 
68
- const entitySchema = this.resolveEntity(environment.getSchema(), entity.entityName, 'entity')
66
+ const entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entity.entityName, 'entity')
69
67
 
70
68
  return environment.withSubTree({
71
69
  type: 'subtree-entity',
@@ -83,7 +81,7 @@ export class TreeNodeEnvironmentFactory {
83
81
  const hasOneEnvironment = this.traverseHasOnePath(environment, relativeEntityList.hasOneRelationPath)
84
82
  const hasManyField = relativeEntityList.hasManyRelation.field
85
83
 
86
- const field = this.assertHasManyRelation(hasOneEnvironment, hasManyField)
84
+ const field = TreeNodeUtils.resolveHasManyRelation(hasOneEnvironment, hasManyField)
87
85
 
88
86
  const targetEntity = environment.getSchema().getEntity(field.targetEntity)
89
87
 
@@ -110,7 +108,7 @@ export class TreeNodeEnvironmentFactory {
110
108
  const relativeSingleField = QueryLanguage.desugarRelativeSingleField(sugaredRelativeSingleField, environment)
111
109
  const hasOneEnvironment = this.traverseHasOnePath(environment, relativeSingleField.hasOneRelationPath)
112
110
 
113
- const field = this.resolveColumn(hasOneEnvironment, relativeSingleField.field)
111
+ const field = TreeNodeUtils.resolveColumn(hasOneEnvironment, relativeSingleField.field)
114
112
 
115
113
  return hasOneEnvironment.withSubTreeChild({
116
114
  type: 'column',
@@ -124,7 +122,7 @@ export class TreeNodeEnvironmentFactory {
124
122
  hasOneRelationPath: HasOneRelation[],
125
123
  ): Environment {
126
124
  for (const pathItem of hasOneRelationPath) {
127
- const field = this.resolveHasOneRelation(environment, pathItem.field, !!pathItem.reducedBy)
125
+ const field = TreeNodeUtils.resolveHasOneRelation(environment, pathItem.field, !!pathItem.reducedBy)
128
126
  const targetEntity = environment.getSchema().getEntity(field.targetEntity)
129
127
 
130
128
  environment = environment.withSubTreeChild({
@@ -136,88 +134,4 @@ export class TreeNodeEnvironmentFactory {
136
134
  return environment
137
135
  }
138
136
 
139
- private static resolveHasOneRelation(environment: Environment, field: string, isReduced: boolean): SchemaRelation {
140
- if (isReduced) {
141
- return this.resolveRelation(environment, field, 'reduced has-one relation', ['ManyHasMany', 'OneHasMany'])
142
- }
143
- return this.resolveRelation(environment, field, 'a has-one relation', ['ManyHasOne', 'OneHasOne'])
144
- }
145
-
146
- private static assertHasManyRelation(environment: Environment, field: string): SchemaRelation {
147
- return this.resolveRelation(environment, field, 'a has-many relation', ['ManyHasMany', 'OneHasMany'])
148
- }
149
-
150
-
151
- private static resolveRelation(environment: Environment, field: string, relationDescription: string, expectedRelationType: BaseRelation['type'][]): SchemaRelation {
152
- return this.resolveField(environment, field, (field): field is SchemaRelation => {
153
- return field.__typename === '_Relation' && expectedRelationType.includes(field.type)
154
- }, relationDescription)
155
- }
156
-
157
- private static resolveEntity(schema: Schema, entityName: string, type: 'entity' | 'entity list'): SchemaEntity {
158
- const entity = schema.getEntityOrUndefined(entityName)
159
- if (!entity) {
160
- const alternative = this.recommendAlternative(entityName, schema.getEntityNames())
161
- const didYouMean = alternative ? `Did you mean '${alternative}'?` : ''
162
- throw new BindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${didYouMean}`)
163
- }
164
- return entity
165
- }
166
-
167
- private static resolveColumn(environment: Environment, fieldName: string): SchemaColumn {
168
- return this.resolveField(environment, fieldName, (field): field is SchemaColumn => field.__typename === '_Column', 'an ordinary field')
169
- // TODO check that defaultValue matches the type
170
- // TODO run custom validators
171
- }
172
-
173
- private static resolveField<T extends SchemaField>(environment: Environment, fieldName: string, matcher: (field: SchemaField) => field is T, expectedDescription: string): T {
174
- const treeLocation = environment.getSubTreeNode()
175
- const entity = treeLocation.entity
176
- const field = entity.fields.get(fieldName)
177
- if (!field) {
178
- const alternative = this.recommendAlternative(
179
- fieldName,
180
- Array.from(entity.fields)
181
- .filter(([, field]) => matcher(field))
182
- .map(([fieldName]) => fieldName),
183
- )
184
- const didYouMean = alternative ? `Did you mean '${alternative}'?` : ''
185
- throw new BindingError(`Field '${fieldName}' doesn't exist on ${this.describeLocation(environment)}. ${didYouMean}`)
186
- }
187
- if (!matcher(field)) {
188
- const actual = field.__typename === '_Column' ? 'an ordinary field' : `a ${field.type} relation`
189
- throw new BindingError(
190
- `Invalid field: the name '${field.name}' on ${this.describeLocation(environment)} ` +
191
- `refers to ${actual} but is being used as a ${expectedDescription}.`,
192
- )
193
- }
194
- return field
195
- }
196
-
197
- private static describeLocation(environment: Environment): string {
198
- const path = []
199
- const loc = environment.getSubTreeNode()
200
- for (let env = environment;; env = env.getParent()) {
201
- const node = env.getSubTreeNode()
202
- if (node.type === 'subtree-entity' || node.type === 'subtree-entity-list') {
203
- return `entity '${loc.entity.name}' in path '${[node.entity.name, ...path.reverse()].join('.')}'`
204
- }
205
- path.push(node.field.name)
206
- }
207
- }
208
-
209
- public static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined {
210
- let bestAlternative: string | undefined = undefined
211
- let bestAlternativeDistance = Number.MAX_SAFE_INTEGER
212
-
213
- for (const alternative of possibleAlternatives) {
214
- const distance = levenshtein(original, alternative)
215
-
216
- if (distance < bestAlternativeDistance) {
217
- bestAlternative = alternative
218
- bestAlternativeDistance = distance
219
- }
220
- }
221
- return bestAlternative
222
- }
223
137
  }
@@ -22,6 +22,9 @@ type LoadState =
22
22
  treeRootId: TreeRootId | undefined
23
23
  }
24
24
 
25
+ /**
26
+ * @group Data binding
27
+ */
25
28
  export const DeferredSubTrees = Component<DeferredSubTreesProps>(
26
29
  props => {
27
30
  const { extendTree } = useBindingOperations()
@@ -8,6 +8,9 @@ export interface EntityViewProps {
8
8
  field?: string | SugaredRelativeSingleEntity
9
9
  }
10
10
 
11
+ /**
12
+ * @group Data binding
13
+ */
11
14
  export function EntityView(props: EntityViewProps) {
12
15
  // eslint-disable-next-line react-hooks/rules-of-hooks
13
16
  const entity = 'field' in props ? useEntity(props.field)! : useEntity()
@@ -31,6 +31,19 @@ type CP = FieldViewCommonProps
31
31
  type RN = ReactNode
32
32
  type REN = ReactElement | null
33
33
 
34
+ /**
35
+ * Base field view component with no default formatting.
36
+ *
37
+ * @example
38
+ * ```
39
+ * <FieldView
40
+ * field="startsAt"
41
+ * render={(accessor) => <>{accessor.value}</>}
42
+ * />
43
+ * ```
44
+ *
45
+ * @group Field Views
46
+ */
34
47
  export const FieldView = Component<FieldViewProps>(
35
48
  props => {
36
49
  const { fallbackIfUnpersisted, render } = props
@@ -22,6 +22,9 @@ export interface IfCallbackProps {
22
22
  children?: ReactNode
23
23
  }
24
24
 
25
+ /**
26
+ * @group Logic Components
27
+ */
25
28
  export const If = Component<IfProps>(props => {
26
29
  return typeof props.condition !== 'function'
27
30
  ? <IfFilter condition={props.condition}>{props.children}</IfFilter>
@@ -5,6 +5,9 @@ export interface StaticRenderProps {
5
5
  children?: ReactNode
6
6
  }
7
7
 
8
+ /**
9
+ * @group Data binding
10
+ */
8
11
  export const StaticRender = Component<StaticRenderProps>(
9
12
  () => null,
10
13
  ({ children }) => <>{children}</>,
@@ -7,6 +7,9 @@ export interface SugaredFieldProps<Persisted extends FieldValue = FieldValue> ex
7
7
  field: string | SugaredRelativeSingleField
8
8
  }
9
9
 
10
+ /**
11
+ * @group Data binding
12
+ */
10
13
  export const SugaredField = Component(<Persisted extends FieldValue = FieldValue>(props: SugaredFieldProps<Persisted>) => {
11
14
  if (typeof props.field === 'string') {
12
15
  return <Field {...props} field={props.field} />
@@ -47,6 +47,6 @@ export const evaluateCondition = (value: FieldValue | null, condition: Input.Con
47
47
  if (!handler) {
48
48
  throw new BindingError()
49
49
  }
50
- return handler(argument)
50
+ return (handler as any)(argument)
51
51
  })
52
52
  }
@@ -9,31 +9,32 @@ import type {
9
9
  } from '../treeParameters'
10
10
  import { Hashing } from '../utils'
11
11
  import { Environment } from '../dao'
12
+ import { PlaceholderParametersGenerator } from './PlaceholderParametersGenerator'
12
13
 
13
14
  export class PlaceholderGenerator {
14
15
  public static getFieldPlaceholder(fieldName: FieldName): string {
15
- return fieldName
16
+ return fieldName // hashing intentionally ignored
16
17
  }
17
18
 
18
19
  public static getHasOneRelationPlaceholder(relation: HasOneRelation): string {
19
- return `${relation.field}_${Hashing.hashHasOneRelation(relation)}`
20
+ return `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasOneRelationParameters(relation))}`
20
21
  }
21
22
 
22
23
  public static getHasManyRelationPlaceholder(relation: HasManyRelation): string {
23
- return `${relation.field}_${Hashing.hashHasManyRelation(relation)}`
24
+ return `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasManyRelationParameters(relation))}`
24
25
  }
25
26
 
26
27
  public static getEntitySubTreePlaceholder(
27
28
  subTreeParameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity,
28
29
  environment: Environment,
29
30
  ): string {
30
- return `est_${Hashing.hashEntitySubTreeParameters(subTreeParameters, environment)}`
31
+ return `est_${Hashing.hashAny(PlaceholderParametersGenerator.createEntitySubTreeParameters(subTreeParameters, environment))}`
31
32
  }
32
33
 
33
34
  public static getEntityListSubTreePlaceholder(
34
35
  subTreeParameters: QualifiedEntityList | UnconstrainedQualifiedEntityList,
35
36
  environment: Environment,
36
37
  ): string {
37
- return `lst_${Hashing.hashEntityListSubTreeParameters(subTreeParameters, environment)}`
38
+ return `lst_${Hashing.hashAny(PlaceholderParametersGenerator.createEntityListSubTreeParameters(subTreeParameters, environment))}`
38
39
  }
39
40
  }