@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
@@ -1,93 +1,9 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (_) try {
28
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
- var __rest = (this && this.__rest) || function (s, e) {
49
- var t = {};
50
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
51
- t[p] = s[p];
52
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
53
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
54
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
55
- t[p[i]] = s[p[i]];
56
- }
57
- return t;
58
- };
59
- var __read = (this && this.__read) || function (o, n) {
60
- var m = typeof Symbol === "function" && o[Symbol.iterator];
61
- if (!m) return o;
62
- var i = m.call(o), r, ar = [], e;
63
- try {
64
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
65
- }
66
- catch (error) { e = { error: error }; }
67
- finally {
68
- try {
69
- if (r && !r.done && (m = i["return"])) m.call(i);
70
- }
71
- finally { if (e) throw e.error; }
72
- }
73
- return ar;
74
- };
75
- var __values = (this && this.__values) || function(o) {
76
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
77
- if (m) return m.call(o);
78
- if (o && typeof o.length === "number") return {
79
- next: function () {
80
- if (o && i >= o.length) o = void 0;
81
- return { value: o && o[i++], done: !o };
82
- }
83
- };
84
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
85
- };
86
- import API from '@aws-amplify/api';
87
- import { ConsoleLogger as Logger, jitteredBackoff, NonRetryableError, retry, } from '@aws-amplify/core';
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, BackgroundProcessManager, } from '@aws-amplify/core';
88
4
  import Observable from 'zen-observable-ts';
89
5
  import { DISCARD, isModelFieldType, isTargetNameAssociation, OpType, ProcessName, } from '../../types';
90
- import { exhaustiveCheck, USER, USER_AGENT_SUFFIX_DATASTORE } from '../../util';
6
+ import { exhaustiveCheck, extractTargetNamesFromSrc, USER, USER_AGENT_SUFFIX_DATASTORE, ID, } from '../../util';
91
7
  import { buildGraphQLOperation, createMutationInstanceFromModelOperation, getModelAuthModes, TransformerMutationType, getTokenForCustomAuth, } from '../utils';
92
8
  import { getMutationErrorType } from './errorMaps';
93
9
  var MAX_ATTEMPTS = 10;
@@ -108,6 +24,7 @@ var MutationProcessor = /** @class */ (function () {
108
24
  this.amplifyContext = amplifyContext;
109
25
  this.typeQuery = new WeakMap();
110
26
  this.processing = false;
27
+ this.runningProcesses = new BackgroundProcessManager();
111
28
  this.amplifyContext.API = this.amplifyContext.API || API;
112
29
  this.generateQueries();
113
30
  }
@@ -138,164 +55,203 @@ var MutationProcessor = /** @class */ (function () {
138
55
  var _this = this;
139
56
  var observable = new Observable(function (observer) {
140
57
  _this.observer = observer;
141
- _this.resume();
142
- return function () {
143
- _this.pause();
144
- };
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
+ }); });
145
71
  });
146
72
  return observable;
147
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
+ };
148
89
  MutationProcessor.prototype.resume = function () {
149
90
  return __awaiter(this, void 0, void 0, function () {
150
- var head, namespaceName, _loop_1, this_1, _a;
151
91
  var _this = this;
152
- return __generator(this, function (_b) {
153
- switch (_b.label) {
154
- case 0:
155
- if (this.processing || !this.isReady()) {
156
- return [2 /*return*/];
157
- }
158
- this.processing = true;
159
- namespaceName = USER;
160
- _loop_1 = function () {
161
- var model, operation, data, condition, modelConstructor, result, opName, modelDefinition, modelAuthModes, operationAuthModes_1, authModeAttempts_1, authModeRetry_1, error_1, record, hasMore;
162
- var _a;
163
- return __generator(this, function (_b) {
164
- switch (_b.label) {
165
- case 0:
166
- model = head.model, operation = head.operation, data = head.data, condition = head.condition;
167
- modelConstructor = this_1.userClasses[model];
168
- result = void 0;
169
- opName = void 0;
170
- modelDefinition = void 0;
171
- _b.label = 1;
172
- case 1:
173
- _b.trys.push([1, 4, , 5]);
174
- return [4 /*yield*/, getModelAuthModes({
175
- authModeStrategy: this_1.authModeStrategy,
176
- defaultAuthMode: this_1.amplifyConfig.aws_appsync_authenticationType,
177
- modelName: model,
178
- schema: this_1.schema,
179
- })];
180
- case 2:
181
- modelAuthModes = _b.sent();
182
- operationAuthModes_1 = modelAuthModes[operation.toUpperCase()];
183
- authModeAttempts_1 = 0;
184
- authModeRetry_1 = function () { return __awaiter(_this, void 0, void 0, function () {
185
- var response, error_2;
186
- return __generator(this, function (_a) {
187
- switch (_a.label) {
188
- case 0:
189
- _a.trys.push([0, 2, , 4]);
190
- logger.debug("Attempting mutation with authMode: " + operationAuthModes_1[authModeAttempts_1]);
191
- return [4 /*yield*/, this.jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, this.MutationEvent, head, operationAuthModes_1[authModeAttempts_1])];
192
- case 1:
193
- response = _a.sent();
194
- logger.debug("Mutation sent successfully with authMode: " + operationAuthModes_1[authModeAttempts_1]);
195
- return [2 /*return*/, response];
196
- case 2:
197
- error_2 = _a.sent();
198
- authModeAttempts_1++;
199
- if (authModeAttempts_1 >= operationAuthModes_1.length) {
200
- logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1]);
201
- throw error_2;
202
- }
203
- logger.debug("Mutation failed with authMode: " + operationAuthModes_1[authModeAttempts_1 - 1] + ". Retrying with authMode: " + operationAuthModes_1[authModeAttempts_1]);
204
- return [4 /*yield*/, authModeRetry_1()];
205
- case 3: return [2 /*return*/, _a.sent()];
206
- case 4: return [2 /*return*/];
207
- }
208
- });
209
- }); };
210
- return [4 /*yield*/, authModeRetry_1()];
211
- case 3:
212
- _a = __read.apply(void 0, [_b.sent(), 3]), result = _a[0], opName = _a[1], modelDefinition = _a[2];
213
- return [3 /*break*/, 5];
214
- case 4:
215
- error_1 = _b.sent();
216
- if (error_1.message === 'Offline' || error_1.message === 'RetryMutation') {
217
- return [2 /*return*/, "continue"];
218
- }
219
- return [3 /*break*/, 5];
220
- case 5:
221
- if (!(result === undefined)) return [3 /*break*/, 7];
222
- logger.debug('done retrying');
223
- return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
224
- return __generator(this, function (_a) {
225
- switch (_a.label) {
226
- case 0: return [4 /*yield*/, this.outbox.dequeue(storage)];
227
- case 1:
228
- _a.sent();
229
- return [2 /*return*/];
230
- }
231
- });
232
- }); })];
233
- case 6:
234
- _b.sent();
235
- return [2 /*return*/, "continue"];
236
- case 7:
237
- record = result.data[opName];
238
- hasMore = false;
239
- return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
240
- return __generator(this, function (_a) {
241
- switch (_a.label) {
242
- case 0:
243
- // using runExclusive to prevent possible race condition
244
- // when another record gets enqueued between dequeue and peek
245
- 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;
246
120
  case 1:
247
- // using runExclusive to prevent possible race condition
248
- // when another record gets enqueued between dequeue and peek
249
- _a.sent();
250
- 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
+ })];
251
128
  case 2:
252
- 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
+ });
253
214
  return [2 /*return*/];
254
215
  }
255
216
  });
256
- }); })];
257
- case 8:
258
- _b.sent();
259
- this_1.observer.next({
260
- operation: operation,
261
- modelDefinition: modelDefinition,
262
- model: record,
263
- hasMore: hasMore,
264
- });
265
- return [2 /*return*/];
266
- }
267
- });
268
- };
269
- this_1 = this;
270
- _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'))];
271
241
  case 1:
272
- _a = this.processing;
273
- if (!_a) return [3 /*break*/, 3];
274
- return [4 /*yield*/, this.outbox.peek(this.storage)];
275
- case 2:
276
- _a = (head = _b.sent()) !== undefined;
277
- _b.label = 3;
278
- case 3:
279
- if (!_a) return [3 /*break*/, 5];
280
- return [5 /*yield**/, _loop_1()];
281
- case 4:
282
- _b.sent();
283
- return [3 /*break*/, 1];
284
- case 5:
285
- // pauses itself
286
- this.pause();
242
+ _a.sent();
287
243
  return [2 /*return*/];
288
244
  }
289
245
  });
290
246
  });
291
247
  };
292
- 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) {
293
249
  return __awaiter(this, void 0, void 0, function () {
294
250
  var _this = this;
295
251
  return __generator(this, function (_a) {
296
252
  switch (_a.label) {
297
253
  case 0: return [4 /*yield*/, retry(function (model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent) { return __awaiter(_this, void 0, void 0, function () {
298
- 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;
299
255
  var _g;
300
256
  return __generator(this, function (_h) {
301
257
  switch (_h.label) {
@@ -315,14 +271,16 @@ var MutationProcessor = /** @class */ (function () {
315
271
  opType = this.opTypeFromTransformerOperation(operation);
316
272
  _h.label = 2;
317
273
  case 2:
318
- _h.trys.push([2, 4, , 20]);
274
+ _h.trys.push([2, 4, , 17]);
319
275
  return [4 /*yield*/, this.amplifyContext.API.graphql(tryWith)];
320
276
  case 3:
321
277
  result = (_h.sent());
278
+ // `as any` because TypeScript doesn't seem to like passing tuples
279
+ // through generic params???
322
280
  return [2 /*return*/, [result, opName, modelDefinition]];
323
281
  case 4:
324
282
  err_1 = _h.sent();
325
- 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];
326
284
  _b = __read(err_1.errors, 1), error = _b[0];
327
285
  _c = error.originalError, _d = (_c === void 0 ? {} : _c).code, code = _d === void 0 ? null : _d;
328
286
  if (error.errorType === 'Unauthorized') {
@@ -359,7 +317,7 @@ var MutationProcessor = /** @class */ (function () {
359
317
  case 7:
360
318
  err_2 = _h.sent();
361
319
  logger.warn('conflict trycatch', err_2);
362
- return [3 /*break*/, 20];
320
+ return [3 /*break*/, 17];
363
321
  case 8:
364
322
  if (!(retryWith === DISCARD)) return [3 /*break*/, 11];
365
323
  _e = __read(buildGraphQLOperation(this.schema.namespaces[namespaceName], modelDefinition, 'GET'), 1), _f = __read(_e[0], 3), opName_1 = _f[1], query_1 = _f[2];
@@ -375,6 +333,7 @@ var MutationProcessor = /** @class */ (function () {
375
333
  })];
376
334
  case 10:
377
335
  serverData = _h.sent();
336
+ // onTerminate cancel graphql()
378
337
  return [2 /*return*/, [serverData, opName_1, modelDefinition]];
379
338
  case 11:
380
339
  namespace = this.schema.namespaces[namespaceName];
@@ -384,8 +343,8 @@ var MutationProcessor = /** @class */ (function () {
384
343
  _h.sent();
385
344
  throw new NonRetryableError('RetryMutation');
386
345
  case 13:
387
- _h.trys.push([13, 15, 16, 17]);
388
- return [4 /*yield*/, this.errorHandler({
346
+ try {
347
+ this.errorHandler({
389
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',
390
349
  localModel: variables.input,
391
350
  message: error.message,
@@ -397,33 +356,32 @@ var MutationProcessor = /** @class */ (function () {
397
356
  remoteModel: error.data
398
357
  ? this.modelInstanceCreator(modelConstructor, error.data)
399
358
  : null,
400
- })];
401
- case 14:
402
- _h.sent();
403
- return [3 /*break*/, 17];
404
- case 15:
405
- err_3 = _h.sent();
406
- logger.warn('Mutation error handler failed with:', err_3);
407
- return [3 /*break*/, 17];
408
- case 16:
409
- // Return empty tuple, dequeues the mutation
410
- return [2 /*return*/, error.data
411
- ? [
412
- { data: (_g = {}, _g[opName] = error.data, _g) },
413
- opName,
414
- modelDefinition,
415
- ]
416
- : []];
417
- case 17: return [3 /*break*/, 19];
418
- 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:
419
377
  // Catch-all for client-side errors that don't come back in the `GraphQLError` format.
420
378
  // These errors should not be retried.
421
379
  throw new NonRetryableError(err_1);
422
- case 19: return [3 /*break*/, 20];
423
- case 20:
380
+ case 16: return [3 /*break*/, 17];
381
+ case 17:
424
382
  if (tryWith) return [3 /*break*/, 2];
425
- _h.label = 21;
426
- case 21: return [2 /*return*/];
383
+ _h.label = 18;
384
+ case 18: return [2 /*return*/];
427
385
  }
428
386
  });
429
387
  }); }, [
@@ -434,25 +392,25 @@ var MutationProcessor = /** @class */ (function () {
434
392
  modelConstructor,
435
393
  MutationEvent,
436
394
  mutationEvent,
437
- ], safeJitteredBackoff)];
395
+ ], safeJitteredBackoff, onTerminate)];
438
396
  case 1: return [2 /*return*/, _a.sent()];
439
397
  }
440
398
  });
441
399
  });
442
400
  };
443
401
  MutationProcessor.prototype.createQueryVariables = function (namespaceName, model, operation, data, condition) {
444
- var e_1, _a;
402
+ var e_1, _a, e_2, _b, e_3, _c;
445
403
  var modelDefinition = this.schema.namespaces[namespaceName].models[model];
446
404
  var primaryKey = this.schema.namespaces[namespaceName].keys[model].primaryKey;
447
405
  var queriesTuples = this.typeQuery.get(modelDefinition);
448
- var _b = __read(queriesTuples.find(function (_a) {
406
+ var _d = __read(queriesTuples.find(function (_a) {
449
407
  var _b = __read(_a, 1), transformerMutationType = _b[0];
450
408
  return transformerMutationType === operation;
451
- }), 3), opName = _b[1], query = _b[2];
452
- 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"]);
453
411
  // include all the fields that comprise a custom PK if one is specified
454
412
  var deleteInput = {};
455
- if (primaryKey && primaryKey.length) {
413
+ if (primaryKey === null || primaryKey === void 0 ? void 0 : primaryKey.length) {
456
414
  try {
457
415
  for (var primaryKey_1 = __values(primaryKey), primaryKey_1_1 = primaryKey_1.next(); !primaryKey_1_1.done; primaryKey_1_1 = primaryKey_1.next()) {
458
416
  var pkField = primaryKey_1_1.value;
@@ -468,48 +426,66 @@ var MutationProcessor = /** @class */ (function () {
468
426
  }
469
427
  }
470
428
  else {
471
- deleteInput['id'] = parsedData.id;
429
+ deleteInput[ID] = parsedData.id;
472
430
  }
473
- var filteredData = operation === TransformerMutationType.DELETE
474
- ? deleteInput // For DELETE mutations, only PK is sent
475
- : Object.values(modelDefinition.fields)
476
- .filter(function (_a) {
477
- var name = _a.name, type = _a.type, association = _a.association;
478
- // connections
479
- if (isModelFieldType(type)) {
480
- // BELONGS_TO
481
- if (isTargetNameAssociation(association) &&
482
- association.connectionType === 'BELONGS_TO') {
483
- 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;
484
467
  }
485
- // All other connections
486
- return false;
487
- }
488
- if (operation === TransformerMutationType.UPDATE) {
489
- // this limits the update mutation input to changed fields only
490
- 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];
491
477
  }
492
- // scalars and non-model types
493
- return true;
494
- })
495
- .map(function (_a) {
496
- var name = _a.name, type = _a.type, association = _a.association;
497
- var fieldName = name;
498
- var val = parsedData[name];
499
- if (isModelFieldType(type) &&
500
- isTargetNameAssociation(association)) {
501
- fieldName = association.targetName;
502
- 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);
503
483
  }
504
- return [fieldName, val];
505
- })
506
- .reduce(function (acc, _a) {
507
- var _b = __read(_a, 2), k = _b[0], v = _b[1];
508
- acc[k] = v;
509
- return acc;
510
- }, {});
484
+ finally { if (e_2) throw e_2.error; }
485
+ }
486
+ }
511
487
  // Build mutation variables input object
512
- var input = __assign(__assign({}, filteredData), { _version: _version });
488
+ var input = __assign(__assign({}, mutationInput), { _version: _version });
513
489
  var graphQLCondition = JSON.parse(condition);
514
490
  var variables = __assign({ input: input }, (operation === TransformerMutationType.CREATE
515
491
  ? {}