@aws-amplify/datastore 5.0.1-api-v6-models.c1977f8.0 → 5.0.1-api-v6-models.891fe0d.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.
- package/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
- package/lib/datastore/datastore.js +887 -1370
- package/lib/index.js +1 -1
- package/lib/predicates/index.js +53 -102
- package/lib/predicates/next.js +310 -557
- package/lib/predicates/sort.js +24 -27
- package/lib/ssr/index.js +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
- package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
- package/lib/storage/adapter/InMemoryStore.js +27 -101
- package/lib/storage/adapter/InMemoryStore.native.js +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
- package/lib/storage/adapter/StorageAdapterBase.js +177 -396
- package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
- package/lib/storage/relationship.js +174 -260
- package/lib/storage/storage.js +244 -507
- package/lib/sync/datastoreConnectivity.js +29 -84
- package/lib/sync/datastoreReachability/index.js +1 -1
- package/lib/sync/datastoreReachability/index.native.js +2 -2
- package/lib/sync/index.js +512 -885
- package/lib/sync/merger.js +30 -133
- package/lib/sync/outbox.js +147 -302
- package/lib/sync/processors/errorMaps.js +30 -80
- package/lib/sync/processors/mutation.js +331 -579
- package/lib/sync/processors/subscription.js +268 -428
- package/lib/sync/processors/sync.js +276 -464
- package/lib/sync/utils.js +248 -393
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.js +16 -58
- package/lib/util.js +335 -575
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
- package/lib-esm/datastore/datastore.js +874 -1359
- package/lib-esm/index.js +6 -6
- package/lib-esm/predicates/index.js +54 -104
- package/lib-esm/predicates/next.js +306 -555
- package/lib-esm/predicates/sort.js +24 -27
- package/lib-esm/ssr/index.js +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
- package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
- package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
- package/lib-esm/storage/relationship.js +173 -260
- package/lib-esm/storage/storage.js +236 -499
- package/lib-esm/sync/datastoreConnectivity.js +26 -82
- package/lib-esm/sync/datastoreReachability/index.js +1 -1
- package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
- package/lib-esm/sync/index.js +498 -872
- package/lib-esm/sync/merger.js +28 -131
- package/lib-esm/sync/outbox.js +143 -298
- package/lib-esm/sync/processors/errorMaps.js +32 -82
- package/lib-esm/sync/processors/mutation.js +324 -572
- package/lib-esm/sync/processors/subscription.js +258 -418
- package/lib-esm/sync/processors/sync.js +269 -457
- package/lib-esm/sync/utils.js +245 -390
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types.js +16 -59
- package/lib-esm/util.js +335 -577
- package/package.json +12 -12
- package/src/datastore/datastore.ts +4 -3
- package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
- package/src/sync/processors/mutation.ts +1 -1
- package/src/sync/processors/sync.ts +1 -1
- package/src/sync/utils.ts +1 -1
- package/src/util.ts +44 -6
|
@@ -1,104 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
-
if (!m) return o;
|
|
63
|
-
var i = m.call(o), r, ar = [], e;
|
|
64
|
-
try {
|
|
65
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
-
}
|
|
67
|
-
catch (error) { e = { error: error }; }
|
|
68
|
-
finally {
|
|
69
|
-
try {
|
|
70
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
-
}
|
|
72
|
-
finally { if (e) throw e.error; }
|
|
73
|
-
}
|
|
74
|
-
return ar;
|
|
75
|
-
};
|
|
76
|
-
var __values = (this && this.__values) || function(o) {
|
|
77
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
78
|
-
if (m) return m.call(o);
|
|
79
|
-
if (o && typeof o.length === "number") return {
|
|
80
|
-
next: function () {
|
|
81
|
-
if (o && i >= o.length) o = void 0;
|
|
82
|
-
return { value: o && o[i++], done: !o };
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
86
|
-
};
|
|
87
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
88
3
|
exports.MutationProcessor = exports.safeJitteredBackoff = void 0;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (amplifyConfig === void 0) { amplifyConfig = {}; }
|
|
4
|
+
const internals_1 = require("@aws-amplify/api/internals");
|
|
5
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
6
|
+
const rxjs_1 = require("rxjs");
|
|
7
|
+
const types_1 = require("../../types");
|
|
8
|
+
const util_1 = require("../../util");
|
|
9
|
+
const utils_2 = require("../utils");
|
|
10
|
+
const errorMaps_1 = require("./errorMaps");
|
|
11
|
+
const core_1 = require("@aws-amplify/core");
|
|
12
|
+
const MAX_ATTEMPTS = 10;
|
|
13
|
+
const logger = new core_1.ConsoleLogger('DataStore');
|
|
14
|
+
class MutationProcessor {
|
|
15
|
+
constructor(schema, storage, userClasses, outbox, modelInstanceCreator, MutationEvent, amplifyConfig = {}, authModeStrategy, errorHandler, conflictHandler, amplifyContext) {
|
|
102
16
|
this.schema = schema;
|
|
103
17
|
this.storage = storage;
|
|
104
18
|
this.userClasses = userClasses;
|
|
@@ -117,446 +31,299 @@ var MutationProcessor = /** @class */ (function () {
|
|
|
117
31
|
this.amplifyContext.InternalAPI || internals_1.InternalAPI;
|
|
118
32
|
this.generateQueries();
|
|
119
33
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
Object.values(this.schema.namespaces).forEach(function (namespace) {
|
|
34
|
+
generateQueries() {
|
|
35
|
+
Object.values(this.schema.namespaces).forEach(namespace => {
|
|
123
36
|
Object.values(namespace.models)
|
|
124
|
-
.filter(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
.
|
|
129
|
-
|
|
130
|
-
var _b = __read((0, utils_2.buildGraphQLOperation)(namespace, model, 'UPDATE'), 1), updateMutation = _b[0];
|
|
131
|
-
var _c = __read((0, utils_2.buildGraphQLOperation)(namespace, model, 'DELETE'), 1), deleteMutation = _c[0];
|
|
132
|
-
_this.typeQuery.set(model, [
|
|
37
|
+
.filter(({ syncable }) => syncable)
|
|
38
|
+
.forEach(model => {
|
|
39
|
+
const [createMutation] = (0, utils_2.buildGraphQLOperation)(namespace, model, 'CREATE');
|
|
40
|
+
const [updateMutation] = (0, utils_2.buildGraphQLOperation)(namespace, model, 'UPDATE');
|
|
41
|
+
const [deleteMutation] = (0, utils_2.buildGraphQLOperation)(namespace, model, 'DELETE');
|
|
42
|
+
this.typeQuery.set(model, [
|
|
133
43
|
createMutation,
|
|
134
44
|
updateMutation,
|
|
135
45
|
deleteMutation,
|
|
136
46
|
]);
|
|
137
47
|
});
|
|
138
48
|
});
|
|
139
|
-
}
|
|
140
|
-
|
|
49
|
+
}
|
|
50
|
+
isReady() {
|
|
141
51
|
return this.observer !== undefined;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
var _this = this;
|
|
52
|
+
}
|
|
53
|
+
start() {
|
|
145
54
|
this.runningProcesses = new utils_1.BackgroundProcessManager();
|
|
146
|
-
|
|
147
|
-
|
|
55
|
+
const observable = new rxjs_1.Observable(observer => {
|
|
56
|
+
this.observer = observer;
|
|
148
57
|
try {
|
|
149
|
-
|
|
58
|
+
this.resume();
|
|
150
59
|
}
|
|
151
60
|
catch (error) {
|
|
152
61
|
logger.error('mutations processor start error', error);
|
|
153
62
|
throw error;
|
|
154
63
|
}
|
|
155
|
-
return
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
this.pause();
|
|
160
|
-
return [2 /*return*/];
|
|
161
|
-
});
|
|
162
|
-
}); });
|
|
163
|
-
});
|
|
164
|
-
return observable;
|
|
165
|
-
};
|
|
166
|
-
MutationProcessor.prototype.stop = function () {
|
|
167
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
-
return __generator(this, function (_a) {
|
|
169
|
-
switch (_a.label) {
|
|
170
|
-
case 0:
|
|
171
|
-
this.removeObserver();
|
|
172
|
-
return [4 /*yield*/, this.runningProcesses.close()];
|
|
173
|
-
case 1:
|
|
174
|
-
_a.sent();
|
|
175
|
-
return [4 /*yield*/, this.runningProcesses.open()];
|
|
176
|
-
case 2:
|
|
177
|
-
_a.sent();
|
|
178
|
-
return [2 /*return*/];
|
|
179
|
-
}
|
|
64
|
+
return this.runningProcesses.addCleaner(async () => {
|
|
65
|
+
// The observer has unsubscribed and/or `stop()` has been called.
|
|
66
|
+
this.removeObserver();
|
|
67
|
+
this.pause();
|
|
180
68
|
});
|
|
181
69
|
});
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
70
|
+
return observable;
|
|
71
|
+
}
|
|
72
|
+
async stop() {
|
|
73
|
+
this.removeObserver();
|
|
74
|
+
await this.runningProcesses.close();
|
|
75
|
+
await this.runningProcesses.open();
|
|
76
|
+
}
|
|
77
|
+
removeObserver() {
|
|
78
|
+
this.observer?.complete?.();
|
|
186
79
|
this.observer = undefined;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return [4 /*yield*/, this.jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, this.MutationEvent, head, operationAuthModes_1[authModeAttempts_1], onTerminate)];
|
|
241
|
-
case 1:
|
|
242
|
-
response = _a.sent();
|
|
243
|
-
logger.debug("Mutation sent successfully with authMode: ".concat(operationAuthModes_1[authModeAttempts_1]));
|
|
244
|
-
return [2 /*return*/, response];
|
|
245
|
-
case 2:
|
|
246
|
-
error_2 = _a.sent();
|
|
247
|
-
authModeAttempts_1++;
|
|
248
|
-
if (!(authModeAttempts_1 >= operationAuthModes_1.length)) return [3 /*break*/, 7];
|
|
249
|
-
logger.debug("Mutation failed with authMode: ".concat(operationAuthModes_1[authModeAttempts_1 - 1]));
|
|
250
|
-
_a.label = 3;
|
|
251
|
-
case 3:
|
|
252
|
-
_a.trys.push([3, 5, , 6]);
|
|
253
|
-
return [4 /*yield*/, this.errorHandler({
|
|
254
|
-
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',
|
|
255
|
-
localModel: null,
|
|
256
|
-
message: error_2.message,
|
|
257
|
-
model: modelConstructor.name,
|
|
258
|
-
operation: opName,
|
|
259
|
-
errorType: (0, errorMaps_1.getMutationErrorType)(error_2),
|
|
260
|
-
process: types_1.ProcessName.sync,
|
|
261
|
-
remoteModel: null,
|
|
262
|
-
cause: error_2,
|
|
263
|
-
})];
|
|
264
|
-
case 4:
|
|
265
|
-
_a.sent();
|
|
266
|
-
return [3 /*break*/, 6];
|
|
267
|
-
case 5:
|
|
268
|
-
e_1 = _a.sent();
|
|
269
|
-
logger.error('Mutation error handler failed with:', e_1);
|
|
270
|
-
return [3 /*break*/, 6];
|
|
271
|
-
case 6: throw error_2;
|
|
272
|
-
case 7:
|
|
273
|
-
logger.debug("Mutation failed with authMode: ".concat(operationAuthModes_1[authModeAttempts_1 - 1], ". Retrying with authMode: ").concat(operationAuthModes_1[authModeAttempts_1]));
|
|
274
|
-
return [4 /*yield*/, authModeRetry_1()];
|
|
275
|
-
case 8: return [2 /*return*/, _a.sent()];
|
|
276
|
-
case 9: return [2 /*return*/];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}); };
|
|
280
|
-
return [4 /*yield*/, authModeRetry_1()];
|
|
281
|
-
case 3:
|
|
282
|
-
_e = __read.apply(void 0, [_f.sent(), 3]), result = _e[0], opName = _e[1], modelDefinition = _e[2];
|
|
283
|
-
return [3 /*break*/, 5];
|
|
284
|
-
case 4:
|
|
285
|
-
error_1 = _f.sent();
|
|
286
|
-
if (error_1.message === 'Offline' ||
|
|
287
|
-
error_1.message === 'RetryMutation') {
|
|
288
|
-
return [2 /*return*/, "continue"];
|
|
289
|
-
}
|
|
290
|
-
return [3 /*break*/, 5];
|
|
291
|
-
case 5:
|
|
292
|
-
if (!(result === undefined)) return [3 /*break*/, 7];
|
|
293
|
-
logger.debug('done retrying');
|
|
294
|
-
return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
|
|
295
|
-
return __generator(this, function (_a) {
|
|
296
|
-
switch (_a.label) {
|
|
297
|
-
case 0: return [4 /*yield*/, this.outbox.dequeue(storage)];
|
|
298
|
-
case 1:
|
|
299
|
-
_a.sent();
|
|
300
|
-
return [2 /*return*/];
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
}); })];
|
|
304
|
-
case 6:
|
|
305
|
-
_f.sent();
|
|
306
|
-
return [2 /*return*/, "continue"];
|
|
307
|
-
case 7:
|
|
308
|
-
record = result.data[opName];
|
|
309
|
-
hasMore = false;
|
|
310
|
-
return [4 /*yield*/, this_1.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
|
|
311
|
-
return __generator(this, function (_a) {
|
|
312
|
-
switch (_a.label) {
|
|
313
|
-
case 0:
|
|
314
|
-
// using runExclusive to prevent possible race condition
|
|
315
|
-
// when another record gets enqueued between dequeue and peek
|
|
316
|
-
return [4 /*yield*/, this.outbox.dequeue(storage, record, operation)];
|
|
317
|
-
case 1:
|
|
318
|
-
// using runExclusive to prevent possible race condition
|
|
319
|
-
// when another record gets enqueued between dequeue and peek
|
|
320
|
-
_a.sent();
|
|
321
|
-
return [4 /*yield*/, this.outbox.peek(storage)];
|
|
322
|
-
case 2:
|
|
323
|
-
hasMore = (_a.sent()) !== undefined;
|
|
324
|
-
return [2 /*return*/];
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
}); })];
|
|
328
|
-
case 8:
|
|
329
|
-
_f.sent();
|
|
330
|
-
(_c = (_b = this_1.observer) === null || _b === void 0 ? void 0 : _b.next) === null || _c === void 0 ? void 0 : _c.call(_b, {
|
|
331
|
-
operation: operation,
|
|
332
|
-
modelDefinition: modelDefinition,
|
|
333
|
-
model: record,
|
|
334
|
-
hasMore: hasMore,
|
|
335
|
-
});
|
|
336
|
-
return [2 /*return*/];
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
};
|
|
340
|
-
this_1 = this;
|
|
341
|
-
_d.label = 1;
|
|
342
|
-
case 1:
|
|
343
|
-
_a = this.processing &&
|
|
344
|
-
this.runningProcesses.isOpen;
|
|
345
|
-
if (!_a) return [3 /*break*/, 3];
|
|
346
|
-
return [4 /*yield*/, this.outbox.peek(this.storage)];
|
|
347
|
-
case 2:
|
|
348
|
-
_a = (head = _d.sent()) !== undefined;
|
|
349
|
-
_d.label = 3;
|
|
350
|
-
case 3:
|
|
351
|
-
if (!_a) return [3 /*break*/, 5];
|
|
352
|
-
return [5 /*yield**/, _loop_1()];
|
|
353
|
-
case 4:
|
|
354
|
-
_d.sent();
|
|
355
|
-
return [3 /*break*/, 1];
|
|
356
|
-
case 5:
|
|
357
|
-
// pauses itself
|
|
358
|
-
this.pause();
|
|
359
|
-
return [2 /*return*/];
|
|
80
|
+
}
|
|
81
|
+
async resume() {
|
|
82
|
+
if (this.runningProcesses.isOpen) {
|
|
83
|
+
await this.runningProcesses.add(async (onTerminate) => {
|
|
84
|
+
if (this.processing ||
|
|
85
|
+
!this.isReady() ||
|
|
86
|
+
!this.runningProcesses.isOpen) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.processing = true;
|
|
90
|
+
let head;
|
|
91
|
+
const namespaceName = util_1.USER;
|
|
92
|
+
// start to drain outbox
|
|
93
|
+
while (this.processing &&
|
|
94
|
+
this.runningProcesses.isOpen &&
|
|
95
|
+
(head = await this.outbox.peek(this.storage)) !== undefined) {
|
|
96
|
+
const { model, operation, data, condition } = head;
|
|
97
|
+
const modelConstructor = this.userClasses[model];
|
|
98
|
+
let result = undefined;
|
|
99
|
+
let opName = undefined;
|
|
100
|
+
let modelDefinition = undefined;
|
|
101
|
+
try {
|
|
102
|
+
const modelAuthModes = await (0, utils_2.getModelAuthModes)({
|
|
103
|
+
authModeStrategy: this.authModeStrategy,
|
|
104
|
+
defaultAuthMode: this.amplifyConfig.aws_appsync_authenticationType,
|
|
105
|
+
modelName: model,
|
|
106
|
+
schema: this.schema,
|
|
107
|
+
});
|
|
108
|
+
const operationAuthModes = modelAuthModes[operation.toUpperCase()];
|
|
109
|
+
let authModeAttempts = 0;
|
|
110
|
+
const authModeRetry = async () => {
|
|
111
|
+
try {
|
|
112
|
+
logger.debug(`Attempting mutation with authMode: ${operationAuthModes[authModeAttempts]}`);
|
|
113
|
+
const response = await this.jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, this.MutationEvent, head, operationAuthModes[authModeAttempts], onTerminate);
|
|
114
|
+
logger.debug(`Mutation sent successfully with authMode: ${operationAuthModes[authModeAttempts]}`);
|
|
115
|
+
return response;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
authModeAttempts++;
|
|
119
|
+
if (authModeAttempts >= operationAuthModes.length) {
|
|
120
|
+
logger.debug(`Mutation failed with authMode: ${operationAuthModes[authModeAttempts - 1]}`);
|
|
121
|
+
try {
|
|
122
|
+
await this.errorHandler({
|
|
123
|
+
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',
|
|
124
|
+
localModel: null,
|
|
125
|
+
message: error.message,
|
|
126
|
+
model: modelConstructor.name,
|
|
127
|
+
operation: opName,
|
|
128
|
+
errorType: (0, errorMaps_1.getMutationErrorType)(error),
|
|
129
|
+
process: types_1.ProcessName.sync,
|
|
130
|
+
remoteModel: null,
|
|
131
|
+
cause: error,
|
|
132
|
+
});
|
|
360
133
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
134
|
+
catch (e) {
|
|
135
|
+
logger.error('Mutation error handler failed with:', e);
|
|
136
|
+
}
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
logger.debug(`Mutation failed with authMode: ${operationAuthModes[authModeAttempts - 1]}. Retrying with authMode: ${operationAuthModes[authModeAttempts]}`);
|
|
140
|
+
return await authModeRetry();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
[result, opName, modelDefinition] = await authModeRetry();
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (error.message === 'Offline' ||
|
|
147
|
+
error.message === 'RetryMutation') {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (result === undefined) {
|
|
152
|
+
logger.debug('done retrying');
|
|
153
|
+
await this.storage.runExclusive(async (storage) => {
|
|
154
|
+
await this.outbox.dequeue(storage);
|
|
155
|
+
});
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const record = result.data[opName];
|
|
159
|
+
let hasMore = false;
|
|
160
|
+
await this.storage.runExclusive(async (storage) => {
|
|
161
|
+
// using runExclusive to prevent possible race condition
|
|
162
|
+
// when another record gets enqueued between dequeue and peek
|
|
163
|
+
await this.outbox.dequeue(storage, record, operation);
|
|
164
|
+
hasMore = (await this.outbox.peek(storage)) !== undefined;
|
|
165
|
+
});
|
|
166
|
+
this.observer?.next?.({
|
|
167
|
+
operation,
|
|
168
|
+
modelDefinition,
|
|
169
|
+
model: record,
|
|
170
|
+
hasMore,
|
|
171
|
+
});
|
|
367
172
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
attempt
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
_j.trys.push([5, 7, , 8]);
|
|
433
|
-
return [4 /*yield*/, this.conflictHandler({
|
|
434
|
-
modelConstructor: modelConstructor,
|
|
435
|
-
localModel: this.modelInstanceCreator(modelConstructor, variables.input),
|
|
436
|
-
remoteModel: this.modelInstanceCreator(modelConstructor, error.data),
|
|
437
|
-
operation: opType,
|
|
438
|
-
attempts: attempt,
|
|
439
|
-
})];
|
|
440
|
-
case 6:
|
|
441
|
-
retryWith = _j.sent();
|
|
442
|
-
return [3 /*break*/, 8];
|
|
443
|
-
case 7:
|
|
444
|
-
err_2 = _j.sent();
|
|
445
|
-
logger.warn('conflict trycatch', err_2);
|
|
446
|
-
return [3 /*break*/, 17];
|
|
447
|
-
case 8:
|
|
448
|
-
if (!(retryWith === types_1.DISCARD)) return [3 /*break*/, 11];
|
|
449
|
-
_f = __read((0, utils_2.buildGraphQLOperation)(this.schema.namespaces[namespaceName], modelDefinition, 'GET'), 1), _g = __read(_f[0], 3), opName_1 = _g[1], query_1 = _g[2];
|
|
450
|
-
return [4 /*yield*/, (0, utils_2.getTokenForCustomAuth)(authMode, this.amplifyConfig)];
|
|
451
|
-
case 9:
|
|
452
|
-
authToken_1 = _j.sent();
|
|
453
|
-
return [4 /*yield*/, this.amplifyContext.InternalAPI.graphql({
|
|
454
|
-
query: query_1,
|
|
455
|
-
variables: { id: variables.input.id },
|
|
456
|
-
authMode: authMode,
|
|
457
|
-
authToken: authToken_1,
|
|
458
|
-
}, undefined, customUserAgentDetails)];
|
|
459
|
-
case 10:
|
|
460
|
-
serverData = _j.sent();
|
|
461
|
-
// onTerminate cancel graphql()
|
|
462
|
-
return [2 /*return*/, [serverData, opName_1, modelDefinition]];
|
|
463
|
-
case 11:
|
|
464
|
-
namespace = this.schema.namespaces[namespaceName];
|
|
465
|
-
updatedMutation = (0, utils_2.createMutationInstanceFromModelOperation)(namespace.relationships, modelDefinition, opType, modelConstructor, retryWith, graphQLCondition, MutationEvent, this.modelInstanceCreator, mutationEvent.id);
|
|
466
|
-
return [4 /*yield*/, this.storage.save(updatedMutation)];
|
|
467
|
-
case 12:
|
|
468
|
-
_j.sent();
|
|
469
|
-
throw new utils_1.NonRetryableError('RetryMutation');
|
|
470
|
-
case 13:
|
|
471
|
-
try {
|
|
472
|
-
this.errorHandler({
|
|
473
|
-
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',
|
|
474
|
-
localModel: variables.input,
|
|
475
|
-
message: error.message,
|
|
476
|
-
operation: operation,
|
|
477
|
-
errorType: (0, errorMaps_1.getMutationErrorType)(error),
|
|
478
|
-
errorInfo: error.errorInfo,
|
|
479
|
-
process: types_1.ProcessName.mutate,
|
|
480
|
-
cause: error,
|
|
481
|
-
remoteModel: error.data
|
|
482
|
-
? this.modelInstanceCreator(modelConstructor, error.data)
|
|
483
|
-
: null,
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
catch (err) {
|
|
487
|
-
logger.warn('Mutation error handler failed with:', err);
|
|
488
|
-
}
|
|
489
|
-
finally {
|
|
490
|
-
// Return empty tuple, dequeues the mutation
|
|
491
|
-
return [2 /*return*/, error.data
|
|
492
|
-
? [
|
|
493
|
-
{ data: (_h = {}, _h[opName] = error.data, _h) },
|
|
494
|
-
opName,
|
|
495
|
-
modelDefinition,
|
|
496
|
-
]
|
|
497
|
-
: []];
|
|
498
|
-
}
|
|
499
|
-
_j.label = 14;
|
|
500
|
-
case 14: return [3 /*break*/, 16];
|
|
501
|
-
case 15:
|
|
502
|
-
// Catch-all for client-side errors that don't come back in the `GraphQLError` format.
|
|
503
|
-
// These errors should not be retried.
|
|
504
|
-
throw new utils_1.NonRetryableError(err_1);
|
|
505
|
-
case 16: return [3 /*break*/, 17];
|
|
506
|
-
case 17:
|
|
507
|
-
if (tryWith) return [3 /*break*/, 2];
|
|
508
|
-
_j.label = 18;
|
|
509
|
-
case 18: return [2 /*return*/];
|
|
173
|
+
// pauses itself
|
|
174
|
+
this.pause();
|
|
175
|
+
}, 'mutation resume loop');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async jitteredRetry(namespaceName, model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent, authMode, onTerminate) {
|
|
179
|
+
return await (0, utils_1.retry)(async (model, operation, data, condition, modelConstructor, MutationEvent, mutationEvent) => {
|
|
180
|
+
const [query, variables, graphQLCondition, opName, modelDefinition] = this.createQueryVariables(namespaceName, model, operation, data, condition);
|
|
181
|
+
const authToken = await (0, utils_2.getTokenForCustomAuth)(authMode, this.amplifyConfig);
|
|
182
|
+
const tryWith = {
|
|
183
|
+
query,
|
|
184
|
+
variables,
|
|
185
|
+
authMode,
|
|
186
|
+
authToken,
|
|
187
|
+
};
|
|
188
|
+
let attempt = 0;
|
|
189
|
+
const opType = this.opTypeFromTransformerOperation(operation);
|
|
190
|
+
const customUserAgentDetails = {
|
|
191
|
+
category: utils_1.Category.DataStore,
|
|
192
|
+
action: utils_1.DataStoreAction.GraphQl,
|
|
193
|
+
};
|
|
194
|
+
do {
|
|
195
|
+
try {
|
|
196
|
+
const result = (await this.amplifyContext.InternalAPI.graphql(tryWith, undefined, customUserAgentDetails));
|
|
197
|
+
// Use `as any` because TypeScript doesn't seem to like passing tuples
|
|
198
|
+
// through generic params.
|
|
199
|
+
return [result, opName, modelDefinition];
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
if (err.errors && err.errors.length > 0) {
|
|
203
|
+
const [error] = err.errors;
|
|
204
|
+
const { originalError: { code = null } = {} } = error;
|
|
205
|
+
if (error.errorType === 'Unauthorized') {
|
|
206
|
+
throw new utils_1.NonRetryableError('Unauthorized');
|
|
207
|
+
}
|
|
208
|
+
if (error.message === 'Network Error' ||
|
|
209
|
+
code === 'ECONNABORTED' // refers to axios timeout error caused by device's bad network condition
|
|
210
|
+
) {
|
|
211
|
+
if (!this.processing) {
|
|
212
|
+
throw new utils_1.NonRetryableError('Offline');
|
|
213
|
+
}
|
|
214
|
+
// TODO: Check errors on different env (react-native or other browsers)
|
|
215
|
+
throw new Error('Network Error');
|
|
216
|
+
}
|
|
217
|
+
if (error.errorType === 'ConflictUnhandled') {
|
|
218
|
+
// TODO: add on ConflictConditionalCheck error query last from server
|
|
219
|
+
attempt++;
|
|
220
|
+
let retryWith;
|
|
221
|
+
if (attempt > MAX_ATTEMPTS) {
|
|
222
|
+
retryWith = types_1.DISCARD;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
try {
|
|
226
|
+
retryWith = await this.conflictHandler({
|
|
227
|
+
modelConstructor,
|
|
228
|
+
localModel: this.modelInstanceCreator(modelConstructor, variables.input),
|
|
229
|
+
remoteModel: this.modelInstanceCreator(modelConstructor, error.data),
|
|
230
|
+
operation: opType,
|
|
231
|
+
attempts: attempt,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
logger.warn('conflict trycatch', err);
|
|
236
|
+
continue;
|
|
510
237
|
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
238
|
+
}
|
|
239
|
+
if (retryWith === types_1.DISCARD) {
|
|
240
|
+
// Query latest from server and notify merger
|
|
241
|
+
const [[, opName, query]] = (0, utils_2.buildGraphQLOperation)(this.schema.namespaces[namespaceName], modelDefinition, 'GET');
|
|
242
|
+
const authToken = await (0, utils_2.getTokenForCustomAuth)(authMode, this.amplifyConfig);
|
|
243
|
+
const serverData = await this.amplifyContext.InternalAPI.graphql({
|
|
244
|
+
query,
|
|
245
|
+
variables: { id: variables.input.id },
|
|
246
|
+
authMode,
|
|
247
|
+
authToken,
|
|
248
|
+
}, undefined, customUserAgentDetails);
|
|
249
|
+
// onTerminate cancel graphql()
|
|
250
|
+
return [serverData, opName, modelDefinition];
|
|
251
|
+
}
|
|
252
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
253
|
+
// convert retry with to tryWith
|
|
254
|
+
const updatedMutation = (0, utils_2.createMutationInstanceFromModelOperation)(namespace.relationships, modelDefinition, opType, modelConstructor, retryWith, graphQLCondition, MutationEvent, this.modelInstanceCreator, mutationEvent.id);
|
|
255
|
+
await this.storage.save(updatedMutation);
|
|
256
|
+
throw new utils_1.NonRetryableError('RetryMutation');
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
try {
|
|
260
|
+
this.errorHandler({
|
|
261
|
+
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',
|
|
262
|
+
localModel: variables.input,
|
|
263
|
+
message: error.message,
|
|
264
|
+
operation,
|
|
265
|
+
errorType: (0, errorMaps_1.getMutationErrorType)(error),
|
|
266
|
+
errorInfo: error.errorInfo,
|
|
267
|
+
process: types_1.ProcessName.mutate,
|
|
268
|
+
cause: error,
|
|
269
|
+
remoteModel: error.data
|
|
270
|
+
? this.modelInstanceCreator(modelConstructor, error.data)
|
|
271
|
+
: null,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
logger.warn('Mutation error handler failed with:', err);
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
// Return empty tuple, dequeues the mutation
|
|
279
|
+
return error.data
|
|
280
|
+
? [
|
|
281
|
+
{ data: { [opName]: error.data } },
|
|
282
|
+
opName,
|
|
283
|
+
modelDefinition,
|
|
284
|
+
]
|
|
285
|
+
: [];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
// Catch-all for client-side errors that don't come back in the `GraphQLError` format.
|
|
291
|
+
// These errors should not be retried.
|
|
292
|
+
throw new utils_1.NonRetryableError(err);
|
|
293
|
+
}
|
|
522
294
|
}
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
295
|
+
} while (tryWith);
|
|
296
|
+
}, [
|
|
297
|
+
model,
|
|
298
|
+
operation,
|
|
299
|
+
data,
|
|
300
|
+
condition,
|
|
301
|
+
modelConstructor,
|
|
302
|
+
MutationEvent,
|
|
303
|
+
mutationEvent,
|
|
304
|
+
], exports.safeJitteredBackoff, onTerminate);
|
|
305
|
+
}
|
|
306
|
+
createQueryVariables(namespaceName, model, operation, data, condition) {
|
|
307
|
+
const modelDefinition = this.schema.namespaces[namespaceName].models[model];
|
|
308
|
+
const { primaryKey } = this.schema.namespaces[namespaceName].keys[model];
|
|
309
|
+
const auth = modelDefinition.attributes?.find(a => a.type === 'auth');
|
|
310
|
+
const ownerFields = auth?.properties?.rules
|
|
311
|
+
.map(rule => rule.ownerField)
|
|
312
|
+
.filter(f => f) || ['owner'];
|
|
313
|
+
const queriesTuples = this.typeQuery.get(modelDefinition);
|
|
314
|
+
const [, opName, query] = queriesTuples.find(([transformerMutationType]) => transformerMutationType === operation);
|
|
315
|
+
const { _version, ...parsedData } = JSON.parse(data);
|
|
539
316
|
// include all the fields that comprise a custom PK if one is specified
|
|
540
|
-
|
|
317
|
+
const deleteInput = {};
|
|
541
318
|
if (primaryKey && primaryKey.length) {
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
var pkField = primaryKey_1_1.value;
|
|
545
|
-
deleteInput[pkField] = parsedData[pkField];
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
549
|
-
finally {
|
|
550
|
-
try {
|
|
551
|
-
if (primaryKey_1_1 && !primaryKey_1_1.done && (_a = primaryKey_1.return)) _a.call(primaryKey_1);
|
|
552
|
-
}
|
|
553
|
-
finally { if (e_2) throw e_2.error; }
|
|
319
|
+
for (const pkField of primaryKey) {
|
|
320
|
+
deleteInput[pkField] = parsedData[pkField];
|
|
554
321
|
}
|
|
555
322
|
}
|
|
556
323
|
else {
|
|
557
324
|
deleteInput[util_1.ID] = parsedData.id;
|
|
558
325
|
}
|
|
559
|
-
|
|
326
|
+
let mutationInput;
|
|
560
327
|
if (operation === utils_2.TransformerMutationType.DELETE) {
|
|
561
328
|
// For DELETE mutations, only the key(s) are included in the input
|
|
562
329
|
mutationInput = deleteInput;
|
|
@@ -564,75 +331,61 @@ var MutationProcessor = /** @class */ (function () {
|
|
|
564
331
|
else {
|
|
565
332
|
// Otherwise, we construct the mutation input with the following logic
|
|
566
333
|
mutationInput = {};
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
//
|
|
580
|
-
if ((0, types_1.
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
// instead of including the connected model itself, we add its key(s) to the mutation input
|
|
588
|
-
for (var targetNames_1 = (e_4 = void 0, __values(targetNames)), targetNames_1_1 = targetNames_1.next(); !targetNames_1_1.done; targetNames_1_1 = targetNames_1.next()) {
|
|
589
|
-
var targetName = targetNames_1_1.value;
|
|
590
|
-
mutationInput[targetName] = parsedData[targetName];
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
594
|
-
finally {
|
|
595
|
-
try {
|
|
596
|
-
if (targetNames_1_1 && !targetNames_1_1.done && (_c = targetNames_1.return)) _c.call(targetNames_1);
|
|
597
|
-
}
|
|
598
|
-
finally { if (e_4) throw e_4.error; }
|
|
599
|
-
}
|
|
334
|
+
const modelFields = Object.values(modelDefinition.fields);
|
|
335
|
+
for (const { name, type, association, isReadOnly } of modelFields) {
|
|
336
|
+
// omit readonly fields. cloud storage doesn't need them and won't take them!
|
|
337
|
+
if (isReadOnly) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
// omit owner fields if it's `null`. cloud storage doesn't allow it.
|
|
341
|
+
if (ownerFields.includes(name) && parsedData[name] === null) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
// model fields should be stripped out from the input
|
|
345
|
+
if ((0, types_1.isModelFieldType)(type)) {
|
|
346
|
+
// except for belongs to relations - we need to replace them with the correct foreign key(s)
|
|
347
|
+
if ((0, types_1.isTargetNameAssociation)(association) &&
|
|
348
|
+
association.connectionType === 'BELONGS_TO') {
|
|
349
|
+
const targetNames = (0, util_1.extractTargetNamesFromSrc)(association);
|
|
350
|
+
if (targetNames) {
|
|
351
|
+
// instead of including the connected model itself, we add its key(s) to the mutation input
|
|
352
|
+
for (const targetName of targetNames) {
|
|
353
|
+
mutationInput[targetName] = parsedData[targetName];
|
|
600
354
|
}
|
|
601
355
|
}
|
|
602
|
-
continue;
|
|
603
|
-
}
|
|
604
|
-
// scalar fields / non-model types
|
|
605
|
-
if (operation === utils_2.TransformerMutationType.UPDATE) {
|
|
606
|
-
if (!parsedData.hasOwnProperty(name_1)) {
|
|
607
|
-
// for update mutations - strip out a field if it's unchanged
|
|
608
|
-
continue;
|
|
609
|
-
}
|
|
610
356
|
}
|
|
611
|
-
|
|
612
|
-
mutationInput[name_1] = parsedData[name_1];
|
|
357
|
+
continue;
|
|
613
358
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
359
|
+
// scalar fields / non-model types
|
|
360
|
+
if (operation === utils_2.TransformerMutationType.UPDATE) {
|
|
361
|
+
if (!parsedData.hasOwnProperty(name)) {
|
|
362
|
+
// for update mutations - strip out a field if it's unchanged
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
619
365
|
}
|
|
620
|
-
|
|
366
|
+
// all other fields are added to the input object
|
|
367
|
+
mutationInput[name] = parsedData[name];
|
|
621
368
|
}
|
|
622
369
|
}
|
|
623
370
|
// Build mutation variables input object
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
371
|
+
const input = {
|
|
372
|
+
...mutationInput,
|
|
373
|
+
_version,
|
|
374
|
+
};
|
|
375
|
+
const graphQLCondition = JSON.parse(condition);
|
|
376
|
+
const variables = {
|
|
377
|
+
input,
|
|
378
|
+
...(operation === utils_2.TransformerMutationType.CREATE
|
|
379
|
+
? {}
|
|
380
|
+
: {
|
|
381
|
+
condition: Object.keys(graphQLCondition).length > 0
|
|
382
|
+
? graphQLCondition
|
|
383
|
+
: null,
|
|
384
|
+
}),
|
|
385
|
+
};
|
|
633
386
|
return [query, variables, graphQLCondition, opName, modelDefinition];
|
|
634
|
-
}
|
|
635
|
-
|
|
387
|
+
}
|
|
388
|
+
opTypeFromTransformerOperation(operation) {
|
|
636
389
|
switch (operation) {
|
|
637
390
|
case utils_2.TransformerMutationType.CREATE:
|
|
638
391
|
return types_1.OpType.INSERT;
|
|
@@ -643,19 +396,18 @@ var MutationProcessor = /** @class */ (function () {
|
|
|
643
396
|
case utils_2.TransformerMutationType.GET: // Intentionally blank
|
|
644
397
|
break;
|
|
645
398
|
default:
|
|
646
|
-
throw new Error(
|
|
399
|
+
throw new Error(`Invalid operation ${operation}`);
|
|
647
400
|
}
|
|
648
401
|
// because it makes TS happy ...
|
|
649
402
|
return undefined;
|
|
650
|
-
}
|
|
651
|
-
|
|
403
|
+
}
|
|
404
|
+
pause() {
|
|
652
405
|
this.processing = false;
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
}());
|
|
406
|
+
}
|
|
407
|
+
}
|
|
656
408
|
exports.MutationProcessor = MutationProcessor;
|
|
657
|
-
|
|
658
|
-
|
|
409
|
+
const MAX_RETRY_DELAY_MS = 5 * 60 * 1000;
|
|
410
|
+
const originalJitteredBackoff = (0, utils_1.jitteredBackoff)(MAX_RETRY_DELAY_MS);
|
|
659
411
|
/**
|
|
660
412
|
* @private
|
|
661
413
|
* Internal use of Amplify only.
|
|
@@ -671,8 +423,8 @@ var originalJitteredBackoff = (0, utils_1.jitteredBackoff)(MAX_RETRY_DELAY_MS);
|
|
|
671
423
|
* @param error tested to see if `.message` is 'Network Error'
|
|
672
424
|
* @returns number | false :
|
|
673
425
|
*/
|
|
674
|
-
|
|
675
|
-
|
|
426
|
+
const safeJitteredBackoff = (attempt, _args, error) => {
|
|
427
|
+
const attemptResult = originalJitteredBackoff(attempt);
|
|
676
428
|
// If this is the last attempt and it is a network error, we retry indefinitively every 5 minutes
|
|
677
429
|
if (attemptResult === false &&
|
|
678
430
|
(error || {}).message === 'Network Error') {
|