@aws-amplify/datastore 4.7.5 → 5.0.1-console-preview.4208d73.0

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 (182) hide show
  1. package/README.md +4 -0
  2. package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
  3. package/lib/authModeStrategies/index.js +3 -3
  4. package/lib/authModeStrategies/multiAuthStrategy.js +81 -24
  5. package/lib/datastore/datastore.d.ts +3 -4
  6. package/lib/datastore/datastore.js +361 -227
  7. package/lib/index.d.ts +1 -1
  8. package/lib/index.js +25 -12
  9. package/lib/predicates/index.js +46 -12
  10. package/lib/predicates/next.d.ts +2 -2
  11. package/lib/predicates/next.js +172 -74
  12. package/lib/predicates/sort.js +3 -4
  13. package/lib/ssr/index.js +1 -1
  14. package/lib/storage/adapter/AsyncStorageAdapter.js +240 -128
  15. package/lib/storage/adapter/AsyncStorageDatabase.js +100 -39
  16. package/lib/storage/adapter/InMemoryStore.js +68 -17
  17. package/lib/storage/adapter/InMemoryStore.native.js +5 -3
  18. package/lib/storage/adapter/IndexedDBAdapter.js +355 -199
  19. package/lib/storage/adapter/StorageAdapterBase.js +154 -85
  20. package/lib/storage/adapter/getDefaultAdapter/index.js +8 -9
  21. package/lib/storage/adapter/getDefaultAdapter/index.native.js +4 -3
  22. package/lib/storage/adapter/index.js +0 -1
  23. package/lib/storage/relationship.js +31 -21
  24. package/lib/storage/storage.d.ts +3 -3
  25. package/lib/storage/storage.js +143 -61
  26. package/lib/sync/datastoreConnectivity.d.ts +1 -1
  27. package/lib/sync/datastoreConnectivity.js +57 -9
  28. package/lib/sync/datastoreReachability/index.js +3 -3
  29. package/lib/sync/datastoreReachability/index.native.js +7 -5
  30. package/lib/sync/index.d.ts +1 -1
  31. package/lib/sync/index.js +193 -125
  32. package/lib/sync/merger.js +81 -10
  33. package/lib/sync/outbox.js +101 -30
  34. package/lib/sync/processors/errorMaps.d.ts +1 -1
  35. package/lib/sync/processors/errorMaps.js +41 -6
  36. package/lib/sync/processors/mutation.d.ts +1 -1
  37. package/lib/sync/processors/mutation.js +192 -101
  38. package/lib/sync/processors/subscription.d.ts +4 -1
  39. package/lib/sync/processors/subscription.js +190 -178
  40. package/lib/sync/processors/sync.d.ts +1 -1
  41. package/lib/sync/processors/sync.js +113 -47
  42. package/lib/sync/utils.d.ts +4 -4
  43. package/lib/sync/utils.js +162 -81
  44. package/lib/tsconfig.tsbuildinfo +1 -0
  45. package/lib/types.d.ts +138 -140
  46. package/lib/types.js +40 -5
  47. package/lib/util.d.ts +9 -17
  48. package/lib/util.js +216 -100
  49. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +0 -1
  50. package/lib-esm/authModeStrategies/index.js +0 -1
  51. package/lib-esm/authModeStrategies/multiAuthStrategy.js +76 -21
  52. package/lib-esm/datastore/datastore.d.ts +3 -4
  53. package/lib-esm/datastore/datastore.js +206 -77
  54. package/lib-esm/index.d.ts +1 -1
  55. package/lib-esm/index.js +0 -1
  56. package/lib-esm/predicates/index.js +40 -7
  57. package/lib-esm/predicates/next.d.ts +2 -2
  58. package/lib-esm/predicates/next.js +148 -52
  59. package/lib-esm/predicates/sort.js +2 -4
  60. package/lib-esm/ssr/index.js +0 -1
  61. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +203 -95
  62. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +66 -5
  63. package/lib-esm/storage/adapter/InMemoryStore.js +52 -2
  64. package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
  65. package/lib-esm/storage/adapter/IndexedDBAdapter.js +271 -138
  66. package/lib-esm/storage/adapter/StorageAdapterBase.js +130 -62
  67. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -5
  68. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +0 -1
  69. package/lib-esm/storage/adapter/index.js +1 -1
  70. package/lib-esm/storage/relationship.js +27 -18
  71. package/lib-esm/storage/storage.d.ts +3 -3
  72. package/lib-esm/storage/storage.js +87 -9
  73. package/lib-esm/sync/datastoreConnectivity.d.ts +1 -1
  74. package/lib-esm/sync/datastoreConnectivity.js +48 -3
  75. package/lib-esm/sync/datastoreReachability/index.js +1 -2
  76. package/lib-esm/sync/datastoreReachability/index.native.js +1 -2
  77. package/lib-esm/sync/index.d.ts +1 -1
  78. package/lib-esm/sync/index.js +136 -72
  79. package/lib-esm/sync/merger.js +73 -3
  80. package/lib-esm/sync/outbox.js +75 -5
  81. package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
  82. package/lib-esm/sync/processors/errorMaps.js +39 -5
  83. package/lib-esm/sync/processors/mutation.d.ts +1 -1
  84. package/lib-esm/sync/processors/mutation.js +130 -44
  85. package/lib-esm/sync/processors/subscription.d.ts +4 -1
  86. package/lib-esm/sync/processors/subscription.js +134 -126
  87. package/lib-esm/sync/processors/sync.d.ts +1 -1
  88. package/lib-esm/sync/processors/sync.js +74 -12
  89. package/lib-esm/sync/utils.d.ts +4 -4
  90. package/lib-esm/sync/utils.js +135 -55
  91. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  92. package/lib-esm/types.d.ts +138 -140
  93. package/lib-esm/types.js +36 -2
  94. package/lib-esm/util.d.ts +9 -17
  95. package/lib-esm/util.js +96 -16
  96. package/package.json +139 -132
  97. package/src/authModeStrategies/multiAuthStrategy.ts +11 -12
  98. package/src/datastore/datastore.ts +32 -25
  99. package/src/predicates/sort.ts +3 -1
  100. package/src/storage/adapter/InMemoryStore.ts +1 -1
  101. package/src/storage/adapter/IndexedDBAdapter.ts +1 -1
  102. package/src/storage/adapter/StorageAdapterBase.ts +1 -1
  103. package/src/storage/adapter/getDefaultAdapter/index.ts +2 -3
  104. package/src/storage/storage.ts +1 -1
  105. package/src/sync/datastoreConnectivity.ts +2 -2
  106. package/src/sync/datastoreReachability/index.native.ts +1 -1
  107. package/src/sync/datastoreReachability/index.ts +1 -1
  108. package/src/sync/index.ts +69 -78
  109. package/src/sync/processors/mutation.ts +16 -10
  110. package/src/sync/processors/subscription.ts +55 -127
  111. package/src/sync/processors/sync.ts +8 -4
  112. package/src/sync/utils.ts +7 -8
  113. package/src/types.ts +4 -8
  114. package/src/util.ts +5 -6
  115. package/lib/.tsbuildinfo +0 -3
  116. package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  117. package/lib/authModeStrategies/index.js.map +0 -1
  118. package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
  119. package/lib/datastore/datastore.js.map +0 -1
  120. package/lib/index.js.map +0 -1
  121. package/lib/predicates/index.js.map +0 -1
  122. package/lib/predicates/next.js.map +0 -1
  123. package/lib/predicates/sort.js.map +0 -1
  124. package/lib/ssr/index.js.map +0 -1
  125. package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  126. package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  127. package/lib/storage/adapter/InMemoryStore.js.map +0 -1
  128. package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
  129. package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
  130. package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
  131. package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  132. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  133. package/lib/storage/adapter/index.js.map +0 -1
  134. package/lib/storage/relationship.js.map +0 -1
  135. package/lib/storage/storage.js.map +0 -1
  136. package/lib/sync/datastoreConnectivity.js.map +0 -1
  137. package/lib/sync/datastoreReachability/index.js.map +0 -1
  138. package/lib/sync/datastoreReachability/index.native.js.map +0 -1
  139. package/lib/sync/index.js.map +0 -1
  140. package/lib/sync/merger.js.map +0 -1
  141. package/lib/sync/outbox.js.map +0 -1
  142. package/lib/sync/processors/errorMaps.js.map +0 -1
  143. package/lib/sync/processors/mutation.js.map +0 -1
  144. package/lib/sync/processors/subscription.js.map +0 -1
  145. package/lib/sync/processors/sync.js.map +0 -1
  146. package/lib/sync/utils.js.map +0 -1
  147. package/lib/types.js.map +0 -1
  148. package/lib/util.js.map +0 -1
  149. package/lib-esm/.tsbuildinfo +0 -3
  150. package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  151. package/lib-esm/authModeStrategies/index.js.map +0 -1
  152. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
  153. package/lib-esm/datastore/datastore.js.map +0 -1
  154. package/lib-esm/index.js.map +0 -1
  155. package/lib-esm/predicates/index.js.map +0 -1
  156. package/lib-esm/predicates/next.js.map +0 -1
  157. package/lib-esm/predicates/sort.js.map +0 -1
  158. package/lib-esm/ssr/index.js.map +0 -1
  159. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  160. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  161. package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
  162. package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
  163. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
  164. package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
  165. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  166. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  167. package/lib-esm/storage/adapter/index.js.map +0 -1
  168. package/lib-esm/storage/relationship.js.map +0 -1
  169. package/lib-esm/storage/storage.js.map +0 -1
  170. package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
  171. package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
  172. package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
  173. package/lib-esm/sync/index.js.map +0 -1
  174. package/lib-esm/sync/merger.js.map +0 -1
  175. package/lib-esm/sync/outbox.js.map +0 -1
  176. package/lib-esm/sync/processors/errorMaps.js.map +0 -1
  177. package/lib-esm/sync/processors/mutation.js.map +0 -1
  178. package/lib-esm/sync/processors/subscription.js.map +0 -1
  179. package/lib-esm/sync/processors/sync.js.map +0 -1
  180. package/lib-esm/sync/utils.js.map +0 -1
  181. package/lib-esm/types.js.map +0 -1
  182. package/lib-esm/util.js.map +0 -1
@@ -1,15 +1,8 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { InternalAPI } from '@aws-amplify/api/internals';
4
- import { Auth } from '@aws-amplify/auth';
5
- import { Cache } from '@aws-amplify/cache';
6
- import {
7
- Amplify,
8
- ConsoleLogger as Logger,
9
- Hub,
10
- browserOrNode,
11
- BackgroundProcessManager,
12
- } from '@aws-amplify/core';
4
+ import { Amplify, Hub, Cache } from '@aws-amplify/core';
5
+
13
6
  import {
14
7
  Draft,
15
8
  immerable,
@@ -110,6 +103,10 @@ import {
110
103
  } from '../predicates/next';
111
104
  import { getIdentifierValue } from '../sync/utils';
112
105
  import DataStoreConnectivity from '../sync/datastoreConnectivity';
106
+ import {
107
+ BackgroundProcessManager,
108
+ Logger,
109
+ } from '@aws-amplify/core/internals/utils';
113
110
 
114
111
  setAutoFreeze(true);
115
112
  enablePatches();
@@ -117,7 +114,6 @@ enablePatches();
117
114
  const logger = new Logger('DataStore');
118
115
 
119
116
  const ulid = monotonicUlidFactory(Date.now());
120
- const { isNode } = browserOrNode();
121
117
 
122
118
  type SettingMetaData = {
123
119
  identifier: ManagedIdentifier<Setting, 'id'>;
@@ -492,7 +488,6 @@ const checkSchemaCodegenVersion = (codegenVersion: string) => {
492
488
  const majorVersion = 3;
493
489
  const minorVersion = 2;
494
490
  let isValid = false;
495
-
496
491
  try {
497
492
  const versionParts = codegenVersion.split('.');
498
493
  const [major, minor, patch, patchrevision] = versionParts;
@@ -1383,7 +1378,6 @@ enum DataStoreState {
1383
1378
  // https://github.com/aws-amplify/amplify-js/pull/10477/files#r1007363485
1384
1379
  class DataStore {
1385
1380
  // reference to configured category instances. Used for preserving SSR context
1386
- private Auth = Auth;
1387
1381
  private InternalAPI = InternalAPI;
1388
1382
  private Cache = Cache;
1389
1383
 
@@ -1414,9 +1408,7 @@ class DataStore {
1414
1408
  private storageAdapter!: Adapter;
1415
1409
  // object that gets passed to descendent classes. Allows us to pass these down by reference
1416
1410
  private amplifyContext: AmplifyContext = {
1417
- Auth: this.Auth,
1418
1411
  InternalAPI: this.InternalAPI,
1419
- Cache: this.Cache,
1420
1412
  };
1421
1413
  private connectivityMonitor?: DataStoreConnectivity;
1422
1414
 
@@ -1531,6 +1523,7 @@ class DataStore {
1531
1523
  await this.storage.init();
1532
1524
  checkSchemaInitialized();
1533
1525
  await checkSchemaVersion(this.storage, schema.version);
1526
+
1534
1527
  const { aws_appsync_graphqlEndpoint } = this.amplifyConfig;
1535
1528
 
1536
1529
  if (aws_appsync_graphqlEndpoint) {
@@ -1540,7 +1533,6 @@ class DataStore {
1540
1533
  );
1541
1534
 
1542
1535
  this.syncPredicates = await this.processSyncExpressions();
1543
-
1544
1536
  this.sync = new SyncEngine(
1545
1537
  schema,
1546
1538
  namespaceResolver,
@@ -1563,11 +1555,8 @@ class DataStore {
1563
1555
  .start({ fullSyncInterval: fullSyncIntervalInMilliseconds })
1564
1556
  .subscribe({
1565
1557
  next: ({ type, data }) => {
1566
- // In Node, we need to wait for queries to be synced to prevent returning empty arrays.
1567
1558
  // In the Browser, we can begin returning data once subscriptions are in place.
1568
- const readyType = isNode
1569
- ? ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY
1570
- : ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED;
1559
+ const readyType = ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED;
1571
1560
 
1572
1561
  if (type === readyType) {
1573
1562
  this.initResolve();
@@ -2426,10 +2415,10 @@ class DataStore {
2426
2415
  data?.model?.name === model.name
2427
2416
  ) {
2428
2417
  generateAndEmitSnapshot();
2429
- Hub.remove('datastore', hubCallback);
2418
+ hubRemove();
2430
2419
  }
2431
2420
  };
2432
- Hub.listen('datastore', hubCallback);
2421
+ const hubRemove = Hub.listen('datastore', hubCallback);
2433
2422
 
2434
2423
  return this.runningProcesses.addCleaner(async () => {
2435
2424
  if (handle) {
@@ -2440,9 +2429,7 @@ class DataStore {
2440
2429
  };
2441
2430
 
2442
2431
  configure = (config: DataStoreConfig = {}) => {
2443
- this.amplifyContext.Auth = this.Auth;
2444
2432
  this.amplifyContext.InternalAPI = this.InternalAPI;
2445
- this.amplifyContext.Cache = this.Cache;
2446
2433
 
2447
2434
  const {
2448
2435
  DataStore: configDataStore,
@@ -2458,9 +2445,24 @@ class DataStore {
2458
2445
  ...configFromAmplify
2459
2446
  } = config;
2460
2447
 
2448
+ let apiKey = '';
2449
+ const currentAppSyncConfig = Amplify.getConfig().API?.AppSync;
2450
+ if (currentAppSyncConfig?.defaultAuthMode.type === 'apiKey') {
2451
+ apiKey = currentAppSyncConfig.defaultAuthMode.apiKey;
2452
+ }
2453
+
2454
+ const appSyncConfig = {
2455
+ aws_appsync_graphqlEndpoint: currentAppSyncConfig?.endpoint,
2456
+ aws_appsync_authenticationType:
2457
+ currentAppSyncConfig?.defaultAuthMode.type,
2458
+ aws_appsync_region: currentAppSyncConfig?.region,
2459
+ aws_appsync_apiKey: apiKey,
2460
+ };
2461
+
2461
2462
  this.amplifyConfig = {
2462
- ...configFromAmplify,
2463
2463
  ...this.amplifyConfig,
2464
+ ...configFromAmplify,
2465
+ ...(currentAppSyncConfig && appSyncConfig),
2464
2466
  };
2465
2467
 
2466
2468
  this.conflictHandler = this.setConflictHandler(config);
@@ -2752,6 +2754,11 @@ class DataStore {
2752
2754
  }
2753
2755
 
2754
2756
  const instance = new DataStore();
2755
- Amplify.register(instance);
2757
+ instance.configure({});
2758
+ Hub.listen('core', capsule => {
2759
+ if (capsule.payload.event === 'configure') {
2760
+ instance.configure({});
2761
+ }
2762
+ });
2756
2763
 
2757
2764
  export { DataStore as DataStoreClass, initSchema, instance as DataStore };
@@ -30,7 +30,9 @@ export class ModelSortPredicateCreator {
30
30
 
31
31
  if (!fieldNames.has(field)) {
32
32
  throw new Error(
33
- `Invalid field for model. field: ${field}, model: ${modelName}`
33
+ `Invalid field for model. field: ${String(
34
+ field
35
+ )}, model: ${modelName}`
34
36
  );
35
37
  }
36
38
 
@@ -15,7 +15,7 @@ export class InMemoryStore {
15
15
  };
16
16
 
17
17
  multiRemove = async (keys: string[], callback?) => {
18
- keys.forEach((k) => this.db.delete(k));
18
+ keys.forEach(k => this.db.delete(k));
19
19
 
20
20
  typeof callback === 'function' && callback();
21
21
  };
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { ConsoleLogger as Logger } from '@aws-amplify/core';
4
3
  import * as idb from 'idb';
5
4
  import {
6
5
  isPredicateObj,
@@ -25,6 +24,7 @@ import {
25
24
  isSafariCompatabilityMode,
26
25
  } from '../../util';
27
26
  import { StorageAdapterBase } from './StorageAdapterBase';
27
+ import { Logger } from '@aws-amplify/core/internals/utils';
28
28
 
29
29
  const logger = new Logger('DataStore');
30
30
 
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { ConsoleLogger as Logger } from '@aws-amplify/core';
4
3
  import { Adapter } from './index';
5
4
  import { ModelInstanceCreator } from '../../datastore/datastore';
6
5
  import { ModelPredicateCreator } from '../../predicates';
@@ -31,6 +30,7 @@ import {
31
30
  import type { IDBPDatabase, IDBPObjectStore } from 'idb';
32
31
  import type AsyncStorageDatabase from './AsyncStorageDatabase';
33
32
  import { ModelRelationship } from '../relationship';
33
+ import { Logger } from '@aws-amplify/core/internals/utils';
34
34
 
35
35
  const logger = new Logger('DataStore');
36
36
  const DB_NAME = 'amplify-datastore';
@@ -1,12 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { browserOrNode, isWebWorker } from '@aws-amplify/core';
4
3
  import { Adapter } from '..';
5
4
  import IndexedDBAdapter from '../IndexedDBAdapter';
6
5
  import AsyncStorageAdapter from '../AsyncStorageAdapter';
7
-
6
+ import { isWebWorker } from '@aws-amplify/core/internals/utils';
8
7
  const getDefaultAdapter: () => Adapter = () => {
9
- const { isBrowser } = browserOrNode();
8
+ const isBrowser = true; // TODO(v6): Update this for SSR
10
9
 
11
10
  if ((isBrowser && window.indexedDB) || (isWebWorker() && self.indexedDB)) {
12
11
  return IndexedDBAdapter as Adapter;
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Logger, Mutex } from '@aws-amplify/core';
4
3
  import Observable, { ZenObservable } from 'zen-observable-ts';
5
4
  import PushStream from 'zen-push';
6
5
  import { Patch } from 'immer';
@@ -32,6 +31,7 @@ import {
32
31
  import { getIdentifierValue } from '../sync/utils';
33
32
  import { Adapter } from './adapter';
34
33
  import getDefaultAdapter from './adapter/getDefaultAdapter';
34
+ import { Logger, Mutex } from '@aws-amplify/core/internals/utils';
35
35
 
36
36
  export type StorageSubscriptionMessage<T extends PersistentModel> =
37
37
  InternalSubscriptionMessage<T> & {
@@ -1,8 +1,8 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import Observable, { ZenObservable } from 'zen-observable-ts';
4
- import { ConsoleLogger as Logger } from '@aws-amplify/core';
5
4
  import { ReachabilityMonitor } from './datastoreReachability';
5
+ import { Logger } from '@aws-amplify/core/internals/utils';
6
6
 
7
7
  const logger = new Logger('DataStore');
8
8
 
@@ -28,7 +28,7 @@ export default class DataStoreConnectivity {
28
28
  if (this.observer) {
29
29
  throw new Error('Subscriber already exists');
30
30
  }
31
- return new Observable((observer) => {
31
+ return new Observable(observer => {
32
32
  this.observer = observer;
33
33
  // Will be used to forward socket connection changes, enhancing Reachability
34
34
 
@@ -1,6 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Reachability } from '@aws-amplify/core';
3
+ import { Reachability } from '@aws-amplify/core/internals/utils';
4
4
  import { default as NetInfo } from '@react-native-community/netinfo';
5
5
 
6
6
  export const ReachabilityMonitor = new Reachability().networkMonitor(NetInfo);
@@ -1,5 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Reachability } from '@aws-amplify/core';
3
+ import { Reachability } from '@aws-amplify/core/internals/utils';
4
4
 
5
5
  export const ReachabilityMonitor = new Reachability().networkMonitor();
package/src/sync/index.ts CHANGED
@@ -1,16 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import {
4
- browserOrNode,
5
- ConsoleLogger as Logger,
4
+ Logger,
6
5
  BackgroundProcessManager,
7
- Hub,
8
- } from '@aws-amplify/core';
9
- import {
10
- CONTROL_MSG as PUBSUB_CONTROL_MSG,
11
- CONNECTION_STATE_CHANGE as PUBSUB_CONNECTION_STATE_CHANGE,
12
- ConnectionState,
13
- } from '@aws-amplify/pubsub';
6
+ } from '@aws-amplify/core/internals/utils';
7
+ import { Hub } from '@aws-amplify/core';
8
+
14
9
  import Observable, { ZenObservable } from 'zen-observable-ts';
15
10
  import { ModelInstanceCreator } from '../datastore/datastore';
16
11
  import { ModelPredicateCreator } from '../predicates';
@@ -53,7 +48,12 @@ import {
53
48
  TransformerMutationType,
54
49
  } from './utils';
55
50
 
56
- const { isNode } = browserOrNode();
51
+ import {
52
+ CONTROL_MSG as PUBSUB_CONTROL_MSG,
53
+ ConnectionState,
54
+ CONNECTION_STATE_CHANGE as PUBSUB_CONNECTION_STATE_CHANGE,
55
+ } from '@aws-amplify/api-graphql';
56
+
57
57
  const logger = new Logger('DataStore');
58
58
 
59
59
  const ownSymbol = Symbol('sync');
@@ -249,53 +249,47 @@ export class SyncEngine {
249
249
  [TransformerMutationType, SchemaModel, PersistentModel]
250
250
  >;
251
251
 
252
- // NOTE: need a way to override this conditional for testing.
253
- if (isNode) {
254
- logger.warn(
255
- 'Realtime disabled when in a server-side environment'
256
- );
257
- } else {
258
- this.stopDisruptionListener =
259
- this.startDisruptionListener();
260
- //#region GraphQL Subscriptions
261
- [ctlSubsObservable, dataSubsObservable] =
262
- this.subscriptionsProcessor.start();
263
-
264
- try {
265
- await new Promise<void>((resolve, reject) => {
266
- onTerminate.then(reject);
267
- const ctlSubsSubscription =
268
- ctlSubsObservable.subscribe({
269
- next: msg => {
270
- if (msg === CONTROL_MSG.CONNECTED) {
271
- resolve();
272
- }
273
- },
274
- error: err => {
275
- reject(err);
276
- const handleDisconnect =
277
- this.disconnectionHandler();
278
- handleDisconnect(err);
279
- },
280
- });
281
-
282
- subscriptions.push(ctlSubsSubscription);
283
- });
284
- } catch (err) {
285
- observer.error(err);
286
- failedStarting();
287
- return;
288
- }
252
+ this.stopDisruptionListener =
253
+ this.startDisruptionListener();
254
+ //#region GraphQL Subscriptions
255
+ [ctlSubsObservable, dataSubsObservable] =
256
+ this.subscriptionsProcessor.start();
289
257
 
290
- logger.log('Realtime ready');
258
+ try {
259
+ await new Promise<void>((resolve, reject) => {
260
+ onTerminate.then(reject);
261
+ const ctlSubsSubscription = ctlSubsObservable.subscribe(
262
+ {
263
+ next: msg => {
264
+ if (msg === CONTROL_MSG.CONNECTED) {
265
+ resolve();
266
+ }
267
+ },
268
+ error: err => {
269
+ reject(err);
270
+ const handleDisconnect =
271
+ this.disconnectionHandler();
272
+ handleDisconnect(err);
273
+ },
274
+ }
275
+ );
291
276
 
292
- observer.next({
293
- type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
277
+ subscriptions.push(ctlSubsSubscription);
294
278
  });
295
-
296
- //#endregion
279
+ } catch (err) {
280
+ observer.error(err);
281
+ failedStarting();
282
+ return;
297
283
  }
298
284
 
285
+ logger.log('Realtime ready');
286
+
287
+ observer.next({
288
+ type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
289
+ });
290
+
291
+ //#endregion
292
+
299
293
  //#region Base & Sync queries
300
294
  try {
301
295
  await new Promise<void>((resolve, reject) => {
@@ -376,32 +370,29 @@ export class SyncEngine {
376
370
  //#endregion
377
371
 
378
372
  //#region Merge subscriptions buffer
379
- // TODO: extract to function
380
- if (!isNode) {
381
- subscriptions.push(
382
- dataSubsObservable!.subscribe(
383
- ([_transformerMutationType, modelDefinition, item]) =>
384
- this.runningProcesses.add(async () => {
385
- const modelConstructor = this.userModelClasses[
386
- modelDefinition.name
387
- ] as PersistentModelConstructor<any>;
388
-
389
- const model = this.modelInstanceCreator(
390
- modelConstructor,
391
- item
392
- );
393
-
394
- await this.storage.runExclusive(storage =>
395
- this.modelMerger.merge(
396
- storage,
397
- model,
398
- modelDefinition
399
- )
400
- );
401
- }, 'subscription dataSubsObservable event')
402
- )
403
- );
404
- }
373
+ subscriptions.push(
374
+ dataSubsObservable!.subscribe(
375
+ ([_transformerMutationType, modelDefinition, item]) =>
376
+ this.runningProcesses.add(async () => {
377
+ const modelConstructor = this.userModelClasses[
378
+ modelDefinition.name
379
+ ] as PersistentModelConstructor<any>;
380
+
381
+ const model = this.modelInstanceCreator(
382
+ modelConstructor,
383
+ item
384
+ );
385
+
386
+ await this.storage.runExclusive(storage =>
387
+ this.modelMerger.merge(
388
+ storage,
389
+ model,
390
+ modelDefinition
391
+ )
392
+ );
393
+ }, 'subscription dataSubsObservable event')
394
+ )
395
+ );
405
396
  //#endregion
406
397
  } else if (!online) {
407
398
  this.online = online;
@@ -1,17 +1,20 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { GraphQLResult, GRAPHQL_AUTH_MODE } from '@aws-amplify/api';
3
+ import { GraphQLResult } from '@aws-amplify/api';
4
4
  import { InternalAPI } from '@aws-amplify/api/internals';
5
5
  import {
6
6
  Category,
7
- ConsoleLogger as Logger,
7
+ Logger,
8
8
  CustomUserAgentDetails,
9
9
  DataStoreAction,
10
10
  jitteredBackoff,
11
11
  NonRetryableError,
12
12
  retry,
13
13
  BackgroundProcessManager,
14
- } from '@aws-amplify/core';
14
+ GraphQLAuthModeKeys,
15
+ AmplifyError,
16
+ } from '@aws-amplify/core/internals/utils';
17
+
15
18
  import Observable, { ZenObservable } from 'zen-observable-ts';
16
19
  import { MutationEvent } from '../';
17
20
  import { ModelInstanceCreator } from '../../datastore/datastore';
@@ -161,8 +164,8 @@ class MutationProcessor {
161
164
  }
162
165
 
163
166
  public async resume(): Promise<void> {
164
- await (this.runningProcesses.isOpen &&
165
- this.runningProcesses.add(async onTerminate => {
167
+ if (this.runningProcesses.isOpen) {
168
+ await this.runningProcesses.add(async onTerminate => {
166
169
  if (
167
170
  this.processing ||
168
171
  !this.isReady() ||
@@ -170,7 +173,6 @@ class MutationProcessor {
170
173
  ) {
171
174
  return;
172
175
  }
173
-
174
176
  this.processing = true;
175
177
  let head: MutationEvent;
176
178
  const namespaceName = USER;
@@ -300,7 +302,8 @@ class MutationProcessor {
300
302
 
301
303
  // pauses itself
302
304
  this.pause();
303
- }, 'mutation resume loop'));
305
+ }, 'mutation resume loop');
306
+ }
304
307
  }
305
308
 
306
309
  private async jitteredRetry(
@@ -312,7 +315,7 @@ class MutationProcessor {
312
315
  modelConstructor: PersistentModelConstructor<PersistentModel>,
313
316
  MutationEvent: PersistentModelConstructor<MutationEvent>,
314
317
  mutationEvent: MutationEvent,
315
- authMode: GRAPHQL_AUTH_MODE,
318
+ authMode: GraphQLAuthModeKeys,
316
319
  onTerminate: Promise<void>
317
320
  ): Promise<
318
321
  [GraphQLResult<Record<string, PersistentModel>>, string, SchemaModel]
@@ -544,7 +547,7 @@ class MutationProcessor {
544
547
 
545
548
  // include all the fields that comprise a custom PK if one is specified
546
549
  const deleteInput = {};
547
- if (primaryKey?.length) {
550
+ if (primaryKey && primaryKey.length) {
548
551
  for (const pkField of primaryKey) {
549
552
  deleteInput[pkField] = parsedData[pkField];
550
553
  }
@@ -679,7 +682,10 @@ export const safeJitteredBackoff: typeof originalJitteredBackoff = (
679
682
  const attemptResult = originalJitteredBackoff(attempt);
680
683
 
681
684
  // If this is the last attempt and it is a network error, we retry indefinitively every 5 minutes
682
- if (attemptResult === false && error?.message === 'Network Error') {
685
+ if (
686
+ attemptResult === false &&
687
+ ((error || {}) as any).message === 'Network Error'
688
+ ) {
683
689
  return MAX_RETRY_DELAY_MS;
684
690
  }
685
691