@contember/binding 1.2.0-alpha.7 → 1.2.0-alpha.9

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 (43) hide show
  1. package/dist/development/accessors/EntityAccessor.js +66 -22
  2. package/dist/development/accessors/EntityAccessor.js.map +1 -1
  3. package/dist/development/dao/TreeNodeEnvironmentFactory.js +6 -76
  4. package/dist/development/dao/TreeNodeEnvironmentFactory.js.map +1 -1
  5. package/dist/development/markers/PlaceholderGenerator.js +5 -4
  6. package/dist/development/markers/PlaceholderGenerator.js.map +1 -1
  7. package/dist/development/markers/PlaceholderParametersGenerator.js +62 -0
  8. package/dist/development/markers/PlaceholderParametersGenerator.js.map +1 -0
  9. package/dist/development/utils/Hashing.js +2 -56
  10. package/dist/development/utils/Hashing.js.map +1 -1
  11. package/dist/development/utils/TreeNodeUtils.js +77 -0
  12. package/dist/development/utils/TreeNodeUtils.js.map +1 -0
  13. package/dist/production/accessors/EntityAccessor.js +66 -22
  14. package/dist/production/accessors/EntityAccessor.js.map +1 -1
  15. package/dist/production/dao/TreeNodeEnvironmentFactory.js +6 -76
  16. package/dist/production/dao/TreeNodeEnvironmentFactory.js.map +1 -1
  17. package/dist/production/markers/PlaceholderGenerator.js +5 -4
  18. package/dist/production/markers/PlaceholderGenerator.js.map +1 -1
  19. package/dist/production/markers/PlaceholderParametersGenerator.js +62 -0
  20. package/dist/production/markers/PlaceholderParametersGenerator.js.map +1 -0
  21. package/dist/production/utils/Hashing.js +2 -56
  22. package/dist/production/utils/Hashing.js.map +1 -1
  23. package/dist/production/utils/TreeNodeUtils.js +77 -0
  24. package/dist/production/utils/TreeNodeUtils.js.map +1 -0
  25. package/dist/types/accessors/EntityAccessor.d.ts +9 -4
  26. package/dist/types/accessors/EntityAccessor.d.ts.map +1 -1
  27. package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts +0 -8
  28. package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts.map +1 -1
  29. package/dist/types/markers/PlaceholderGenerator.d.ts.map +1 -1
  30. package/dist/types/markers/PlaceholderParametersGenerator.d.ts +9 -0
  31. package/dist/types/markers/PlaceholderParametersGenerator.d.ts.map +1 -0
  32. package/dist/types/tsconfig.tsbuildinfo +1 -1
  33. package/dist/types/utils/Hashing.d.ts +1 -7
  34. package/dist/types/utils/Hashing.d.ts.map +1 -1
  35. package/dist/types/utils/TreeNodeUtils.d.ts +13 -0
  36. package/dist/types/utils/TreeNodeUtils.d.ts.map +1 -0
  37. package/package.json +5 -5
  38. package/src/accessors/EntityAccessor.ts +96 -28
  39. package/src/dao/TreeNodeEnvironmentFactory.ts +6 -92
  40. package/src/markers/PlaceholderGenerator.ts +6 -5
  41. package/src/markers/PlaceholderParametersGenerator.ts +79 -0
  42. package/src/utils/Hashing.ts +2 -68
  43. 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.7",
4
+ "version": "1.2.0-alpha.9",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "./dist/production/index.js",
@@ -28,10 +28,10 @@
28
28
  "test": "vitest"
29
29
  },
30
30
  "dependencies": {
31
- "@contember/client": "1.2.0-alpha.7",
32
- "@contember/react-client": "1.2.0-alpha.7",
33
- "@contember/react-multipass-rendering": "1.2.0-alpha.7",
34
- "@contember/react-utils": "1.2.0-alpha.7",
31
+ "@contember/client": "1.2.0-alpha.9",
32
+ "@contember/react-client": "1.2.0-alpha.9",
33
+ "@contember/react-multipass-rendering": "1.2.0-alpha.9",
34
+ "@contember/react-utils": "1.2.0-alpha.9",
35
35
  "chevrotain": "6.5.0",
36
36
  "fast-deep-equal": "^3.1.3",
37
37
  "js-levenshtein": "1.1.6",
@@ -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
  | {
@@ -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
  }
@@ -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
  }
@@ -0,0 +1,79 @@
1
+ import {
2
+ HasManyRelation,
3
+ HasOneRelation,
4
+ QualifiedEntityList,
5
+ QualifiedSingleEntity,
6
+ UnconstrainedQualifiedEntityList,
7
+ UnconstrainedQualifiedSingleEntity,
8
+ } from '../treeParameters'
9
+ import { Environment } from '../dao'
10
+
11
+ export class PlaceholderParametersGenerator {
12
+
13
+ public static createHasOneRelationParameters(relation: HasOneRelation): any {
14
+ return {
15
+ type: 'hasOne',
16
+ field: relation.field,
17
+ filter: relation.filter,
18
+ reducedBy: relation.reducedBy,
19
+ }
20
+ }
21
+
22
+ public static createHasManyRelationParameters(relation: HasManyRelation): any {
23
+ return {
24
+ type: 'hasMany',
25
+ field: relation.field,
26
+ filter: relation.filter,
27
+ offset: relation.offset,
28
+ limit: relation.limit,
29
+ orderBy: relation.orderBy,
30
+ }
31
+ }
32
+
33
+ public static createEntityListSubTreeParameters(
34
+ parameters: QualifiedEntityList | UnconstrainedQualifiedEntityList,
35
+ environment: Environment,
36
+ ): any {
37
+ if (parameters.isCreating) {
38
+ return {
39
+ type: 'entityListSubtree',
40
+ isCreating: true,
41
+ entityName: parameters.entityName,
42
+ variables: environment.getAllVariables(),
43
+ }
44
+ }
45
+ return {
46
+ type: 'entityListSubtree',
47
+ isCreating: false,
48
+ entityName: parameters.entityName,
49
+ filter: parameters.filter,
50
+ offset: parameters.offset,
51
+ limit: parameters.limit,
52
+ orderBy: parameters.orderBy,
53
+ variables: environment.getAllVariables(),
54
+ }
55
+ }
56
+
57
+ public static createEntitySubTreeParameters(
58
+ parameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity,
59
+ environment: Environment,
60
+ ): any {
61
+ if (parameters.isCreating) {
62
+ return {
63
+ type: 'entitySubTree',
64
+ isCreating: true,
65
+ entityName: parameters.entityName,
66
+ variables: environment.getAllVariables(),
67
+ }
68
+ }
69
+ return {
70
+ type: 'entitySubTree',
71
+ isCreating: false,
72
+ where: parameters.where,
73
+ entityName: parameters.entityName,
74
+ filter: parameters.filter,
75
+ variables: environment.getAllVariables(),
76
+ }
77
+ }
78
+
79
+ }
@@ -10,74 +10,8 @@ import { Environment } from '../dao'
10
10
 
11
11
  // TODO update hashing so that for offset, 0 == undefined
12
12
  export class Hashing {
13
- public static hashHasOneRelation(relation: HasOneRelation): number {
14
- const where = [
15
- 'upToOne',
16
- relation.field,
17
- relation.filter,
18
- relation.reducedBy,
19
- ]
20
-
21
- return Hashing.hashArray(where)
22
- }
23
-
24
- public static hashHasManyRelation(relation: HasManyRelation): number {
25
- const where = [
26
- 'possiblyMany',
27
- relation.field,
28
- relation.filter,
29
- 'offset' in relation ? relation.offset : undefined,
30
- 'limit' in relation ? relation.limit : undefined,
31
- 'orderBy' in relation ? relation.orderBy : undefined,
32
- ]
33
-
34
- return Hashing.hashArray(where)
35
- }
36
-
37
- public static hashEntityListSubTreeParameters(
38
- parameters: QualifiedEntityList | UnconstrainedQualifiedEntityList,
39
- environment: Environment,
40
- ): number {
41
- if (parameters.isCreating) {
42
- return Hashing.hashArray([
43
- parameters.isCreating,
44
- parameters.entityName,
45
- environment.getAllVariables(),
46
- ])
47
- }
48
- return Hashing.hashArray([
49
- parameters.isCreating,
50
- parameters.entityName,
51
- parameters.filter,
52
- parameters.orderBy,
53
- parameters.offset,
54
- parameters.limit,
55
- environment.getAllVariables(),
56
- ])
57
- }
58
-
59
- public static hashEntitySubTreeParameters(
60
- parameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity,
61
- environment: Environment,
62
- ): number {
63
- if (parameters.isCreating) {
64
- return Hashing.hashArray([
65
- parameters.isCreating,
66
- parameters.entityName,
67
- environment.getAllVariables(),
68
- ])
69
- }
70
- return Hashing.hashArray([
71
- parameters.isCreating,
72
- parameters.where,
73
- parameters.entityName,
74
- parameters.filter,
75
- environment.getAllVariables(),
76
- ])
77
- }
78
-
79
- private static hashArray(array: any[]): number {
80
- const json = array.map(item => JSON.stringify(item)).join('_')
13
+ public static hashAny(array: any[]): number {
14
+ const json = JSON.stringify(array)
81
15
  return Hashing.hash(json)
82
16
  }
83
17