@contember/binding 1.1.0-alpha.0 → 1.1.0-alpha.1
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/accessorTree/RuntimeId.js +17 -4
- package/dist/development/accessorTree/RuntimeId.js.map +1 -1
- package/dist/development/accessors/EntityAccessor.js +0 -32
- package/dist/development/accessors/EntityAccessor.js.map +1 -1
- package/dist/development/accessors/EntityListAccessor.js +1 -1
- package/dist/development/accessors/EntityListAccessor.js.map +1 -1
- package/dist/development/core/AccessorErrorManager.js +3 -0
- package/dist/development/core/AccessorErrorManager.js.map +1 -1
- package/dist/development/core/DataBinding.js +6 -5
- package/dist/development/core/DataBinding.js.map +1 -1
- package/dist/development/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/development/core/MutationGenerator.js +5 -5
- package/dist/development/core/MutationGenerator.js.map +1 -1
- package/dist/development/core/RequestResponseNormalizer.js +27 -31
- package/dist/development/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/development/core/StateInitializer.js +9 -9
- package/dist/development/core/StateInitializer.js.map +1 -1
- package/dist/development/core/TreeAugmenter.js +10 -10
- package/dist/development/core/TreeAugmenter.js.map +1 -1
- package/dist/development/core/TreeFilterGenerator.js +2 -2
- package/dist/development/core/TreeFilterGenerator.js.map +1 -1
- package/dist/development/core/TreeStore.js +3 -3
- package/dist/development/core/TreeStore.js.map +1 -1
- package/dist/development/core/operations/EntityOperations.js +5 -5
- package/dist/development/core/operations/EntityOperations.js.map +1 -1
- package/dist/development/core/operations/FieldOperations.js +2 -2
- package/dist/development/core/operations/FieldOperations.js.map +1 -1
- package/dist/development/core/operations/ListOperations.js +2 -2
- package/dist/development/core/operations/ListOperations.js.map +1 -1
- package/dist/development/core/operations/OperationsHelpers.js +2 -2
- package/dist/development/core/operations/OperationsHelpers.js.map +1 -1
- package/dist/development/core/requestAliases/MutationAlias.js +5 -3
- package/dist/development/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/development/dao/Environment.js +1 -1
- package/dist/development/dao/Environment.js.map +1 -1
- package/dist/development/index.js +1 -1
- package/dist/production/accessorTree/NormalizedPersistedData.js.map +1 -1
- package/dist/production/accessorTree/RuntimeId.js +17 -4
- package/dist/production/accessorTree/RuntimeId.js.map +1 -1
- package/dist/production/accessors/EntityAccessor.js +0 -32
- package/dist/production/accessors/EntityAccessor.js.map +1 -1
- package/dist/production/accessors/EntityListAccessor.js +1 -1
- package/dist/production/accessors/EntityListAccessor.js.map +1 -1
- package/dist/production/core/AccessorErrorManager.js +3 -0
- package/dist/production/core/AccessorErrorManager.js.map +1 -1
- package/dist/production/core/DataBinding.js +6 -5
- package/dist/production/core/DataBinding.js.map +1 -1
- package/dist/production/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/production/core/MutationGenerator.js +5 -5
- package/dist/production/core/MutationGenerator.js.map +1 -1
- package/dist/production/core/RequestResponseNormalizer.js +27 -31
- package/dist/production/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/production/core/StateInitializer.js +9 -9
- package/dist/production/core/StateInitializer.js.map +1 -1
- package/dist/production/core/TreeAugmenter.js +10 -10
- package/dist/production/core/TreeAugmenter.js.map +1 -1
- package/dist/production/core/TreeFilterGenerator.js +2 -2
- package/dist/production/core/TreeFilterGenerator.js.map +1 -1
- package/dist/production/core/TreeStore.js +3 -3
- package/dist/production/core/TreeStore.js.map +1 -1
- package/dist/production/core/operations/EntityOperations.js +5 -5
- package/dist/production/core/operations/EntityOperations.js.map +1 -1
- package/dist/production/core/operations/FieldOperations.js.map +1 -1
- package/dist/production/core/operations/ListOperations.js +2 -2
- package/dist/production/core/operations/ListOperations.js.map +1 -1
- package/dist/production/core/operations/OperationsHelpers.js +2 -2
- package/dist/production/core/operations/OperationsHelpers.js.map +1 -1
- package/dist/production/core/requestAliases/MutationAlias.js +5 -3
- package/dist/production/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/production/dao/Environment.js +1 -1
- package/dist/production/dao/Environment.js.map +1 -1
- package/dist/production/index.js +1 -1
- package/dist/types/accessorTree/MutationRequestResponse.d.ts +5 -1
- package/dist/types/accessorTree/MutationRequestResponse.d.ts.map +1 -1
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts +4 -4
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts.map +1 -1
- package/dist/types/accessorTree/PersistResult.d.ts +2 -1
- package/dist/types/accessorTree/PersistResult.d.ts.map +1 -1
- package/dist/types/accessorTree/QueryRequestResponse.d.ts +11 -10
- package/dist/types/accessorTree/QueryRequestResponse.d.ts.map +1 -1
- package/dist/types/accessorTree/RuntimeId.d.ts +16 -6
- package/dist/types/accessorTree/RuntimeId.d.ts.map +1 -1
- package/dist/types/accessors/EntityAccessor.d.ts +1 -3
- package/dist/types/accessors/EntityAccessor.d.ts.map +1 -1
- package/dist/types/accessors/EntityListAccessor.d.ts +2 -2
- package/dist/types/accessors/EntityListAccessor.d.ts.map +1 -1
- package/dist/types/core/AccessorErrorManager.d.ts.map +1 -1
- package/dist/types/core/ErrorsPreprocessor.d.ts +2 -2
- package/dist/types/core/ErrorsPreprocessor.d.ts.map +1 -1
- package/dist/types/core/RequestResponseNormalizer.d.ts.map +1 -1
- package/dist/types/core/StateInitializer.d.ts.map +1 -1
- package/dist/types/core/TreeStore.d.ts +3 -3
- package/dist/types/core/TreeStore.d.ts.map +1 -1
- package/dist/types/core/operations/ListOperations.d.ts +2 -1
- package/dist/types/core/operations/ListOperations.d.ts.map +1 -1
- package/dist/types/core/requestAliases/MutationAlias.d.ts.map +1 -1
- package/dist/types/dao/Environment.d.ts +5 -5
- package/dist/types/dao/Environment.d.ts.map +1 -1
- package/dist/types/treeParameters/primitives/EntityId.d.ts +1 -1
- package/dist/types/treeParameters/primitives/EntityId.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/accessorTree/MutationRequestResponse.ts +3 -1
- package/src/accessorTree/NormalizedPersistedData.ts +4 -4
- package/src/accessorTree/PersistResult.ts +2 -1
- package/src/accessorTree/QueryRequestResponse.ts +17 -14
- package/src/accessorTree/RuntimeId.ts +27 -6
- package/src/accessors/EntityAccessor.ts +2 -51
- package/src/accessors/EntityListAccessor.ts +3 -3
- package/src/core/AccessorErrorManager.ts +3 -0
- package/src/core/DataBinding.ts +6 -6
- package/src/core/ErrorsPreprocessor.ts +2 -2
- package/src/core/MutationGenerator.ts +5 -5
- package/src/core/RequestResponseNormalizer.ts +37 -38
- package/src/core/StateInitializer.ts +14 -14
- package/src/core/TreeAugmenter.ts +11 -11
- package/src/core/TreeFilterGenerator.ts +5 -5
- package/src/core/TreeStore.ts +12 -6
- package/src/core/operations/EntityOperations.ts +5 -5
- package/src/core/operations/FieldOperations.ts +2 -2
- package/src/core/operations/ListOperations.ts +4 -4
- package/src/core/operations/OperationsHelpers.ts +2 -2
- package/src/core/requestAliases/MutationAlias.ts +5 -3
- package/src/dao/Environment.ts +6 -6
- package/src/treeParameters/primitives/EntityId.ts +1 -1
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
import { generateEnumerabilityPreventingEntropy } from '../utils'
|
|
2
|
+
import { EntityId } from '../treeParameters'
|
|
3
|
+
|
|
4
|
+
export type UniqueEntityId = string & { __type: 'UniqueEntityId' }
|
|
2
5
|
|
|
3
6
|
export interface RuntimeIdSpec {
|
|
4
7
|
existsOnServer: boolean
|
|
5
|
-
value:
|
|
8
|
+
value: EntityId
|
|
9
|
+
uniqueValue: UniqueEntityId
|
|
6
10
|
}
|
|
7
11
|
|
|
8
|
-
export class
|
|
12
|
+
export class ServerId implements RuntimeIdSpec {
|
|
9
13
|
public get existsOnServer(): true {
|
|
10
14
|
return true
|
|
11
15
|
}
|
|
12
|
-
public constructor(public readonly value: string) {
|
|
16
|
+
public constructor(public readonly value: EntityId, public readonly entityName: string) {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get uniqueValue() {
|
|
20
|
+
return ServerId.formatUniqueValue(this.value, this.entityName)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static formatUniqueValue(id: EntityId, entityName: string): UniqueEntityId {
|
|
24
|
+
return `${entityName}_${id}` as UniqueEntityId
|
|
25
|
+
}
|
|
13
26
|
}
|
|
14
27
|
|
|
15
28
|
export class ClientGeneratedUuid implements RuntimeIdSpec {
|
|
@@ -17,6 +30,10 @@ export class ClientGeneratedUuid implements RuntimeIdSpec {
|
|
|
17
30
|
return false
|
|
18
31
|
}
|
|
19
32
|
public constructor(public readonly value: string) {}
|
|
33
|
+
|
|
34
|
+
get uniqueValue() {
|
|
35
|
+
return this.value as UniqueEntityId
|
|
36
|
+
}
|
|
20
37
|
}
|
|
21
38
|
|
|
22
39
|
export class UnpersistedEntityDummyId implements RuntimeIdSpec {
|
|
@@ -41,9 +58,13 @@ export class UnpersistedEntityDummyId implements RuntimeIdSpec {
|
|
|
41
58
|
|
|
42
59
|
private static entropyLength = 5
|
|
43
60
|
private static dummyIdRegex = new RegExp(`^adminOnlyDummyId-\\d{${UnpersistedEntityDummyId.entropyLength}}-\\d+$`)
|
|
44
|
-
public static matchesDummyId(candidate:
|
|
45
|
-
return UnpersistedEntityDummyId.dummyIdRegex.test(candidate)
|
|
61
|
+
public static matchesDummyId(candidate: EntityId): boolean {
|
|
62
|
+
return typeof candidate === 'string' && UnpersistedEntityDummyId.dummyIdRegex.test(candidate)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get uniqueValue() {
|
|
66
|
+
return this.value as UniqueEntityId
|
|
46
67
|
}
|
|
47
68
|
}
|
|
48
69
|
|
|
49
|
-
export type RuntimeId =
|
|
70
|
+
export type RuntimeId = ServerId | ClientGeneratedUuid | UnpersistedEntityDummyId
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RuntimeId,
|
|
1
|
+
import { RuntimeId, SingleEntityPersistedData } from '../accessorTree'
|
|
2
2
|
import { BindingError } from '../BindingError'
|
|
3
3
|
import type { EntityOperations } from '../core/operations'
|
|
4
4
|
import type { Environment } from '../dao'
|
|
@@ -42,7 +42,7 @@ class EntityAccessor implements Errorable {
|
|
|
42
42
|
public readonly getAccessor: EntityAccessor.GetEntityAccessor,
|
|
43
43
|
) {}
|
|
44
44
|
|
|
45
|
-
public get idOnServer():
|
|
45
|
+
public get idOnServer(): EntityId | undefined {
|
|
46
46
|
return this.runtimeId.existsOnServer ? this.runtimeId.value : undefined
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -116,21 +116,6 @@ class EntityAccessor implements Errorable {
|
|
|
116
116
|
return this.getRelativeEntityList(QueryLanguage.desugarRelativeEntityList(entityList, this.environment))
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
public getKeyConnectedOnServer(entity: SugaredRelativeSingleEntity | string): string | null {
|
|
120
|
-
const desugared = QueryLanguage.desugarRelativeSingleEntity(entity, this.environment)
|
|
121
|
-
const lastHasOne = desugared.hasOneRelationPath[desugared.hasOneRelationPath.length - 1]
|
|
122
|
-
const lastHasOnePlaceholder = PlaceholderGenerator.getHasOneRelationPlaceholder(lastHasOne)
|
|
123
|
-
|
|
124
|
-
let containingAccessor: EntityAccessor = this
|
|
125
|
-
|
|
126
|
-
if (desugared.hasOneRelationPath.length > 1) {
|
|
127
|
-
containingAccessor = this.getRelativeSingleEntity({
|
|
128
|
-
hasOneRelationPath: desugared.hasOneRelationPath.slice(0, -1),
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
return containingAccessor.getPersistedKeyByPlaceholder(lastHasOnePlaceholder)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
119
|
//
|
|
135
120
|
|
|
136
121
|
/**
|
|
@@ -173,40 +158,6 @@ class EntityAccessor implements Errorable {
|
|
|
173
158
|
}
|
|
174
159
|
return record.getAccessor()
|
|
175
160
|
}
|
|
176
|
-
|
|
177
|
-
private getPersistedKeyByPlaceholder(placeholderName: string): string | null {
|
|
178
|
-
if (this.dataFromServer === undefined) {
|
|
179
|
-
return null
|
|
180
|
-
}
|
|
181
|
-
const key = this.dataFromServer.get(placeholderName)
|
|
182
|
-
|
|
183
|
-
if (key instanceof ServerGeneratedUuid) {
|
|
184
|
-
return key.value
|
|
185
|
-
}
|
|
186
|
-
if (key === null) {
|
|
187
|
-
// TODO this also returns null and fails to throw when placeholderName points to a regular scalar field that
|
|
188
|
-
// happens to be also null.
|
|
189
|
-
return null
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// From now on, we're past the happy path, way into error land.
|
|
193
|
-
if (key === undefined) {
|
|
194
|
-
throw new BindingError(
|
|
195
|
-
`EntityAccessor.getKeyConnectedOnServer: unknown placeholder '${placeholderName}'. Unless this is just ` +
|
|
196
|
-
`a typo, this typically happens when a has-one relation hasn't been registered during static render.`,
|
|
197
|
-
)
|
|
198
|
-
}
|
|
199
|
-
if (key instanceof Set) {
|
|
200
|
-
throw new BindingError(
|
|
201
|
-
`EntityAccessor.getKeyConnectedOnServer: the placeholder '${placeholderName}' refers to a has-many relation, ` +
|
|
202
|
-
`not a has-one. This method is meant exclusively for has-one relations.`,
|
|
203
|
-
)
|
|
204
|
-
}
|
|
205
|
-
throw new BindingError(
|
|
206
|
-
`EntityAccessor.getKeyConnectedOnServer: the placeholder '${placeholderName}' refers to a scalar field, not a` +
|
|
207
|
-
`has-one relation. This method is meant exclusively for has-one relations.`,
|
|
208
|
-
)
|
|
209
|
-
}
|
|
210
161
|
}
|
|
211
162
|
|
|
212
163
|
namespace EntityAccessor {
|
|
@@ -16,7 +16,7 @@ class EntityListAccessor implements Errorable {
|
|
|
16
16
|
private readonly stateKey: EntityListState,
|
|
17
17
|
private readonly operations: ListOperations,
|
|
18
18
|
private readonly _children: ReadonlyMap<EntityId, { getAccessor: EntityAccessor.GetEntityAccessor }>,
|
|
19
|
-
private readonly _idsPersistedOnServer: ReadonlySet<
|
|
19
|
+
private readonly _idsPersistedOnServer: ReadonlySet<EntityId>,
|
|
20
20
|
public readonly hasUnpersistedChanges: boolean,
|
|
21
21
|
public readonly errors: ErrorAccessor | undefined,
|
|
22
22
|
public readonly environment: Environment,
|
|
@@ -42,7 +42,7 @@ class EntityListAccessor implements Errorable {
|
|
|
42
42
|
* This will only contain the ids that the server knows about. Not necessarily the ids that have been added on
|
|
43
43
|
* the list since the last server query.
|
|
44
44
|
*/
|
|
45
|
-
public get idsPersistedOnServer(): Set<
|
|
45
|
+
public get idsPersistedOnServer(): Set<EntityId> {
|
|
46
46
|
return new Set(this._idsPersistedOnServer)
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -65,7 +65,7 @@ class EntityListAccessor implements Errorable {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
public hasEntityOnServer(entityOrItsId: EntityAccessor | EntityId): boolean {
|
|
68
|
-
if (typeof entityOrItsId === 'string') {
|
|
68
|
+
if (typeof entityOrItsId === 'string' || typeof entityOrItsId === 'number') {
|
|
69
69
|
return this.idsPersistedOnServer.has(entityOrItsId)
|
|
70
70
|
}
|
|
71
71
|
const idOnServer = entityOrItsId.idOnServer
|
|
@@ -134,6 +134,9 @@ export class AccessorErrorManager {
|
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
for (const [childKey, child] of errors.children) {
|
|
137
|
+
if (typeof childKey !== 'string') {
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
137
140
|
if (child.nodeType === 'leaf') {
|
|
138
141
|
const fieldState = state.children.get(childKey)
|
|
139
142
|
|
package/src/core/DataBinding.ts
CHANGED
|
@@ -171,15 +171,15 @@ export class DataBinding {
|
|
|
171
171
|
errors: mutationResponse.errors,
|
|
172
172
|
})
|
|
173
173
|
}
|
|
174
|
-
|
|
175
|
-
const mutationData = mutationResponse.data?.transaction ?? {}
|
|
176
|
-
const aliases = Object.keys(mutationData)
|
|
177
|
-
if (!mutationResponse.data) {
|
|
174
|
+
if (!mutationResponse.data?.transaction) {
|
|
178
175
|
throw {
|
|
179
|
-
errors:
|
|
176
|
+
errors: [],
|
|
180
177
|
type: 'invalidResponse',
|
|
181
178
|
}
|
|
182
|
-
}
|
|
179
|
+
}
|
|
180
|
+
const { __typename, ...mutationData } = mutationResponse.data.transaction
|
|
181
|
+
const aliases = Object.keys(mutationData)
|
|
182
|
+
if (aliases.every(item => mutationData[item].ok)) {
|
|
183
183
|
const persistedEntityIds = aliases.map(alias => mutationData[alias].node?.id).filter(id => id !== undefined)
|
|
184
184
|
const result: SuccessfulPersistResult = {
|
|
185
185
|
type: 'justSuccess',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ErrorAccessor } from '../accessors'
|
|
2
2
|
import type { ExecutionError, MutationDataResponse, MutationResponse, ValidationError } from '../accessorTree'
|
|
3
|
-
import type { PlaceholderName } from '../treeParameters'
|
|
3
|
+
import type { EntityId, FieldName, PlaceholderName } from '../treeParameters'
|
|
4
4
|
import { assertNever } from '../utils'
|
|
5
5
|
import { MutationAlias, mutationOperationSubTreeType } from './requestAliases'
|
|
6
6
|
|
|
@@ -227,7 +227,7 @@ namespace ErrorsPreprocessor {
|
|
|
227
227
|
|
|
228
228
|
export interface ErrorINode extends BaseErrorNode {
|
|
229
229
|
nodeType: 'iNode'
|
|
230
|
-
children: Map<
|
|
230
|
+
children: Map<FieldName | EntityId, ErrorNode>
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
export type ErrorNode = ErrorINode | LeafErrorNode
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// noinspection JSVoidFunctionReturnValueUsed // IntelliJ seems to be super confused througouht this file.
|
|
2
2
|
|
|
3
3
|
import { CrudQueryBuilder, GraphQlBuilder } from '@contember/client'
|
|
4
|
-
import { ClientGeneratedUuid,
|
|
4
|
+
import { ClientGeneratedUuid, ServerId } from '../accessorTree'
|
|
5
5
|
import { BindingError } from '../BindingError'
|
|
6
6
|
import { PRIMARY_KEY_NAME, TYPENAME_KEY_NAME } from '../bindingTypes'
|
|
7
7
|
import { FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '../markers'
|
|
@@ -503,7 +503,7 @@ export class MutationGenerator {
|
|
|
503
503
|
}
|
|
504
504
|
|
|
505
505
|
const pathBack = this.treeStore.getPathBackToParent(currentState)
|
|
506
|
-
const entityData = this.treeStore.persistedEntityData.get(currentState.entity.id.
|
|
506
|
+
const entityData = this.treeStore.persistedEntityData.get(currentState.entity.id.uniqueValue)
|
|
507
507
|
|
|
508
508
|
for (const [placeholderName, fieldState] of currentState.children) {
|
|
509
509
|
if (placeholderName === PRIMARY_KEY_NAME || placeholderName === TYPENAME_KEY_NAME) {
|
|
@@ -542,7 +542,7 @@ export class MutationGenerator {
|
|
|
542
542
|
) => {
|
|
543
543
|
const persistedValue = entityData?.get?.(placeholderName)
|
|
544
544
|
|
|
545
|
-
if (persistedValue instanceof
|
|
545
|
+
if (persistedValue instanceof ServerId) {
|
|
546
546
|
if (persistedValue.value === runtimeId.value) {
|
|
547
547
|
// The persisted and currently referenced ids match, and so this is an update.
|
|
548
548
|
if (fieldState.type === 'entityRealmStub') {
|
|
@@ -598,7 +598,7 @@ export class MutationGenerator {
|
|
|
598
598
|
const persistedValue = entityData?.get?.(placeholderName)
|
|
599
599
|
const alias = MutationAlias.encodeEntityId(runtimeId)
|
|
600
600
|
|
|
601
|
-
if (persistedValue instanceof
|
|
601
|
+
if (persistedValue instanceof ServerId) {
|
|
602
602
|
if (persistedValue.value === runtimeId.value) {
|
|
603
603
|
if (fieldState.type === 'entityRealmStub') {
|
|
604
604
|
return builder
|
|
@@ -677,7 +677,7 @@ export class MutationGenerator {
|
|
|
677
677
|
}
|
|
678
678
|
if (fieldState.plannedRemovals) {
|
|
679
679
|
for (const [removedId, removalType] of fieldState.plannedRemovals) {
|
|
680
|
-
const alias = MutationAlias.encodeEntityId(new
|
|
680
|
+
const alias = MutationAlias.encodeEntityId(new ServerId(removedId, fieldState.entityName))
|
|
681
681
|
if (removalType === 'delete') {
|
|
682
682
|
builder = builder.delete({ [PRIMARY_KEY_NAME]: removedId }, alias)
|
|
683
683
|
} else if (removalType === 'disconnect') {
|
|
@@ -5,13 +5,14 @@ import {
|
|
|
5
5
|
ReceivedDataTree,
|
|
6
6
|
ReceivedEntityData,
|
|
7
7
|
ReceivedFieldData,
|
|
8
|
-
|
|
8
|
+
ServerId,
|
|
9
9
|
SingleEntityPersistedData,
|
|
10
10
|
} from '../accessorTree'
|
|
11
11
|
import { BindingError } from '../BindingError'
|
|
12
12
|
import { PRIMARY_KEY_NAME } from '../bindingTypes'
|
|
13
13
|
import { assertNever } from '../utils'
|
|
14
14
|
import { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'
|
|
15
|
+
import { EntityId } from '../treeParameters'
|
|
15
16
|
|
|
16
17
|
export class RequestResponseNormalizer {
|
|
17
18
|
public static mergeInQueryResponse(
|
|
@@ -52,36 +53,32 @@ export class RequestResponseNormalizer {
|
|
|
52
53
|
}
|
|
53
54
|
const { type, entityId, subTreeType, subTreePlaceholder } = operation
|
|
54
55
|
|
|
56
|
+
const treeDatum = newPersistedData[operationAlias]
|
|
57
|
+
const fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)
|
|
58
|
+
if (!(fieldData instanceof ServerId)) {
|
|
59
|
+
return this.rejectData()
|
|
60
|
+
}
|
|
61
|
+
|
|
55
62
|
switch (type) {
|
|
56
63
|
case mutationOperationType.update:
|
|
57
64
|
case mutationOperationType.create: {
|
|
58
|
-
const treeDatum = newPersistedData[operationAlias]
|
|
59
|
-
const fieldData = this.createFieldData(persistedEntityDataStore, treeDatum)
|
|
60
65
|
|
|
61
66
|
if (subTreeType === mutationOperationSubTreeType.singleEntity) {
|
|
62
|
-
|
|
63
|
-
subTreeDataStore.set(subTreePlaceholder, fieldData)
|
|
64
|
-
} else {
|
|
65
|
-
return this.rejectData()
|
|
66
|
-
}
|
|
67
|
+
subTreeDataStore.set(subTreePlaceholder, fieldData)
|
|
67
68
|
} else if (subTreeType === mutationOperationSubTreeType.entityList) {
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return this.rejectData()
|
|
81
|
-
}
|
|
69
|
+
if (type === mutationOperationType.create) {
|
|
70
|
+
const list = subTreeDataStore.get(subTreePlaceholder)
|
|
71
|
+
|
|
72
|
+
if (list instanceof Set) {
|
|
73
|
+
// TODO this is somewhat dubious because we're essentially just guessing the order of the entities
|
|
74
|
+
// and just carelessly put the new one at the end.
|
|
75
|
+
list.add(fieldData.value)
|
|
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()
|
|
82
81
|
}
|
|
83
|
-
} else {
|
|
84
|
-
return this.rejectData()
|
|
85
82
|
}
|
|
86
83
|
} else {
|
|
87
84
|
return assertNever(subTreeType)
|
|
@@ -92,7 +89,7 @@ export class RequestResponseNormalizer {
|
|
|
92
89
|
case mutationOperationType.delete: {
|
|
93
90
|
// TODO there are potentially some references to entityId that this whole process won't quite remove.
|
|
94
91
|
// That's a memory leak. Probably not particularly severe in most cases but still.
|
|
95
|
-
persistedEntityDataStore.delete(
|
|
92
|
+
persistedEntityDataStore.delete(fieldData.uniqueValue)
|
|
96
93
|
if (subTreeType === mutationOperationSubTreeType.singleEntity) {
|
|
97
94
|
subTreeDataStore.delete(subTreePlaceholder)
|
|
98
95
|
} else if (subTreeType === mutationOperationSubTreeType.entityList) {
|
|
@@ -119,30 +116,32 @@ export class RequestResponseNormalizer {
|
|
|
119
116
|
fieldData: ReceivedFieldData,
|
|
120
117
|
): EntityFieldPersistedData {
|
|
121
118
|
if (Array.isArray(fieldData)) {
|
|
122
|
-
const subTreeListIds = new Set<
|
|
119
|
+
const subTreeListIds = new Set<EntityId>()
|
|
123
120
|
for (const entityDatum of fieldData) {
|
|
124
|
-
subTreeListIds.add(this.createEntityData(entityMap, entityDatum))
|
|
121
|
+
subTreeListIds.add(this.createEntityData(entityMap, entityDatum).value)
|
|
125
122
|
}
|
|
126
123
|
return subTreeListIds
|
|
127
124
|
} else if (fieldData !== null && typeof fieldData === 'object') {
|
|
128
|
-
return
|
|
125
|
+
return this.createEntityData(entityMap, fieldData)
|
|
129
126
|
} else {
|
|
130
127
|
return fieldData
|
|
131
128
|
}
|
|
132
129
|
}
|
|
133
130
|
|
|
134
|
-
private static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData):
|
|
135
|
-
const primaryKey:
|
|
131
|
+
private static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData): ServerId {
|
|
132
|
+
const primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]
|
|
136
133
|
|
|
137
134
|
if (primaryKey === undefined) {
|
|
138
135
|
throw new BindingError(`The server has responded with an entity that lacks a primary key.`)
|
|
139
136
|
}
|
|
140
137
|
|
|
141
|
-
const
|
|
138
|
+
const id = new ServerId(primaryKey, entityData.__typename)
|
|
139
|
+
|
|
140
|
+
const presentEntityData = entityMap.get(id.uniqueValue)
|
|
142
141
|
|
|
143
142
|
if (presentEntityData === undefined) {
|
|
144
143
|
const fieldsMap: SingleEntityPersistedData = new Map()
|
|
145
|
-
entityMap.set(
|
|
144
|
+
entityMap.set(id.uniqueValue, fieldsMap)
|
|
146
145
|
for (const field in entityData) {
|
|
147
146
|
const fieldDatum = entityData[field]
|
|
148
147
|
|
|
@@ -151,7 +150,7 @@ export class RequestResponseNormalizer {
|
|
|
151
150
|
} else {
|
|
152
151
|
this.mergeInEntityData(entityMap, presentEntityData, entityData)
|
|
153
152
|
}
|
|
154
|
-
return
|
|
153
|
+
return id
|
|
155
154
|
}
|
|
156
155
|
|
|
157
156
|
private static mergeInFieldData(
|
|
@@ -162,28 +161,28 @@ export class RequestResponseNormalizer {
|
|
|
162
161
|
if (fromTarget === undefined) {
|
|
163
162
|
return this.createFieldData(entityMap, newDatum)
|
|
164
163
|
}
|
|
165
|
-
if (fromTarget instanceof
|
|
164
|
+
if (fromTarget instanceof ServerId || (fromTarget === null && typeof newDatum === 'object')) {
|
|
166
165
|
if (newDatum === null) {
|
|
167
166
|
return null
|
|
168
167
|
} else if (typeof newDatum === 'object' && !Array.isArray(newDatum)) {
|
|
169
168
|
const newId = newDatum[PRIMARY_KEY_NAME]
|
|
170
|
-
const target = entityMap.get(newId)
|
|
169
|
+
const target = entityMap.get(ServerId.formatUniqueValue(newId, newDatum.__typename))
|
|
171
170
|
if (target === undefined) {
|
|
172
|
-
return
|
|
171
|
+
return this.createEntityData(entityMap, newDatum)
|
|
173
172
|
}
|
|
174
173
|
|
|
175
174
|
if (fromTarget?.value === newId) {
|
|
176
175
|
this.mergeInEntityData(entityMap, target, newDatum)
|
|
177
176
|
return fromTarget
|
|
178
177
|
} else {
|
|
179
|
-
return
|
|
178
|
+
return this.createEntityData(entityMap, newDatum)
|
|
180
179
|
}
|
|
181
180
|
}
|
|
182
181
|
} else if (fromTarget instanceof Set) {
|
|
183
182
|
if (Array.isArray(newDatum)) {
|
|
184
183
|
fromTarget.clear()
|
|
185
184
|
for (const entityData of newDatum) {
|
|
186
|
-
fromTarget.add(this.createEntityData(entityMap, entityData))
|
|
185
|
+
fromTarget.add(this.createEntityData(entityMap, entityData).value)
|
|
187
186
|
}
|
|
188
187
|
return fromTarget
|
|
189
188
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, FieldAccessor } from '../accessors'
|
|
2
|
-
import { EntityFieldPersistedData, RuntimeId,
|
|
2
|
+
import { EntityFieldPersistedData, RuntimeId, ServerId, UnpersistedEntityDummyId } from '../accessorTree'
|
|
3
3
|
import { BindingError } from '../BindingError'
|
|
4
4
|
import {
|
|
5
5
|
EntityListSubTreeMarker,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from '../markers'
|
|
11
11
|
import { BijectiveIndexedMap } from '../structures'
|
|
12
12
|
import type {
|
|
13
|
-
EntityEventListenerStore,
|
|
13
|
+
EntityEventListenerStore, EntityId,
|
|
14
14
|
EntityListEventListenerStore,
|
|
15
15
|
EntityName,
|
|
16
16
|
FieldEventListenerStore,
|
|
@@ -72,7 +72,7 @@ export class StateInitializer {
|
|
|
72
72
|
const persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)
|
|
73
73
|
|
|
74
74
|
if (tree instanceof EntityListSubTreeMarker) {
|
|
75
|
-
const persistedEntityIds: Set<
|
|
75
|
+
const persistedEntityIds: Set<EntityId> = persistedRootData instanceof Set ? persistedRootData : new Set()
|
|
76
76
|
subTreeState = this.initializeEntityListState(
|
|
77
77
|
{ marker: tree, parent: undefined },
|
|
78
78
|
tree.entityName,
|
|
@@ -81,7 +81,7 @@ export class StateInitializer {
|
|
|
81
81
|
} else {
|
|
82
82
|
subTreeState = this.materializeEntityRealm(
|
|
83
83
|
this.initializeEntityRealm(
|
|
84
|
-
persistedRootData instanceof
|
|
84
|
+
persistedRootData instanceof ServerId ? persistedRootData : new UnpersistedEntityDummyId(),
|
|
85
85
|
tree.entityName,
|
|
86
86
|
{
|
|
87
87
|
type: 'subTree',
|
|
@@ -197,7 +197,7 @@ export class StateInitializer {
|
|
|
197
197
|
// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and
|
|
198
198
|
// keep in sync two copies of the same data. TS hides the extra info anyway.
|
|
199
199
|
entityRealm.children,
|
|
200
|
-
this.treeStore.persistedEntityData.get(entity.id.
|
|
200
|
+
this.treeStore.persistedEntityData.get(entity.id.uniqueValue),
|
|
201
201
|
entityRealm.unpersistedChangesCount !== 0,
|
|
202
202
|
entityRealm.errors,
|
|
203
203
|
getEntityMarker(entityRealm).environment,
|
|
@@ -229,7 +229,7 @@ export class StateInitializer {
|
|
|
229
229
|
// entityRealm.unpersistedChangesCount += 1
|
|
230
230
|
// }
|
|
231
231
|
|
|
232
|
-
const persistedData = this.treeStore.persistedEntityData.get(entity.id.
|
|
232
|
+
const persistedData = this.treeStore.persistedEntityData.get(entity.id.uniqueValue)
|
|
233
233
|
|
|
234
234
|
const marker = getEntityMarker(state)
|
|
235
235
|
const pathBack = this.treeStore.getPathBackToParent(entityRealm)
|
|
@@ -252,7 +252,7 @@ export class StateInitializer {
|
|
|
252
252
|
if (pathBack?.fieldBackToParent === field.parameters.field) {
|
|
253
253
|
runtimeId = pathBack.parent.entity.id
|
|
254
254
|
} else {
|
|
255
|
-
runtimeId = fieldDatum instanceof
|
|
255
|
+
runtimeId = fieldDatum instanceof ServerId ? fieldDatum : new UnpersistedEntityDummyId()
|
|
256
256
|
}
|
|
257
257
|
this.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)
|
|
258
258
|
} else {
|
|
@@ -279,7 +279,7 @@ export class StateInitializer {
|
|
|
279
279
|
}
|
|
280
280
|
|
|
281
281
|
public initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {
|
|
282
|
-
const entityId = id.
|
|
282
|
+
const entityId = id.uniqueValue
|
|
283
283
|
|
|
284
284
|
const existing = this.treeStore.entityStore.get(entityId)
|
|
285
285
|
if (existing !== undefined) {
|
|
@@ -288,7 +288,7 @@ export class StateInitializer {
|
|
|
288
288
|
|
|
289
289
|
const entityState: EntityState = {
|
|
290
290
|
entityName,
|
|
291
|
-
hasIdSetInStone: id instanceof
|
|
291
|
+
hasIdSetInStone: id instanceof ServerId,
|
|
292
292
|
id,
|
|
293
293
|
isScheduledForDeletion: false,
|
|
294
294
|
maidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,
|
|
@@ -302,7 +302,7 @@ export class StateInitializer {
|
|
|
302
302
|
private initializeEntityListState(
|
|
303
303
|
blueprint: EntityListBlueprint,
|
|
304
304
|
entityName: EntityName,
|
|
305
|
-
initialEntityIds: Set<
|
|
305
|
+
initialEntityIds: Set<EntityId>,
|
|
306
306
|
): EntityListState {
|
|
307
307
|
const entityListState: EntityListState = {
|
|
308
308
|
type: 'entityList',
|
|
@@ -334,13 +334,13 @@ export class StateInitializer {
|
|
|
334
334
|
},
|
|
335
335
|
}
|
|
336
336
|
|
|
337
|
-
const initialData: Iterable<
|
|
337
|
+
const initialData: Iterable<EntityId | undefined> =
|
|
338
338
|
initialEntityIds.size === 0
|
|
339
339
|
? Array.from({ length: blueprint.marker.parameters.initialEntityCount })
|
|
340
340
|
: initialEntityIds
|
|
341
341
|
for (const entityId of initialData) {
|
|
342
342
|
this.initializeEntityRealm(
|
|
343
|
-
entityId ? new
|
|
343
|
+
entityId ? new ServerId(entityId, entityName) : new UnpersistedEntityDummyId(),
|
|
344
344
|
entityListState.entityName,
|
|
345
345
|
{
|
|
346
346
|
type: 'listEntity',
|
|
@@ -417,7 +417,7 @@ export class StateInitializer {
|
|
|
417
417
|
`Perhaps you wanted to use a <Repeater />?`,
|
|
418
418
|
)
|
|
419
419
|
}
|
|
420
|
-
if (fieldDatum instanceof
|
|
420
|
+
if (fieldDatum instanceof ServerId) {
|
|
421
421
|
throw new BindingError(
|
|
422
422
|
`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +
|
|
423
423
|
`Perhaps you wanted to use <HasOne />?`,
|
|
@@ -463,7 +463,7 @@ export class StateInitializer {
|
|
|
463
463
|
private initializeFromHasManyRelationMarker(
|
|
464
464
|
parent: EntityRealmState,
|
|
465
465
|
field: HasManyRelationMarker,
|
|
466
|
-
persistedEntityIds: Set<
|
|
466
|
+
persistedEntityIds: Set<EntityId>,
|
|
467
467
|
) {
|
|
468
468
|
parent.children.set(
|
|
469
469
|
field.placeholderName,
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
EntityListPersistedData,
|
|
4
4
|
ReceivedDataTree,
|
|
5
5
|
RuntimeId,
|
|
6
|
-
|
|
6
|
+
ServerId,
|
|
7
7
|
UnpersistedEntityDummyId,
|
|
8
8
|
} from '../accessorTree'
|
|
9
9
|
import { BindingError } from '../BindingError'
|
|
@@ -63,7 +63,7 @@ export class TreeAugmenter {
|
|
|
63
63
|
}
|
|
64
64
|
this.updateEntityListPersistedData(rootState, rootData)
|
|
65
65
|
} else if (rootState.type === 'entityRealm') {
|
|
66
|
-
if (rootData !== undefined && !(rootData instanceof
|
|
66
|
+
if (rootData !== undefined && !(rootData instanceof ServerId)) {
|
|
67
67
|
continue // This should never happen.
|
|
68
68
|
}
|
|
69
69
|
this.updateRealmIdIfNecessary(rootState, rootData)
|
|
@@ -130,12 +130,12 @@ export class TreeAugmenter {
|
|
|
130
130
|
|
|
131
131
|
private updateRealmIdIfNecessary(
|
|
132
132
|
realm: EntityRealmState | EntityRealmStateStub,
|
|
133
|
-
newId:
|
|
133
|
+
newId: ServerId | undefined | null,
|
|
134
134
|
) {
|
|
135
135
|
const currentId = realm.entity.id
|
|
136
136
|
let idToChangeTo: RuntimeId | undefined = undefined
|
|
137
137
|
|
|
138
|
-
if (currentId instanceof
|
|
138
|
+
if (currentId instanceof ServerId) {
|
|
139
139
|
if (newId === undefined || newId === null) {
|
|
140
140
|
idToChangeTo = new UnpersistedEntityDummyId()
|
|
141
141
|
} else if (currentId.value !== newId.value) {
|
|
@@ -157,14 +157,14 @@ export class TreeAugmenter {
|
|
|
157
157
|
|
|
158
158
|
private updateEntityRealmPersistedData(realm: EntityRealmState) {
|
|
159
159
|
const realmId = realm.entity.id
|
|
160
|
-
const persistedData = realmId.existsOnServer ? this.treeStore.persistedEntityData.get(realmId.
|
|
160
|
+
const persistedData = realmId.existsOnServer ? this.treeStore.persistedEntityData.get(realmId.uniqueValue) : undefined
|
|
161
161
|
|
|
162
162
|
for (const [placeholderName, child] of realm.children) {
|
|
163
163
|
const childData = persistedData?.get(placeholderName)
|
|
164
164
|
|
|
165
165
|
switch (child.type) {
|
|
166
166
|
case 'field': {
|
|
167
|
-
if (childData instanceof
|
|
167
|
+
if (childData instanceof ServerId || childData instanceof Set) {
|
|
168
168
|
throw new BindingError()
|
|
169
169
|
}
|
|
170
170
|
|
|
@@ -181,7 +181,7 @@ export class TreeAugmenter {
|
|
|
181
181
|
}
|
|
182
182
|
case 'entityRealm':
|
|
183
183
|
case 'entityRealmStub': {
|
|
184
|
-
if (!(childData instanceof
|
|
184
|
+
if (!(childData instanceof ServerId) && childData !== undefined && childData !== null) {
|
|
185
185
|
throw new BindingError()
|
|
186
186
|
}
|
|
187
187
|
this.updateRealmIdIfNecessary(child, childData)
|
|
@@ -220,7 +220,7 @@ export class TreeAugmenter {
|
|
|
220
220
|
if (
|
|
221
221
|
// It's a uuid but it's not among the persisted so either it got deleted/disconnected or the client-side
|
|
222
222
|
// id generation didn't quite pan out.
|
|
223
|
-
childRuntimeId instanceof
|
|
223
|
+
childRuntimeId instanceof ServerId ||
|
|
224
224
|
childRuntimeId instanceof ClientGeneratedUuid ||
|
|
225
225
|
// No persisted entity id to allocate to this so that's a goodbye.
|
|
226
226
|
persistedWithoutState.length === 0
|
|
@@ -234,7 +234,7 @@ export class TreeAugmenter {
|
|
|
234
234
|
this.eventManager,
|
|
235
235
|
this.stateInitializer,
|
|
236
236
|
child,
|
|
237
|
-
new
|
|
237
|
+
new ServerId(drawnId, child.entity.entityName),
|
|
238
238
|
)
|
|
239
239
|
}
|
|
240
240
|
} else if (childRuntimeId instanceof ClientGeneratedUuid) {
|
|
@@ -243,7 +243,7 @@ export class TreeAugmenter {
|
|
|
243
243
|
this.eventManager,
|
|
244
244
|
this.stateInitializer,
|
|
245
245
|
child,
|
|
246
|
-
new
|
|
246
|
+
new ServerId(childRuntimeId.value, child.entity.entityName),
|
|
247
247
|
)
|
|
248
248
|
}
|
|
249
249
|
if (child.type === 'entityRealm') {
|
|
@@ -251,7 +251,7 @@ export class TreeAugmenter {
|
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
253
|
for (const unclaimedPersistedId of persistedWithoutState) {
|
|
254
|
-
this.stateInitializer.initializeEntityRealm(new
|
|
254
|
+
this.stateInitializer.initializeEntityRealm(new ServerId(unclaimedPersistedId, state.entityName), state.entityName, {
|
|
255
255
|
type: 'listEntity',
|
|
256
256
|
parent: state,
|
|
257
257
|
})
|