@aws-amplify/datastore 3.12.6-next.20 → 3.12.6-next.36

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