@aws-amplify/datastore 3.12.6-next.20 → 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 (144) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/lib/authModeStrategies/multiAuthStrategy.js +13 -2
  3. package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
  4. package/lib/datastore/datastore.js +648 -344
  5. package/lib/datastore/datastore.js.map +1 -1
  6. package/lib/predicates/index.js +12 -2
  7. package/lib/predicates/index.js.map +1 -1
  8. package/lib/storage/adapter/AsyncStorageAdapter.js +354 -203
  9. package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  10. package/lib/storage/adapter/AsyncStorageDatabase.js +65 -28
  11. package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  12. package/lib/storage/adapter/IndexedDBAdapter.js +444 -271
  13. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  14. package/lib/storage/storage.js +93 -28
  15. package/lib/storage/storage.js.map +1 -1
  16. package/lib/sync/datastoreConnectivity.js +9 -0
  17. package/lib/sync/datastoreConnectivity.js.map +1 -1
  18. package/lib/sync/index.js +522 -397
  19. package/lib/sync/index.js.map +1 -1
  20. package/lib/sync/merger.js +13 -6
  21. package/lib/sync/merger.js.map +1 -1
  22. package/lib/sync/outbox.js +77 -71
  23. package/lib/sync/outbox.js.map +1 -1
  24. package/lib/sync/processors/mutation.js +269 -209
  25. package/lib/sync/processors/mutation.js.map +1 -1
  26. package/lib/sync/processors/subscription.js +213 -178
  27. package/lib/sync/processors/subscription.js.map +1 -1
  28. package/lib/sync/processors/sync.js +126 -121
  29. package/lib/sync/processors/sync.js.map +1 -1
  30. package/lib/sync/utils.js +43 -8
  31. package/lib/sync/utils.js.map +1 -1
  32. package/lib/types.js +10 -1
  33. package/lib/types.js.map +1 -1
  34. package/lib/util.js +419 -166
  35. package/lib/util.js.map +1 -1
  36. package/lib-esm/authModeStrategies/multiAuthStrategy.d.ts +11 -0
  37. package/lib-esm/authModeStrategies/multiAuthStrategy.js +12 -1
  38. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
  39. package/lib-esm/datastore/datastore.d.ts +107 -17
  40. package/lib-esm/datastore/datastore.js +648 -344
  41. package/lib-esm/datastore/datastore.js.map +1 -1
  42. package/lib-esm/index.d.ts +3 -19
  43. package/lib-esm/predicates/index.d.ts +3 -2
  44. package/lib-esm/predicates/index.js +13 -3
  45. package/lib-esm/predicates/index.js.map +1 -1
  46. package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +4 -3
  47. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +355 -204
  48. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  49. package/lib-esm/storage/adapter/AsyncStorageDatabase.d.ts +14 -4
  50. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +66 -29
  51. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  52. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +26 -4
  53. package/lib-esm/storage/adapter/IndexedDBAdapter.js +445 -272
  54. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  55. package/lib-esm/storage/adapter/index.d.ts +1 -1
  56. package/lib-esm/storage/storage.d.ts +1 -1
  57. package/lib-esm/storage/storage.js +93 -28
  58. package/lib-esm/storage/storage.js.map +1 -1
  59. package/lib-esm/sync/datastoreConnectivity.d.ts +1 -0
  60. package/lib-esm/sync/datastoreConnectivity.js +10 -1
  61. package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
  62. package/lib-esm/sync/index.d.ts +31 -5
  63. package/lib-esm/sync/index.js +524 -399
  64. package/lib-esm/sync/index.js.map +1 -1
  65. package/lib-esm/sync/merger.d.ts +9 -3
  66. package/lib-esm/sync/merger.js +13 -6
  67. package/lib-esm/sync/merger.js.map +1 -1
  68. package/lib-esm/sync/outbox.d.ts +2 -2
  69. package/lib-esm/sync/outbox.js +78 -72
  70. package/lib-esm/sync/outbox.js.map +1 -1
  71. package/lib-esm/sync/processors/mutation.d.ts +2 -0
  72. package/lib-esm/sync/processors/mutation.js +270 -210
  73. package/lib-esm/sync/processors/mutation.js.map +1 -1
  74. package/lib-esm/sync/processors/subscription.d.ts +2 -0
  75. package/lib-esm/sync/processors/subscription.js +213 -178
  76. package/lib-esm/sync/processors/subscription.js.map +1 -1
  77. package/lib-esm/sync/processors/sync.d.ts +2 -1
  78. package/lib-esm/sync/processors/sync.js +126 -121
  79. package/lib-esm/sync/processors/sync.js.map +1 -1
  80. package/lib-esm/sync/utils.d.ts +3 -2
  81. package/lib-esm/sync/utils.js +45 -11
  82. package/lib-esm/sync/utils.js.map +1 -1
  83. package/lib-esm/types.d.ts +65 -26
  84. package/lib-esm/types.js +9 -2
  85. package/lib-esm/types.js.map +1 -1
  86. package/lib-esm/util.d.ts +67 -24
  87. package/lib-esm/util.js +419 -166
  88. package/lib-esm/util.js.map +1 -1
  89. package/package.json +13 -7
  90. package/src/authModeStrategies/multiAuthStrategy.ts +12 -1
  91. package/src/datastore/datastore.ts +798 -397
  92. package/src/predicates/index.ts +32 -10
  93. package/src/storage/adapter/AsyncStorageAdapter.ts +309 -93
  94. package/src/storage/adapter/AsyncStorageDatabase.ts +74 -26
  95. package/src/storage/adapter/IndexedDBAdapter.ts +358 -134
  96. package/src/storage/adapter/index.ts +1 -1
  97. package/src/storage/storage.ts +69 -22
  98. package/src/sync/datastoreConnectivity.ts +6 -0
  99. package/src/sync/index.ts +521 -412
  100. package/src/sync/merger.ts +20 -4
  101. package/src/sync/outbox.ts +22 -9
  102. package/src/sync/processors/mutation.ts +188 -150
  103. package/src/sync/processors/subscription.ts +289 -253
  104. package/src/sync/processors/sync.ts +151 -138
  105. package/src/sync/utils.ts +67 -12
  106. package/src/types.ts +182 -30
  107. package/src/util.ts +505 -176
  108. package/build.js +0 -5
  109. package/dist/aws-amplify-datastore.js +0 -83311
  110. package/dist/aws-amplify-datastore.js.map +0 -1
  111. package/dist/aws-amplify-datastore.min.js +0 -168
  112. package/dist/aws-amplify-datastore.min.js.map +0 -1
  113. package/index.js +0 -7
  114. package/lib/authModeStrategies/defaultAuthStrategy.d.ts +0 -2
  115. package/lib/authModeStrategies/index.d.ts +0 -2
  116. package/lib/authModeStrategies/multiAuthStrategy.d.ts +0 -2
  117. package/lib/datastore/datastore.d.ts +0 -66
  118. package/lib/index.d.ts +0 -31
  119. package/lib/predicates/index.d.ts +0 -15
  120. package/lib/predicates/sort.d.ts +0 -8
  121. package/lib/ssr/index.d.ts +0 -3
  122. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +0 -40
  123. package/lib/storage/adapter/AsyncStorageDatabase.d.ts +0 -29
  124. package/lib/storage/adapter/InMemoryStore.d.ts +0 -11
  125. package/lib/storage/adapter/InMemoryStore.native.d.ts +0 -1
  126. package/lib/storage/adapter/IndexedDBAdapter.d.ts +0 -37
  127. package/lib/storage/adapter/getDefaultAdapter/index.d.ts +0 -3
  128. package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +0 -3
  129. package/lib/storage/adapter/index.d.ts +0 -9
  130. package/lib/storage/storage.d.ts +0 -49
  131. package/lib/sync/datastoreConnectivity.d.ts +0 -15
  132. package/lib/sync/datastoreReachability/index.d.ts +0 -3
  133. package/lib/sync/datastoreReachability/index.native.d.ts +0 -3
  134. package/lib/sync/index.d.ts +0 -63
  135. package/lib/sync/merger.d.ts +0 -11
  136. package/lib/sync/outbox.d.ts +0 -27
  137. package/lib/sync/processors/errorMaps.d.ts +0 -17
  138. package/lib/sync/processors/mutation.d.ts +0 -56
  139. package/lib/sync/processors/subscription.d.ts +0 -31
  140. package/lib/sync/processors/sync.d.ts +0 -27
  141. package/lib/sync/utils.d.ts +0 -41
  142. package/lib/types.d.ts +0 -462
  143. package/lib/util.d.ts +0 -113
  144. package/webpack.config.dev.js +0 -6
@@ -1,9 +1,9 @@
1
1
  import { __assign, __awaiter, __generator, __read, __rest, __values } from "tslib";
2
- import API from '@aws-amplify/api';
3
- import { ConsoleLogger as Logger, jitteredBackoff, NonRetryableError, retry, } from '@aws-amplify/core';
2
+ import { API } from '@aws-amplify/api';
3
+ import { ConsoleLogger as Logger, jitteredBackoff, NonRetryableError, retry, BackgroundProcessManager, } from '@aws-amplify/core';
4
4
  import Observable from 'zen-observable-ts';
5
5
  import { DISCARD, isModelFieldType, isTargetNameAssociation, OpType, ProcessName, } from '../../types';
6
- import { exhaustiveCheck, USER, USER_AGENT_SUFFIX_DATASTORE } from '../../util';
6
+ import { exhaustiveCheck, extractTargetNamesFromSrc, USER, USER_AGENT_SUFFIX_DATASTORE, ID, } from '../../util';
7
7
  import { buildGraphQLOperation, createMutationInstanceFromModelOperation, getModelAuthModes, TransformerMutationType, getTokenForCustomAuth, } from '../utils';
8
8
  import { getMutationErrorType } from './errorMaps';
9
9
  var MAX_ATTEMPTS = 10;
@@ -24,6 +24,7 @@ var MutationProcessor = /** @class */ (function () {
24
24
  this.amplifyContext = amplifyContext;
25
25
  this.typeQuery = new WeakMap();
26
26
  this.processing = false;
27
+ this.runningProcesses = new BackgroundProcessManager();
27
28
  this.amplifyContext.API = this.amplifyContext.API || API;
28
29
  this.generateQueries();
29
30
  }
@@ -54,164 +55,203 @@ var MutationProcessor = /** @class */ (function () {
54
55
  var _this = this;
55
56
  var observable = new Observable(function (observer) {
56
57
  _this.observer = observer;
57
- _this.resume();
58
- return function () {
59
- _this.pause();
60
- };
58
+ try {
59
+ _this.resume();
60
+ }
61
+ catch (error) {
62
+ logger.error('mutations processor start error', error);
63
+ throw error;
64
+ }
65
+ return _this.runningProcesses.addCleaner(function () { return __awaiter(_this, void 0, void 0, function () {
66
+ return __generator(this, function (_a) {
67
+ this.pause();
68
+ return [2 /*return*/];
69
+ });
70
+ }); });
61
71
  });
62
72
  return observable;
63
73
  };
74
+ MutationProcessor.prototype.stop = function () {
75
+ return __awaiter(this, void 0, void 0, function () {
76
+ return __generator(this, function (_a) {
77
+ switch (_a.label) {
78
+ case 0: return [4 /*yield*/, this.runningProcesses.close()];
79
+ case 1:
80
+ _a.sent();
81
+ return [4 /*yield*/, this.runningProcesses.open()];
82
+ case 2:
83
+ _a.sent();
84
+ return [2 /*return*/];
85
+ }
86
+ });
87
+ });
88
+ };
64
89
  MutationProcessor.prototype.resume = function () {
65
90
  return __awaiter(this, void 0, void 0, function () {
66
- var head, namespaceName, _loop_1, this_1, _a;
67
91
  var _this = this;
68
- return __generator(this, function (_b) {
69
- switch (_b.label) {
70
- case 0:
71
- if (this.processing || !this.isReady()) {
72
- return [2 /*return*/];
73
- }
74
- this.processing = true;
75
- namespaceName = USER;
76
- _loop_1 = function () {
77
- var model, operation, data, condition, modelConstructor, result, opName, modelDefinition, modelAuthModes, operationAuthModes_1, authModeAttempts_1, authModeRetry_1, error_1, record, hasMore;
78
- var _a;
79
- return __generator(this, function (_b) {
80
- switch (_b.label) {
81
- case 0:
82
- model = head.model, operation = head.operation, data = head.data, condition = head.condition;
83
- modelConstructor = this_1.userClasses[model];
84
- result = void 0;
85
- opName = void 0;
86
- modelDefinition = void 0;
87
- _b.label = 1;
88
- case 1:
89
- _b.trys.push([1, 4, , 5]);
90
- return [4 /*yield*/, getModelAuthModes({
91
- authModeStrategy: this_1.authModeStrategy,
92
- defaultAuthMode: this_1.amplifyConfig.aws_appsync_authenticationType,
93
- modelName: model,
94
- schema: this_1.schema,
95
- })];
96
- case 2:
97
- modelAuthModes = _b.sent();
98
- operationAuthModes_1 = modelAuthModes[operation.toUpperCase()];
99
- authModeAttempts_1 = 0;
100
- authModeRetry_1 = function () { return __awaiter(_this, void 0, void 0, function () {
101
- var response, error_2;
102
- return __generator(this, function (_a) {
103
- switch (_a.label) {
104
- case 0:
105
- _a.trys.push([0, 2, , 4]);
106
- logger.debug("Attempting mutation with authMode: " + operationAuthModes_1[authModeAttempts_1]);
107
- return [4 /*yield*/, this.jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, this.MutationEvent, head, operationAuthModes_1[authModeAttempts_1])];
108
- case 1:
109
- response = _a.sent();
110
- logger.debug("Mutation sent successfully with authMode: " + operationAuthModes_1[authModeAttempts_1]);
111
- return [2 /*return*/, response];
112
- case 2:
113
- error_2 = _a.sent();
114
- authModeAttempts_1++;
115
- if (authModeAttempts_1 >= operationAuthModes_1.length) {
116
- logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1]);
117
- throw error_2;
118
- }
119
- logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1] + ". Retrying with authMode: " + operationAuthModes_1[authModeAttempts_1]);
120
- return [4 /*yield*/, authModeRetry_1()];
121
- case 3: return [2 /*return*/, _a.sent()];
122
- case 4: return [2 /*return*/];
123
- }
124
- });
125
- }); };
126
- return [4 /*yield*/, authModeRetry_1()];
127
- case 3:
128
- _a = __read.apply(void 0, [_b.sent(), 3]), result = _a[0], opName = _a[1], modelDefinition = _a[2];
129
- return [3 /*break*/, 5];
130
- case 4:
131
- error_1 = _b.sent();
132
- if (error_1.message === 'Offline' || error_1.message === 'RetryMutation') {
133
- return [2 /*return*/, "continue"];
134
- }
135
- return [3 /*break*/, 5];
136
- case 5:
137
- if (!(result === undefined)) return [3 /*break*/, 7];
138
- logger.debug('done retrying');
139
- return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
140
- return __generator(this, function (_a) {
141
- switch (_a.label) {
142
- case 0: return [4 /*yield*/, this.outbox.dequeue(storage)];
143
- case 1:
144
- _a.sent();
145
- return [2 /*return*/];
146
- }
147
- });
148
- }); })];
149
- case 6:
150
- _b.sent();
151
- return [2 /*return*/, "continue"];
152
- case 7:
153
- record = result.data[opName];
154
- hasMore = false;
155
- return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
156
- return __generator(this, function (_a) {
157
- switch (_a.label) {
158
- case 0:
159
- // using runExclusive to prevent possible race condition
160
- // when another record gets enqueued between dequeue and peek
161
- return [4 /*yield*/, this.outbox.dequeue(storage, record, operation)];
92
+ return __generator(this, function (_a) {
93
+ switch (_a.label) {
94
+ case 0: return [4 /*yield*/, (this.runningProcesses.isOpen &&
95
+ this.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
96
+ var head, namespaceName, _loop_1, this_1, _a;
97
+ var _this = this;
98
+ return __generator(this, function (_b) {
99
+ switch (_b.label) {
100
+ case 0:
101
+ if (this.processing ||
102
+ !this.isReady() ||
103
+ !this.runningProcesses.isOpen) {
104
+ return [2 /*return*/];
105
+ }
106
+ this.processing = true;
107
+ namespaceName = USER;
108
+ _loop_1 = function () {
109
+ var model, operation, data, condition, modelConstructor, result, opName, modelDefinition, modelAuthModes, operationAuthModes_1, authModeAttempts_1, authModeRetry_1, error_1, record, hasMore;
110
+ var _a;
111
+ return __generator(this, function (_b) {
112
+ switch (_b.label) {
113
+ case 0:
114
+ model = head.model, operation = head.operation, data = head.data, condition = head.condition;
115
+ modelConstructor = this_1.userClasses[model];
116
+ result = void 0;
117
+ opName = void 0;
118
+ modelDefinition = void 0;
119
+ _b.label = 1;
162
120
  case 1:
163
- // using runExclusive to prevent possible race condition
164
- // when another record gets enqueued between dequeue and peek
165
- _a.sent();
166
- return [4 /*yield*/, this.outbox.peek(storage)];
121
+ _b.trys.push([1, 4, , 5]);
122
+ return [4 /*yield*/, getModelAuthModes({
123
+ authModeStrategy: this_1.authModeStrategy,
124
+ defaultAuthMode: this_1.amplifyConfig.aws_appsync_authenticationType,
125
+ modelName: model,
126
+ schema: this_1.schema,
127
+ })];
167
128
  case 2:
168
- hasMore = (_a.sent()) !== undefined;
129
+ modelAuthModes = _b.sent();
130
+ operationAuthModes_1 = modelAuthModes[operation.toUpperCase()];
131
+ authModeAttempts_1 = 0;
132
+ authModeRetry_1 = function () { return __awaiter(_this, void 0, void 0, function () {
133
+ var response, error_2;
134
+ return __generator(this, function (_a) {
135
+ switch (_a.label) {
136
+ case 0:
137
+ _a.trys.push([0, 2, , 4]);
138
+ logger.debug("Attempting mutation with authMode: " + operationAuthModes_1[authModeAttempts_1]);
139
+ return [4 /*yield*/, this.jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, this.MutationEvent, head, operationAuthModes_1[authModeAttempts_1], onTerminate)];
140
+ case 1:
141
+ response = _a.sent();
142
+ logger.debug("Mutation sent successfully with authMode: " + operationAuthModes_1[authModeAttempts_1]);
143
+ return [2 /*return*/, response];
144
+ case 2:
145
+ error_2 = _a.sent();
146
+ authModeAttempts_1++;
147
+ if (authModeAttempts_1 >= operationAuthModes_1.length) {
148
+ logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1]);
149
+ throw error_2;
150
+ }
151
+ logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1] + ". Retrying with authMode: " + operationAuthModes_1[authModeAttempts_1]);
152
+ return [4 /*yield*/, authModeRetry_1()];
153
+ case 3: return [2 /*return*/, _a.sent()];
154
+ case 4: return [2 /*return*/];
155
+ }
156
+ });
157
+ }); };
158
+ return [4 /*yield*/, authModeRetry_1()];
159
+ case 3:
160
+ _a = __read.apply(void 0, [_b.sent(), 3]), result = _a[0], opName = _a[1], modelDefinition = _a[2];
161
+ return [3 /*break*/, 5];
162
+ case 4:
163
+ error_1 = _b.sent();
164
+ if (error_1.message === 'Offline' ||
165
+ error_1.message === 'RetryMutation') {
166
+ return [2 /*return*/, "continue"];
167
+ }
168
+ return [3 /*break*/, 5];
169
+ case 5:
170
+ if (!(result === undefined)) return [3 /*break*/, 7];
171
+ logger.debug('done retrying');
172
+ return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
173
+ return __generator(this, function (_a) {
174
+ switch (_a.label) {
175
+ case 0: return [4 /*yield*/, this.outbox.dequeue(storage)];
176
+ case 1:
177
+ _a.sent();
178
+ return [2 /*return*/];
179
+ }
180
+ });
181
+ }); })];
182
+ case 6:
183
+ _b.sent();
184
+ return [2 /*return*/, "continue"];
185
+ case 7:
186
+ record = result.data[opName];
187
+ hasMore = false;
188
+ return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
189
+ return __generator(this, function (_a) {
190
+ switch (_a.label) {
191
+ case 0:
192
+ // using runExclusive to prevent possible race condition
193
+ // when another record gets enqueued between dequeue and peek
194
+ return [4 /*yield*/, this.outbox.dequeue(storage, record, operation)];
195
+ case 1:
196
+ // using runExclusive to prevent possible race condition
197
+ // when another record gets enqueued between dequeue and peek
198
+ _a.sent();
199
+ return [4 /*yield*/, this.outbox.peek(storage)];
200
+ case 2:
201
+ hasMore = (_a.sent()) !== undefined;
202
+ return [2 /*return*/];
203
+ }
204
+ });
205
+ }); })];
206
+ case 8:
207
+ _b.sent();
208
+ this_1.observer.next({
209
+ operation: operation,
210
+ modelDefinition: modelDefinition,
211
+ model: record,
212
+ hasMore: hasMore,
213
+ });
169
214
  return [2 /*return*/];
170
215
  }
171
216
  });
172
- }); })];
173
- case 8:
174
- _b.sent();
175
- this_1.observer.next({
176
- operation: operation,
177
- modelDefinition: modelDefinition,
178
- model: record,
179
- hasMore: hasMore,
180
- });
181
- return [2 /*return*/];
182
- }
183
- });
184
- };
185
- this_1 = this;
186
- _b.label = 1;
217
+ };
218
+ this_1 = this;
219
+ _b.label = 1;
220
+ case 1:
221
+ _a = this.processing &&
222
+ this.runningProcesses.isOpen;
223
+ if (!_a) return [3 /*break*/, 3];
224
+ return [4 /*yield*/, this.outbox.peek(this.storage)];
225
+ case 2:
226
+ _a = (head = _b.sent()) !== undefined;
227
+ _b.label = 3;
228
+ case 3:
229
+ if (!_a) return [3 /*break*/, 5];
230
+ return [5 /*yield**/, _loop_1()];
231
+ case 4:
232
+ _b.sent();
233
+ return [3 /*break*/, 1];
234
+ case 5:
235
+ // pauses itself
236
+ this.pause();
237
+ return [2 /*return*/];
238
+ }
239
+ });
240
+ }); }, 'mutation resume loop'))];
187
241
  case 1:
188
- _a = this.processing;
189
- if (!_a) return [3 /*break*/, 3];
190
- return [4 /*yield*/, this.outbox.peek(this.storage)];
191
- case 2:
192
- _a = (head = _b.sent()) !== undefined;
193
- _b.label = 3;
194
- case 3:
195
- if (!_a) return [3 /*break*/, 5];
196
- return [5 /*yield**/, _loop_1()];
197
- case 4:
198
- _b.sent();
199
- return [3 /*break*/, 1];
200
- case 5:
201
- // pauses itself
202
- this.pause();
242
+ _a.sent();
203
243
  return [2 /*return*/];
204
244
  }
205
245
  });
206
246
  });
207
247
  };
208
- MutationProcessor.prototype.jitteredRetry = function (namespaceName, model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent, authMode) {
248
+ MutationProcessor.prototype.jitteredRetry = function (namespaceName, model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent, authMode, onTerminate) {
209
249
  return __awaiter(this, void 0, void 0, function () {
210
250
  var _this = this;
211
251
  return __generator(this, function (_a) {
212
252
  switch (_a.label) {
213
253
  case 0: return [4 /*yield*/, retry(function (model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent) { return __awaiter(_this, void 0, void 0, function () {
214
- var _a, query, variables, graphQLCondition, opName, modelDefinition, authToken, tryWith, attempt, opType, result, err_1, _b, error, _c, _d, code, retryWith, err_2, _e, _f, opName_1, query_1, authToken_1, serverData, namespace, updatedMutation, err_3;
254
+ var _a, query, variables, graphQLCondition, opName, modelDefinition, authToken, tryWith, attempt, opType, result, err_1, _b, error, _c, _d, code, retryWith, err_2, _e, _f, opName_1, query_1, authToken_1, serverData, namespace, updatedMutation;
215
255
  var _g;
216
256
  return __generator(this, function (_h) {
217
257
  switch (_h.label) {
@@ -231,14 +271,16 @@ var MutationProcessor = /** @class */ (function () {
231
271
  opType = this.opTypeFromTransformerOperation(operation);
232
272
  _h.label = 2;
233
273
  case 2:
234
- _h.trys.push([2, 4, , 20]);
274
+ _h.trys.push([2, 4, , 17]);
235
275
  return [4 /*yield*/, this.amplifyContext.API.graphql(tryWith)];
236
276
  case 3:
237
277
  result = (_h.sent());
278
+ // `as any` because TypeScript doesn't seem to like passing tuples
279
+ // through generic params???
238
280
  return [2 /*return*/, [result, opName, modelDefinition]];
239
281
  case 4:
240
282
  err_1 = _h.sent();
241
- if (!(err_1.errors && err_1.errors.length > 0)) return [3 /*break*/, 18];
283
+ if (!(err_1.errors && err_1.errors.length > 0)) return [3 /*break*/, 15];
242
284
  _b = __read(err_1.errors, 1), error = _b[0];
243
285
  _c = error.originalError, _d = (_c === void 0 ? {} : _c).code, code = _d === void 0 ? null : _d;
244
286
  if (error.errorType === 'Unauthorized') {
@@ -275,7 +317,7 @@ var MutationProcessor = /** @class */ (function () {
275
317
  case 7:
276
318
  err_2 = _h.sent();
277
319
  logger.warn('conflict trycatch', err_2);
278
- return [3 /*break*/, 20];
320
+ return [3 /*break*/, 17];
279
321
  case 8:
280
322
  if (!(retryWith === DISCARD)) return [3 /*break*/, 11];
281
323
  _e = __read(buildGraphQLOperation(this.schema.namespaces[namespaceName], modelDefinition, 'GET'), 1), _f = __read(_e[0], 3), opName_1 = _f[1], query_1 = _f[2];
@@ -291,6 +333,7 @@ var MutationProcessor = /** @class */ (function () {
291
333
  })];
292
334
  case 10:
293
335
  serverData = _h.sent();
336
+ // onTerminate cancel graphql()
294
337
  return [2 /*return*/, [serverData, opName_1, modelDefinition]];
295
338
  case 11:
296
339
  namespace = this.schema.namespaces[namespaceName];
@@ -300,8 +343,8 @@ var MutationProcessor = /** @class */ (function () {
300
343
  _h.sent();
301
344
  throw new NonRetryableError('RetryMutation');
302
345
  case 13:
303
- _h.trys.push([13, 15, 16, 17]);
304
- return [4 /*yield*/, this.errorHandler({
346
+ try {
347
+ this.errorHandler({
305
348
  recoverySuggestion: 'Ensure app code is up to date, auth directives exist and are correct on each model, and that server-side data has not been invalidated by a schema change. If the problem persists, search for or create an issue: https://github.com/aws-amplify/amplify-js/issues',
306
349
  localModel: variables.input,
307
350
  message: error.message,
@@ -313,33 +356,32 @@ var MutationProcessor = /** @class */ (function () {
313
356
  remoteModel: error.data
314
357
  ? this.modelInstanceCreator(modelConstructor, error.data)
315
358
  : null,
316
- })];
317
- case 14:
318
- _h.sent();
319
- return [3 /*break*/, 17];
320
- case 15:
321
- err_3 = _h.sent();
322
- logger.warn('Mutation error handler failed with:', err_3);
323
- return [3 /*break*/, 17];
324
- case 16:
325
- // Return empty tuple, dequeues the mutation
326
- return [2 /*return*/, error.data
327
- ? [
328
- { data: (_g = {}, _g[opName] = error.data, _g) },
329
- opName,
330
- modelDefinition,
331
- ]
332
- : []];
333
- case 17: return [3 /*break*/, 19];
334
- case 18:
359
+ });
360
+ }
361
+ catch (err) {
362
+ logger.warn('Mutation error handler failed with:', err);
363
+ }
364
+ finally {
365
+ // Return empty tuple, dequeues the mutation
366
+ return [2 /*return*/, error.data
367
+ ? [
368
+ { data: (_g = {}, _g[opName] = error.data, _g) },
369
+ opName,
370
+ modelDefinition,
371
+ ]
372
+ : []];
373
+ }
374
+ _h.label = 14;
375
+ case 14: return [3 /*break*/, 16];
376
+ case 15:
335
377
  // Catch-all for client-side errors that don't come back in the `GraphQLError` format.
336
378
  // These errors should not be retried.
337
379
  throw new NonRetryableError(err_1);
338
- case 19: return [3 /*break*/, 20];
339
- case 20:
380
+ case 16: return [3 /*break*/, 17];
381
+ case 17:
340
382
  if (tryWith) return [3 /*break*/, 2];
341
- _h.label = 21;
342
- case 21: return [2 /*return*/];
383
+ _h.label = 18;
384
+ case 18: return [2 /*return*/];
343
385
  }
344
386
  });
345
387
  }); }, [
@@ -350,25 +392,25 @@ var MutationProcessor = /** @class */ (function () {
350
392
  modelConstructor,
351
393
  MutationEvent,
352
394
  mutationEvent,
353
- ], safeJitteredBackoff)];
395
+ ], safeJitteredBackoff, onTerminate)];
354
396
  case 1: return [2 /*return*/, _a.sent()];
355
397
  }
356
398
  });
357
399
  });
358
400
  };
359
401
  MutationProcessor.prototype.createQueryVariables = function (namespaceName, model, operation, data, condition) {
360
- var e_1, _a;
402
+ var e_1, _a, e_2, _b, e_3, _c;
361
403
  var modelDefinition = this.schema.namespaces[namespaceName].models[model];
362
404
  var primaryKey = this.schema.namespaces[namespaceName].keys[model].primaryKey;
363
405
  var queriesTuples = this.typeQuery.get(modelDefinition);
364
- var _b = __read(queriesTuples.find(function (_a) {
406
+ var _d = __read(queriesTuples.find(function (_a) {
365
407
  var _b = __read(_a, 1), transformerMutationType = _b[0];
366
408
  return transformerMutationType === operation;
367
- }), 3), opName = _b[1], query = _b[2];
368
- var _c = JSON.parse(data), _version = _c._version, parsedData = __rest(_c, ["_version"]);
409
+ }), 3), opName = _d[1], query = _d[2];
410
+ var _e = JSON.parse(data), _version = _e._version, parsedData = __rest(_e, ["_version"]);
369
411
  // include all the fields that comprise a custom PK if one is specified
370
412
  var deleteInput = {};
371
- if (primaryKey && primaryKey.length) {
413
+ if (primaryKey === null || primaryKey === void 0 ? void 0 : primaryKey.length) {
372
414
  try {
373
415
  for (var primaryKey_1 = __values(primaryKey), primaryKey_1_1 = primaryKey_1.next(); !primaryKey_1_1.done; primaryKey_1_1 = primaryKey_1.next()) {
374
416
  var pkField = primaryKey_1_1.value;
@@ -384,48 +426,66 @@ var MutationProcessor = /** @class */ (function () {
384
426
  }
385
427
  }
386
428
  else {
387
- deleteInput['id'] = parsedData.id;
429
+ deleteInput[ID] = parsedData.id;
388
430
  }
389
- var filteredData = operation === TransformerMutationType.DELETE
390
- ? deleteInput // For DELETE mutations, only PK is sent
391
- : Object.values(modelDefinition.fields)
392
- .filter(function (_a) {
393
- var name = _a.name, type = _a.type, association = _a.association;
394
- // connections
395
- if (isModelFieldType(type)) {
396
- // BELONGS_TO
397
- if (isTargetNameAssociation(association) &&
398
- association.connectionType === 'BELONGS_TO') {
399
- return true;
431
+ var mutationInput;
432
+ if (operation === TransformerMutationType.DELETE) {
433
+ // For DELETE mutations, only the key(s) are included in the input
434
+ mutationInput = deleteInput;
435
+ }
436
+ else {
437
+ // Otherwise, we construct the mutation input with the following logic
438
+ mutationInput = {};
439
+ var modelFields = Object.values(modelDefinition.fields);
440
+ try {
441
+ for (var modelFields_1 = __values(modelFields), modelFields_1_1 = modelFields_1.next(); !modelFields_1_1.done; modelFields_1_1 = modelFields_1.next()) {
442
+ var _f = modelFields_1_1.value, name_1 = _f.name, type = _f.type, association = _f.association;
443
+ // model fields should be stripped out from the input
444
+ if (isModelFieldType(type)) {
445
+ // except for belongs to relations - we need to replace them with the correct foreign key(s)
446
+ if (isTargetNameAssociation(association) &&
447
+ association.connectionType === 'BELONGS_TO') {
448
+ var targetNames = extractTargetNamesFromSrc(association);
449
+ if (targetNames) {
450
+ try {
451
+ // instead of including the connected model itself, we add its key(s) to the mutation input
452
+ for (var targetNames_1 = (e_3 = void 0, __values(targetNames)), targetNames_1_1 = targetNames_1.next(); !targetNames_1_1.done; targetNames_1_1 = targetNames_1.next()) {
453
+ var targetName = targetNames_1_1.value;
454
+ mutationInput[targetName] = parsedData[targetName];
455
+ }
456
+ }
457
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
458
+ finally {
459
+ try {
460
+ if (targetNames_1_1 && !targetNames_1_1.done && (_c = targetNames_1.return)) _c.call(targetNames_1);
461
+ }
462
+ finally { if (e_3) throw e_3.error; }
463
+ }
464
+ }
465
+ }
466
+ continue;
400
467
  }
401
- // All other connections
402
- return false;
403
- }
404
- if (operation === TransformerMutationType.UPDATE) {
405
- // this limits the update mutation input to changed fields only
406
- return parsedData.hasOwnProperty(name);
468
+ // scalar fields / non-model types
469
+ if (operation === TransformerMutationType.UPDATE) {
470
+ if (!parsedData.hasOwnProperty(name_1)) {
471
+ // for update mutations - strip out a field if it's unchanged
472
+ continue;
473
+ }
474
+ }
475
+ // all other fields are added to the input object
476
+ mutationInput[name_1] = parsedData[name_1];
407
477
  }
408
- // scalars and non-model types
409
- return true;
410
- })
411
- .map(function (_a) {
412
- var name = _a.name, type = _a.type, association = _a.association;
413
- var fieldName = name;
414
- var val = parsedData[name];
415
- if (isModelFieldType(type) &&
416
- isTargetNameAssociation(association)) {
417
- fieldName = association.targetName;
418
- val = parsedData[fieldName];
478
+ }
479
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
480
+ finally {
481
+ try {
482
+ if (modelFields_1_1 && !modelFields_1_1.done && (_b = modelFields_1.return)) _b.call(modelFields_1);
419
483
  }
420
- return [fieldName, val];
421
- })
422
- .reduce(function (acc, _a) {
423
- var _b = __read(_a, 2), k = _b[0], v = _b[1];
424
- acc[k] = v;
425
- return acc;
426
- }, {});
484
+ finally { if (e_2) throw e_2.error; }
485
+ }
486
+ }
427
487
  // Build mutation variables input object
428
- var input = __assign(__assign({}, filteredData), { _version: _version });
488
+ var input = __assign(__assign({}, mutationInput), { _version: _version });
429
489
  var graphQLCondition = JSON.parse(condition);
430
490
  var variables = __assign({ input: input }, (operation === TransformerMutationType.CREATE
431
491
  ? {}