@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.
- package/dist/development/accessors/EntityAccessor.js +66 -22
- package/dist/development/accessors/EntityAccessor.js.map +1 -1
- package/dist/development/dao/TreeNodeEnvironmentFactory.js +6 -76
- package/dist/development/dao/TreeNodeEnvironmentFactory.js.map +1 -1
- package/dist/development/markers/PlaceholderGenerator.js +5 -4
- package/dist/development/markers/PlaceholderGenerator.js.map +1 -1
- package/dist/development/markers/PlaceholderParametersGenerator.js +62 -0
- package/dist/development/markers/PlaceholderParametersGenerator.js.map +1 -0
- package/dist/development/utils/Hashing.js +2 -56
- package/dist/development/utils/Hashing.js.map +1 -1
- package/dist/development/utils/TreeNodeUtils.js +77 -0
- package/dist/development/utils/TreeNodeUtils.js.map +1 -0
- package/dist/production/accessors/EntityAccessor.js +66 -22
- package/dist/production/accessors/EntityAccessor.js.map +1 -1
- package/dist/production/dao/TreeNodeEnvironmentFactory.js +6 -76
- package/dist/production/dao/TreeNodeEnvironmentFactory.js.map +1 -1
- package/dist/production/markers/PlaceholderGenerator.js +5 -4
- package/dist/production/markers/PlaceholderGenerator.js.map +1 -1
- package/dist/production/markers/PlaceholderParametersGenerator.js +62 -0
- package/dist/production/markers/PlaceholderParametersGenerator.js.map +1 -0
- package/dist/production/utils/Hashing.js +2 -56
- package/dist/production/utils/Hashing.js.map +1 -1
- package/dist/production/utils/TreeNodeUtils.js +77 -0
- package/dist/production/utils/TreeNodeUtils.js.map +1 -0
- package/dist/types/accessors/EntityAccessor.d.ts +9 -4
- package/dist/types/accessors/EntityAccessor.d.ts.map +1 -1
- package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts +0 -8
- package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts.map +1 -1
- package/dist/types/markers/PlaceholderGenerator.d.ts.map +1 -1
- package/dist/types/markers/PlaceholderParametersGenerator.d.ts +9 -0
- package/dist/types/markers/PlaceholderParametersGenerator.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/Hashing.d.ts +1 -7
- package/dist/types/utils/Hashing.d.ts.map +1 -1
- package/dist/types/utils/TreeNodeUtils.d.ts +13 -0
- package/dist/types/utils/TreeNodeUtils.d.ts.map +1 -0
- package/package.json +5 -5
- package/src/accessors/EntityAccessor.ts +96 -28
- package/src/dao/TreeNodeEnvironmentFactory.ts +6 -92
- package/src/markers/PlaceholderGenerator.ts +6 -5
- package/src/markers/PlaceholderParametersGenerator.ts +79 -0
- package/src/utils/Hashing.ts +2 -68
- package/src/utils/TreeNodeUtils.ts +92 -0
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { BindingError } from "../BindingError.js";
|
|
2
2
|
import { ErrorAccessor } from "./ErrorAccessor.js";
|
|
3
|
+
import { TreeNodeUtils } from "../utils/TreeNodeUtils.js";
|
|
4
|
+
import { PlaceholderParametersGenerator } from "../markers/PlaceholderParametersGenerator.js";
|
|
3
5
|
import { QueryLanguage } from "../queryLanguage/QueryLanguage.js";
|
|
4
6
|
import { PlaceholderGenerator } from "../markers/PlaceholderGenerator.js";
|
|
5
7
|
import { getEntityMarker } from "../core/state/getEntityMarker.js";
|
|
8
|
+
import { HasOneRelationMarker } from "../markers/HasOneRelationMarker.js";
|
|
9
|
+
import { HasManyRelationMarker } from "../markers/HasManyRelationMarker.js";
|
|
6
10
|
class EntityAccessor {
|
|
7
11
|
constructor(state, operations, runtimeId, key, name, fieldData, dataFromServer, hasUnpersistedChanges, errors, environment, getAccessor) {
|
|
8
12
|
this.state = state;
|
|
@@ -75,24 +79,79 @@ class EntityAccessor {
|
|
|
75
79
|
* @see EntityAccessor.getField
|
|
76
80
|
*/
|
|
77
81
|
getRelativeSingleField(field) {
|
|
78
|
-
|
|
79
|
-
|
|
82
|
+
const entity = this.getRelativeSingleEntity(field);
|
|
83
|
+
const fieldPlaceholder = PlaceholderGenerator.getFieldPlaceholder(field.field);
|
|
84
|
+
const accessor = entity.fieldData.get(fieldPlaceholder);
|
|
85
|
+
if (accessor) {
|
|
86
|
+
return accessor.getAccessor();
|
|
87
|
+
}
|
|
88
|
+
TreeNodeUtils.resolveColumn(entity.environment, field.field);
|
|
89
|
+
throw new BindingError(
|
|
90
|
+
`EntityAccessor: cannot access field '${field.field}' on ${TreeNodeUtils.describeLocation(entity.environment)}.
|
|
91
|
+
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.`
|
|
80
92
|
);
|
|
81
93
|
}
|
|
82
94
|
getRelativeSingleEntity(relativeSingleEntity) {
|
|
83
95
|
let relativeTo = this;
|
|
84
96
|
for (const hasOneRelation of relativeSingleEntity.hasOneRelationPath) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
)
|
|
97
|
+
const fieldPlaceholder = PlaceholderGenerator.getHasOneRelationPlaceholder(hasOneRelation);
|
|
98
|
+
const accessor = relativeTo.fieldData.get(fieldPlaceholder);
|
|
99
|
+
if (!accessor) {
|
|
100
|
+
return this.raiseInvalidHasOneRelationError(relativeTo, hasOneRelation);
|
|
101
|
+
}
|
|
102
|
+
relativeTo = accessor.getAccessor();
|
|
88
103
|
}
|
|
89
104
|
return relativeTo;
|
|
90
105
|
}
|
|
91
106
|
getRelativeEntityList(entityList) {
|
|
92
|
-
|
|
93
|
-
|
|
107
|
+
const entity = this.getRelativeSingleEntity(entityList);
|
|
108
|
+
const fieldPlaceholder = PlaceholderGenerator.getHasManyRelationPlaceholder(entityList.hasManyRelation);
|
|
109
|
+
const accessor = entity.fieldData.get(fieldPlaceholder);
|
|
110
|
+
if (!accessor) {
|
|
111
|
+
return this.raiseInvalidHasManyRelationError(entity, entityList.hasManyRelation);
|
|
112
|
+
}
|
|
113
|
+
return accessor.getAccessor();
|
|
114
|
+
}
|
|
115
|
+
raiseInvalidHasOneRelationError(relativeTo, hasOneRelation) {
|
|
116
|
+
TreeNodeUtils.resolveHasOneRelation(relativeTo.environment, hasOneRelation.field, !!hasOneRelation.reducedBy);
|
|
117
|
+
const entityMarker = relativeTo.getMarker();
|
|
118
|
+
const possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasOneRelationMarker, hasOneRelation.field);
|
|
119
|
+
const markerParams = possibleMarkers.map((it) => PlaceholderParametersGenerator.createHasOneRelationParameters(it.parameters));
|
|
120
|
+
const relationParams = PlaceholderParametersGenerator.createHasOneRelationParameters(hasOneRelation);
|
|
121
|
+
this.raiseUndefinedRelationError(hasOneRelation.field, relativeTo.environment, relationParams, markerParams);
|
|
122
|
+
}
|
|
123
|
+
raiseInvalidHasManyRelationError(entity, hasManyRelation) {
|
|
124
|
+
TreeNodeUtils.resolveHasManyRelation(entity.environment, hasManyRelation.field);
|
|
125
|
+
const entityMarker = entity.getMarker();
|
|
126
|
+
const possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasManyRelationMarker, hasManyRelation.field);
|
|
127
|
+
const markerParams = possibleMarkers.map((it) => PlaceholderParametersGenerator.createHasManyRelationParameters(it.parameters));
|
|
128
|
+
const relationParams = PlaceholderParametersGenerator.createHasManyRelationParameters(hasManyRelation);
|
|
129
|
+
this.raiseUndefinedRelationError(hasManyRelation.field, entity.environment, relationParams, markerParams);
|
|
130
|
+
}
|
|
131
|
+
raiseUndefinedRelationError(field, environment, relationParams, otherMarkersParams) {
|
|
132
|
+
const hint = this.createHintMessage(otherMarkersParams);
|
|
133
|
+
throw new BindingError(
|
|
134
|
+
`EntityAccessor: cannot access relation '${field}' on ${TreeNodeUtils.describeLocation(environment)}.
|
|
135
|
+
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.
|
|
136
|
+
|
|
137
|
+
Provided parameters:
|
|
138
|
+
` + JSON.stringify(relationParams) + hint
|
|
94
139
|
);
|
|
95
140
|
}
|
|
141
|
+
createHintMessage(params) {
|
|
142
|
+
if (params.length === 0) {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
const serializedParams = params.map((it) => JSON.stringify(it)).join("\n");
|
|
146
|
+
const message = params.length === 1 ? "However, there is existing marker for the same field, but it have different parameters:" : "However, there are existing markers for the same field, but they have different parameters:";
|
|
147
|
+
return `
|
|
148
|
+
|
|
149
|
+
${message}
|
|
150
|
+
${serializedParams}`;
|
|
151
|
+
}
|
|
152
|
+
getMarkersOfType(markers, markerConstructor, fieldName) {
|
|
153
|
+
return Array.from(markers.values()).filter((it) => it instanceof markerConstructor && it.parameters.field === fieldName);
|
|
154
|
+
}
|
|
96
155
|
getParent() {
|
|
97
156
|
const blueprint = this.state.blueprint;
|
|
98
157
|
if (blueprint.type === "subTree") {
|
|
@@ -103,21 +162,6 @@ class EntityAccessor {
|
|
|
103
162
|
getMarker() {
|
|
104
163
|
return getEntityMarker(this.state);
|
|
105
164
|
}
|
|
106
|
-
getAccessorByPlaceholder(placeholderName) {
|
|
107
|
-
const record = this.fieldData.get(placeholderName);
|
|
108
|
-
if (record === void 0) {
|
|
109
|
-
throw new BindingError(
|
|
110
|
-
`EntityAccessor: unknown field placeholder '${placeholderName} on ${this.name}'. Unless this is just a typo, this is typically caused by one of the following:
|
|
111
|
-
• Trying to access a field that has not been registered during static render, and thus lacks a marker and an accessor.
|
|
112
|
-
• Misusing an EntityAccessor getter. If you used one of the getRelative[…] family, please make sure all parameters match the marker tree exactly.
|
|
113
|
-
|
|
114
|
-
For more information, please consult the documentation.
|
|
115
|
-
|
|
116
|
-
`
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
return record.getAccessor();
|
|
120
|
-
}
|
|
121
165
|
}
|
|
122
166
|
export {
|
|
123
167
|
EntityAccessor
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityAccessor.js","sources":["../../../src/accessors/EntityAccessor.ts"],"sourcesContent":["import { RuntimeId, SingleEntityPersistedData } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport type { EntityOperations } from '../core/operations'\nimport type { Environment } from '../dao'\nimport { PlaceholderGenerator } from '../markers'\nimport { QueryLanguage } from '../queryLanguage'\nimport type {\n\tEntityId,\n\tEntityName,\n\tEntityRealmKey,\n\tFieldName,\n\tFieldValue,\n\tRelativeEntityList,\n\tRelativeSingleEntity,\n\tRelativeSingleField,\n\tSugaredRelativeEntityList,\n\tSugaredRelativeSingleEntity,\n\tSugaredRelativeSingleField,\n} from '../treeParameters'\nimport type { AsyncBatchUpdatesOptions } from './AsyncBatchUpdatesOptions'\nimport type { BatchUpdatesOptions } from './BatchUpdatesOptions'\nimport type { EntityListAccessor } from './EntityListAccessor'\nimport type { Errorable } from './Errorable'\nimport { ErrorAccessor } from './ErrorAccessor'\nimport type { FieldAccessor } from './FieldAccessor'\nimport type { PersistErrorOptions } from './PersistErrorOptions'\nimport type { PersistSuccessOptions } from './PersistSuccessOptions'\nimport type { EntityRealmState } from '../core/state'\nimport { getEntityMarker } from '../core/state'\n\nclass EntityAccessor implements Errorable {\n\tpublic constructor(\n\t\tprivate readonly state: EntityRealmState,\n\t\tprivate readonly operations: EntityOperations,\n\t\tprivate readonly runtimeId: RuntimeId,\n\t\tpublic readonly key: EntityRealmKey, // ⚠️ This is *NOT* the id! ⚠️\n\t\tpublic readonly name: EntityName,\n\t\tprivate readonly fieldData: EntityAccessor.FieldData,\n\t\tprivate readonly dataFromServer: SingleEntityPersistedData | undefined,\n\t\tpublic readonly hasUnpersistedChanges: boolean,\n\t\tpublic readonly errors: ErrorAccessor | undefined,\n\t\tpublic readonly environment: Environment,\n\t\tpublic readonly getAccessor: EntityAccessor.GetEntityAccessor,\n\t) {}\n\n\tpublic get idOnServer(): EntityId | undefined {\n\t\treturn this.runtimeId.existsOnServer ? this.runtimeId.value : undefined\n\t}\n\n\t/**\n\t * Note that for entities that don't yet exist on server this will return a dummy id.\n\t */\n\tpublic get id(): EntityId {\n\t\treturn this.runtimeId.value\n\t}\n\n\tpublic get existsOnServer(): boolean {\n\t\treturn this.runtimeId.existsOnServer\n\t}\n\n\t//\n\n\tpublic addError(error: ErrorAccessor.Error | string): () => void {\n\t\treturn this.operations.addError(this.state, ErrorAccessor.normalizeError(error))\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t): () => void {\n\t\treturn this.operations.addEventListener(this.state, event, listener)\n\t}\n\n\tpublic batchUpdates(performUpdates: EntityAccessor.BatchUpdatesHandler): void {\n\t\tthis.operations.batchUpdates(this.state, performUpdates)\n\t}\n\n\tpublic connectEntityAtField(field: FieldName, entityToConnect: EntityAccessor): void {\n\t\tthis.operations.connectEntityAtField(this.state, field, entityToConnect)\n\t}\n\n\tpublic disconnectEntityAtField(field: FieldName, initializeReplacement?: EntityAccessor.BatchUpdatesHandler): void {\n\t\tthis.operations.disconnectEntityAtField(this.state, field, initializeReplacement)\n\t}\n\n\tpublic deleteEntity(): void {\n\t\tthis.operations.deleteEntity(this.state)\n\t}\n\n\t//\n\n\tpublic updateValues(fieldValuePairs: EntityAccessor.FieldValuePairs): void {\n\t\tthis.batchUpdates(getAccessor => {\n\t\t\tconst entries = Array.isArray(fieldValuePairs) ? fieldValuePairs : Object.entries(fieldValuePairs)\n\n\t\t\tfor (const [field, value] of entries) {\n\t\t\t\tgetAccessor().getField(field).updateValue(value)\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * Please keep in mind that this method signature is literally impossible to implement safely. The generic parameter\n\t * is really just a way to succinctly write a type cast. Nothing more, really.\n\t */\n\tpublic getField<Value extends FieldValue = FieldValue>(\n\t\tfield: SugaredRelativeSingleField | string,\n\t): FieldAccessor<Value> {\n\t\treturn this.getRelativeSingleField<Value>(QueryLanguage.desugarRelativeSingleField(field, this.environment))\n\t}\n\n\tpublic getEntity(entity: SugaredRelativeSingleEntity | string): EntityAccessor {\n\t\treturn this.getRelativeSingleEntity(QueryLanguage.desugarRelativeSingleEntity(entity, this.environment))\n\t}\n\n\tpublic getEntityList(entityList: SugaredRelativeEntityList | string): EntityListAccessor {\n\t\treturn this.getRelativeEntityList(QueryLanguage.desugarRelativeEntityList(entityList, this.environment))\n\t}\n\n\t//\n\n\t/**\n\t * @see EntityAccessor.getField\n\t */\n\tpublic getRelativeSingleField<Value extends FieldValue = FieldValue>(field: RelativeSingleField): FieldAccessor<Value> {\n\t\treturn this.getRelativeSingleEntity(field).getAccessorByPlaceholder(\n\t\t\tPlaceholderGenerator.getFieldPlaceholder(field.field),\n\t\t) as unknown as FieldAccessor<Value>\n\t}\n\n\tpublic getRelativeSingleEntity(relativeSingleEntity: RelativeSingleEntity): EntityAccessor {\n\t\tlet relativeTo: EntityAccessor = this\n\n\t\tfor (const hasOneRelation of relativeSingleEntity.hasOneRelationPath) {\n\t\t\trelativeTo = relativeTo.getAccessorByPlaceholder(\n\t\t\t\tPlaceholderGenerator.getHasOneRelationPlaceholder(hasOneRelation),\n\t\t\t) as EntityAccessor\n\t\t}\n\t\treturn relativeTo\n\t}\n\n\tpublic getRelativeEntityList(entityList: RelativeEntityList): EntityListAccessor {\n\t\treturn this.getRelativeSingleEntity(entityList).getAccessorByPlaceholder(\n\t\t\tPlaceholderGenerator.getHasManyRelationPlaceholder(entityList.hasManyRelation),\n\t\t) as EntityListAccessor\n\t}\n\n\tpublic getParent(): EntityAccessor | EntityListAccessor | undefined {\n\t\tconst blueprint = this.state.blueprint\n\t\tif (blueprint.type === 'subTree') {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprint.parent.getAccessor()\n\t}\n\n\tpublic getMarker() {\n\t\treturn getEntityMarker(this.state)\n\t}\n\n\tprivate getAccessorByPlaceholder(placeholderName: FieldName): EntityAccessor.NestedAccessor {\n\t\tconst record = this.fieldData.get(placeholderName)\n\t\tif (record === undefined) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`EntityAccessor: unknown field placeholder '${placeholderName} on ${this.name}'. Unless this is just a typo, this is ` +\n\t\t\t\t\t`typically caused by one of the following:\\n` +\n\t\t\t\t\t`\\t• Trying to access a field that has not been registered during static render, and thus lacks a marker and an accessor.\\n` +\n\t\t\t\t\t`\\t• Misusing an EntityAccessor getter. If you used one of the getRelative[…] family, please make sure all ` +\n\t\t\t\t\t`parameters match the marker tree exactly.` +\n\t\t\t\t\t`\\n\\nFor more information, please consult the documentation.\\n\\n`,\n\t\t\t)\n\t\t}\n\t\treturn record.getAccessor()\n\t}\n}\n\nnamespace EntityAccessor {\n\texport interface FieldDatum {\n\t\tgetAccessor(): NestedAccessor\n\t}\n\texport type NestedAccessor = EntityAccessor | EntityListAccessor | FieldAccessor\n\n\texport type FieldValuePairs =\n\t\t| {\n\t\t\t\t[field: string]: FieldValue\n\t\t }\n\t\t| Iterable<[SugaredRelativeSingleField | string, FieldValue]>\n\n\texport type FieldData = Map<FieldName, FieldDatum>\n\n\texport type GetEntityAccessor = () => EntityAccessor\n\texport type BatchUpdatesHandler = (getAccessor: GetEntityAccessor, options: BatchUpdatesOptions) => void\n\texport type UpdateListener = (accessor: EntityAccessor) => void\n\n\texport type BeforePersistHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: AsyncBatchUpdatesOptions,\n\t) => void | Promise<void | BeforePersistHandler>\n\n\texport type PersistErrorHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: PersistErrorOptions,\n\t) => void | Promise<void>\n\n\texport type PersistSuccessHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: PersistSuccessOptions,\n\t) => void | Promise<void | PersistSuccessHandler>\n\n\texport interface RuntimeEntityEventListenerMap {\n\t\tbeforePersist: BeforePersistHandler\n\t\tbeforeUpdate: BatchUpdatesHandler\n\t\tconnectionUpdate: UpdateListener\n\t\tpersistError: PersistErrorHandler\n\t\tpersistSuccess: PersistSuccessHandler\n\t\tupdate: UpdateListener\n\t}\n\texport interface EntityEventListenerMap extends RuntimeEntityEventListenerMap {\n\t\tinitialize: BatchUpdatesHandler\n\t}\n\texport type EntityEventType = keyof EntityEventListenerMap\n}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I,eAAe;AACjD,QAAI,WAAW,QAAW;AACzB,YAAM,IAAI;AAAA,QACT,8CAA8C,sBAAsB,KAAK;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAAA;AAAA,IAO3E;AACA,WAAO,OAAO;EACf;AACD;"}
|
|
1
|
+
{"version":3,"file":"EntityAccessor.js","sources":["../../../src/accessors/EntityAccessor.ts"],"sourcesContent":["import { RuntimeId, SingleEntityPersistedData } from '../accessorTree'\nimport { BindingError } from '../BindingError'\nimport type { EntityOperations } from '../core/operations'\nimport type { Environment } from '../dao'\nimport { EntityFieldMarkers, HasManyRelationMarker, HasOneRelationMarker, PlaceholderGenerator } from '../markers'\nimport { QueryLanguage } from '../queryLanguage'\nimport type {\n\tEntityId,\n\tEntityName,\n\tEntityRealmKey,\n\tFieldName,\n\tFieldValue,\n\tHasManyRelation,\n\tHasOneRelation,\n\tRelativeEntityList,\n\tRelativeSingleEntity,\n\tRelativeSingleField,\n\tSugaredRelativeEntityList,\n\tSugaredRelativeSingleEntity,\n\tSugaredRelativeSingleField,\n} from '../treeParameters'\nimport type { AsyncBatchUpdatesOptions } from './AsyncBatchUpdatesOptions'\nimport type { BatchUpdatesOptions } from './BatchUpdatesOptions'\nimport { EntityListAccessor } from './EntityListAccessor'\nimport type { Errorable } from './Errorable'\nimport { ErrorAccessor } from './ErrorAccessor'\nimport type { FieldAccessor } from './FieldAccessor'\nimport type { PersistErrorOptions } from './PersistErrorOptions'\nimport type { PersistSuccessOptions } from './PersistSuccessOptions'\nimport type { EntityRealmState } from '../core/state'\nimport { getEntityMarker } from '../core/state'\nimport { TreeNodeUtils } from '../utils/TreeNodeUtils'\nimport { PlaceholderParametersGenerator } from '../markers/PlaceholderParametersGenerator'\n\nclass EntityAccessor implements Errorable {\n\tpublic constructor(\n\t\tprivate readonly state: EntityRealmState,\n\t\tprivate readonly operations: EntityOperations,\n\t\tprivate readonly runtimeId: RuntimeId,\n\t\tpublic readonly key: EntityRealmKey, // ⚠️ This is *NOT* the id! ⚠️\n\t\tpublic readonly name: EntityName,\n\t\tprivate readonly fieldData: EntityAccessor.FieldData,\n\t\tprivate readonly dataFromServer: SingleEntityPersistedData | undefined,\n\t\tpublic readonly hasUnpersistedChanges: boolean,\n\t\tpublic readonly errors: ErrorAccessor | undefined,\n\t\tpublic readonly environment: Environment,\n\t\tpublic readonly getAccessor: EntityAccessor.GetEntityAccessor,\n\t) {\n\t}\n\n\tpublic get idOnServer(): EntityId | undefined {\n\t\treturn this.runtimeId.existsOnServer ? this.runtimeId.value : undefined\n\t}\n\n\t/**\n\t * Note that for entities that don't yet exist on server this will return a dummy id.\n\t */\n\tpublic get id(): EntityId {\n\t\treturn this.runtimeId.value\n\t}\n\n\tpublic get existsOnServer(): boolean {\n\t\treturn this.runtimeId.existsOnServer\n\t}\n\n\t//\n\n\tpublic addError(error: ErrorAccessor.Error | string): () => void {\n\t\treturn this.operations.addError(this.state, ErrorAccessor.normalizeError(error))\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t): () => void {\n\t\treturn this.operations.addEventListener(this.state, event, listener)\n\t}\n\n\tpublic batchUpdates(performUpdates: EntityAccessor.BatchUpdatesHandler): void {\n\t\tthis.operations.batchUpdates(this.state, performUpdates)\n\t}\n\n\tpublic connectEntityAtField(field: FieldName, entityToConnect: EntityAccessor): void {\n\t\tthis.operations.connectEntityAtField(this.state, field, entityToConnect)\n\t}\n\n\tpublic disconnectEntityAtField(field: FieldName, initializeReplacement?: EntityAccessor.BatchUpdatesHandler): void {\n\t\tthis.operations.disconnectEntityAtField(this.state, field, initializeReplacement)\n\t}\n\n\tpublic deleteEntity(): void {\n\t\tthis.operations.deleteEntity(this.state)\n\t}\n\n\t//\n\n\tpublic updateValues(fieldValuePairs: EntityAccessor.FieldValuePairs): void {\n\t\tthis.batchUpdates(getAccessor => {\n\t\t\tconst entries = Array.isArray(fieldValuePairs) ? fieldValuePairs : Object.entries(fieldValuePairs)\n\n\t\t\tfor (const [field, value] of entries) {\n\t\t\t\tgetAccessor().getField(field).updateValue(value)\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * Please keep in mind that this method signature is literally impossible to implement safely. The generic parameter\n\t * is really just a way to succinctly write a type cast. Nothing more, really.\n\t */\n\tpublic getField<Value extends FieldValue = FieldValue>(\n\t\tfield: SugaredRelativeSingleField | string,\n\t): FieldAccessor<Value> {\n\t\treturn this.getRelativeSingleField<Value>(QueryLanguage.desugarRelativeSingleField(field, this.environment))\n\t}\n\n\tpublic getEntity(entity: SugaredRelativeSingleEntity | string): EntityAccessor {\n\t\treturn this.getRelativeSingleEntity(QueryLanguage.desugarRelativeSingleEntity(entity, this.environment))\n\t}\n\n\tpublic getEntityList(entityList: SugaredRelativeEntityList | string): EntityListAccessor {\n\t\treturn this.getRelativeEntityList(QueryLanguage.desugarRelativeEntityList(entityList, this.environment))\n\t}\n\n\t//\n\n\t/**\n\t * @see EntityAccessor.getField\n\t */\n\tpublic getRelativeSingleField<Value extends FieldValue = FieldValue>(field: RelativeSingleField): FieldAccessor<Value> {\n\t\tconst entity = this.getRelativeSingleEntity(field)\n\t\tconst fieldPlaceholder = PlaceholderGenerator.getFieldPlaceholder(field.field)\n\t\tconst accessor = entity.fieldData.get(fieldPlaceholder)\n\t\tif (accessor) {\n\t\t\treturn accessor.getAccessor() as FieldAccessor<Value>\n\t\t}\n\n\t\tTreeNodeUtils.resolveColumn(entity.environment, field.field)\n\n\t\tthrow new BindingError(''\n\t\t\t+ 'EntityAccessor: cannot access field '\n\t\t\t+ `'${field.field}' on ${TreeNodeUtils.describeLocation(entity.environment)}.\\n`\n\t\t\t+ '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\t\t)\n\t}\n\n\tpublic getRelativeSingleEntity(relativeSingleEntity: RelativeSingleEntity): EntityAccessor {\n\t\tlet relativeTo: EntityAccessor = this\n\n\t\tfor (const hasOneRelation of relativeSingleEntity.hasOneRelationPath) {\n\t\t\tconst fieldPlaceholder = PlaceholderGenerator.getHasOneRelationPlaceholder(hasOneRelation)\n\t\t\tconst accessor = relativeTo.fieldData.get(fieldPlaceholder)\n\t\t\tif (!accessor) {\n\t\t\t\treturn this.raiseInvalidHasOneRelationError(relativeTo, hasOneRelation)\n\t\t\t}\n\n\t\t\trelativeTo = accessor.getAccessor() as EntityAccessor\n\t\t}\n\t\treturn relativeTo\n\t}\n\n\tpublic getRelativeEntityList(entityList: RelativeEntityList): EntityListAccessor {\n\t\tconst entity = this.getRelativeSingleEntity(entityList)\n\t\tconst fieldPlaceholder = PlaceholderGenerator.getHasManyRelationPlaceholder(entityList.hasManyRelation)\n\t\tconst accessor = entity.fieldData.get(fieldPlaceholder)\n\t\tif (!accessor) {\n\t\t\treturn this.raiseInvalidHasManyRelationError(entity, entityList.hasManyRelation)\n\t\t}\n\n\t\treturn accessor.getAccessor() as EntityListAccessor\n\t}\n\n\tprivate raiseInvalidHasOneRelationError(relativeTo: EntityAccessor, hasOneRelation: HasOneRelation): never {\n\t\tTreeNodeUtils.resolveHasOneRelation(relativeTo.environment, hasOneRelation.field, !!hasOneRelation.reducedBy)\n\n\t\tconst entityMarker = relativeTo.getMarker()\n\t\tconst possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasOneRelationMarker, hasOneRelation.field)\n\t\tconst markerParams = possibleMarkers.map(it => PlaceholderParametersGenerator.createHasOneRelationParameters(it.parameters))\n\n\t\tconst relationParams = PlaceholderParametersGenerator.createHasOneRelationParameters(hasOneRelation)\n\n\t\tthis.raiseUndefinedRelationError(hasOneRelation.field, relativeTo.environment, relationParams, markerParams)\n\t}\n\n\tprivate raiseInvalidHasManyRelationError(entity: EntityAccessor, hasManyRelation: HasManyRelation): never {\n\t\tTreeNodeUtils.resolveHasManyRelation(entity.environment, hasManyRelation.field)\n\n\t\tconst entityMarker = entity.getMarker()\n\t\tconst possibleMarkers = this.getMarkersOfType(entityMarker.fields.markers, HasManyRelationMarker, hasManyRelation.field)\n\t\tconst markerParams = possibleMarkers.map(it => PlaceholderParametersGenerator.createHasManyRelationParameters(it.parameters))\n\n\t\tconst relationParams = PlaceholderParametersGenerator.createHasManyRelationParameters(hasManyRelation)\n\n\t\tthis.raiseUndefinedRelationError(hasManyRelation.field, entity.environment, relationParams, markerParams)\n\t}\n\n\tprivate raiseUndefinedRelationError(field: string, environment: Environment, relationParams: any, otherMarkersParams: any[]): never {\n\t\tconst hint = this.createHintMessage(otherMarkersParams)\n\t\tthrow new BindingError(''\n\t\t\t+ `EntityAccessor: cannot access relation `\n\t\t\t+ `'${field}' on ${TreeNodeUtils.describeLocation(environment)}.\\n`\n\t\t\t+ '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'\n\t\t\t+ 'Provided parameters:\\n'\n\t\t\t+ JSON.stringify(relationParams)\n\t\t\t+ hint,\n\t\t)\n\t}\n\n\tprivate createHintMessage(params: any[]): string {\n\t\tif (params.length === 0) {\n\t\t\treturn ''\n\t\t}\n\n\t\tconst serializedParams = params.map(it => JSON.stringify(it)).join('\\n')\n\t\tconst message = params.length === 1\n\t\t\t? 'However, there is existing marker for the same field, but it have different parameters:'\n\t\t\t: 'However, there are existing markers for the same field, but they have different parameters:'\n\n\t\treturn `\\n\\n${message}\\n${serializedParams}`\n\t}\n\n\tprivate getMarkersOfType<T extends { new(...args: any[]): HasManyRelationMarker | HasOneRelationMarker }>(\n\t\tmarkers: EntityFieldMarkers,\n\t\tmarkerConstructor: T,\n\t\tfieldName: string,\n\t): InstanceType<T>[] {\n\t\treturn Array.from(markers.values())\n\t\t\t.filter((it): it is InstanceType<T> => (it instanceof markerConstructor) && it.parameters.field === fieldName)\n\t}\n\n\tpublic getParent(): EntityAccessor | EntityListAccessor | undefined {\n\t\tconst blueprint = this.state.blueprint\n\t\tif (blueprint.type === 'subTree') {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprint.parent.getAccessor()\n\t}\n\n\tpublic getMarker() {\n\t\treturn getEntityMarker(this.state)\n\t}\n}\n\nnamespace EntityAccessor {\n\texport interface FieldDatum {\n\t\tgetAccessor(): NestedAccessor\n\t}\n\texport type NestedAccessor = EntityAccessor | EntityListAccessor | FieldAccessor<any>\n\n\texport type FieldValuePairs =\n\t\t| {\n\t\t\t\t[field: string]: FieldValue\n\t\t }\n\t\t| Iterable<[SugaredRelativeSingleField | string, FieldValue]>\n\n\texport type FieldData = Map<FieldName, FieldDatum>\n\n\texport type GetEntityAccessor = () => EntityAccessor\n\texport type BatchUpdatesHandler = (getAccessor: GetEntityAccessor, options: BatchUpdatesOptions) => void\n\texport type UpdateListener = (accessor: EntityAccessor) => void\n\n\texport type BeforePersistHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: AsyncBatchUpdatesOptions,\n\t) => void | Promise<void | BeforePersistHandler>\n\n\texport type PersistErrorHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: PersistErrorOptions,\n\t) => void | Promise<void>\n\n\texport type PersistSuccessHandler = (\n\t\tgetAccessor: GetEntityAccessor,\n\t\toptions: PersistSuccessOptions,\n\t) => void | Promise<void | PersistSuccessHandler>\n\n\texport interface RuntimeEntityEventListenerMap {\n\t\tbeforePersist: BeforePersistHandler\n\t\tbeforeUpdate: BatchUpdatesHandler\n\t\tconnectionUpdate: UpdateListener\n\t\tpersistError: PersistErrorHandler\n\t\tpersistSuccess: PersistSuccessHandler\n\t\tupdate: UpdateListener\n\t}\n\texport interface EntityEventListenerMap extends RuntimeEntityEventListenerMap {\n\t\tinitialize: BatchUpdatesHandler\n\t}\n\texport type EntityEventType = keyof EntityEventListenerMap\n}\n\nexport { EntityAccessor }\n"],"names":[],"mappings":";;;;;;;;;AAkCA,MAAM,eAAoC;AAAA,EAClC,YACW,OACA,YACA,WACD,KACA,MACC,WACA,gBACD,uBACA,QACA,aACA,aACf;AAXgB,SAAA,QA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AAC,EAAE,KAAK,IAAI;AACvE,UAAM,UAAU,OAAO,WAAW,IAC/B,4FACA;AAEI,WAAA;AAAA;AAAA,EAAO;AAAA,EAAY;AAAA,EAC3B;AAAA,EAEQ,iBACP,SACA,mBACA,WACoB;AACpB,WAAO,MAAM,KAAK,QAAQ,OAAQ,CAAA,EAChC,OAAO,CAAC,OAA+B,cAAc,qBAAsB,GAAG,WAAW,UAAU,SAAS;AAAA,EAC/G;AAAA,EAEO,YAA6D;AAC7D,UAAA,YAAY,KAAK,MAAM;AACzB,QAAA,UAAU,SAAS,WAAW;AAC1B,aAAA;AAAA,IACR;AACO,WAAA,UAAU,OAAO;EACzB;AAAA,EAEO,YAAY;AACX,WAAA,gBAAgB,KAAK,KAAK;AAAA,EAClC;AACD;"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { BindingError } from "../BindingError.js";
|
|
2
1
|
import { whereToFilter } from "@contember/client";
|
|
3
|
-
import
|
|
2
|
+
import { TreeNodeUtils } from "../utils/TreeNodeUtils.js";
|
|
4
3
|
import { QueryLanguage } from "../queryLanguage/QueryLanguage.js";
|
|
5
4
|
class TreeNodeEnvironmentFactory {
|
|
6
5
|
static createEnvironmentForEntityListSubtree(environment, sugaredEntityList) {
|
|
@@ -16,7 +15,7 @@ class TreeNodeEnvironmentFactory {
|
|
|
16
15
|
rootWhere = entityList.filter ?? {};
|
|
17
16
|
expectedCardinality = "zero-to-many";
|
|
18
17
|
}
|
|
19
|
-
const entitySchema =
|
|
18
|
+
const entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entityList.entityName, "entity list");
|
|
20
19
|
return environment.withSubTree({
|
|
21
20
|
type: "subtree-entity-list",
|
|
22
21
|
entity: entitySchema,
|
|
@@ -37,7 +36,7 @@ class TreeNodeEnvironmentFactory {
|
|
|
37
36
|
rootWhere = whereToFilter(entity.where);
|
|
38
37
|
expectedCardinality = entity.setOnCreate ? "zero-or-one" : "one";
|
|
39
38
|
}
|
|
40
|
-
const entitySchema =
|
|
39
|
+
const entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entity.entityName, "entity");
|
|
41
40
|
return environment.withSubTree({
|
|
42
41
|
type: "subtree-entity",
|
|
43
42
|
entity: entitySchema,
|
|
@@ -49,7 +48,7 @@ class TreeNodeEnvironmentFactory {
|
|
|
49
48
|
const relativeEntityList = QueryLanguage.desugarRelativeEntityList(sugaredRelativeEntityList, environment);
|
|
50
49
|
const hasOneEnvironment = this.traverseHasOnePath(environment, relativeEntityList.hasOneRelationPath);
|
|
51
50
|
const hasManyField = relativeEntityList.hasManyRelation.field;
|
|
52
|
-
const field =
|
|
51
|
+
const field = TreeNodeUtils.resolveHasManyRelation(hasOneEnvironment, hasManyField);
|
|
53
52
|
const targetEntity = environment.getSchema().getEntity(field.targetEntity);
|
|
54
53
|
return hasOneEnvironment.withSubTreeChild({
|
|
55
54
|
type: "entity-list",
|
|
@@ -64,7 +63,7 @@ class TreeNodeEnvironmentFactory {
|
|
|
64
63
|
static createEnvironmentForField(environment, sugaredRelativeSingleField) {
|
|
65
64
|
const relativeSingleField = QueryLanguage.desugarRelativeSingleField(sugaredRelativeSingleField, environment);
|
|
66
65
|
const hasOneEnvironment = this.traverseHasOnePath(environment, relativeSingleField.hasOneRelationPath);
|
|
67
|
-
const field =
|
|
66
|
+
const field = TreeNodeUtils.resolveColumn(hasOneEnvironment, relativeSingleField.field);
|
|
68
67
|
return hasOneEnvironment.withSubTreeChild({
|
|
69
68
|
type: "column",
|
|
70
69
|
entity: environment.getSubTreeNode().entity,
|
|
@@ -73,7 +72,7 @@ class TreeNodeEnvironmentFactory {
|
|
|
73
72
|
}
|
|
74
73
|
static traverseHasOnePath(environment, hasOneRelationPath) {
|
|
75
74
|
for (const pathItem of hasOneRelationPath) {
|
|
76
|
-
const field =
|
|
75
|
+
const field = TreeNodeUtils.resolveHasOneRelation(environment, pathItem.field, !!pathItem.reducedBy);
|
|
77
76
|
const targetEntity = environment.getSchema().getEntity(field.targetEntity);
|
|
78
77
|
environment = environment.withSubTreeChild({
|
|
79
78
|
type: "entity",
|
|
@@ -83,75 +82,6 @@ class TreeNodeEnvironmentFactory {
|
|
|
83
82
|
}
|
|
84
83
|
return environment;
|
|
85
84
|
}
|
|
86
|
-
static resolveHasOneRelation(environment, field, isReduced) {
|
|
87
|
-
if (isReduced) {
|
|
88
|
-
return this.resolveRelation(environment, field, "reduced has-one relation", ["ManyHasMany", "OneHasMany"]);
|
|
89
|
-
}
|
|
90
|
-
return this.resolveRelation(environment, field, "a has-one relation", ["ManyHasOne", "OneHasOne"]);
|
|
91
|
-
}
|
|
92
|
-
static assertHasManyRelation(environment, field) {
|
|
93
|
-
return this.resolveRelation(environment, field, "a has-many relation", ["ManyHasMany", "OneHasMany"]);
|
|
94
|
-
}
|
|
95
|
-
static resolveRelation(environment, field, relationDescription, expectedRelationType) {
|
|
96
|
-
return this.resolveField(environment, field, (field2) => {
|
|
97
|
-
return field2.__typename === "_Relation" && expectedRelationType.includes(field2.type);
|
|
98
|
-
}, relationDescription);
|
|
99
|
-
}
|
|
100
|
-
static resolveEntity(schema, entityName, type) {
|
|
101
|
-
const entity = schema.getEntityOrUndefined(entityName);
|
|
102
|
-
if (!entity) {
|
|
103
|
-
const alternative = this.recommendAlternative(entityName, schema.getEntityNames());
|
|
104
|
-
const didYouMean = alternative ? `Did you mean '${alternative}'?` : "";
|
|
105
|
-
throw new BindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${didYouMean}`);
|
|
106
|
-
}
|
|
107
|
-
return entity;
|
|
108
|
-
}
|
|
109
|
-
static resolveColumn(environment, fieldName) {
|
|
110
|
-
return this.resolveField(environment, fieldName, (field) => field.__typename === "_Column", "an ordinary field");
|
|
111
|
-
}
|
|
112
|
-
static resolveField(environment, fieldName, matcher, expectedDescription) {
|
|
113
|
-
const treeLocation = environment.getSubTreeNode();
|
|
114
|
-
const entity = treeLocation.entity;
|
|
115
|
-
const field = entity.fields.get(fieldName);
|
|
116
|
-
if (!field) {
|
|
117
|
-
const alternative = this.recommendAlternative(
|
|
118
|
-
fieldName,
|
|
119
|
-
Array.from(entity.fields).filter(([, field2]) => matcher(field2)).map(([fieldName2]) => fieldName2)
|
|
120
|
-
);
|
|
121
|
-
const didYouMean = alternative ? `Did you mean '${alternative}'?` : "";
|
|
122
|
-
throw new BindingError(`Field '${fieldName}' doesn't exist on ${this.describeLocation(environment)}. ${didYouMean}`);
|
|
123
|
-
}
|
|
124
|
-
if (!matcher(field)) {
|
|
125
|
-
const actual = field.__typename === "_Column" ? "an ordinary field" : `a ${field.type} relation`;
|
|
126
|
-
throw new BindingError(
|
|
127
|
-
`Invalid field: the name '${field.name}' on ${this.describeLocation(environment)} refers to ${actual} but is being used as a ${expectedDescription}.`
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
return field;
|
|
131
|
-
}
|
|
132
|
-
static describeLocation(environment) {
|
|
133
|
-
const path = [];
|
|
134
|
-
const loc = environment.getSubTreeNode();
|
|
135
|
-
for (let env = environment; ; env = env.getParent()) {
|
|
136
|
-
const node = env.getSubTreeNode();
|
|
137
|
-
if (node.type === "subtree-entity" || node.type === "subtree-entity-list") {
|
|
138
|
-
return `entity '${loc.entity.name}' in path '${[node.entity.name, ...path.reverse()].join(".")}'`;
|
|
139
|
-
}
|
|
140
|
-
path.push(node.field.name);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
static recommendAlternative(original, possibleAlternatives) {
|
|
144
|
-
let bestAlternative = void 0;
|
|
145
|
-
let bestAlternativeDistance = Number.MAX_SAFE_INTEGER;
|
|
146
|
-
for (const alternative of possibleAlternatives) {
|
|
147
|
-
const distance = levenshtein(original, alternative);
|
|
148
|
-
if (distance < bestAlternativeDistance) {
|
|
149
|
-
bestAlternative = alternative;
|
|
150
|
-
bestAlternativeDistance = distance;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return bestAlternative;
|
|
154
|
-
}
|
|
155
85
|
}
|
|
156
86
|
export {
|
|
157
87
|
TreeNodeEnvironmentFactory
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TreeNodeEnvironmentFactory.js","sources":["../../../src/dao/TreeNodeEnvironmentFactory.ts"],"sourcesContent":["import { Environment } from './Environment'\nimport {\n\tHasOneRelation,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredRelativeEntityList,\n\tSugaredRelativeSingleEntity,\n\tSugaredRelativeSingleField,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { QueryLanguage } from '../queryLanguage'\nimport { BindingError } from '../BindingError'\nimport { whereToFilter } from '@contember/client'\nimport { BaseRelation, Schema, SchemaColumn, SchemaEntity, SchemaField, SchemaRelation } from '../core/schema'\nimport levenshtein from 'js-levenshtein'\n\nexport class TreeNodeEnvironmentFactory {\n\n\tpublic static createEnvironmentForEntityListSubtree(\n\t\tenvironment: Environment,\n\t\tsugaredEntityList: SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t): Environment {\n\t\tlet entityList\n\t\tlet rootWhere\n\t\tlet expectedCardinality: Environment.SubTreeNode['expectedCardinality']\n\n\t\tif (sugaredEntityList.isCreating) {\n\t\t\tentityList = QueryLanguage.desugarUnconstrainedQualifiedEntityList(sugaredEntityList, environment)\n\t\t\trootWhere = {} as const\n\t\t\texpectedCardinality = 'zero'\n\n\t\t} else {\n\t\t\tentityList = QueryLanguage.desugarQualifiedEntityList(sugaredEntityList, environment)\n\t\t\trootWhere = entityList.filter ?? {}\n\t\t\texpectedCardinality = 'zero-to-many'\n\t\t}\n\n\t\tconst entitySchema = this.resolveEntity(environment.getSchema(), entityList.entityName, 'entity list')\n\n\t\treturn environment.withSubTree({\n\t\t\ttype: 'subtree-entity-list',\n\t\t\tentity: entitySchema,\n\t\t\texpectedCardinality,\n\t\t\tfilter: rootWhere,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntitySubtree(\n\t\tenvironment: Environment,\n\t\tsugaredEntityList: SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t): Environment {\n\t\tlet entity\n\t\tlet rootWhere\n\t\tlet expectedCardinality: Environment.SubTreeNode['expectedCardinality']\n\n\t\tif (sugaredEntityList.isCreating) {\n\t\t\trootWhere = { } as const\n\t\t\texpectedCardinality = 'zero'\n\t\t\tentity = QueryLanguage.desugarUnconstrainedQualifiedSingleEntity(sugaredEntityList, environment)\n\n\t\t} else {\n\t\t\tentity = QueryLanguage.desugarQualifiedSingleEntity(sugaredEntityList, environment, { missingSetOnCreate: 'fill' })\n\t\t\trootWhere = whereToFilter(entity.where)\n\t\t\texpectedCardinality = entity.setOnCreate ? 'zero-or-one' : 'one'\n\t\t}\n\n\t\tconst entitySchema = this.resolveEntity(environment.getSchema(), entity.entityName, 'entity')\n\n\t\treturn environment.withSubTree({\n\t\t\ttype: 'subtree-entity',\n\t\t\tentity: entitySchema,\n\t\t\texpectedCardinality: expectedCardinality,\n\t\t\tfilter: rootWhere,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntityList(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeEntityList: SugaredRelativeEntityList,\n\t): Environment {\n\t\tconst relativeEntityList = QueryLanguage.desugarRelativeEntityList(sugaredRelativeEntityList, environment)\n\t\tconst hasOneEnvironment = this.traverseHasOnePath(environment, relativeEntityList.hasOneRelationPath)\n\t\tconst hasManyField = relativeEntityList.hasManyRelation.field\n\n\t\tconst field = this.assertHasManyRelation(hasOneEnvironment, hasManyField)\n\n\t\tconst targetEntity = environment.getSchema().getEntity(field.targetEntity)\n\n\t\treturn hasOneEnvironment.withSubTreeChild({\n\t\t\ttype: 'entity-list',\n\t\t\tfield,\n\t\t\tentity: targetEntity,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntity(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeSingleEntity: SugaredRelativeSingleEntity,\n\t) {\n\t\tconst relativeSingleEntity = QueryLanguage.desugarRelativeSingleEntity(sugaredRelativeSingleEntity, environment)\n\n\t\treturn this.traverseHasOnePath(environment, relativeSingleEntity.hasOneRelationPath)\n\t}\n\n\tpublic static createEnvironmentForField(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeSingleField: SugaredRelativeSingleField,\n\t) {\n\t\tconst relativeSingleField = QueryLanguage.desugarRelativeSingleField(sugaredRelativeSingleField, environment)\n\t\tconst hasOneEnvironment = this.traverseHasOnePath(environment, relativeSingleField.hasOneRelationPath)\n\n\t\tconst field = this.resolveColumn(hasOneEnvironment, relativeSingleField.field)\n\n\t\treturn hasOneEnvironment.withSubTreeChild({\n\t\t\ttype: 'column',\n\t\t\tentity: environment.getSubTreeNode().entity,\n\t\t\tfield,\n\t\t})\n\t}\n\n\tprivate static traverseHasOnePath(\n\t\tenvironment: Environment,\n\t\thasOneRelationPath: HasOneRelation[],\n\t): Environment {\n\t\tfor (const pathItem of hasOneRelationPath) {\n\t\t\tconst field = this.resolveHasOneRelation(environment, pathItem.field, !!pathItem.reducedBy)\n\t\t\tconst targetEntity = environment.getSchema().getEntity(field.targetEntity)\n\n\t\t\tenvironment = environment.withSubTreeChild({\n\t\t\t\ttype: 'entity',\n\t\t\t\tfield,\n\t\t\t\tentity: targetEntity,\n\t\t\t})\n\t\t}\n\t\treturn environment\n\t}\n\n\tprivate static resolveHasOneRelation(environment: Environment, field: string, isReduced: boolean): SchemaRelation {\n\t\tif (isReduced) {\n\t\t\treturn this.resolveRelation(environment, field, 'reduced has-one relation', ['ManyHasMany', 'OneHasMany'])\n\t\t}\n\t\treturn this.resolveRelation(environment, field, 'a has-one relation', ['ManyHasOne', 'OneHasOne'])\n\t}\n\n\tprivate static assertHasManyRelation(environment: Environment, field: string): SchemaRelation {\n\t\treturn this.resolveRelation(environment, field, 'a has-many relation', ['ManyHasMany', 'OneHasMany'])\n\t}\n\n\n\tprivate static resolveRelation(environment: Environment, field: string, relationDescription: string, expectedRelationType: BaseRelation['type'][]): SchemaRelation {\n\t\treturn this.resolveField(environment, field, (field): field is SchemaRelation => {\n\t\t\treturn field.__typename === '_Relation' && expectedRelationType.includes(field.type)\n\t\t}, relationDescription)\n\t}\n\n\tprivate static resolveEntity(schema: Schema, entityName: string, type: 'entity' | 'entity list'): SchemaEntity {\n\t\tconst entity = schema.getEntityOrUndefined(entityName)\n\t\tif (!entity) {\n\t\t\tconst alternative = this.recommendAlternative(entityName, schema.getEntityNames())\n\t\t\tconst didYouMean = alternative ? `Did you mean '${alternative}'?` : ''\n\t\t\tthrow new BindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${didYouMean}`)\n\t\t}\n\t\treturn entity\n\t}\n\n\tprivate static resolveColumn(environment: Environment, fieldName: string): SchemaColumn {\n\t\treturn this.resolveField(environment, fieldName, (field): field is SchemaColumn => field.__typename === '_Column', 'an ordinary field')\n\t\t// TODO check that defaultValue matches the type\n\t\t// TODO run custom validators\n\t}\n\n\tprivate static resolveField<T extends SchemaField>(environment: Environment, fieldName: string, matcher: (field: SchemaField) => field is T, expectedDescription: string): T {\n\t\tconst treeLocation = environment.getSubTreeNode()\n\t\tconst entity = treeLocation.entity\n\t\tconst field = entity.fields.get(fieldName)\n\t\tif (!field) {\n\t\t\tconst alternative = this.recommendAlternative(\n\t\t\t\tfieldName,\n\t\t\t\tArray.from(entity.fields)\n\t\t\t\t\t.filter(([, field]) => matcher(field))\n\t\t\t\t\t.map(([fieldName]) => fieldName),\n\t\t\t)\n\t\t\tconst didYouMean = alternative ? `Did you mean '${alternative}'?` : ''\n\t\t\tthrow new BindingError(`Field '${fieldName}' doesn't exist on ${this.describeLocation(environment)}. ${didYouMean}`)\n\t\t}\n\t\tif (!matcher(field)) {\n\t\t\tconst actual = field.__typename === '_Column' ? 'an ordinary field' : `a ${field.type} relation`\n\t\t\tthrow new BindingError(\n\t\t\t\t`Invalid field: the name '${field.name}' on ${this.describeLocation(environment)} ` +\n\t\t\t\t`refers to ${actual} but is being used as a ${expectedDescription}.`,\n\t\t\t)\n\t\t}\n\t\treturn field\n\t}\n\n\tprivate static describeLocation(environment: Environment): string {\n\t\tconst path = []\n\t\tconst loc = environment.getSubTreeNode()\n\t\tfor (let env = environment;; env = env.getParent()) {\n\t\t\tconst node = env.getSubTreeNode()\n\t\t\tif (node.type === 'subtree-entity' || node.type === 'subtree-entity-list') {\n\t\t\t\treturn `entity '${loc.entity.name}' in path '${[node.entity.name, ...path.reverse()].join('.')}'`\n\t\t\t}\n\t\t\tpath.push(node.field.name)\n\t\t}\n\t}\n\n\tpublic static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined {\n\t\tlet bestAlternative: string | undefined = undefined\n\t\tlet bestAlternativeDistance = Number.MAX_SAFE_INTEGER\n\n\t\tfor (const alternative of possibleAlternatives) {\n\t\t\tconst distance = levenshtein(original, alternative)\n\n\t\t\tif (distance < bestAlternativeDistance) {\n\t\t\t\tbestAlternative = alternative\n\t\t\t\tbestAlternativeDistance = distance\n\t\t\t}\n\t\t}\n\t\treturn bestAlternative\n\t}\n}\n"],"names":["field","fieldName"],"mappings":";;;;AAiBO,MAAM,2BAA2B;AAAA,EAEvC,OAAc,sCACb,aACA,mBACc;AACV,QAAA;AACA,QAAA;AACA,QAAA;AAEJ,QAAI,kBAAkB,YAAY;AACpB,mBAAA,cAAc,wCAAwC,mBAAmB,WAAW;AACjG,kBAAY,CAAA;AACU,4BAAA;AAAA,IAAA,OAEhB;AACO,mBAAA,cAAc,2BAA2B,mBAAmB,WAAW;AACxE,kBAAA,WAAW,UAAU;AACX,4BAAA;AAAA,IACvB;AAEM,UAAA,eAAe,KAAK,cAAc,YAAY,aAAa,WAAW,YAAY,aAAa;AAErG,WAAO,YAAY,YAAY;AAAA,MAC9B,MAAM;AAAA,MACN,QAAQ;AAAA,MACR;AAAA,MACA,QAAQ;AAAA,IAAA,CACR;AAAA,EACF;AAAA,EAEA,OAAc,kCACb,aACA,mBACc;AACV,QAAA;AACA,QAAA;AACA,QAAA;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4D;AAChH,QAAI,kBAAsC;AAC1C,QAAI,0BAA0B,OAAO;AAErC,eAAW,eAAe,sBAAsB;AACzC,YAAA,WAAW,YAAY,UAAU,WAAW;AAElD,UAAI,WAAW,yBAAyB;AACrB,0BAAA;AACQ,kCAAA;AAAA,MAC3B;AAAA,IACD;AACO,WAAA;AAAA,EACR;AACD;"}
|
|
1
|
+
{"version":3,"file":"TreeNodeEnvironmentFactory.js","sources":["../../../src/dao/TreeNodeEnvironmentFactory.ts"],"sourcesContent":["import { Environment } from './Environment'\nimport {\n\tHasOneRelation,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredRelativeEntityList,\n\tSugaredRelativeSingleEntity,\n\tSugaredRelativeSingleField,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { QueryLanguage } from '../queryLanguage'\nimport { whereToFilter } from '@contember/client'\nimport { TreeNodeUtils } from '../utils/TreeNodeUtils'\n\nexport class TreeNodeEnvironmentFactory {\n\n\tpublic static createEnvironmentForEntityListSubtree(\n\t\tenvironment: Environment,\n\t\tsugaredEntityList: SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t): Environment {\n\t\tlet entityList\n\t\tlet rootWhere\n\t\tlet expectedCardinality: Environment.SubTreeNode['expectedCardinality']\n\n\t\tif (sugaredEntityList.isCreating) {\n\t\t\tentityList = QueryLanguage.desugarUnconstrainedQualifiedEntityList(sugaredEntityList, environment)\n\t\t\trootWhere = {} as const\n\t\t\texpectedCardinality = 'zero'\n\n\t\t} else {\n\t\t\tentityList = QueryLanguage.desugarQualifiedEntityList(sugaredEntityList, environment)\n\t\t\trootWhere = entityList.filter ?? {}\n\t\t\texpectedCardinality = 'zero-to-many'\n\t\t}\n\n\t\tconst entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entityList.entityName, 'entity list')\n\n\t\treturn environment.withSubTree({\n\t\t\ttype: 'subtree-entity-list',\n\t\t\tentity: entitySchema,\n\t\t\texpectedCardinality,\n\t\t\tfilter: rootWhere,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntitySubtree(\n\t\tenvironment: Environment,\n\t\tsugaredEntityList: SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t): Environment {\n\t\tlet entity\n\t\tlet rootWhere\n\t\tlet expectedCardinality: Environment.SubTreeNode['expectedCardinality']\n\n\t\tif (sugaredEntityList.isCreating) {\n\t\t\trootWhere = { } as const\n\t\t\texpectedCardinality = 'zero'\n\t\t\tentity = QueryLanguage.desugarUnconstrainedQualifiedSingleEntity(sugaredEntityList, environment)\n\n\t\t} else {\n\t\t\tentity = QueryLanguage.desugarQualifiedSingleEntity(sugaredEntityList, environment, { missingSetOnCreate: 'fill' })\n\t\t\trootWhere = whereToFilter(entity.where)\n\t\t\texpectedCardinality = entity.setOnCreate ? 'zero-or-one' : 'one'\n\t\t}\n\n\t\tconst entitySchema = TreeNodeUtils.resolveEntity(environment.getSchema(), entity.entityName, 'entity')\n\n\t\treturn environment.withSubTree({\n\t\t\ttype: 'subtree-entity',\n\t\t\tentity: entitySchema,\n\t\t\texpectedCardinality: expectedCardinality,\n\t\t\tfilter: rootWhere,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntityList(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeEntityList: SugaredRelativeEntityList,\n\t): Environment {\n\t\tconst relativeEntityList = QueryLanguage.desugarRelativeEntityList(sugaredRelativeEntityList, environment)\n\t\tconst hasOneEnvironment = this.traverseHasOnePath(environment, relativeEntityList.hasOneRelationPath)\n\t\tconst hasManyField = relativeEntityList.hasManyRelation.field\n\n\t\tconst field = TreeNodeUtils.resolveHasManyRelation(hasOneEnvironment, hasManyField)\n\n\t\tconst targetEntity = environment.getSchema().getEntity(field.targetEntity)\n\n\t\treturn hasOneEnvironment.withSubTreeChild({\n\t\t\ttype: 'entity-list',\n\t\t\tfield,\n\t\t\tentity: targetEntity,\n\t\t})\n\t}\n\n\tpublic static createEnvironmentForEntity(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeSingleEntity: SugaredRelativeSingleEntity,\n\t) {\n\t\tconst relativeSingleEntity = QueryLanguage.desugarRelativeSingleEntity(sugaredRelativeSingleEntity, environment)\n\n\t\treturn this.traverseHasOnePath(environment, relativeSingleEntity.hasOneRelationPath)\n\t}\n\n\tpublic static createEnvironmentForField(\n\t\tenvironment: Environment,\n\t\tsugaredRelativeSingleField: SugaredRelativeSingleField,\n\t) {\n\t\tconst relativeSingleField = QueryLanguage.desugarRelativeSingleField(sugaredRelativeSingleField, environment)\n\t\tconst hasOneEnvironment = this.traverseHasOnePath(environment, relativeSingleField.hasOneRelationPath)\n\n\t\tconst field = TreeNodeUtils.resolveColumn(hasOneEnvironment, relativeSingleField.field)\n\n\t\treturn hasOneEnvironment.withSubTreeChild({\n\t\t\ttype: 'column',\n\t\t\tentity: environment.getSubTreeNode().entity,\n\t\t\tfield,\n\t\t})\n\t}\n\n\tprivate static traverseHasOnePath(\n\t\tenvironment: Environment,\n\t\thasOneRelationPath: HasOneRelation[],\n\t): Environment {\n\t\tfor (const pathItem of hasOneRelationPath) {\n\t\t\tconst field = TreeNodeUtils.resolveHasOneRelation(environment, pathItem.field, !!pathItem.reducedBy)\n\t\t\tconst targetEntity = environment.getSchema().getEntity(field.targetEntity)\n\n\t\t\tenvironment = environment.withSubTreeChild({\n\t\t\t\ttype: 'entity',\n\t\t\t\tfield,\n\t\t\t\tentity: targetEntity,\n\t\t\t})\n\t\t}\n\t\treturn environment\n\t}\n\n}\n"],"names":[],"mappings":";;;AAeO,MAAM,2BAA2B;AAAA,EAEvC,OAAc,sCACb,aACA,mBACc;AACV,QAAA;AACA,QAAA;AACA,QAAA;AAEJ,QAAI,kBAAkB,YAAY;AACpB,mBAAA,cAAc,wCAAwC,mBAAmB,WAAW;AACjG,kBAAY,CAAA;AACU,4BAAA;AAAA,IAAA,OAEhB;AACO,mBAA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AE/G,WAAO,KAAK,mBAAmB,aAAa,qBAAqB,kBAAkB;AAAA,EACpF;AAAA,EAEA,OAAc,0BACb,aACA,4BACC;AACD,UAAM,sBAAsB,cAAc,2BAA2B,4BAA4B,WAAW;AAC5G,UAAM,oBAAoB,KAAK,mBAAmB,aAAa,oBAAoB,kBAAkB;AAErG,UAAM,QAAQ,cAAc,cAAc,mBAAmB,oBAAoB,KAAK;AAEtF,WAAO,kBAAkB,iBAAiB;AAAA,MACzC,MAAM;AAAA,MACN,QAAQ,YAAY,eAAA,EAAiB;AAAA,MACrC;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAEA,OAAe,mBACd,aACA,oBACc;AACd,eAAW,YAAY,oBAAoB;AACpC,YAAA,QAAQ,cAAc,sBAAsB,aAAa,SAAS,OAAO,CAAC,CAAC,SAAS,SAAS;AACnG,YAAM,eAAe,YAAY,UAAA,EAAY,UAAU,MAAM,YAAY;AAEzE,oBAAc,YAAY,iBAAiB;AAAA,QAC1C,MAAM;AAAA,QACN;AAAA,QACA,QAAQ;AAAA,MAAA,CACR;AAAA,IACF;AACO,WAAA;AAAA,EACR;AAED;"}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
import { PlaceholderParametersGenerator } from "./PlaceholderParametersGenerator.js";
|
|
1
2
|
import { Hashing } from "../utils/Hashing.js";
|
|
2
3
|
class PlaceholderGenerator {
|
|
3
4
|
static getFieldPlaceholder(fieldName) {
|
|
4
5
|
return fieldName;
|
|
5
6
|
}
|
|
6
7
|
static getHasOneRelationPlaceholder(relation) {
|
|
7
|
-
return `${relation.field}_${Hashing.
|
|
8
|
+
return `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasOneRelationParameters(relation))}`;
|
|
8
9
|
}
|
|
9
10
|
static getHasManyRelationPlaceholder(relation) {
|
|
10
|
-
return `${relation.field}_${Hashing.
|
|
11
|
+
return `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasManyRelationParameters(relation))}`;
|
|
11
12
|
}
|
|
12
13
|
static getEntitySubTreePlaceholder(subTreeParameters, environment) {
|
|
13
|
-
return `est_${Hashing.
|
|
14
|
+
return `est_${Hashing.hashAny(PlaceholderParametersGenerator.createEntitySubTreeParameters(subTreeParameters, environment))}`;
|
|
14
15
|
}
|
|
15
16
|
static getEntityListSubTreePlaceholder(subTreeParameters, environment) {
|
|
16
|
-
return `lst_${Hashing.
|
|
17
|
+
return `lst_${Hashing.hashAny(PlaceholderParametersGenerator.createEntityListSubTreeParameters(subTreeParameters, environment))}`;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlaceholderGenerator.js","sources":["../../../src/markers/PlaceholderGenerator.ts"],"sourcesContent":["import type {\n\tFieldName,\n\tHasManyRelation,\n\tHasOneRelation,\n\tQualifiedEntityList,\n\tQualifiedSingleEntity,\n\tUnconstrainedQualifiedEntityList,\n\tUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { Hashing } from '../utils'\nimport { Environment } from '../dao'\n\nexport class PlaceholderGenerator {\n\tpublic static getFieldPlaceholder(fieldName: FieldName): string {\n\t\treturn fieldName\n\t}\n\n\tpublic static getHasOneRelationPlaceholder(relation: HasOneRelation): string {\n\t\treturn `${relation.field}_${Hashing.
|
|
1
|
+
{"version":3,"file":"PlaceholderGenerator.js","sources":["../../../src/markers/PlaceholderGenerator.ts"],"sourcesContent":["import type {\n\tFieldName,\n\tHasManyRelation,\n\tHasOneRelation,\n\tQualifiedEntityList,\n\tQualifiedSingleEntity,\n\tUnconstrainedQualifiedEntityList,\n\tUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { Hashing } from '../utils'\nimport { Environment } from '../dao'\nimport { PlaceholderParametersGenerator } from './PlaceholderParametersGenerator'\n\nexport class PlaceholderGenerator {\n\tpublic static getFieldPlaceholder(fieldName: FieldName): string {\n\t\treturn fieldName // hashing intentionally ignored\n\t}\n\n\tpublic static getHasOneRelationPlaceholder(relation: HasOneRelation): string {\n\t\treturn `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasOneRelationParameters(relation))}`\n\t}\n\n\tpublic static getHasManyRelationPlaceholder(relation: HasManyRelation): string {\n\t\treturn `${relation.field}_${Hashing.hashAny(PlaceholderParametersGenerator.createHasManyRelationParameters(relation))}`\n\t}\n\n\tpublic static getEntitySubTreePlaceholder(\n\t\tsubTreeParameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity,\n\t\tenvironment: Environment,\n\t): string {\n\t\treturn `est_${Hashing.hashAny(PlaceholderParametersGenerator.createEntitySubTreeParameters(subTreeParameters, environment))}`\n\t}\n\n\tpublic static getEntityListSubTreePlaceholder(\n\t\tsubTreeParameters: QualifiedEntityList | UnconstrainedQualifiedEntityList,\n\t\tenvironment: Environment,\n\t): string {\n\t\treturn `lst_${Hashing.hashAny(PlaceholderParametersGenerator.createEntityListSubTreeParameters(subTreeParameters, environment))}`\n\t}\n}\n"],"names":[],"mappings":";;AAaO,MAAM,qBAAqB;AAAA,EACjC,OAAc,oBAAoB,WAA8B;AACxD,WAAA;AAAA,EACR;AAAA,EAEA,OAAc,6BAA6B,UAAkC;AACrE,WAAA,GAAG,SAAS,SAAS,QAAQ,QAAQ,+BAA+B,+BAA+B,QAAQ,CAAC;AAAA,EACpH;AAAA,EAEA,OAAc,8BAA8B,UAAmC;AACvE,WAAA,GAAG,SAAS,SAAS,QAAQ,QAAQ,+BAA+B,gCAAgC,QAAQ,CAAC;AAAA,EACrH;AAAA,EAEA,OAAc,4BACb,mBACA,aACS;AACT,WAAO,OAAO,QAAQ,QAAQ,+BAA+B,8BAA8B,mBAAmB,WAAW,CAAC;AAAA,EAC3H;AAAA,EAEA,OAAc,gCACb,mBACA,aACS;AACT,WAAO,OAAO,QAAQ,QAAQ,+BAA+B,kCAAkC,mBAAmB,WAAW,CAAC;AAAA,EAC/H;AACD;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class PlaceholderParametersGenerator {
|
|
2
|
+
static createHasOneRelationParameters(relation) {
|
|
3
|
+
return {
|
|
4
|
+
type: "hasOne",
|
|
5
|
+
field: relation.field,
|
|
6
|
+
filter: relation.filter,
|
|
7
|
+
reducedBy: relation.reducedBy
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
static createHasManyRelationParameters(relation) {
|
|
11
|
+
return {
|
|
12
|
+
type: "hasMany",
|
|
13
|
+
field: relation.field,
|
|
14
|
+
filter: relation.filter,
|
|
15
|
+
offset: relation.offset,
|
|
16
|
+
limit: relation.limit,
|
|
17
|
+
orderBy: relation.orderBy
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
static createEntityListSubTreeParameters(parameters, environment) {
|
|
21
|
+
if (parameters.isCreating) {
|
|
22
|
+
return {
|
|
23
|
+
type: "entityListSubtree",
|
|
24
|
+
isCreating: true,
|
|
25
|
+
entityName: parameters.entityName,
|
|
26
|
+
variables: environment.getAllVariables()
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
type: "entityListSubtree",
|
|
31
|
+
isCreating: false,
|
|
32
|
+
entityName: parameters.entityName,
|
|
33
|
+
filter: parameters.filter,
|
|
34
|
+
offset: parameters.offset,
|
|
35
|
+
limit: parameters.limit,
|
|
36
|
+
orderBy: parameters.orderBy,
|
|
37
|
+
variables: environment.getAllVariables()
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
static createEntitySubTreeParameters(parameters, environment) {
|
|
41
|
+
if (parameters.isCreating) {
|
|
42
|
+
return {
|
|
43
|
+
type: "entitySubTree",
|
|
44
|
+
isCreating: true,
|
|
45
|
+
entityName: parameters.entityName,
|
|
46
|
+
variables: environment.getAllVariables()
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
type: "entitySubTree",
|
|
51
|
+
isCreating: false,
|
|
52
|
+
where: parameters.where,
|
|
53
|
+
entityName: parameters.entityName,
|
|
54
|
+
filter: parameters.filter,
|
|
55
|
+
variables: environment.getAllVariables()
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
PlaceholderParametersGenerator
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=PlaceholderParametersGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlaceholderParametersGenerator.js","sources":["../../../src/markers/PlaceholderParametersGenerator.ts"],"sourcesContent":["import {\n\tHasManyRelation,\n\tHasOneRelation,\n\tQualifiedEntityList,\n\tQualifiedSingleEntity,\n\tUnconstrainedQualifiedEntityList,\n\tUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { Environment } from '../dao'\n\nexport class PlaceholderParametersGenerator {\n\n\tpublic static createHasOneRelationParameters(relation: HasOneRelation): any {\n\t\treturn {\n\t\t\ttype: 'hasOne',\n\t\t\tfield: relation.field,\n\t\t\tfilter: relation.filter,\n\t\t\treducedBy: relation.reducedBy,\n\t\t}\n\t}\n\n\tpublic static createHasManyRelationParameters(relation: HasManyRelation): any {\n\t\treturn {\n\t\t\ttype: 'hasMany',\n\t\t\tfield: relation.field,\n\t\t\tfilter: relation.filter,\n\t\t\toffset: relation.offset,\n\t\t\tlimit: relation.limit,\n\t\t\torderBy: relation.orderBy,\n\t\t}\n\t}\n\n\tpublic static createEntityListSubTreeParameters(\n\t\tparameters: QualifiedEntityList | UnconstrainedQualifiedEntityList,\n\t\tenvironment: Environment,\n\t): any {\n\t\tif (parameters.isCreating) {\n\t\t\treturn {\n\t\t\t\ttype: 'entityListSubtree',\n\t\t\t\tisCreating: true,\n\t\t\t\tentityName: parameters.entityName,\n\t\t\t\tvariables: environment.getAllVariables(),\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\ttype: 'entityListSubtree',\n\t\t\tisCreating: false,\n\t\t\tentityName: parameters.entityName,\n\t\t\tfilter: parameters.filter,\n\t\t\toffset: parameters.offset,\n\t\t\tlimit: parameters.limit,\n\t\t\torderBy: parameters.orderBy,\n\t\t\tvariables: environment.getAllVariables(),\n\t\t}\n\t}\n\n\tpublic static createEntitySubTreeParameters(\n\t\tparameters: QualifiedSingleEntity | UnconstrainedQualifiedSingleEntity,\n\t\tenvironment: Environment,\n\t): any {\n\t\tif (parameters.isCreating) {\n\t\t\treturn {\n\t\t\t\ttype: 'entitySubTree',\n\t\t\t\tisCreating: true,\n\t\t\t\tentityName: parameters.entityName,\n\t\t\t\tvariables: environment.getAllVariables(),\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\ttype: 'entitySubTree',\n\t\t\tisCreating: false,\n\t\t\twhere: parameters.where,\n\t\t\tentityName: parameters.entityName,\n\t\t\tfilter: parameters.filter,\n\t\t\tvariables: environment.getAllVariables(),\n\t\t}\n\t}\n\n}\n"],"names":[],"mappings":"AAUO,MAAM,+BAA+B;AAAA,EAE3C,OAAc,+BAA+B,UAA+B;AACpE,WAAA;AAAA,MACN,MAAM;AAAA,MACN,OAAO,SAAS;AAAA,MAChB,QAAQ,SAAS;AAAA,MACjB,WAAW,SAAS;AAAA,IAAA;AAAA,EAEtB;AAAA,EAEA,OAAc,gCAAgC,UAAgC;AACtE,WAAA;AAAA,MACN,MAAM;AAAA,MACN,OAAO,SAAS;AAAA,MAChB,QAAQ,SAAS;AAAA,MACjB,QAAQ,SAAS;AAAA,MACjB,OAAO,SAAS;AAAA,MAChB,SAAS,SAAS;AAAA,IAAA;AAAA,EAEpB;AAAA,EAEA,OAAc,kCACb,YACA,aACM;AACN,QAAI,WAAW,YAAY;AACnB,aAAA;AAAA,QACN,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,YAAY,WAAW;AAAA,QACvB,WAAW,YAAY,gBAAgB;AAAA,MAAA;AAAA,IAEzC;AACO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,YAAY,WAAW;AAAA,MACvB,QAAQ,WAAW;AAAA,MACnB,QAAQ,WAAW;AAAA,MACnB,OAAO,WAAW;AAAA,MAClB,SAAS,WAAW;AAAA,MACpB,WAAW,YAAY,gBAAgB;AAAA,IAAA;AAAA,EAEzC;AAAA,EAEA,OAAc,8BACb,YACA,aACM;AACN,QAAI,WAAW,YAAY;AACnB,aAAA;AAAA,QACN,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,YAAY,WAAW;AAAA,QACvB,WAAW,YAAY,gBAAgB;AAAA,MAAA;AAAA,IAEzC;AACO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,OAAO,WAAW;AAAA,MAClB,YAAY,WAAW;AAAA,MACvB,QAAQ,WAAW;AAAA,MACnB,WAAW,YAAY,gBAAgB;AAAA,IAAA;AAAA,EAEzC;AAED;"}
|
|
@@ -1,60 +1,6 @@
|
|
|
1
1
|
class Hashing {
|
|
2
|
-
static
|
|
3
|
-
const
|
|
4
|
-
"upToOne",
|
|
5
|
-
relation.field,
|
|
6
|
-
relation.filter,
|
|
7
|
-
relation.reducedBy
|
|
8
|
-
];
|
|
9
|
-
return Hashing.hashArray(where);
|
|
10
|
-
}
|
|
11
|
-
static hashHasManyRelation(relation) {
|
|
12
|
-
const where = [
|
|
13
|
-
"possiblyMany",
|
|
14
|
-
relation.field,
|
|
15
|
-
relation.filter,
|
|
16
|
-
"offset" in relation ? relation.offset : void 0,
|
|
17
|
-
"limit" in relation ? relation.limit : void 0,
|
|
18
|
-
"orderBy" in relation ? relation.orderBy : void 0
|
|
19
|
-
];
|
|
20
|
-
return Hashing.hashArray(where);
|
|
21
|
-
}
|
|
22
|
-
static hashEntityListSubTreeParameters(parameters, environment) {
|
|
23
|
-
if (parameters.isCreating) {
|
|
24
|
-
return Hashing.hashArray([
|
|
25
|
-
parameters.isCreating,
|
|
26
|
-
parameters.entityName,
|
|
27
|
-
environment.getAllVariables()
|
|
28
|
-
]);
|
|
29
|
-
}
|
|
30
|
-
return Hashing.hashArray([
|
|
31
|
-
parameters.isCreating,
|
|
32
|
-
parameters.entityName,
|
|
33
|
-
parameters.filter,
|
|
34
|
-
parameters.orderBy,
|
|
35
|
-
parameters.offset,
|
|
36
|
-
parameters.limit,
|
|
37
|
-
environment.getAllVariables()
|
|
38
|
-
]);
|
|
39
|
-
}
|
|
40
|
-
static hashEntitySubTreeParameters(parameters, environment) {
|
|
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.where,
|
|
51
|
-
parameters.entityName,
|
|
52
|
-
parameters.filter,
|
|
53
|
-
environment.getAllVariables()
|
|
54
|
-
]);
|
|
55
|
-
}
|
|
56
|
-
static hashArray(array) {
|
|
57
|
-
const json = array.map((item) => JSON.stringify(item)).join("_");
|
|
2
|
+
static hashAny(array) {
|
|
3
|
+
const json = JSON.stringify(array);
|
|
58
4
|
return Hashing.hash(json);
|
|
59
5
|
}
|
|
60
6
|
// Taken from Java
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Hashing.js","sources":["../../../src/utils/Hashing.ts"],"sourcesContent":["import type {\n\tHasManyRelation,\n\tHasOneRelation,\n\tQualifiedEntityList,\n\tQualifiedSingleEntity,\n\tUnconstrainedQualifiedEntityList,\n\tUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { Environment } from '../dao'\n\n// TODO update hashing so that for offset, 0 == undefined\nexport class Hashing {\n\tpublic static
|
|
1
|
+
{"version":3,"file":"Hashing.js","sources":["../../../src/utils/Hashing.ts"],"sourcesContent":["import type {\n\tHasManyRelation,\n\tHasOneRelation,\n\tQualifiedEntityList,\n\tQualifiedSingleEntity,\n\tUnconstrainedQualifiedEntityList,\n\tUnconstrainedQualifiedSingleEntity,\n} from '../treeParameters'\nimport { Environment } from '../dao'\n\n// TODO update hashing so that for offset, 0 == undefined\nexport class Hashing {\n\tpublic static hashAny(array: any[]): number {\n\t\tconst json = JSON.stringify(array)\n\t\treturn Hashing.hash(json)\n\t}\n\n\t// Taken from Java\n\tpublic static hash(str: string): number {\n\t\tlet hash = 0\n\t\tif (str.length === 0) {\n\t\t\treturn hash\n\t\t}\n\t\tfor (let i = 0; i < str.length; i++) {\n\t\t\thash = (hash << 5) - hash + str.charCodeAt(i)\n\t\t\thash = hash & hash // Convert to 32bit integer\n\t\t}\n\t\treturn Math.abs(hash)\n\t}\n}\n"],"names":[],"mappings":"AAWO,MAAM,QAAQ;AAAA,EACpB,OAAc,QAAQ,OAAsB;AACrC,UAAA,OAAO,KAAK,UAAU,KAAK;AAC1B,WAAA,QAAQ,KAAK,IAAI;AAAA,EACzB;AAAA;AAAA,EAGA,OAAc,KAAK,KAAqB;AACvC,QAAI,OAAO;AACP,QAAA,IAAI,WAAW,GAAG;AACd,aAAA;AAAA,IACR;AACA,aAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACpC,cAAQ,QAAQ,KAAK,OAAO,IAAI,WAAW,CAAC;AAC5C,aAAO,OAAO;AAAA,IACf;AACO,WAAA,KAAK,IAAI,IAAI;AAAA,EACrB;AACD;"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BindingError } from "../BindingError.js";
|
|
2
|
+
import levenshtein from "js-levenshtein";
|
|
3
|
+
class TreeNodeUtils {
|
|
4
|
+
static resolveEntity(schema, entityName, type) {
|
|
5
|
+
const entity = schema.getEntityOrUndefined(entityName);
|
|
6
|
+
if (!entity) {
|
|
7
|
+
const alternative = this.recommendAlternative(entityName, schema.getEntityNames());
|
|
8
|
+
const didYouMean = alternative ? `Did you mean '${alternative}'?` : "";
|
|
9
|
+
throw new BindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${didYouMean}`);
|
|
10
|
+
}
|
|
11
|
+
return entity;
|
|
12
|
+
}
|
|
13
|
+
static resolveHasOneRelation(environment, field, isReduced) {
|
|
14
|
+
if (isReduced) {
|
|
15
|
+
return this.resolveRelation(environment, field, "reduced has-one relation", ["ManyHasMany", "OneHasMany"]);
|
|
16
|
+
}
|
|
17
|
+
return this.resolveRelation(environment, field, "a has-one relation", ["ManyHasOne", "OneHasOne"]);
|
|
18
|
+
}
|
|
19
|
+
static resolveHasManyRelation(environment, field) {
|
|
20
|
+
return this.resolveRelation(environment, field, "a has-many relation", ["ManyHasMany", "OneHasMany"]);
|
|
21
|
+
}
|
|
22
|
+
static resolveRelation(environment, field, relationDescription, expectedRelationType) {
|
|
23
|
+
return this.resolveField(environment, field, (field2) => {
|
|
24
|
+
return field2.__typename === "_Relation" && expectedRelationType.includes(field2.type);
|
|
25
|
+
}, relationDescription);
|
|
26
|
+
}
|
|
27
|
+
static resolveColumn(environment, fieldName) {
|
|
28
|
+
return this.resolveField(environment, fieldName, (field) => field.__typename === "_Column", "an ordinary field");
|
|
29
|
+
}
|
|
30
|
+
static resolveField(environment, fieldName, matcher, expectedDescription) {
|
|
31
|
+
const treeLocation = environment.getSubTreeNode();
|
|
32
|
+
const entity = treeLocation.entity;
|
|
33
|
+
const field = entity.fields.get(fieldName);
|
|
34
|
+
if (!field) {
|
|
35
|
+
const alternative = this.recommendAlternative(
|
|
36
|
+
fieldName,
|
|
37
|
+
Array.from(entity.fields).filter(([, field2]) => matcher(field2)).map(([fieldName2]) => fieldName2)
|
|
38
|
+
);
|
|
39
|
+
const didYouMean = alternative ? `Did you mean '${alternative}'?` : "";
|
|
40
|
+
throw new BindingError(`Field '${fieldName}' doesn't exist on ${this.describeLocation(environment)}. ${didYouMean}`);
|
|
41
|
+
}
|
|
42
|
+
if (!matcher(field)) {
|
|
43
|
+
const actual = field.__typename === "_Column" ? "an ordinary field" : `a ${field.type} relation`;
|
|
44
|
+
throw new BindingError(
|
|
45
|
+
`Invalid field: the name '${field.name}' on ${this.describeLocation(environment)} refers to ${actual} but is being used as a ${expectedDescription}.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return field;
|
|
49
|
+
}
|
|
50
|
+
static describeLocation(environment) {
|
|
51
|
+
const path = [];
|
|
52
|
+
const loc = environment.getSubTreeNode();
|
|
53
|
+
for (let env = environment; ; env = env.getParent()) {
|
|
54
|
+
const node = env.getSubTreeNode();
|
|
55
|
+
if (node.type === "subtree-entity" || node.type === "subtree-entity-list") {
|
|
56
|
+
return `entity '${loc.entity.name}' in path '${[node.entity.name, ...path.reverse()].join(".")}'`;
|
|
57
|
+
}
|
|
58
|
+
path.push(node.field.name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
static recommendAlternative(original, possibleAlternatives) {
|
|
62
|
+
let bestAlternative = void 0;
|
|
63
|
+
let bestAlternativeDistance = Number.MAX_SAFE_INTEGER;
|
|
64
|
+
for (const alternative of possibleAlternatives) {
|
|
65
|
+
const distance = levenshtein(original, alternative);
|
|
66
|
+
if (distance < bestAlternativeDistance) {
|
|
67
|
+
bestAlternative = alternative;
|
|
68
|
+
bestAlternativeDistance = distance;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return bestAlternative;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
TreeNodeUtils
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=TreeNodeUtils.js.map
|