@contember/binding-legacy 2.0.0 → 2.0.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.
@@ -36,18 +36,22 @@ export type PersistMutationResult = {
36
36
  export class MutationGenerator {
37
37
 
38
38
  private aliasCounter = 1
39
+ private processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()
40
+ private processedDeletes = new Set<string>()
39
41
 
40
- public constructor(
42
+ private constructor(
41
43
  private readonly treeStore: TreeStore,
42
44
  private readonly qb: ContentQueryBuilder,
43
45
  ) {
44
46
  }
45
47
 
46
- public getPersistMutation(): PersistMutationResult {
48
+ public static getPersistMutation(treeStore: TreeStore, qb: ContentQueryBuilder): PersistMutationResult {
49
+ return new MutationGenerator(treeStore, qb).getPersistMutation()
50
+ }
51
+
52
+ private getPersistMutation(): PersistMutationResult {
47
53
  const mutations: Record<string, ContentMutation<any>> = {}
48
54
  const operations: SubMutationOperation[] = []
49
- const processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()
50
- const processedDeletes = new Set<string>()
51
55
 
52
56
  for (const [treeRootId, rootStates] of this.treeStore.subTreeStatesByRoot.entries()) {
53
57
  for (const [placeholderName, subTreeState] of Array.from(rootStates.entries()).reverse()) {
@@ -59,7 +63,6 @@ export class MutationGenerator {
59
63
  continue
60
64
  }
61
65
  const subMutation = this.createMutation(
62
- processedPlaceholdersByEntity,
63
66
  placeholderName,
64
67
  'single',
65
68
  subTreeState.entity.id.value,
@@ -79,7 +82,6 @@ export class MutationGenerator {
79
82
  continue
80
83
  }
81
84
  const subMutation = this.createMutation(
82
- processedPlaceholdersByEntity,
83
85
  placeholderName,
84
86
  'list',
85
87
  childState.entity.id.value,
@@ -97,10 +99,10 @@ export class MutationGenerator {
97
99
  }
98
100
  if (removalType === 'delete') {
99
101
  const key = `${subTreeState.entityName}#${removedId}`
100
- if (processedDeletes.has(key)) {
102
+ if (this.processedDeletes.has(key)) {
101
103
  continue
102
104
  }
103
- processedDeletes.add(key)
105
+ this.processedDeletes.add(key)
104
106
  const alias = this.createAlias()
105
107
  mutations[alias] = this.createDeleteMutation(
106
108
  subTreeState.entityName,
@@ -122,7 +124,6 @@ export class MutationGenerator {
122
124
  }
123
125
 
124
126
  private createMutation(
125
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
126
127
  subTreePlaceholder: PlaceholderName,
127
128
  subTreeType: 'list' | 'single',
128
129
  entityId: EntityId,
@@ -153,7 +154,6 @@ export class MutationGenerator {
153
154
  ]
154
155
  } else if (!realmState.entity.id.existsOnServer) {
155
156
  const builder = this.createCreateMutation(
156
- processedPlaceholdersByEntity,
157
157
  realmState,
158
158
  fieldMarkers,
159
159
  )
@@ -173,7 +173,6 @@ export class MutationGenerator {
173
173
  ]
174
174
  }
175
175
  const builder = this.createUpdateMutation(
176
- processedPlaceholdersByEntity,
177
176
  realmState,
178
177
  fieldMarkers,
179
178
  )
@@ -203,11 +202,10 @@ export class MutationGenerator {
203
202
  }
204
203
 
205
204
  private createUpdateMutation(
206
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
207
205
  entityRealm: EntityRealmState,
208
206
  fieldMarkers: EntityFieldMarkers,
209
207
  ): ContentMutation<any> | undefined {
210
- const input = this.getUpdateDataInput(processedPlaceholdersByEntity, entityRealm)
208
+ const input = this.getUpdateDataInput(entityRealm)
211
209
 
212
210
  if (input === undefined) {
213
211
  return undefined
@@ -228,14 +226,10 @@ export class MutationGenerator {
228
226
  }
229
227
 
230
228
  private createCreateMutation(
231
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
232
229
  entityRealm: EntityRealmState,
233
230
  fieldMarkers: EntityFieldMarkers,
234
231
  ): ContentMutation<any> | undefined {
235
- const input = this.getCreateDataInput(
236
- processedPlaceholdersByEntity,
237
- entityRealm,
238
- )
232
+ const input = this.getCreateDataInput(entityRealm)
239
233
  if (input === undefined) {
240
234
  return undefined
241
235
  }
@@ -246,13 +240,12 @@ export class MutationGenerator {
246
240
  }
247
241
 
248
242
  private getCreateDataInput(
249
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
250
243
  currentState: EntityRealmState | EntityRealmStateStub,
251
244
  ): Input.CreateDataInput | undefined {
252
- let processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)
245
+ let processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity)
253
246
 
254
247
  if (processedPlaceholders === undefined) {
255
- processedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))
248
+ this.processedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))
256
249
  }
257
250
 
258
251
  if (currentState.type === 'entityRealmStub') {
@@ -318,7 +311,7 @@ export class MutationGenerator {
318
311
  })
319
312
  continue
320
313
  }
321
- const input = this.getCreateOneRelationInput(processedPlaceholdersByEntity, fieldState, marker)
314
+ const input = this.getCreateOneRelationInput(fieldState, marker)
322
315
  if (input !== undefined) {
323
316
  const current = result[marker.parameters.field]
324
317
  if (Array.isArray(input) && Array.isArray(current)) {
@@ -345,7 +338,7 @@ export class MutationGenerator {
345
338
  })
346
339
  continue
347
340
  }
348
- const input = this.getCreateManyRelationInput(processedPlaceholdersByEntity, fieldState)
341
+ const input = this.getCreateManyRelationInput(fieldState)
349
342
  if (input !== undefined) {
350
343
  const current = result[marker.parameters.field]
351
344
  if (Array.isArray(current)) {
@@ -375,7 +368,6 @@ export class MutationGenerator {
375
368
  }
376
369
  case 'hasOne': {
377
370
  const input = this.getCreateOneRelationInput(
378
- processedPlaceholdersByEntity,
379
371
  field.fieldState,
380
372
  field.marker,
381
373
  )
@@ -391,7 +383,6 @@ export class MutationGenerator {
391
383
  }
392
384
  case 'hasMany': {
393
385
  const input = this.getCreateManyRelationInput(
394
- processedPlaceholdersByEntity,
395
386
  field.fieldState,
396
387
  )
397
388
  if (input !== undefined) {
@@ -432,7 +423,6 @@ export class MutationGenerator {
432
423
  }
433
424
 
434
425
  private getCreateOneRelationInput(
435
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
436
426
  fieldState: EntityRealmState | EntityRealmStateStub,
437
427
  marker: HasOneRelationMarker,
438
428
  ): Input.CreateOneRelationInput | Input.CreateManyRelationInput | undefined {
@@ -440,11 +430,11 @@ export class MutationGenerator {
440
430
  const runtimeId = fieldState.entity.id
441
431
 
442
432
  if (reducedBy === undefined) {
443
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, fieldState)) {
433
+ if (this.shouldConnectInsteadOfCreate(fieldState)) {
444
434
  return { connect: { [PRIMARY_KEY_NAME]: runtimeId.value } }
445
435
  }
446
436
 
447
- const createData = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
437
+ const createData = this.getCreateDataInput(fieldState)
448
438
  if (createData === undefined) {
449
439
  return undefined
450
440
  }
@@ -452,32 +442,29 @@ export class MutationGenerator {
452
442
  }
453
443
 
454
444
  const alias = MutationAlias.encodeEntityId(runtimeId)
455
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, fieldState)) {
445
+ if (this.shouldConnectInsteadOfCreate(fieldState)) {
456
446
  // TODO also potentially update
457
447
  return [{ connect: { [PRIMARY_KEY_NAME]: runtimeId.value }, alias }]
458
448
  }
459
- const createData = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
449
+ const createData = this.getCreateDataInput(fieldState)
460
450
  if (createData === undefined) {
461
451
  return undefined
462
452
  }
463
453
  return [{ create: createData, alias }]
464
454
  }
465
455
 
466
- private getCreateManyRelationInput(
467
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
468
- fieldState: EntityListState,
469
- ): Input.CreateManyRelationInput | undefined {
456
+ private getCreateManyRelationInput(fieldState: EntityListState): Input.CreateManyRelationInput | undefined {
470
457
  const result: Input.CreateManyRelationInput = []
471
458
  for (const entityRealm of fieldState.children.values()) {
472
459
  const runtimeId = entityRealm.entity.id
473
460
  const alias = MutationAlias.encodeEntityId(runtimeId)
474
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, entityRealm)) {
461
+ if (this.shouldConnectInsteadOfCreate(entityRealm)) {
475
462
  result.push({
476
463
  alias,
477
464
  connect: { [PRIMARY_KEY_NAME]: runtimeId.value },
478
465
  })
479
466
  } else {
480
- const createData = this.getCreateDataInput(processedPlaceholdersByEntity, entityRealm)
467
+ const createData = this.getCreateDataInput(entityRealm)
481
468
  if (createData === undefined) {
482
469
  continue
483
470
  }
@@ -490,24 +477,18 @@ export class MutationGenerator {
490
477
  return result.length === 0 ? undefined : result
491
478
  }
492
479
 
493
- private shouldConnectInsteadOfCreate(
494
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
495
- currentState: EntityRealmState | EntityRealmStateStub,
496
- ): boolean {
480
+ private shouldConnectInsteadOfCreate(currentState: EntityRealmState | EntityRealmStateStub): boolean {
497
481
  const runtimeId = currentState.entity.id
498
- const processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)
482
+ const processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity)
499
483
 
500
484
  return runtimeId.existsOnServer || (runtimeId instanceof ClientGeneratedUuid && (processedPlaceholders?.has(PRIMARY_KEY_NAME) ?? false))
501
485
  }
502
486
 
503
- private getUpdateDataInput(
504
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
505
- currentState: EntityRealmState,
506
- ): Input.UpdateDataInput | undefined {
507
- let processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity)
487
+ private getUpdateDataInput(currentState: EntityRealmState): Input.UpdateDataInput | undefined {
488
+ let processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity)
508
489
 
509
490
  if (processedPlaceholders === undefined) {
510
- processedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))
491
+ this.processedPlaceholdersByEntity.set(currentState.entity, (processedPlaceholders = new Set()))
511
492
  }
512
493
 
513
494
  const pathBack = this.treeStore.getPathBackToParent(currentState)
@@ -549,14 +530,14 @@ export class MutationGenerator {
549
530
  const persistedValue = entityData?.get?.(placeholderName)?.value
550
531
  if (reducedBy === undefined) {
551
532
 
552
- const relationData = this.getUpdateOneRelationInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName)
533
+ const relationData = this.getUpdateOneRelationInput(currentState, fieldState, persistedValue, placeholderName)
553
534
  if (relationData !== undefined) {
554
535
  result[marker.parameters.field] = relationData
555
536
  }
556
537
 
557
538
 
558
539
  } else {
559
- const relationData = this.getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName, reducedBy)
540
+ const relationData = this.getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, placeholderName, reducedBy)
560
541
  if (relationData !== undefined) {
561
542
  const current = result[marker.parameters.field]
562
543
  if (Array.isArray(current)) {
@@ -582,7 +563,7 @@ export class MutationGenerator {
582
563
  if (!(persistedEntityIds instanceof Set)) {
583
564
  continue
584
565
  }
585
- const relationData = this.getUpdateManyRelationInput(fieldState, persistedEntityIds, processedPlaceholdersByEntity)
566
+ const relationData = this.getUpdateManyRelationInput(fieldState, persistedEntityIds)
586
567
  if (relationData !== undefined) {
587
568
  const current = result[marker.parameters.field]
588
569
  if (Array.isArray(current)) {
@@ -605,7 +586,6 @@ export class MutationGenerator {
605
586
  currentState: EntityRealmState,
606
587
  fieldState: EntityRealmState | EntityRealmStateStub,
607
588
  persistedValue: EntityFieldPersistedValue | undefined,
608
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
609
589
  placeholderName: PlaceholderName,
610
590
  ): Input.UpdateOneRelationInput | undefined {
611
591
  const runtimeId = fieldState.entity.id
@@ -615,7 +595,7 @@ export class MutationGenerator {
615
595
  if (fieldState.type === 'entityRealmStub') {
616
596
  return undefined// …unless we're dealing with a stub. There cannot be any updates there.
617
597
  }
618
- const input = this.getUpdateDataInput(processedPlaceholdersByEntity, fieldState)
598
+ const input = this.getUpdateDataInput(fieldState)
619
599
  if (input === undefined) {
620
600
  return undefined
621
601
  }
@@ -628,6 +608,12 @@ export class MutationGenerator {
628
608
 
629
609
  const plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)
630
610
  if (plannedDeletion !== undefined) {
611
+ const key = `${persistedValue.entityName}#${persistedValue.value}`
612
+ if (this.processedDeletes.has(key)) {
613
+ return undefined
614
+ }
615
+ this.processedDeletes.add(key)
616
+
631
617
  // It's planned for deletion.
632
618
  // TODO also potentially do something about the current entity
633
619
  return {
@@ -643,7 +629,7 @@ export class MutationGenerator {
643
629
 
644
630
  }
645
631
  // The currently present entity doesn't exist on the server. Try if creating yields anything…
646
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
632
+ const createInput = this.getCreateDataInput(fieldState)
647
633
  if (createInput !== undefined) {
648
634
  return {
649
635
  create: createInput,
@@ -662,7 +648,7 @@ export class MutationGenerator {
662
648
  connect: { [PRIMARY_KEY_NAME]: runtimeId.value },
663
649
  }
664
650
  } else {
665
- const input = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
651
+ const input = this.getCreateDataInput(fieldState)
666
652
  if (input === undefined) {
667
653
  return undefined
668
654
  }
@@ -676,7 +662,6 @@ export class MutationGenerator {
676
662
  currentState: EntityRealmState,
677
663
  fieldState: EntityRealmState | EntityRealmStateStub,
678
664
  persistedValue: EntityFieldPersistedValue | undefined,
679
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
680
665
  placeholderName: PlaceholderName,
681
666
  reducedBy: UniqueWhere,
682
667
  ): Input.UpdateManyRelationInput | undefined {
@@ -689,7 +674,7 @@ export class MutationGenerator {
689
674
  if (fieldState.type === 'entityRealmStub') {
690
675
  return undefined
691
676
  }
692
- const updateData = this.getUpdateDataInput(processedPlaceholdersByEntity, fieldState)
677
+ const updateData = this.getUpdateDataInput(fieldState)
693
678
  if (updateData === undefined) {
694
679
  return undefined
695
680
  }
@@ -703,7 +688,11 @@ export class MutationGenerator {
703
688
  }
704
689
  const plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName)
705
690
  if (plannedDeletion !== undefined) {
706
- // TODO also potentially do something about the current entity
691
+ const key = `${persistedValue.entityName}#${persistedValue.value}`
692
+ if (this.processedDeletes.has(key)) {
693
+ return undefined
694
+ }
695
+ this.processedDeletes.add(key)
707
696
  return [{
708
697
  alias,
709
698
  delete: reducedBy,
@@ -723,7 +712,7 @@ export class MutationGenerator {
723
712
  },
724
713
  ]
725
714
  }
726
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
715
+ const createInput = this.getCreateDataInput(fieldState)
727
716
 
728
717
  if (createInput === undefined) {
729
718
  return [{
@@ -741,7 +730,7 @@ export class MutationGenerator {
741
730
  connect: { [PRIMARY_KEY_NAME]: runtimeId.value },
742
731
  }]
743
732
  } else {
744
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState)
733
+ const createInput = this.getCreateDataInput(fieldState)
745
734
  if (createInput === undefined) {
746
735
  return undefined
747
736
  }
@@ -755,7 +744,6 @@ export class MutationGenerator {
755
744
  private getUpdateManyRelationInput(
756
745
  fieldState: EntityListState,
757
746
  persistedEntityIds: Set<EntityId>,
758
- processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
759
747
  ): Input.UpdateManyRelationInput | undefined {
760
748
 
761
749
  const input: Input.UpdateManyRelationInput = []
@@ -766,7 +754,7 @@ export class MutationGenerator {
766
754
  if (runtimeId.existsOnServer) {
767
755
  if (persistedEntityIds.has(runtimeId.value)) {
768
756
  if (childState.type !== 'entityRealmStub') {
769
- const dataInput = this.getUpdateDataInput(processedPlaceholdersByEntity, childState)
757
+ const dataInput = this.getUpdateDataInput(childState)
770
758
 
771
759
  if (dataInput !== undefined) {
772
760
  input.push({
@@ -785,7 +773,7 @@ export class MutationGenerator {
785
773
  })
786
774
  }
787
775
  } else {
788
- const dataInput = this.getCreateDataInput(processedPlaceholdersByEntity, childState)
776
+ const dataInput = this.getCreateDataInput(childState)
789
777
  if (dataInput !== undefined) {
790
778
  input.push({
791
779
  alias,
@@ -798,6 +786,11 @@ export class MutationGenerator {
798
786
  for (const [removedId, removalType] of fieldState.plannedRemovals) {
799
787
  const alias = MutationAlias.encodeEntityId(new ServerId(removedId, fieldState.entityName))
800
788
  if (removalType === 'delete') {
789
+ const key = `${fieldState.entityName}#${removedId}`
790
+ if (this.processedDeletes.has(key)) {
791
+ continue
792
+ }
793
+ this.processedDeletes.add(key)
801
794
 
802
795
  input.push({
803
796
  alias,