@aws-amplify/datastore 3.11.3 → 3.11.4-geo.13

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/src/util.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import { monotonicFactory, ULID } from 'ulid';
3
3
  import { v4 as uuid } from 'uuid';
4
+ import { produce, applyPatches, Patch } from 'immer';
4
5
  import { ModelInstanceCreator } from './datastore/datastore';
5
6
  import {
6
7
  AllOperators,
@@ -146,7 +147,7 @@ export const isNonModelConstructor = (
146
147
  return nonModelClasses.has(obj);
147
148
  };
148
149
 
149
- /*
150
+ /*
150
151
  When we have GSI(s) with composite sort keys defined on a model
151
152
  There are some very particular rules regarding which fields must be included in the update mutation input
152
153
  The field selection becomes more complex as the number of GSIs with composite sort keys grows
@@ -156,7 +157,7 @@ export const isNonModelConstructor = (
156
157
  2. all of the fields from any other composite sort key that intersect with the fields from 1.
157
158
 
158
159
  E.g.,
159
- Model @model
160
+ Model @model
160
161
  @key(name: 'key1' fields: ['hk', 'a', 'b', 'c'])
161
162
  @key(name: 'key2' fields: ['hk', 'a', 'b', 'd'])
162
163
  @key(name: 'key3' fields: ['hk', 'x', 'y', 'z'])
@@ -192,7 +193,7 @@ export const processCompositeKeys = (
192
193
  .filter(isModelAttributeCompositeKey)
193
194
  .map(extractCompositeSortKey);
194
195
 
195
- /*
196
+ /*
196
197
  if 2 sets of fields have any intersecting fields => combine them into 1 union set
197
198
  e.g., ['a', 'b', 'c'] and ['a', 'b', 'd'] => ['a', 'b', 'c', 'd']
198
199
  */
@@ -773,3 +774,39 @@ export class DeferredCallbackResolver {
773
774
  this.limitPromise.resolve(LimitTimerRaceResolvedValues.LIMIT);
774
775
  }
775
776
  }
777
+
778
+ /**
779
+ * merge two sets of patches created by immer produce.
780
+ * newPatches take precedent over oldPatches for patches modifying the same path.
781
+ * In the case many consecutive pathces are merged the original model should
782
+ * always be the root model.
783
+ *
784
+ * Example:
785
+ * A -> B, patches1
786
+ * B -> C, patches2
787
+ *
788
+ * mergePatches(A, patches1, patches2) to get patches for A -> C
789
+ *
790
+ * @param originalSource the original Model the patches should be applied to
791
+ * @param oldPatches immer produce patch list
792
+ * @param newPatches immer produce patch list (will take precedence)
793
+ * @return merged patches
794
+ */
795
+ export function mergePatches<T>(
796
+ originalSource: T,
797
+ oldPatches: Patch[],
798
+ newPatches: Patch[]
799
+ ): Patch[] {
800
+ const patchesToMerge = oldPatches.concat(newPatches);
801
+ let patches: Patch[];
802
+ produce(
803
+ originalSource,
804
+ draft => {
805
+ applyPatches(draft, patchesToMerge);
806
+ },
807
+ p => {
808
+ patches = p;
809
+ }
810
+ );
811
+ return patches;
812
+ }