@contember/binding 1.2.0-alpha.8 → 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
@@ -0,0 +1,92 @@
1
+ import { Environment } from '../dao'
2
+ import { BaseRelation, Schema, SchemaColumn, SchemaEntity, SchemaField, SchemaRelation } from '../core'
3
+ import { BindingError } from '../BindingError'
4
+ import levenshtein from 'js-levenshtein'
5
+
6
+ export class TreeNodeUtils {
7
+
8
+ public static resolveEntity(schema: Schema, entityName: string, type: 'entity' | 'entity list'): SchemaEntity {
9
+ const entity = schema.getEntityOrUndefined(entityName)
10
+ if (!entity) {
11
+ const alternative = this.recommendAlternative(entityName, schema.getEntityNames())
12
+ const didYouMean = alternative ? `Did you mean '${alternative}'?` : ''
13
+ throw new BindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${didYouMean}`)
14
+ }
15
+ return entity
16
+ }
17
+
18
+ public static resolveHasOneRelation(environment: Environment, field: string, isReduced: boolean): SchemaRelation {
19
+ if (isReduced) {
20
+ return this.resolveRelation(environment, field, 'reduced has-one relation', ['ManyHasMany', 'OneHasMany'])
21
+ }
22
+ return this.resolveRelation(environment, field, 'a has-one relation', ['ManyHasOne', 'OneHasOne'])
23
+ }
24
+
25
+
26
+ public static resolveHasManyRelation(environment: Environment, field: string): SchemaRelation {
27
+ return this.resolveRelation(environment, field, 'a has-many relation', ['ManyHasMany', 'OneHasMany'])
28
+ }
29
+
30
+ private static resolveRelation(environment: Environment, field: string, relationDescription: string, expectedRelationType: BaseRelation['type'][]): SchemaRelation {
31
+ return this.resolveField(environment, field, (field): field is SchemaRelation => {
32
+ return field.__typename === '_Relation' && expectedRelationType.includes(field.type)
33
+ }, relationDescription)
34
+ }
35
+
36
+ public static resolveColumn(environment: Environment, fieldName: string): SchemaColumn {
37
+ return this.resolveField(environment, fieldName, (field): field is SchemaColumn => field.__typename === '_Column', 'an ordinary field')
38
+ // TODO check that defaultValue matches the type
39
+ // TODO run custom validators
40
+ }
41
+
42
+ private static resolveField<T extends SchemaField>(environment: Environment, fieldName: string, matcher: (field: SchemaField) => field is T, expectedDescription: string): T {
43
+ const treeLocation = environment.getSubTreeNode()
44
+ const entity = treeLocation.entity
45
+ const field = entity.fields.get(fieldName)
46
+ if (!field) {
47
+ const alternative = this.recommendAlternative(
48
+ fieldName,
49
+ Array.from(entity.fields)
50
+ .filter(([, field]) => matcher(field))
51
+ .map(([fieldName]) => fieldName),
52
+ )
53
+ const didYouMean = alternative ? `Did you mean '${alternative}'?` : ''
54
+ throw new BindingError(`Field '${fieldName}' doesn't exist on ${this.describeLocation(environment)}. ${didYouMean}`)
55
+ }
56
+ if (!matcher(field)) {
57
+ const actual = field.__typename === '_Column' ? 'an ordinary field' : `a ${field.type} relation`
58
+ throw new BindingError(
59
+ `Invalid field: the name '${field.name}' on ${this.describeLocation(environment)} ` +
60
+ `refers to ${actual} but is being used as a ${expectedDescription}.`,
61
+ )
62
+ }
63
+ return field
64
+ }
65
+
66
+ public static describeLocation(environment: Environment): string {
67
+ const path = []
68
+ const loc = environment.getSubTreeNode()
69
+ for (let env = environment; ; env = env.getParent()) {
70
+ const node = env.getSubTreeNode()
71
+ if (node.type === 'subtree-entity' || node.type === 'subtree-entity-list') {
72
+ return `entity '${loc.entity.name}' in path '${[node.entity.name, ...path.reverse()].join('.')}'`
73
+ }
74
+ path.push(node.field.name)
75
+ }
76
+ }
77
+
78
+ public static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined {
79
+ let bestAlternative: string | undefined = undefined
80
+ let bestAlternativeDistance = Number.MAX_SAFE_INTEGER
81
+
82
+ for (const alternative of possibleAlternatives) {
83
+ const distance = levenshtein(original, alternative)
84
+
85
+ if (distance < bestAlternativeDistance) {
86
+ bestAlternative = alternative
87
+ bestAlternativeDistance = distance
88
+ }
89
+ }
90
+ return bestAlternative
91
+ }
92
+ }