@aws-amplify/datastore 3.12.6-next.13 → 3.12.6-next.32

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.
Files changed (162) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/lib/authModeStrategies/multiAuthStrategy.js +17 -64
  3. package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
  4. package/lib/datastore/datastore.js +682 -469
  5. package/lib/datastore/datastore.js.map +1 -1
  6. package/lib/index.js +2 -4
  7. package/lib/index.js.map +1 -1
  8. package/lib/predicates/index.js +12 -2
  9. package/lib/predicates/index.js.map +1 -1
  10. package/lib/storage/adapter/AsyncStorageAdapter.js +393 -298
  11. package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  12. package/lib/storage/adapter/AsyncStorageDatabase.js +97 -122
  13. package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  14. package/lib/storage/adapter/InMemoryStore.js +16 -67
  15. package/lib/storage/adapter/InMemoryStore.js.map +1 -1
  16. package/lib/storage/adapter/InMemoryStore.native.js +2 -4
  17. package/lib/storage/adapter/InMemoryStore.native.js.map +1 -1
  18. package/lib/storage/adapter/IndexedDBAdapter.js +497 -404
  19. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  20. package/lib/storage/adapter/getDefaultAdapter/index.js +3 -5
  21. package/lib/storage/adapter/getDefaultAdapter/index.js.map +1 -1
  22. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -4
  23. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
  24. package/lib/storage/storage.js +129 -151
  25. package/lib/storage/storage.js.map +1 -1
  26. package/lib/sync/datastoreConnectivity.js +13 -17
  27. package/lib/sync/datastoreConnectivity.js.map +1 -1
  28. package/lib/sync/datastoreReachability/index.native.js +2 -4
  29. package/lib/sync/datastoreReachability/index.native.js.map +1 -1
  30. package/lib/sync/index.js +544 -488
  31. package/lib/sync/index.js.map +1 -1
  32. package/lib/sync/merger.js +21 -80
  33. package/lib/sync/merger.js.map +1 -1
  34. package/lib/sync/outbox.js +95 -162
  35. package/lib/sync/outbox.js.map +1 -1
  36. package/lib/sync/processors/errorMaps.js +4 -34
  37. package/lib/sync/processors/errorMaps.js.map +1 -1
  38. package/lib/sync/processors/mutation.js +285 -312
  39. package/lib/sync/processors/mutation.js.map +1 -1
  40. package/lib/sync/processors/subscription.js +218 -259
  41. package/lib/sync/processors/subscription.js.map +1 -1
  42. package/lib/sync/processors/sync.js +141 -212
  43. package/lib/sync/processors/sync.js.map +1 -1
  44. package/lib/sync/utils.js +50 -61
  45. package/lib/sync/utils.js.map +1 -1
  46. package/lib/types.js +13 -39
  47. package/lib/types.js.map +1 -1
  48. package/lib/util.js +429 -242
  49. package/lib/util.js.map +1 -1
  50. package/lib-esm/authModeStrategies/multiAuthStrategy.d.ts +11 -0
  51. package/lib-esm/authModeStrategies/multiAuthStrategy.js +13 -57
  52. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
  53. package/lib-esm/datastore/datastore.d.ts +107 -17
  54. package/lib-esm/datastore/datastore.js +649 -433
  55. package/lib-esm/datastore/datastore.js.map +1 -1
  56. package/lib-esm/index.d.ts +3 -19
  57. package/lib-esm/predicates/index.d.ts +3 -2
  58. package/lib-esm/predicates/index.js +13 -3
  59. package/lib-esm/predicates/index.js.map +1 -1
  60. package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +4 -3
  61. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +356 -258
  62. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  63. package/lib-esm/storage/adapter/AsyncStorageDatabase.d.ts +14 -4
  64. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +67 -92
  65. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  66. package/lib-esm/storage/adapter/InMemoryStore.js +1 -52
  67. package/lib-esm/storage/adapter/InMemoryStore.js.map +1 -1
  68. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +26 -4
  69. package/lib-esm/storage/adapter/IndexedDBAdapter.js +446 -346
  70. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  71. package/lib-esm/storage/adapter/index.d.ts +1 -1
  72. package/lib-esm/storage/storage.d.ts +1 -1
  73. package/lib-esm/storage/storage.js +94 -113
  74. package/lib-esm/storage/storage.js.map +1 -1
  75. package/lib-esm/sync/datastoreConnectivity.d.ts +1 -0
  76. package/lib-esm/sync/datastoreConnectivity.js +10 -11
  77. package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
  78. package/lib-esm/sync/index.d.ts +31 -5
  79. package/lib-esm/sync/index.js +525 -466
  80. package/lib-esm/sync/index.js.map +1 -1
  81. package/lib-esm/sync/merger.d.ts +9 -3
  82. package/lib-esm/sync/merger.js +14 -73
  83. package/lib-esm/sync/merger.js.map +1 -1
  84. package/lib-esm/sync/outbox.d.ts +2 -2
  85. package/lib-esm/sync/outbox.js +79 -146
  86. package/lib-esm/sync/outbox.js.map +1 -1
  87. package/lib-esm/sync/processors/errorMaps.js +1 -31
  88. package/lib-esm/sync/processors/errorMaps.js.map +1 -1
  89. package/lib-esm/sync/processors/mutation.d.ts +2 -0
  90. package/lib-esm/sync/processors/mutation.js +271 -295
  91. package/lib-esm/sync/processors/mutation.js.map +1 -1
  92. package/lib-esm/sync/processors/subscription.d.ts +2 -0
  93. package/lib-esm/sync/processors/subscription.js +214 -245
  94. package/lib-esm/sync/processors/subscription.js.map +1 -1
  95. package/lib-esm/sync/processors/sync.d.ts +2 -1
  96. package/lib-esm/sync/processors/sync.js +127 -195
  97. package/lib-esm/sync/processors/sync.js.map +1 -1
  98. package/lib-esm/sync/utils.d.ts +3 -2
  99. package/lib-esm/sync/utils.js +45 -57
  100. package/lib-esm/sync/utils.js.map +1 -1
  101. package/lib-esm/types.d.ts +65 -26
  102. package/lib-esm/types.js +10 -38
  103. package/lib-esm/types.js.map +1 -1
  104. package/lib-esm/util.d.ts +67 -24
  105. package/lib-esm/util.js +420 -233
  106. package/lib-esm/util.js.map +1 -1
  107. package/package.json +14 -7
  108. package/src/authModeStrategies/multiAuthStrategy.ts +12 -1
  109. package/src/datastore/datastore.ts +798 -397
  110. package/src/predicates/index.ts +32 -10
  111. package/src/storage/adapter/AsyncStorageAdapter.ts +309 -93
  112. package/src/storage/adapter/AsyncStorageDatabase.ts +74 -26
  113. package/src/storage/adapter/IndexedDBAdapter.ts +358 -134
  114. package/src/storage/adapter/index.ts +1 -1
  115. package/src/storage/storage.ts +69 -22
  116. package/src/sync/datastoreConnectivity.ts +6 -0
  117. package/src/sync/index.ts +521 -412
  118. package/src/sync/merger.ts +20 -4
  119. package/src/sync/outbox.ts +22 -9
  120. package/src/sync/processors/mutation.ts +188 -150
  121. package/src/sync/processors/subscription.ts +289 -253
  122. package/src/sync/processors/sync.ts +151 -138
  123. package/src/sync/utils.ts +67 -12
  124. package/src/types.ts +182 -30
  125. package/src/util.ts +505 -176
  126. package/build.js +0 -5
  127. package/dist/aws-amplify-datastore.js +0 -98255
  128. package/dist/aws-amplify-datastore.js.map +0 -1
  129. package/dist/aws-amplify-datastore.min.js +0 -66
  130. package/dist/aws-amplify-datastore.min.js.map +0 -1
  131. package/index.js +0 -7
  132. package/lib/authModeStrategies/defaultAuthStrategy.d.ts +0 -2
  133. package/lib/authModeStrategies/index.d.ts +0 -2
  134. package/lib/authModeStrategies/multiAuthStrategy.d.ts +0 -2
  135. package/lib/datastore/datastore.d.ts +0 -66
  136. package/lib/index.d.ts +0 -31
  137. package/lib/predicates/index.d.ts +0 -15
  138. package/lib/predicates/sort.d.ts +0 -8
  139. package/lib/ssr/index.d.ts +0 -3
  140. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +0 -40
  141. package/lib/storage/adapter/AsyncStorageDatabase.d.ts +0 -29
  142. package/lib/storage/adapter/InMemoryStore.d.ts +0 -11
  143. package/lib/storage/adapter/InMemoryStore.native.d.ts +0 -1
  144. package/lib/storage/adapter/IndexedDBAdapter.d.ts +0 -37
  145. package/lib/storage/adapter/getDefaultAdapter/index.d.ts +0 -3
  146. package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +0 -3
  147. package/lib/storage/adapter/index.d.ts +0 -9
  148. package/lib/storage/storage.d.ts +0 -49
  149. package/lib/sync/datastoreConnectivity.d.ts +0 -15
  150. package/lib/sync/datastoreReachability/index.d.ts +0 -3
  151. package/lib/sync/datastoreReachability/index.native.d.ts +0 -3
  152. package/lib/sync/index.d.ts +0 -63
  153. package/lib/sync/merger.d.ts +0 -11
  154. package/lib/sync/outbox.d.ts +0 -27
  155. package/lib/sync/processors/errorMaps.d.ts +0 -17
  156. package/lib/sync/processors/mutation.d.ts +0 -56
  157. package/lib/sync/processors/subscription.d.ts +0 -31
  158. package/lib/sync/processors/sync.d.ts +0 -27
  159. package/lib/sync/utils.d.ts +0 -41
  160. package/lib/types.d.ts +0 -462
  161. package/lib/util.d.ts +0 -113
  162. package/webpack.config.dev.js +0 -6
@@ -29,7 +29,7 @@ export interface Adapter extends SystemComponent {
29
29
  firstOrLast: QueryOne
30
30
  ): Promise<T | undefined>;
31
31
  batchSave<T extends PersistentModel>(
32
- modelConstructor: PersistentModelConstructor<any>,
32
+ modelConstructor: PersistentModelConstructor<T>,
33
33
  items: ModelInstanceMetadata[]
34
34
  ): Promise<[T, OpType][]>;
35
35
  }
@@ -26,6 +26,7 @@ import {
26
26
  validatePredicate,
27
27
  valuesEqual,
28
28
  } from '../util';
29
+ import { getIdentifierValue } from '../sync/utils';
29
30
  import { Adapter } from './adapter';
30
31
  import getDefaultAdapter from './adapter/getDefaultAdapter';
31
32
 
@@ -188,7 +189,21 @@ class StorageClass implements StorageFacade {
188
189
  condition
189
190
  );
190
191
 
191
- const modelIds = new Set(models.map(({ id }) => id));
192
+ const modelConstructor = isModelConstructor(modelOrModelConstructor)
193
+ ? modelOrModelConstructor
194
+ : (Object.getPrototypeOf(modelOrModelConstructor || {})
195
+ .constructor as PersistentModelConstructor<T>);
196
+ const namespaceName = this.namespaceResolver(modelConstructor);
197
+
198
+ const modelDefinition =
199
+ this.schema.namespaces[namespaceName].models[modelConstructor.name];
200
+
201
+ const modelIds = new Set(
202
+ models.map(model => {
203
+ const modelId = getIdentifierValue(modelDefinition, model);
204
+ return modelId;
205
+ })
206
+ );
192
207
 
193
208
  if (
194
209
  !isModelConstructor(modelOrModelConstructor) &&
@@ -204,7 +219,8 @@ class StorageClass implements StorageFacade {
204
219
  let theCondition: PredicatesGroup<any>;
205
220
 
206
221
  if (!isModelConstructor(modelOrModelConstructor)) {
207
- theCondition = modelIds.has(model.id)
222
+ const modelId = getIdentifierValue(modelDefinition, model);
223
+ theCondition = modelIds.has(modelId)
208
224
  ? ModelPredicateCreator.getPredicates(condition, false)
209
225
  : undefined;
210
226
  }
@@ -337,30 +353,65 @@ class StorageClass implements StorageFacade {
337
353
 
338
354
  // set original values for these fields
339
355
  updatedFields.forEach((field: string) => {
340
- const targetName: any = isTargetNameAssociation(
356
+ const targetNames: any = isTargetNameAssociation(
341
357
  fields[field]?.association
342
358
  );
343
359
 
344
- // if field refers to a belongsTo relation, use the target field instead
345
- const key = targetName || field;
360
+ if (Array.isArray(targetNames)) {
361
+ // if field refers to a belongsTo relation, use the target field instead
362
+
363
+ for (const targetName of targetNames) {
364
+ // check field values by value. Ignore unchanged fields
365
+ if (!valuesEqual(source[targetName], originalElement[targetName])) {
366
+ // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
367
+
368
+ updatedElement[targetName] =
369
+ originalElement[targetName] === undefined
370
+ ? null
371
+ : originalElement[targetName];
372
+
373
+ for (const fieldSet of compositeKeys) {
374
+ // include all of the fields that comprise the composite key
375
+ if (fieldSet.has(targetName)) {
376
+ for (const compositeField of fieldSet) {
377
+ updatedElement[compositeField] =
378
+ originalElement[compositeField];
379
+ }
380
+ }
381
+ }
382
+ }
383
+ }
384
+ } else {
385
+ // Backwards compatibility pre-CPK
386
+
387
+ // if field refers to a belongsTo relation, use the target field instead
388
+ const key = targetNames || field;
389
+
390
+ // check field values by value. Ignore unchanged fields
391
+ if (!valuesEqual(source[key], originalElement[key])) {
392
+ // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
346
393
 
347
- // check field values by value. Ignore unchanged fields
348
- if (!valuesEqual(source[key], originalElement[key])) {
349
- // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
350
- updatedElement[key] =
351
- originalElement[key] === undefined ? null : originalElement[key];
394
+ updatedElement[key] =
395
+ originalElement[key] === undefined ? null : originalElement[key];
352
396
 
353
- for (const fieldSet of compositeKeys) {
354
- // include all of the fields that comprise the composite key
355
- if (fieldSet.has(key)) {
356
- for (const compositeField of fieldSet) {
357
- updatedElement[compositeField] = originalElement[compositeField];
397
+ for (const fieldSet of compositeKeys) {
398
+ // include all of the fields that comprise the composite key
399
+ if (fieldSet.has(key)) {
400
+ for (const compositeField of fieldSet) {
401
+ updatedElement[compositeField] =
402
+ originalElement[compositeField];
403
+ }
358
404
  }
359
405
  }
360
406
  }
361
407
  }
362
408
  });
363
409
 
410
+ // Exit early when there are no changes introduced in the update mutation
411
+ if (Object.keys(updatedElement).length === 0) {
412
+ return null;
413
+ }
414
+
364
415
  // include field(s) from custom PK if one is specified for the model
365
416
  if (primaryKey && primaryKey.length) {
366
417
  for (const pkField of primaryKey) {
@@ -368,10 +419,6 @@ class StorageClass implements StorageFacade {
368
419
  }
369
420
  }
370
421
 
371
- if (Object.keys(updatedElement).length === 0) {
372
- return null;
373
- }
374
-
375
422
  const { id, _version, _lastChangedAt, _deleted } = originalElement;
376
423
 
377
424
  // For update mutations we only want to send fields with changes
@@ -421,7 +468,7 @@ class ExclusiveStorage implements StorageFacade {
421
468
  patchesTuple?: [Patch[], PersistentModel]
422
469
  ): Promise<[T, OpType.INSERT | OpType.UPDATE][]> {
423
470
  return this.runExclusive<[T, OpType.INSERT | OpType.UPDATE][]>(storage =>
424
- storage.save<T>(model, condition, mutator, patchesTuple)
471
+ storage.save(model, condition, mutator, patchesTuple)
425
472
  );
426
473
  }
427
474
 
@@ -485,11 +532,11 @@ class ExclusiveStorage implements StorageFacade {
485
532
  }
486
533
 
487
534
  async clear() {
488
- await this.storage.clear();
535
+ await this.runExclusive(storage => storage.clear());
489
536
  }
490
537
 
491
538
  batchSave<T extends PersistentModel>(
492
- modelConstructor: PersistentModelConstructor<any>,
539
+ modelConstructor: PersistentModelConstructor<T>,
493
540
  items: ModelInstanceMetadata[]
494
541
  ): Promise<[T, OpType][]> {
495
542
  return this.storage.batchSave(modelConstructor, items);
@@ -52,6 +52,12 @@ export default class DataStoreConnectivity {
52
52
  }
53
53
  }
54
54
 
55
+ // for consistency with other background processors.
56
+ async stop() {
57
+ this.unsubscribe();
58
+ return;
59
+ }
60
+
55
61
  socketDisconnected() {
56
62
  if (this.observer && typeof this.observer.next === 'function') {
57
63
  this.observer.next({ online: false }); // Notify network issue from the socket