@contember/binding 1.1.0-alpha.5 → 1.1.0-alpha.8
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/accessorTree/NormalizedPersistedData.js.map +1 -1
- package/dist/development/core/AccessorErrorManager.js +2 -2
- package/dist/development/core/AccessorErrorManager.js.map +1 -1
- package/dist/development/core/DataBinding.js +8 -7
- package/dist/development/core/DataBinding.js.map +1 -1
- package/dist/development/core/ErrorsPreprocessor.js +9 -13
- package/dist/development/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/development/core/MutationGenerator.js +79 -43
- package/dist/development/core/MutationGenerator.js.map +1 -1
- package/dist/development/core/RequestResponseNormalizer.js +93 -130
- package/dist/development/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/development/core/StateInitializer.js +5 -1
- package/dist/development/core/StateInitializer.js.map +1 -1
- package/dist/development/core/TreeAugmenter.js +4 -4
- package/dist/development/core/TreeAugmenter.js.map +1 -1
- package/dist/development/core/TreeStore.js +4 -4
- package/dist/development/core/TreeStore.js.map +1 -1
- package/dist/development/core/VariableInputTransformer.js.map +1 -1
- package/dist/development/core/requestAliases/MutationAlias.js +2 -40
- package/dist/development/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/development/coreComponents/Entity.js +1 -1
- package/dist/development/coreComponents/Entity.js.map +1 -1
- package/dist/development/coreComponents/EntityList.js +1 -1
- package/dist/development/coreComponents/EntityList.js.map +1 -1
- package/dist/development/helperComponents/helpers/evaluateCondition.js.map +1 -1
- package/dist/production/accessorTree/NormalizedPersistedData.js.map +1 -1
- package/dist/production/core/AccessorErrorManager.js +2 -2
- package/dist/production/core/AccessorErrorManager.js.map +1 -1
- package/dist/production/core/DataBinding.js +8 -7
- package/dist/production/core/DataBinding.js.map +1 -1
- package/dist/production/core/ErrorsPreprocessor.js +9 -13
- package/dist/production/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/production/core/MutationGenerator.js +79 -43
- package/dist/production/core/MutationGenerator.js.map +1 -1
- package/dist/production/core/RequestResponseNormalizer.js +93 -130
- package/dist/production/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/production/core/StateInitializer.js +5 -1
- package/dist/production/core/StateInitializer.js.map +1 -1
- package/dist/production/core/TreeAugmenter.js +4 -4
- package/dist/production/core/TreeAugmenter.js.map +1 -1
- package/dist/production/core/TreeStore.js +4 -4
- package/dist/production/core/TreeStore.js.map +1 -1
- package/dist/production/core/VariableInputTransformer.js.map +1 -1
- package/dist/production/core/requestAliases/MutationAlias.js +2 -33
- package/dist/production/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/production/coreComponents/Entity.js +1 -1
- package/dist/production/coreComponents/Entity.js.map +1 -1
- package/dist/production/coreComponents/EntityList.js +1 -1
- package/dist/production/coreComponents/EntityList.js.map +1 -1
- package/dist/production/helperComponents/helpers/evaluateCondition.js.map +1 -1
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts +2 -2
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts.map +1 -1
- package/dist/types/accessorTree/QueryRequestResponse.d.ts +2 -2
- package/dist/types/accessorTree/QueryRequestResponse.d.ts.map +1 -1
- package/dist/types/core/AccessorErrorManager.d.ts +2 -1
- package/dist/types/core/AccessorErrorManager.d.ts.map +1 -1
- package/dist/types/core/DataBinding.d.ts.map +1 -1
- package/dist/types/core/ErrorsPreprocessor.d.ts +3 -1
- package/dist/types/core/ErrorsPreprocessor.d.ts.map +1 -1
- package/dist/types/core/MutationGenerator.d.ts +25 -1
- package/dist/types/core/MutationGenerator.d.ts.map +1 -1
- package/dist/types/core/RequestResponseNormalizer.d.ts +6 -7
- package/dist/types/core/RequestResponseNormalizer.d.ts.map +1 -1
- package/dist/types/core/StateInitializer.d.ts.map +1 -1
- package/dist/types/core/TreeAugmenter.d.ts +3 -2
- package/dist/types/core/TreeAugmenter.d.ts.map +1 -1
- package/dist/types/core/TreeStore.d.ts +3 -2
- package/dist/types/core/TreeStore.d.ts.map +1 -1
- package/dist/types/core/VariableInputTransformer.d.ts +2 -2
- package/dist/types/core/VariableInputTransformer.d.ts.map +1 -1
- package/dist/types/core/requestAliases/MutationAlias.d.ts +0 -3
- package/dist/types/core/requestAliases/MutationAlias.d.ts.map +1 -1
- package/dist/types/core/requestAliases/index.d.ts +0 -3
- package/dist/types/core/requestAliases/index.d.ts.map +1 -1
- package/dist/types/core/state/FieldState.d.ts +2 -2
- package/dist/types/core/state/FieldState.d.ts.map +1 -1
- package/dist/types/coreComponents/Field.d.ts +1 -1
- package/dist/types/coreComponents/Field.d.ts.map +1 -1
- package/dist/types/entityEvents/useDerivedField.d.ts +2 -2
- package/dist/types/entityEvents/useDerivedField.d.ts.map +1 -1
- package/dist/types/helperComponents/FieldView.d.ts +7 -7
- package/dist/types/helperComponents/FieldView.d.ts.map +1 -1
- package/dist/types/helperComponents/SugaredField.d.ts +1 -1
- package/dist/types/helperComponents/SugaredField.d.ts.map +1 -1
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts +2 -2
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts.map +1 -1
- package/dist/types/markers/FieldMarker.d.ts +3 -3
- package/dist/types/markers/FieldMarker.d.ts.map +1 -1
- package/dist/types/treeParameters/primitives/FieldValue.d.ts +2 -2
- package/dist/types/treeParameters/primitives/FieldValue.d.ts.map +1 -1
- package/dist/types/treeParameters/primitives/Json.d.ts +7 -0
- package/dist/types/treeParameters/primitives/Json.d.ts.map +1 -0
- package/dist/types/treeParameters/primitives/index.d.ts +1 -0
- package/dist/types/treeParameters/primitives/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/accessorTree/NormalizedPersistedData.ts +2 -2
- package/src/accessorTree/QueryRequestResponse.ts +2 -2
- package/src/core/AccessorErrorManager.ts +3 -2
- package/src/core/DataBinding.ts +8 -6
- package/src/core/ErrorsPreprocessor.ts +14 -16
- package/src/core/MutationGenerator.ts +112 -56
- package/src/core/RequestResponseNormalizer.ts +110 -161
- package/src/core/StateInitializer.ts +10 -6
- package/src/core/TreeAugmenter.ts +5 -4
- package/src/core/TreeStore.ts +5 -4
- package/src/core/VariableInputTransformer.ts +2 -2
- package/src/core/requestAliases/MutationAlias.ts +2 -57
- package/src/core/requestAliases/index.ts +0 -3
- package/src/core/state/FieldState.ts +2 -2
- package/src/coreComponents/Entity.tsx +1 -1
- package/src/coreComponents/EntityList.tsx +1 -1
- package/src/helperComponents/helpers/evaluateCondition.ts +2 -2
- package/src/treeParameters/primitives/FieldValue.ts +2 -2
- package/src/treeParameters/primitives/Json.ts +7 -0
- package/src/treeParameters/primitives/index.ts +1 -0
- package/dist/development/core/requestAliases/mutationOperationSubTreeType.js +0 -6
- package/dist/development/core/requestAliases/mutationOperationSubTreeType.js.map +0 -1
- package/dist/development/core/requestAliases/mutationOperationType.js +0 -7
- package/dist/development/core/requestAliases/mutationOperationType.js.map +0 -1
- package/dist/production/core/requestAliases/mutationOperationSubTreeType.js +0 -6
- package/dist/production/core/requestAliases/mutationOperationSubTreeType.js.map +0 -1
- package/dist/production/core/requestAliases/mutationOperationType.js +0 -7
- package/dist/production/core/requestAliases/mutationOperationType.js.map +0 -1
- package/dist/types/core/requestAliases/TopLevelMutationOperation.d.ts +0 -11
- package/dist/types/core/requestAliases/TopLevelMutationOperation.d.ts.map +0 -1
- package/dist/types/core/requestAliases/mutationOperationSubTreeType.d.ts +0 -5
- package/dist/types/core/requestAliases/mutationOperationSubTreeType.d.ts.map +0 -1
- package/dist/types/core/requestAliases/mutationOperationType.d.ts +0 -6
- package/dist/types/core/requestAliases/mutationOperationType.d.ts.map +0 -1
- package/src/core/requestAliases/TopLevelMutationOperation.ts +0 -11
- package/src/core/requestAliases/mutationOperationSubTreeType.ts +0 -4
- package/src/core/requestAliases/mutationOperationType.ts +0 -5
|
@@ -1,212 +1,161 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EntityFieldPersistedData,
|
|
3
2
|
NormalizedPersistedData,
|
|
4
3
|
PersistedEntityDataStore,
|
|
5
4
|
ReceivedDataTree,
|
|
6
5
|
ReceivedEntityData,
|
|
7
|
-
ReceivedFieldData,
|
|
8
6
|
ServerId,
|
|
9
|
-
SingleEntityPersistedData,
|
|
10
7
|
} from '../accessorTree'
|
|
11
8
|
import { BindingError } from '../BindingError'
|
|
12
9
|
import { PRIMARY_KEY_NAME } from '../bindingTypes'
|
|
13
|
-
import { assertNever } from '../utils'
|
|
14
|
-
import { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'
|
|
15
10
|
import { EntityId } from '../treeParameters'
|
|
11
|
+
import {
|
|
12
|
+
EntityFieldMarkers,
|
|
13
|
+
EntityListSubTreeMarker,
|
|
14
|
+
EntitySubTreeMarker,
|
|
15
|
+
FieldMarker,
|
|
16
|
+
HasManyRelationMarker,
|
|
17
|
+
HasOneRelationMarker,
|
|
18
|
+
MarkerTreeRoot,
|
|
19
|
+
} from '../markers'
|
|
20
|
+
import { SubMutationOperation } from './MutationGenerator'
|
|
21
|
+
import { assertNever } from '../utils'
|
|
16
22
|
|
|
17
23
|
export class RequestResponseNormalizer {
|
|
18
24
|
public static mergeInQueryResponse(
|
|
19
25
|
original: NormalizedPersistedData,
|
|
20
26
|
newPersistedData: ReceivedDataTree,
|
|
21
|
-
|
|
27
|
+
markerTree: MarkerTreeRoot,
|
|
28
|
+
): void {
|
|
22
29
|
const { subTreeDataStore, persistedEntityDataStore } = original
|
|
23
|
-
const presentIds: Set<string> = new Set()
|
|
24
|
-
|
|
25
|
-
for (const placeholderName in newPersistedData) {
|
|
26
|
-
const treeDatum = newPersistedData[placeholderName]
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
for (const [placeholder, marker] of markerTree.subTrees) {
|
|
32
|
+
const data = newPersistedData[placeholder] ?? null
|
|
33
|
+
if (marker instanceof EntitySubTreeMarker) {
|
|
34
|
+
if (data === null) {
|
|
35
|
+
subTreeDataStore.delete(placeholder)
|
|
36
|
+
} else {
|
|
37
|
+
if (Array.isArray(data)) {
|
|
38
|
+
throw new BindingError()
|
|
39
|
+
}
|
|
40
|
+
const id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)
|
|
41
|
+
subTreeDataStore.set(placeholder, id)
|
|
42
|
+
}
|
|
43
|
+
} else if (EntityListSubTreeMarker) {
|
|
44
|
+
const subTreeListIds = new Set<EntityId>()
|
|
45
|
+
if (!Array.isArray(data)) {
|
|
46
|
+
throw new BindingError()
|
|
47
|
+
}
|
|
48
|
+
for (const row of data) {
|
|
49
|
+
const id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)
|
|
50
|
+
subTreeListIds.add(id.value)
|
|
51
|
+
}
|
|
52
|
+
subTreeDataStore.set(placeholder, subTreeListIds)
|
|
34
53
|
} else {
|
|
35
|
-
|
|
54
|
+
throw new BindingError()
|
|
36
55
|
}
|
|
37
56
|
}
|
|
38
|
-
return presentIds
|
|
39
57
|
}
|
|
40
58
|
|
|
41
59
|
public static mergeInMutationResponse(
|
|
42
60
|
original: NormalizedPersistedData,
|
|
43
61
|
newPersistedData: ReceivedDataTree,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const presentIds: Set<string> = new Set()
|
|
62
|
+
operations: SubMutationOperation[],
|
|
63
|
+
): void {
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
const operation = MutationAlias.decodeTopLevel(operationAlias)
|
|
65
|
+
const { subTreeDataStore, persistedEntityDataStore } = original
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
const { type, entityId, subTreeType, subTreePlaceholder } = operation
|
|
67
|
+
for (const operation of operations) {
|
|
68
|
+
const { subTreePlaceholder, subTreeType, alias } = operation
|
|
55
69
|
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return this.rejectData()
|
|
70
|
+
const receivedData = newPersistedData[alias]
|
|
71
|
+
if (!receivedData || Array.isArray(receivedData)) {
|
|
72
|
+
throw new BindingError()
|
|
60
73
|
}
|
|
61
74
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} else if (list === undefined) {
|
|
77
|
-
// That's fine. This is probably just an isCreating sub-tree.
|
|
78
|
-
subTreeDataStore.set(subTreePlaceholder, new Set([fieldData.value]))
|
|
79
|
-
} else {
|
|
80
|
-
return this.rejectData()
|
|
81
|
-
}
|
|
82
|
-
}
|
|
75
|
+
if (operation.type === 'update' || operation.type === 'create') {
|
|
76
|
+
const id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)
|
|
77
|
+
if (subTreeType === 'single') {
|
|
78
|
+
subTreeDataStore.set(subTreePlaceholder, id)
|
|
79
|
+
} else if (subTreeType === 'list') {
|
|
80
|
+
const list = subTreeDataStore.get(subTreePlaceholder)
|
|
81
|
+
|
|
82
|
+
if (list instanceof Set) {
|
|
83
|
+
// TODO this is somewhat dubious because we're essentially just guessing the order of the entities
|
|
84
|
+
// and just carelessly put the new one at the end.
|
|
85
|
+
list.add(id.value)
|
|
86
|
+
} else if (list === undefined) {
|
|
87
|
+
// That's fine. This is probably just an isCreating sub-tree.
|
|
88
|
+
subTreeDataStore.set(subTreePlaceholder, new Set([id.value]))
|
|
83
89
|
} else {
|
|
84
|
-
|
|
90
|
+
throw new BindingError()
|
|
85
91
|
}
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
} else {
|
|
93
|
+
return assertNever(subTreeType)
|
|
88
94
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
list.delete(entityId)
|
|
102
|
-
} else {
|
|
103
|
-
return assertNever(subTreeType)
|
|
95
|
+
} else if (operation.type === 'delete') {
|
|
96
|
+
const id = this.extractId(receivedData)
|
|
97
|
+
// TODO there are potentially some references to entityId that this whole process won't quite remove.
|
|
98
|
+
// That's a memory leak. Probably not particularly severe in most cases but still.
|
|
99
|
+
persistedEntityDataStore.delete(id.uniqueValue)
|
|
100
|
+
if (subTreeType === 'single') {
|
|
101
|
+
subTreeDataStore.delete(subTreePlaceholder)
|
|
102
|
+
} else if (subTreeType === 'list') {
|
|
103
|
+
const list = subTreeDataStore.get(subTreePlaceholder)
|
|
104
|
+
|
|
105
|
+
if (!(list instanceof Set)) {
|
|
106
|
+
throw new BindingError()
|
|
104
107
|
}
|
|
105
|
-
|
|
108
|
+
list.delete(id.value)
|
|
109
|
+
} else {
|
|
110
|
+
return assertNever(subTreeType)
|
|
106
111
|
}
|
|
107
|
-
|
|
108
|
-
return assertNever(
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return presentIds
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private static createFieldData(
|
|
115
|
-
entityMap: PersistedEntityDataStore,
|
|
116
|
-
fieldData: ReceivedFieldData,
|
|
117
|
-
): EntityFieldPersistedData {
|
|
118
|
-
if (Array.isArray(fieldData)) {
|
|
119
|
-
const subTreeListIds = new Set<EntityId>()
|
|
120
|
-
for (const entityDatum of fieldData) {
|
|
121
|
-
subTreeListIds.add(this.createEntityData(entityMap, entityDatum).value)
|
|
122
|
-
}
|
|
123
|
-
return subTreeListIds
|
|
124
|
-
} else if (fieldData !== null && typeof fieldData === 'object') {
|
|
125
|
-
return this.createEntityData(entityMap, fieldData)
|
|
126
|
-
} else {
|
|
127
|
-
return fieldData
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData): ServerId {
|
|
132
|
-
const primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]
|
|
133
|
-
|
|
134
|
-
if (primaryKey === undefined) {
|
|
135
|
-
throw new BindingError(`The server has responded with an entity that lacks a primary key.`)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const id = new ServerId(primaryKey, entityData.__typename)
|
|
139
|
-
|
|
140
|
-
const presentEntityData = entityMap.get(id.uniqueValue)
|
|
141
|
-
|
|
142
|
-
if (presentEntityData === undefined) {
|
|
143
|
-
const fieldsMap: SingleEntityPersistedData = new Map()
|
|
144
|
-
entityMap.set(id.uniqueValue, fieldsMap)
|
|
145
|
-
for (const field in entityData) {
|
|
146
|
-
const fieldDatum = entityData[field]
|
|
147
|
-
|
|
148
|
-
fieldsMap.set(field, this.createFieldData(entityMap, fieldDatum))
|
|
112
|
+
} else {
|
|
113
|
+
return assertNever(operation)
|
|
149
114
|
}
|
|
150
|
-
} else {
|
|
151
|
-
this.mergeInEntityData(entityMap, presentEntityData, entityData)
|
|
152
115
|
}
|
|
153
|
-
return id
|
|
154
116
|
}
|
|
155
117
|
|
|
156
|
-
private static
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const newId = newDatum[PRIMARY_KEY_NAME]
|
|
169
|
-
const target = entityMap.get(ServerId.formatUniqueValue(newId, newDatum.__typename))
|
|
170
|
-
if (target === undefined) {
|
|
171
|
-
return this.createEntityData(entityMap, newDatum)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (fromTarget?.value === newId) {
|
|
175
|
-
this.mergeInEntityData(entityMap, target, newDatum)
|
|
176
|
-
return fromTarget
|
|
118
|
+
private static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {
|
|
119
|
+
const id = this.extractId(entityData)
|
|
120
|
+
const presentEntityData = entityMap.get(id.uniqueValue) ?? new Map()
|
|
121
|
+
entityMap.set(id.uniqueValue, presentEntityData)
|
|
122
|
+
|
|
123
|
+
for (const [placeholder, field] of fields) {
|
|
124
|
+
const value = entityData[placeholder]
|
|
125
|
+
if (field instanceof FieldMarker) {
|
|
126
|
+
presentEntityData.set(placeholder, value)
|
|
127
|
+
} else if (field instanceof HasOneRelationMarker) {
|
|
128
|
+
if (value === null) {
|
|
129
|
+
presentEntityData.set(placeholder, null)
|
|
177
130
|
} else {
|
|
178
|
-
|
|
131
|
+
presentEntityData.set(placeholder, this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers))
|
|
179
132
|
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
133
|
+
} else if (field instanceof HasManyRelationMarker) {
|
|
134
|
+
const ids = presentEntityData.get(placeholder) ?? new Set()
|
|
135
|
+
presentEntityData.set(placeholder, ids)
|
|
136
|
+
if (!(ids instanceof Set)) {
|
|
137
|
+
throw new BindingError()
|
|
138
|
+
}
|
|
139
|
+
ids.clear()
|
|
140
|
+
for (const row of value as ReceivedEntityData[]) {
|
|
141
|
+
const id = this.processEntityData(entityMap, row, field.fields.markers)
|
|
142
|
+
ids.add(id.value)
|
|
186
143
|
}
|
|
187
|
-
|
|
144
|
+
} else {
|
|
145
|
+
throw new Error()
|
|
188
146
|
}
|
|
189
|
-
} else if (!Array.isArray(newDatum) && (newDatum === null || typeof newDatum !== 'object')) {
|
|
190
|
-
return newDatum
|
|
191
147
|
}
|
|
192
|
-
|
|
148
|
+
|
|
149
|
+
return id
|
|
193
150
|
}
|
|
194
151
|
|
|
195
|
-
private static
|
|
196
|
-
|
|
197
|
-
target: SingleEntityPersistedData,
|
|
198
|
-
newData: ReceivedEntityData,
|
|
199
|
-
): SingleEntityPersistedData {
|
|
200
|
-
for (const field in newData) {
|
|
201
|
-
const fromTarget = target.get(field)
|
|
202
|
-
const newDatum = newData[field]
|
|
152
|
+
private static extractId(entityData: ReceivedEntityData): ServerId {
|
|
153
|
+
const primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]
|
|
203
154
|
|
|
204
|
-
|
|
155
|
+
if (primaryKey === undefined) {
|
|
156
|
+
throw new BindingError(`The server has responded with an entity that lacks a primary key.`)
|
|
205
157
|
}
|
|
206
|
-
return target
|
|
207
|
-
}
|
|
208
158
|
|
|
209
|
-
|
|
210
|
-
throw new BindingError(`Failed to process data received from the API.${extraMessage ? `\n${extraMessage}` : ''}`)
|
|
159
|
+
return new ServerId(primaryKey, entityData.__typename)
|
|
211
160
|
}
|
|
212
161
|
}
|
|
@@ -10,13 +10,15 @@ import {
|
|
|
10
10
|
} from '../markers'
|
|
11
11
|
import { BijectiveIndexedMap } from '../structures'
|
|
12
12
|
import type {
|
|
13
|
-
EntityEventListenerStore,
|
|
13
|
+
EntityEventListenerStore,
|
|
14
|
+
EntityId,
|
|
14
15
|
EntityListEventListenerStore,
|
|
15
16
|
EntityName,
|
|
16
17
|
FieldEventListenerStore,
|
|
17
18
|
FieldName,
|
|
18
|
-
|
|
19
|
+
FieldValue,
|
|
19
20
|
} from '../treeParameters'
|
|
21
|
+
import { EventListenersStore } from '../treeParameters'
|
|
20
22
|
import { assertNever } from '../utils'
|
|
21
23
|
import type { AccessorErrorManager } from './AccessorErrorManager'
|
|
22
24
|
import type { EventManager } from './EventManager'
|
|
@@ -35,7 +37,6 @@ import {
|
|
|
35
37
|
} from './state'
|
|
36
38
|
import { TreeParameterMerger } from './TreeParameterMerger'
|
|
37
39
|
import type { TreeStore } from './TreeStore'
|
|
38
|
-
import { EventListenersStore } from '../treeParameters'
|
|
39
40
|
|
|
40
41
|
export class StateInitializer {
|
|
41
42
|
private readonly fieldOperations: FieldOperations
|
|
@@ -127,8 +128,11 @@ export class StateInitializer {
|
|
|
127
128
|
}
|
|
128
129
|
this.initializeFromFieldMarker(realm, field, undefined, value)
|
|
129
130
|
} else if (field instanceof HasManyRelationMarker) {
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
const value = copyFrom.children.get(placeholderName)
|
|
132
|
+
if (!value || value.type !== 'entityList') {
|
|
133
|
+
throw new BindingError()
|
|
134
|
+
}
|
|
135
|
+
this.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()))
|
|
132
136
|
} else if (field instanceof HasOneRelationMarker) {
|
|
133
137
|
let runtimeId: RuntimeId
|
|
134
138
|
let subCopyFrom: EntityRealmState | undefined = undefined
|
|
@@ -349,7 +353,7 @@ export class StateInitializer {
|
|
|
349
353
|
parent: EntityRealmState,
|
|
350
354
|
placeholderName: FieldName,
|
|
351
355
|
fieldMarker: FieldMarker,
|
|
352
|
-
persistedValue:
|
|
356
|
+
persistedValue: FieldValue | undefined,
|
|
353
357
|
copyFromState: FieldState | undefined,
|
|
354
358
|
): FieldState {
|
|
355
359
|
const resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)
|
|
@@ -15,6 +15,7 @@ import { OperationsHelpers } from './operations/OperationsHelpers'
|
|
|
15
15
|
import type { EntityListState, EntityRealmState, EntityRealmStateStub, RootStateNode } from './state'
|
|
16
16
|
import type { StateInitializer } from './StateInitializer'
|
|
17
17
|
import type { TreeStore } from './TreeStore'
|
|
18
|
+
import { SubMutationOperation } from './MutationGenerator'
|
|
18
19
|
|
|
19
20
|
export class TreeAugmenter {
|
|
20
21
|
public constructor(
|
|
@@ -23,9 +24,9 @@ export class TreeAugmenter {
|
|
|
23
24
|
private readonly treeStore: TreeStore,
|
|
24
25
|
) {}
|
|
25
26
|
|
|
26
|
-
public extendPersistedData(newPersistedData: ReceivedDataTree) {
|
|
27
|
+
public extendPersistedData(newPersistedData: ReceivedDataTree, markerTree: MarkerTreeRoot) {
|
|
27
28
|
// TODO this doesn't yet handle updates for entities whose persisted data just gets magically changed without notice.
|
|
28
|
-
this.treeStore.mergeInQueryResponse(newPersistedData)
|
|
29
|
+
this.treeStore.mergeInQueryResponse(newPersistedData, markerTree)
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
public extendTreeStates(newTreeId: TreeRootId | undefined, newMarkerTree: MarkerTreeRoot): void {
|
|
@@ -40,8 +41,8 @@ export class TreeAugmenter {
|
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
public updatePersistedData(response: ReceivedDataTree) {
|
|
44
|
-
this.treeStore.mergeInMutationResponse(response)
|
|
44
|
+
public updatePersistedData(response: ReceivedDataTree, operations: SubMutationOperation[]) {
|
|
45
|
+
this.treeStore.mergeInMutationResponse(response, operations)
|
|
45
46
|
|
|
46
47
|
this.eventManager.syncTransaction(() => {
|
|
47
48
|
for (const rootStates of this.treeStore.subTreeStatesByRoot.values()) {
|
package/src/core/TreeStore.ts
CHANGED
|
@@ -27,6 +27,7 @@ import { MarkerComparator } from './MarkerComparator'
|
|
|
27
27
|
import { RequestResponseNormalizer } from './RequestResponseNormalizer'
|
|
28
28
|
import type { Schema } from './schema'
|
|
29
29
|
import type { EntityListState, EntityRealmState, EntityRealmStateStub, EntityState, RootStateNode } from './state'
|
|
30
|
+
import { SubMutationOperation } from './MutationGenerator'
|
|
30
31
|
|
|
31
32
|
const emptyEntityIdSet: ReadonlySet<EntityId> = new Set()
|
|
32
33
|
|
|
@@ -44,12 +45,12 @@ export class TreeStore {
|
|
|
44
45
|
|
|
45
46
|
public constructor() {}
|
|
46
47
|
|
|
47
|
-
public mergeInQueryResponse(response: ReceivedDataTree): void {
|
|
48
|
-
RequestResponseNormalizer.mergeInQueryResponse(this.persistedData, response)
|
|
48
|
+
public mergeInQueryResponse(response: ReceivedDataTree, markerTree: MarkerTreeRoot): void {
|
|
49
|
+
RequestResponseNormalizer.mergeInQueryResponse(this.persistedData, response, markerTree)
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
public mergeInMutationResponse(response: ReceivedDataTree): void {
|
|
52
|
-
RequestResponseNormalizer.mergeInMutationResponse(this.persistedData, response)
|
|
52
|
+
public mergeInMutationResponse(response: ReceivedDataTree, operations: SubMutationOperation[]): void {
|
|
53
|
+
RequestResponseNormalizer.mergeInMutationResponse(this.persistedData, response, operations)
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
public setSchema(newSchema: Schema): void {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BindingError } from '../BindingError'
|
|
2
2
|
import { Environment, VariableFieldValue } from '../dao'
|
|
3
|
-
import type { FieldValue, OptionallyVariableFieldValue
|
|
3
|
+
import type { FieldValue, OptionallyVariableFieldValue } from '../treeParameters'
|
|
4
4
|
|
|
5
5
|
export class VariableInputTransformer {
|
|
6
6
|
public static transformValue(value: OptionallyVariableFieldValue, environment: Environment): FieldValue {
|
|
@@ -10,7 +10,7 @@ export class VariableInputTransformer {
|
|
|
10
10
|
return value
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
public static transformVariableFieldValue(variableFieldValue: VariableFieldValue, environment: Environment):
|
|
13
|
+
public static transformVariableFieldValue(variableFieldValue: VariableFieldValue, environment: Environment): FieldValue {
|
|
14
14
|
const value = environment.getVariableOrElse(variableFieldValue.variableName, undefined)
|
|
15
15
|
?? environment.getParameterOrElse(variableFieldValue.variableName, undefined)
|
|
16
16
|
|
|
@@ -1,67 +1,12 @@
|
|
|
1
1
|
import type { RuntimeId } from '../../accessorTree'
|
|
2
|
-
import { BindingError } from '../../BindingError'
|
|
3
|
-
import type { PlaceholderName, TreeRootId } from '../../treeParameters'
|
|
4
|
-
import { mutationOperationSubTreeType } from './mutationOperationSubTreeType'
|
|
5
|
-
import type { mutationOperationType } from './mutationOperationType'
|
|
6
|
-
import type { TopLevelMutationOperation } from './TopLevelMutationOperation'
|
|
7
|
-
|
|
8
|
-
const mutationAliasRegex = /^(?:(u)|(t)([^t]+)t)(.+)__([cud])([sl])(.+)$/
|
|
9
|
-
|
|
10
|
-
const uuidToAliasRegex = /-/g
|
|
11
|
-
const uuidToAliasReplacement = '_'
|
|
12
|
-
|
|
13
|
-
const aliasToUuidRegex = /_/g
|
|
14
|
-
const aliasToUuidReplacement = '-'
|
|
15
|
-
|
|
16
|
-
// This is just a random character we use to make sure the alias doesn't start with a number as UUIDs often do.
|
|
17
|
-
const standaloneUuidPrefix = uuidToAliasReplacement
|
|
18
2
|
|
|
19
3
|
export class MutationAlias {
|
|
20
|
-
public static encodeTopLevel(operation: TopLevelMutationOperation): string {
|
|
21
|
-
const treeRootId = operation.treeRootId === undefined ? 'u' : `t${operation.treeRootId.replace('-', '_')}t`
|
|
22
|
-
const entityId = String(operation.entityId).replace(uuidToAliasRegex, uuidToAliasReplacement)
|
|
23
|
-
|
|
24
|
-
const alias = `${treeRootId}${operation.subTreePlaceholder}__${operation.type}${operation.subTreeType}${entityId}`
|
|
25
|
-
|
|
26
|
-
if (import.meta.env.DEV) {
|
|
27
|
-
const matches = alias.match(mutationAliasRegex)
|
|
28
|
-
|
|
29
|
-
if (!matches) {
|
|
30
|
-
throw new BindingError()
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return alias
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public static decodeTopLevel(alias: string): TopLevelMutationOperation | undefined {
|
|
38
|
-
const matches = alias.match(mutationAliasRegex)
|
|
39
|
-
|
|
40
|
-
if (!matches) {
|
|
41
|
-
return undefined
|
|
42
|
-
}
|
|
43
|
-
const treeRootId: TreeRootId | undefined = matches[1] === 'u' ? undefined : matches[3]
|
|
44
|
-
const subTreePlaceholder: PlaceholderName = matches[4]
|
|
45
|
-
const type = matches[5] as typeof mutationOperationType[keyof typeof mutationOperationType]
|
|
46
|
-
const subTreeType =
|
|
47
|
-
matches[6] === 's' ? mutationOperationSubTreeType.singleEntity : mutationOperationSubTreeType.entityList
|
|
48
|
-
const entityId = matches[7].replace(aliasToUuidRegex, aliasToUuidReplacement)
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
treeRootId,
|
|
52
|
-
subTreePlaceholder,
|
|
53
|
-
type,
|
|
54
|
-
subTreeType,
|
|
55
|
-
entityId,
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
4
|
public static encodeEntityId(entityId: RuntimeId): string {
|
|
60
|
-
return
|
|
5
|
+
return `_${String(entityId.value).replace(/-/g, '_')}`
|
|
61
6
|
}
|
|
62
7
|
|
|
63
8
|
public static decodeEntityId(alias: string): RuntimeId['value'] {
|
|
64
|
-
const stringId = `${alias.substring(
|
|
9
|
+
const stringId = `${alias.substring(1).replace(/_/g, '-')}`
|
|
65
10
|
const intId = parseInt(stringId)
|
|
66
11
|
return intId.toString() === stringId ? intId : stringId
|
|
67
12
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ErrorAccessor, FieldAccessor } from '../../accessors'
|
|
2
2
|
import type { FieldMarker } from '../../markers'
|
|
3
|
-
import type { FieldEventListenerStore, FieldName, FieldValue
|
|
3
|
+
import type { FieldEventListenerStore, FieldName, FieldValue } from '../../treeParameters'
|
|
4
4
|
import type { EntityRealmState } from './EntityRealmState'
|
|
5
5
|
|
|
6
6
|
export interface FieldState<Value extends FieldValue = FieldValue> {
|
|
@@ -13,7 +13,7 @@ export interface FieldState<Value extends FieldValue = FieldValue> {
|
|
|
13
13
|
readonly getAccessor: () => FieldAccessor
|
|
14
14
|
hasUnpersistedChanges: boolean
|
|
15
15
|
parent: EntityRealmState
|
|
16
|
-
persistedValue:
|
|
16
|
+
persistedValue: Value | undefined // Undefined means that the parent entity doesn't exist on server
|
|
17
17
|
placeholderName: FieldName
|
|
18
18
|
touchLog: Set<string> | undefined
|
|
19
19
|
value: Value | null
|
|
@@ -14,7 +14,7 @@ export const Entity = Component(
|
|
|
14
14
|
({ children, accessor }: EntityProps) => {
|
|
15
15
|
return (
|
|
16
16
|
// HACK: the ?. is actually important, despite the typings.
|
|
17
|
-
<AccessorProvider accessor={accessor} key={accessor?.
|
|
17
|
+
<AccessorProvider accessor={accessor} key={accessor?.key}>
|
|
18
18
|
{children}
|
|
19
19
|
</AccessorProvider>
|
|
20
20
|
)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GraphQlLiteral, Input } from '@contember/client'
|
|
2
|
-
import {
|
|
2
|
+
import { FieldValue } from '../../treeParameters'
|
|
3
3
|
import { BindingError } from '../../BindingError'
|
|
4
4
|
|
|
5
5
|
const unwrapGraphqlLiteral = (value: Input.ColumnValue<GraphQlLiteral>): any => {
|
|
@@ -9,7 +9,7 @@ const unwrapGraphqlLiteral = (value: Input.ColumnValue<GraphQlLiteral>): any =>
|
|
|
9
9
|
return value
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export const evaluateCondition = (value:
|
|
12
|
+
export const evaluateCondition = (value: FieldValue | null, condition: Input.Condition<Input.ColumnValue<GraphQlLiteral>>) => {
|
|
13
13
|
const handlers: {
|
|
14
14
|
[K in keyof Required<Input.Condition<any>>]: (
|
|
15
15
|
param: Exclude<Input.Condition<any>[K], undefined>,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { VariableFieldValue } from '../../dao'
|
|
2
|
-
import
|
|
2
|
+
import { JsonValue } from './Json'
|
|
3
3
|
|
|
4
|
-
export type FieldValue =
|
|
4
|
+
export type FieldValue = JsonValue
|
|
5
5
|
|
|
6
6
|
export type OptionallyVariableFieldValue = FieldValue | VariableFieldValue
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface JsonObject<Ext = never> {
|
|
2
|
+
[key: string]: JsonValue<Ext>
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface JsonArray<Ext = never> extends Array<JsonValue<Ext>> {}
|
|
6
|
+
|
|
7
|
+
export type JsonValue<Ext = never> = string | number | boolean | null | JsonObject<Ext> | JsonArray<Ext> | Ext
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mutationOperationSubTreeType.js","sources":["../../../../src/core/requestAliases/mutationOperationSubTreeType.ts"],"sourcesContent":["export const mutationOperationSubTreeType = {\n\tsingleEntity: 's',\n\tentityList: 'l',\n} as const\n"],"names":[],"mappings":"AAAO,MAAM,+BAA+B;AAAA,EAC3C,cAAc;AAAA,EACd,YAAY;AACb;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mutationOperationType.js","sources":["../../../../src/core/requestAliases/mutationOperationType.ts"],"sourcesContent":["export const mutationOperationType = {\n\tcreate: 'c',\n\tupdate: 'u',\n\tdelete: 'd',\n} as const\n"],"names":[],"mappings":"AAAO,MAAM,wBAAwB;AAAA,EACpC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AACT;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mutationOperationSubTreeType.js","sources":["../../../../src/core/requestAliases/mutationOperationSubTreeType.ts"],"sourcesContent":["export const mutationOperationSubTreeType = {\n\tsingleEntity: 's',\n\tentityList: 'l',\n} as const\n"],"names":[],"mappings":"AAAO,MAAM,+BAA+B;AAAA,EAC3C,cAAc;AAAA,EACd,YAAY;AACb;;"}
|