@aws-amplify/datastore 4.7.6-api-v6-models.b3abc9b.0 → 5.0.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/README.md +4 -0
- package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
- package/lib/authModeStrategies/index.js +3 -3
- package/lib/authModeStrategies/multiAuthStrategy.js +38 -53
- package/lib/datastore/datastore.d.ts +4 -5
- package/lib/datastore/datastore.js +929 -1284
- package/lib/index.d.ts +1 -1
- package/lib/index.js +26 -13
- package/lib/predicates/index.js +54 -69
- package/lib/predicates/next.d.ts +2 -2
- package/lib/predicates/next.js +313 -462
- package/lib/predicates/sort.js +24 -28
- package/lib/ssr/index.js +2 -2
- package/lib/storage/adapter/AsyncStorageAdapter.js +120 -342
- package/lib/storage/adapter/AsyncStorageDatabase.js +217 -421
- package/lib/storage/adapter/InMemoryStore.js +28 -51
- package/lib/storage/adapter/InMemoryStore.native.js +5 -3
- package/lib/storage/adapter/IndexedDBAdapter.js +466 -871
- package/lib/storage/adapter/StorageAdapterBase.js +180 -330
- package/lib/storage/adapter/getDefaultAdapter/index.js +8 -10
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +5 -4
- package/lib/storage/adapter/index.js +0 -1
- package/lib/storage/relationship.js +177 -253
- package/lib/storage/storage.d.ts +4 -4
- package/lib/storage/storage.js +255 -433
- package/lib/sync/datastoreConnectivity.d.ts +2 -2
- package/lib/sync/datastoreConnectivity.js +29 -39
- package/lib/sync/datastoreReachability/index.d.ts +1 -3
- package/lib/sync/datastoreReachability/index.js +3 -3
- package/lib/sync/datastoreReachability/index.native.d.ts +1 -3
- package/lib/sync/datastoreReachability/index.native.js +4 -5
- package/lib/sync/index.d.ts +2 -2
- package/lib/sync/index.js +522 -827
- package/lib/sync/merger.js +31 -63
- package/lib/sync/outbox.js +148 -232
- package/lib/sync/processors/errorMaps.d.ts +1 -1
- package/lib/sync/processors/errorMaps.js +30 -47
- package/lib/sync/processors/mutation.d.ts +2 -2
- package/lib/sync/processors/mutation.js +343 -502
- package/lib/sync/processors/subscription.d.ts +5 -2
- package/lib/sync/processors/subscription.js +283 -437
- package/lib/sync/processors/sync.d.ts +2 -2
- package/lib/sync/processors/sync.js +279 -404
- package/lib/sync/utils.d.ts +5 -4
- package/lib/sync/utils.js +267 -320
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types.d.ts +138 -140
- package/lib/types.js +17 -24
- package/lib/util.d.ts +9 -17
- package/lib/util.js +387 -511
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -2
- package/lib-esm/authModeStrategies/index.js +0 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +35 -52
- package/lib-esm/datastore/datastore.d.ts +4 -5
- package/lib-esm/datastore/datastore.js +888 -1247
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +6 -7
- package/lib-esm/predicates/index.js +53 -70
- package/lib-esm/predicates/next.d.ts +2 -2
- package/lib-esm/predicates/next.js +306 -459
- package/lib-esm/predicates/sort.js +23 -28
- package/lib-esm/ssr/index.js +1 -2
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -338
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -416
- package/lib-esm/storage/adapter/InMemoryStore.js +27 -52
- package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +438 -866
- package/lib-esm/storage/adapter/StorageAdapterBase.js +173 -325
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -6
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -2
- package/lib-esm/storage/adapter/index.js +1 -1
- package/lib-esm/storage/relationship.js +173 -251
- package/lib-esm/storage/storage.d.ts +4 -4
- package/lib-esm/storage/storage.js +242 -424
- package/lib-esm/sync/datastoreConnectivity.d.ts +2 -2
- package/lib-esm/sync/datastoreConnectivity.js +28 -39
- package/lib-esm/sync/datastoreReachability/index.d.ts +1 -3
- package/lib-esm/sync/datastoreReachability/index.js +2 -3
- package/lib-esm/sync/datastoreReachability/index.native.d.ts +1 -3
- package/lib-esm/sync/datastoreReachability/index.native.js +3 -4
- package/lib-esm/sync/index.d.ts +2 -2
- package/lib-esm/sync/index.js +502 -812
- package/lib-esm/sync/merger.js +28 -61
- package/lib-esm/sync/outbox.js +143 -228
- package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
- package/lib-esm/sync/processors/errorMaps.js +32 -50
- package/lib-esm/sync/processors/mutation.d.ts +2 -2
- package/lib-esm/sync/processors/mutation.js +329 -490
- package/lib-esm/sync/processors/subscription.d.ts +5 -2
- package/lib-esm/sync/processors/subscription.js +266 -421
- package/lib-esm/sync/processors/sync.d.ts +2 -2
- package/lib-esm/sync/processors/sync.js +271 -397
- package/lib-esm/sync/utils.d.ts +5 -4
- package/lib-esm/sync/utils.js +252 -307
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types.d.ts +138 -140
- package/lib-esm/types.js +16 -25
- package/lib-esm/util.d.ts +9 -17
- package/lib-esm/util.js +335 -497
- package/package.json +31 -26
- package/src/authModeStrategies/multiAuthStrategy.ts +15 -12
- package/src/datastore/datastore.ts +36 -35
- package/src/predicates/sort.ts +3 -1
- package/src/storage/adapter/InMemoryStore.ts +1 -1
- package/src/storage/adapter/IndexedDBAdapter.ts +2 -2
- package/src/storage/adapter/StorageAdapterBase.ts +2 -2
- package/src/storage/adapter/getDefaultAdapter/index.ts +1 -4
- package/src/storage/storage.ts +29 -24
- package/src/sync/datastoreConnectivity.ts +6 -6
- package/src/sync/datastoreReachability/index.native.ts +5 -3
- package/src/sync/datastoreReachability/index.ts +1 -1
- package/src/sync/index.ts +79 -89
- package/src/sync/processors/errorMaps.ts +7 -7
- package/src/sync/processors/mutation.ts +19 -13
- package/src/sync/processors/subscription.ts +221 -295
- package/src/sync/processors/sync.ts +11 -8
- package/src/sync/utils.ts +30 -15
- package/src/types.ts +4 -8
- package/src/util.ts +46 -9
- package/lib/.tsbuildinfo +0 -3
- package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
- package/lib/authModeStrategies/index.js.map +0 -1
- package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
- package/lib/datastore/datastore.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/predicates/index.js.map +0 -1
- package/lib/predicates/next.js.map +0 -1
- package/lib/predicates/sort.js.map +0 -1
- package/lib/ssr/index.js.map +0 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
- package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
- package/lib/storage/adapter/InMemoryStore.js.map +0 -1
- package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
- package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
- package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
- package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
- package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
- package/lib/storage/adapter/index.js.map +0 -1
- package/lib/storage/relationship.js.map +0 -1
- package/lib/storage/storage.js.map +0 -1
- package/lib/sync/datastoreConnectivity.js.map +0 -1
- package/lib/sync/datastoreReachability/index.js.map +0 -1
- package/lib/sync/datastoreReachability/index.native.js.map +0 -1
- package/lib/sync/index.js.map +0 -1
- package/lib/sync/merger.js.map +0 -1
- package/lib/sync/outbox.js.map +0 -1
- package/lib/sync/processors/errorMaps.js.map +0 -1
- package/lib/sync/processors/mutation.js.map +0 -1
- package/lib/sync/processors/subscription.js.map +0 -1
- package/lib/sync/processors/sync.js.map +0 -1
- package/lib/sync/utils.js.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/util.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
- package/lib-esm/authModeStrategies/index.js.map +0 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
- package/lib-esm/datastore/datastore.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/predicates/index.js.map +0 -1
- package/lib-esm/predicates/next.js.map +0 -1
- package/lib-esm/predicates/sort.js.map +0 -1
- package/lib-esm/ssr/index.js.map +0 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
- package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
- package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
- package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
- package/lib-esm/storage/adapter/index.js.map +0 -1
- package/lib-esm/storage/relationship.js.map +0 -1
- package/lib-esm/storage/storage.js.map +0 -1
- package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
- package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
- package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
- package/lib-esm/sync/index.js.map +0 -1
- package/lib-esm/sync/merger.js.map +0 -1
- package/lib-esm/sync/outbox.js.map +0 -1
- package/lib-esm/sync/processors/errorMaps.js.map +0 -1
- package/lib-esm/sync/processors/mutation.js.map +0 -1
- package/lib-esm/sync/processors/subscription.js.map +0 -1
- package/lib-esm/sync/processors/sync.js.map +0 -1
- package/lib-esm/sync/utils.js.map +0 -1
- package/lib-esm/types.js.map +0 -1
- package/lib-esm/util.js.map +0 -1
package/lib-esm/sync/merger.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { __awaiter, __generator, __read, __spread, __values } from "tslib";
|
|
2
1
|
import { OpType, } from '../types';
|
|
3
2
|
import { getIdentifierValue } from './utils';
|
|
4
3
|
// https://github.com/aws-amplify/amplify-js/blob/datastore-docs/packages/datastore/docs/sync-engine.md#merger
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
class ModelMerger {
|
|
5
|
+
constructor(outbox, ownSymbol) {
|
|
7
6
|
this.outbox = outbox;
|
|
8
7
|
this.ownSymbol = ownSymbol;
|
|
9
8
|
}
|
|
@@ -13,62 +12,30 @@ var ModelMerger = /** @class */ (function () {
|
|
|
13
12
|
* @param model The model from an outbox mutation.
|
|
14
13
|
* @returns The type of operation (INSERT/UPDATE/DELETE)
|
|
15
14
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ModelMerger.prototype.mergePage = function (storage, modelConstructor, items, modelDefinition) {
|
|
43
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
-
var itemsMap, items_1, items_1_1, item, modelId, page;
|
|
45
|
-
var e_1, _a;
|
|
46
|
-
return __generator(this, function (_b) {
|
|
47
|
-
switch (_b.label) {
|
|
48
|
-
case 0:
|
|
49
|
-
itemsMap = new Map();
|
|
50
|
-
try {
|
|
51
|
-
for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
52
|
-
item = items_1_1.value;
|
|
53
|
-
modelId = getIdentifierValue(modelDefinition, item);
|
|
54
|
-
itemsMap.set(modelId, item);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
58
|
-
finally {
|
|
59
|
-
try {
|
|
60
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
61
|
-
}
|
|
62
|
-
finally { if (e_1) throw e_1.error; }
|
|
63
|
-
}
|
|
64
|
-
page = __spread(itemsMap.values());
|
|
65
|
-
return [4 /*yield*/, storage.batchSave(modelConstructor, page, this.ownSymbol)];
|
|
66
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
return ModelMerger;
|
|
72
|
-
}());
|
|
15
|
+
async merge(storage, model, modelDefinition) {
|
|
16
|
+
let result;
|
|
17
|
+
const mutationsForModel = await this.outbox.getForModel(storage, model, modelDefinition);
|
|
18
|
+
const isDelete = model._deleted;
|
|
19
|
+
if (mutationsForModel.length === 0) {
|
|
20
|
+
if (isDelete) {
|
|
21
|
+
result = OpType.DELETE;
|
|
22
|
+
await storage.delete(model, undefined, this.ownSymbol);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
[[, result]] = await storage.save(model, undefined, this.ownSymbol);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
async mergePage(storage, modelConstructor, items, modelDefinition) {
|
|
31
|
+
const itemsMap = new Map();
|
|
32
|
+
for (const item of items) {
|
|
33
|
+
// merge items by model id. Latest record for a given id remains.
|
|
34
|
+
const modelId = getIdentifierValue(modelDefinition, item);
|
|
35
|
+
itemsMap.set(modelId, item);
|
|
36
|
+
}
|
|
37
|
+
const page = [...itemsMap.values()];
|
|
38
|
+
return await storage.batchSave(modelConstructor, page, this.ownSymbol);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
73
41
|
export { ModelMerger };
|
|
74
|
-
//# sourceMappingURL=merger.js.map
|
package/lib-esm/sync/outbox.js
CHANGED
|
@@ -1,243 +1,161 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator, __read, __rest } from "tslib";
|
|
2
1
|
import { ModelPredicateCreator } from '../predicates';
|
|
3
2
|
import { QueryOne, } from '../types';
|
|
4
3
|
import { USER, SYNC, valuesEqual } from '../util';
|
|
5
4
|
import { getIdentifierValue, TransformerMutationType } from './utils';
|
|
6
5
|
// TODO: Persist deleted ids
|
|
7
6
|
// https://github.com/aws-amplify/amplify-js/blob/datastore-docs/packages/datastore/docs/sync-engine.md#outbox
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
class MutationEventOutbox {
|
|
8
|
+
constructor(schema, MutationEvent, modelInstanceCreator, ownSymbol) {
|
|
10
9
|
this.schema = schema;
|
|
11
10
|
this.MutationEvent = MutationEvent;
|
|
12
11
|
this.modelInstanceCreator = modelInstanceCreator;
|
|
13
12
|
this.ownSymbol = ownSymbol;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
mutationEventModelDefinition = this.schema.namespaces[SYNC].models['MutationEvent'];
|
|
26
|
-
predicate = ModelPredicateCreator.createFromAST(mutationEventModelDefinition, {
|
|
27
|
-
and: [
|
|
28
|
-
{ modelId: { eq: mutationEvent.modelId } },
|
|
29
|
-
{ id: { ne: this.inProgressMutationEventId } },
|
|
30
|
-
],
|
|
31
|
-
});
|
|
32
|
-
return [4 /*yield*/, s.query(this.MutationEvent, predicate)];
|
|
33
|
-
case 1:
|
|
34
|
-
_a = __read.apply(void 0, [_b.sent(), 1]), first = _a[0];
|
|
35
|
-
if (!(first === undefined)) return [3 /*break*/, 3];
|
|
36
|
-
return [4 /*yield*/, s.save(mutationEvent, undefined, this.ownSymbol)];
|
|
37
|
-
case 2:
|
|
38
|
-
_b.sent();
|
|
39
|
-
return [2 /*return*/];
|
|
40
|
-
case 3:
|
|
41
|
-
incomingMutationType = mutationEvent.operation;
|
|
42
|
-
if (!(first.operation === TransformerMutationType.CREATE)) return [3 /*break*/, 8];
|
|
43
|
-
if (!(incomingMutationType === TransformerMutationType.DELETE)) return [3 /*break*/, 5];
|
|
44
|
-
return [4 /*yield*/, s.delete(this.MutationEvent, predicate)];
|
|
45
|
-
case 4:
|
|
46
|
-
_b.sent();
|
|
47
|
-
return [3 /*break*/, 7];
|
|
48
|
-
case 5:
|
|
49
|
-
merged_1 = this.mergeUserFields(first, mutationEvent);
|
|
50
|
-
return [4 /*yield*/, s.save(this.MutationEvent.copyOf(first, function (draft) {
|
|
51
|
-
draft.data = merged_1.data;
|
|
52
|
-
}), undefined, this.ownSymbol)];
|
|
53
|
-
case 6:
|
|
54
|
-
_b.sent();
|
|
55
|
-
_b.label = 7;
|
|
56
|
-
case 7: return [3 /*break*/, 12];
|
|
57
|
-
case 8:
|
|
58
|
-
incomingConditionJSON = mutationEvent.condition;
|
|
59
|
-
incomingCondition = JSON.parse(incomingConditionJSON);
|
|
60
|
-
merged = void 0;
|
|
61
|
-
if (!(Object.keys(incomingCondition).length === 0)) return [3 /*break*/, 10];
|
|
62
|
-
merged = this.mergeUserFields(first, mutationEvent);
|
|
63
|
-
// delete all for model
|
|
64
|
-
return [4 /*yield*/, s.delete(this.MutationEvent, predicate)];
|
|
65
|
-
case 9:
|
|
66
|
-
// delete all for model
|
|
67
|
-
_b.sent();
|
|
68
|
-
_b.label = 10;
|
|
69
|
-
case 10:
|
|
70
|
-
merged = merged || mutationEvent;
|
|
71
|
-
// Enqueue new one
|
|
72
|
-
return [4 /*yield*/, s.save(merged, undefined, this.ownSymbol)];
|
|
73
|
-
case 11:
|
|
74
|
-
// Enqueue new one
|
|
75
|
-
_b.sent();
|
|
76
|
-
_b.label = 12;
|
|
77
|
-
case 12: return [2 /*return*/];
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}); })];
|
|
81
|
-
case 1:
|
|
82
|
-
_a.sent();
|
|
83
|
-
return [2 /*return*/];
|
|
84
|
-
}
|
|
14
|
+
async enqueue(storage, mutationEvent) {
|
|
15
|
+
await storage.runExclusive(async (s) => {
|
|
16
|
+
const mutationEventModelDefinition = this.schema.namespaces[SYNC].models['MutationEvent'];
|
|
17
|
+
// `id` is the key for the record in the mutationEvent;
|
|
18
|
+
// `modelId` is the key for the actual record that was mutated
|
|
19
|
+
const predicate = ModelPredicateCreator.createFromAST(mutationEventModelDefinition, {
|
|
20
|
+
and: [
|
|
21
|
+
{ modelId: { eq: mutationEvent.modelId } },
|
|
22
|
+
{ id: { ne: this.inProgressMutationEventId } },
|
|
23
|
+
],
|
|
85
24
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
case 2:
|
|
99
|
-
_a.sent();
|
|
100
|
-
_a.label = 3;
|
|
101
|
-
case 3: return [4 /*yield*/, storage.delete(head)];
|
|
102
|
-
case 4:
|
|
103
|
-
_a.sent();
|
|
104
|
-
this.inProgressMutationEventId = undefined;
|
|
105
|
-
return [2 /*return*/, head];
|
|
25
|
+
// Check if there are any other records with same id
|
|
26
|
+
const [first] = await s.query(this.MutationEvent, predicate);
|
|
27
|
+
// No other record with same modelId, so enqueue
|
|
28
|
+
if (first === undefined) {
|
|
29
|
+
await s.save(mutationEvent, undefined, this.ownSymbol);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// There was an enqueued mutation for the modelId, so continue
|
|
33
|
+
const { operation: incomingMutationType } = mutationEvent;
|
|
34
|
+
if (first.operation === TransformerMutationType.CREATE) {
|
|
35
|
+
if (incomingMutationType === TransformerMutationType.DELETE) {
|
|
36
|
+
await s.delete(this.MutationEvent, predicate);
|
|
106
37
|
}
|
|
107
|
-
|
|
38
|
+
else {
|
|
39
|
+
// first gets updated with the incoming mutation's data, condition intentionally skipped
|
|
40
|
+
// we need to merge the fields for a create and update mutation to prevent
|
|
41
|
+
// data loss, since update mutations only include changed fields
|
|
42
|
+
const merged = this.mergeUserFields(first, mutationEvent);
|
|
43
|
+
await s.save(this.MutationEvent.copyOf(first, draft => {
|
|
44
|
+
draft.data = merged.data;
|
|
45
|
+
}), undefined, this.ownSymbol);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const { condition: incomingConditionJSON } = mutationEvent;
|
|
50
|
+
const incomingCondition = JSON.parse(incomingConditionJSON);
|
|
51
|
+
let merged;
|
|
52
|
+
// If no condition
|
|
53
|
+
if (Object.keys(incomingCondition).length === 0) {
|
|
54
|
+
merged = this.mergeUserFields(first, mutationEvent);
|
|
55
|
+
// delete all for model
|
|
56
|
+
await s.delete(this.MutationEvent, predicate);
|
|
57
|
+
}
|
|
58
|
+
merged = merged || mutationEvent;
|
|
59
|
+
// Enqueue new one
|
|
60
|
+
await s.save(merged, undefined, this.ownSymbol);
|
|
61
|
+
}
|
|
108
62
|
});
|
|
109
|
-
}
|
|
63
|
+
}
|
|
64
|
+
async dequeue(storage, record, recordOp) {
|
|
65
|
+
const head = await this.peek(storage);
|
|
66
|
+
if (record) {
|
|
67
|
+
await this.syncOutboxVersionsOnDequeue(storage, record, head, recordOp);
|
|
68
|
+
}
|
|
69
|
+
await storage.delete(head);
|
|
70
|
+
this.inProgressMutationEventId = undefined;
|
|
71
|
+
return head;
|
|
72
|
+
}
|
|
110
73
|
/**
|
|
111
74
|
* Doing a peek() implies that the mutation goes "inProgress"
|
|
112
75
|
*
|
|
113
76
|
* @param storage
|
|
114
77
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
case 0:
|
|
135
|
-
mutationEventModelDefinition = this.schema.namespaces[SYNC].models.MutationEvent;
|
|
136
|
-
modelId = getIdentifierValue(userModelDefinition, model);
|
|
137
|
-
return [4 /*yield*/, storage.query(this.MutationEvent, ModelPredicateCreator.createFromAST(mutationEventModelDefinition, {
|
|
138
|
-
and: { modelId: { eq: modelId } },
|
|
139
|
-
}))];
|
|
140
|
-
case 1:
|
|
141
|
-
mutationEvents = _a.sent();
|
|
142
|
-
return [2 /*return*/, mutationEvents];
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
};
|
|
147
|
-
MutationEventOutbox.prototype.getModelIds = function (storage) {
|
|
148
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
149
|
-
var mutationEvents, result;
|
|
150
|
-
return __generator(this, function (_a) {
|
|
151
|
-
switch (_a.label) {
|
|
152
|
-
case 0: return [4 /*yield*/, storage.query(this.MutationEvent)];
|
|
153
|
-
case 1:
|
|
154
|
-
mutationEvents = _a.sent();
|
|
155
|
-
result = new Set();
|
|
156
|
-
mutationEvents.forEach(function (_a) {
|
|
157
|
-
var modelId = _a.modelId;
|
|
158
|
-
return result.add(modelId);
|
|
159
|
-
});
|
|
160
|
-
return [2 /*return*/, result];
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
};
|
|
78
|
+
async peek(storage) {
|
|
79
|
+
const head = await storage.queryOne(this.MutationEvent, QueryOne.FIRST);
|
|
80
|
+
this.inProgressMutationEventId = head ? head.id : undefined;
|
|
81
|
+
return head;
|
|
82
|
+
}
|
|
83
|
+
async getForModel(storage, model, userModelDefinition) {
|
|
84
|
+
const mutationEventModelDefinition = this.schema.namespaces[SYNC].models.MutationEvent;
|
|
85
|
+
const modelId = getIdentifierValue(userModelDefinition, model);
|
|
86
|
+
const mutationEvents = await storage.query(this.MutationEvent, ModelPredicateCreator.createFromAST(mutationEventModelDefinition, {
|
|
87
|
+
and: { modelId: { eq: modelId } },
|
|
88
|
+
}));
|
|
89
|
+
return mutationEvents;
|
|
90
|
+
}
|
|
91
|
+
async getModelIds(storage) {
|
|
92
|
+
const mutationEvents = await storage.query(this.MutationEvent);
|
|
93
|
+
const result = new Set();
|
|
94
|
+
mutationEvents.forEach(({ modelId }) => result.add(modelId));
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
165
97
|
// applies _version from the AppSync mutation response to other items
|
|
166
98
|
// in the mutation queue with the same id
|
|
167
99
|
// see https://github.com/aws-amplify/amplify-js/pull/7354 for more details
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
if (!outdatedMutations.length) {
|
|
208
|
-
return [2 /*return*/];
|
|
209
|
-
}
|
|
210
|
-
reconciledMutations = outdatedMutations.map(function (m) {
|
|
211
|
-
var oldData = JSON.parse(m.data);
|
|
212
|
-
var newData = __assign(__assign({}, oldData), { _version: _version, _lastChangedAt: _lastChangedAt });
|
|
213
|
-
return _this.MutationEvent.copyOf(m, function (draft) {
|
|
214
|
-
draft.data = JSON.stringify(newData);
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
return [4 /*yield*/, storage.delete(this.MutationEvent, predicate)];
|
|
218
|
-
case 2:
|
|
219
|
-
_a.sent();
|
|
220
|
-
return [4 /*yield*/, Promise.all(reconciledMutations.map(function (m) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
221
|
-
switch (_a.label) {
|
|
222
|
-
case 0: return [4 /*yield*/, storage.save(m, undefined, this.ownSymbol)];
|
|
223
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
224
|
-
}
|
|
225
|
-
}); }); }))];
|
|
226
|
-
case 3:
|
|
227
|
-
_a.sent();
|
|
228
|
-
return [2 /*return*/];
|
|
229
|
-
}
|
|
100
|
+
async syncOutboxVersionsOnDequeue(storage, record, head, recordOp) {
|
|
101
|
+
if (head.operation !== recordOp) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const { _version, _lastChangedAt, _deleted, ..._incomingData } = record;
|
|
105
|
+
const incomingData = this.removeTimestampFields(head.model, _incomingData);
|
|
106
|
+
const data = JSON.parse(head.data);
|
|
107
|
+
if (!data) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const { _version: __version, _lastChangedAt: __lastChangedAt, _deleted: __deleted, ..._outgoingData } = data;
|
|
111
|
+
const outgoingData = this.removeTimestampFields(head.model, _outgoingData);
|
|
112
|
+
// Don't sync the version when the data in the response does not match the data
|
|
113
|
+
// in the request, i.e., when there's a handled conflict
|
|
114
|
+
//
|
|
115
|
+
// NOTE: `incomingData` contains all the fields in the record, and `outgoingData`
|
|
116
|
+
// only contains updated fields, resulting in an error when doing a comparison
|
|
117
|
+
// of two equal mutations. Fix this, or mitigate otherwise.
|
|
118
|
+
if (!valuesEqual(incomingData, outgoingData, true)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const mutationEventModelDefinition = this.schema.namespaces[SYNC].models['MutationEvent'];
|
|
122
|
+
const userModelDefinition = this.schema.namespaces['user'].models[head.model];
|
|
123
|
+
const recordId = getIdentifierValue(userModelDefinition, record);
|
|
124
|
+
const predicate = ModelPredicateCreator.createFromAST(mutationEventModelDefinition, {
|
|
125
|
+
and: [
|
|
126
|
+
{ modelId: { eq: recordId } },
|
|
127
|
+
{ id: { ne: this.inProgressMutationEventId } },
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
const outdatedMutations = await storage.query(this.MutationEvent, predicate);
|
|
131
|
+
if (!outdatedMutations.length) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const reconciledMutations = outdatedMutations.map(m => {
|
|
135
|
+
const oldData = JSON.parse(m.data);
|
|
136
|
+
const newData = { ...oldData, _version, _lastChangedAt };
|
|
137
|
+
return this.MutationEvent.copyOf(m, draft => {
|
|
138
|
+
draft.data = JSON.stringify(newData);
|
|
230
139
|
});
|
|
231
140
|
});
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
141
|
+
await storage.delete(this.MutationEvent, predicate);
|
|
142
|
+
await Promise.all(reconciledMutations.map(async (m) => await storage.save(m, undefined, this.ownSymbol)));
|
|
143
|
+
}
|
|
144
|
+
mergeUserFields(previous, current) {
|
|
145
|
+
const { _version, _lastChangedAt, _deleted, ...previousData } = JSON.parse(previous.data);
|
|
146
|
+
const { _version: __version, _lastChangedAt: __lastChangedAt, _deleted: __deleted, ...currentData } = JSON.parse(current.data);
|
|
147
|
+
const data = JSON.stringify({
|
|
148
|
+
_version,
|
|
149
|
+
_lastChangedAt,
|
|
150
|
+
_deleted,
|
|
151
|
+
...previousData,
|
|
152
|
+
...currentData,
|
|
153
|
+
});
|
|
154
|
+
return this.modelInstanceCreator(this.MutationEvent, {
|
|
155
|
+
...current,
|
|
156
|
+
data,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
241
159
|
/*
|
|
242
160
|
if a model is using custom timestamp fields
|
|
243
161
|
the custom field names will be stored in the model attributes
|
|
@@ -255,14 +173,13 @@ var MutationEventOutbox = /** @class */ (function () {
|
|
|
255
173
|
}
|
|
256
174
|
]
|
|
257
175
|
*/
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
var timestampFieldsMap = (_b = modelAttributes === null || modelAttributes === void 0 ? void 0 : modelAttributes.properties) === null || _b === void 0 ? void 0 : _b.timestamps;
|
|
176
|
+
removeTimestampFields(model, record) {
|
|
177
|
+
const CREATED_AT_DEFAULT_KEY = 'createdAt';
|
|
178
|
+
const UPDATED_AT_DEFAULT_KEY = 'updatedAt';
|
|
179
|
+
let createdTimestampKey = CREATED_AT_DEFAULT_KEY;
|
|
180
|
+
let updatedTimestampKey = UPDATED_AT_DEFAULT_KEY;
|
|
181
|
+
const modelAttributes = this.schema.namespaces[USER].models[model].attributes?.find(attr => attr.type === 'model');
|
|
182
|
+
const timestampFieldsMap = modelAttributes?.properties?.timestamps;
|
|
266
183
|
if (timestampFieldsMap) {
|
|
267
184
|
createdTimestampKey = timestampFieldsMap[CREATED_AT_DEFAULT_KEY];
|
|
268
185
|
updatedTimestampKey = timestampFieldsMap[UPDATED_AT_DEFAULT_KEY];
|
|
@@ -270,8 +187,6 @@ var MutationEventOutbox = /** @class */ (function () {
|
|
|
270
187
|
delete record[createdTimestampKey];
|
|
271
188
|
delete record[updatedTimestampKey];
|
|
272
189
|
return record;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
}());
|
|
190
|
+
}
|
|
191
|
+
}
|
|
276
192
|
export { MutationEventOutbox };
|
|
277
|
-
//# sourceMappingURL=outbox.js.map
|
|
@@ -1,43 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
export var mutationErrorMap = {
|
|
9
|
-
BadModel: function () { return false; },
|
|
10
|
-
BadRecord: function (error) {
|
|
11
|
-
var message = error.message;
|
|
1
|
+
import { resolveServiceErrorStatusCode } from '../utils';
|
|
2
|
+
const connectionTimeout = error => /^Connection failed: Connection Timeout/.test(error.message);
|
|
3
|
+
const serverError = error => resolveServiceErrorStatusCode(error) >= 500;
|
|
4
|
+
export const mutationErrorMap = {
|
|
5
|
+
BadModel: () => false,
|
|
6
|
+
BadRecord: error => {
|
|
7
|
+
const { message } = error;
|
|
12
8
|
return (/^Cannot return \w+ for [\w-_]+ type/.test(message) ||
|
|
13
9
|
/^Variable '.+' has coerced Null value for NonNull type/.test(message)); // newly required field, out of date client
|
|
14
10
|
},
|
|
15
|
-
ConfigError:
|
|
16
|
-
Transient:
|
|
17
|
-
Unauthorized:
|
|
18
|
-
|
|
19
|
-
/^Request failed with status code 401/.test(error.message);
|
|
20
|
-
},
|
|
11
|
+
ConfigError: () => false,
|
|
12
|
+
Transient: error => connectionTimeout(error) || serverError(error),
|
|
13
|
+
Unauthorized: error => error.message === 'Unauthorized' ||
|
|
14
|
+
resolveServiceErrorStatusCode(error) === 401,
|
|
21
15
|
};
|
|
22
|
-
export
|
|
23
|
-
BadModel:
|
|
24
|
-
BadRecord:
|
|
25
|
-
ConfigError:
|
|
26
|
-
Transient:
|
|
27
|
-
|
|
16
|
+
export const subscriptionErrorMap = {
|
|
17
|
+
BadModel: () => false,
|
|
18
|
+
BadRecord: () => false,
|
|
19
|
+
ConfigError: () => false,
|
|
20
|
+
Transient: observableError => {
|
|
21
|
+
const error = unwrapObservableError(observableError);
|
|
28
22
|
return connectionTimeout(error) || serverError(error);
|
|
29
23
|
},
|
|
30
|
-
Unauthorized:
|
|
31
|
-
|
|
24
|
+
Unauthorized: observableError => {
|
|
25
|
+
const error = unwrapObservableError(observableError);
|
|
32
26
|
return /Connection failed.+Unauthorized/.test(error.message);
|
|
33
27
|
},
|
|
34
28
|
};
|
|
35
|
-
export
|
|
36
|
-
BadModel:
|
|
37
|
-
BadRecord:
|
|
38
|
-
ConfigError:
|
|
39
|
-
Transient:
|
|
40
|
-
Unauthorized:
|
|
29
|
+
export const syncErrorMap = {
|
|
30
|
+
BadModel: () => false,
|
|
31
|
+
BadRecord: error => /^Cannot return \w+ for [\w-_]+ type/.test(error.message),
|
|
32
|
+
ConfigError: () => false,
|
|
33
|
+
Transient: error => connectionTimeout(error) || serverError(error),
|
|
34
|
+
Unauthorized: error => error.errorType === 'Unauthorized',
|
|
41
35
|
};
|
|
42
36
|
/**
|
|
43
37
|
* Get the first error reason of an observable.
|
|
@@ -46,9 +40,9 @@ export var syncErrorMap = {
|
|
|
46
40
|
* @param observableError an error from ZenObservable subscribe error callback
|
|
47
41
|
*/
|
|
48
42
|
function unwrapObservableError(observableError) {
|
|
49
|
-
|
|
43
|
+
const { errors: [error], } = ({
|
|
50
44
|
errors: [],
|
|
51
|
-
}
|
|
45
|
+
} = observableError);
|
|
52
46
|
return error;
|
|
53
47
|
}
|
|
54
48
|
export function getMutationErrorType(error) {
|
|
@@ -67,24 +61,12 @@ export function getSyncErrorType(error) {
|
|
|
67
61
|
* @param error The underying error to categorize.
|
|
68
62
|
*/
|
|
69
63
|
export function mapErrorToType(errorMap, error) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var matcher = errorMap[errorType];
|
|
76
|
-
if (matcher === null || matcher === void 0 ? void 0 : matcher(error)) {
|
|
77
|
-
return errorType;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
82
|
-
finally {
|
|
83
|
-
try {
|
|
84
|
-
if (errorTypes_1_1 && !errorTypes_1_1.done && (_a = errorTypes_1.return)) _a.call(errorTypes_1);
|
|
64
|
+
const errorTypes = [...Object.keys(errorMap)];
|
|
65
|
+
for (const errorType of errorTypes) {
|
|
66
|
+
const matcher = errorMap[errorType];
|
|
67
|
+
if (matcher?.(error)) {
|
|
68
|
+
return errorType;
|
|
85
69
|
}
|
|
86
|
-
finally { if (e_1) throw e_1.error; }
|
|
87
70
|
}
|
|
88
71
|
return 'Unknown';
|
|
89
72
|
}
|
|
90
|
-
//# sourceMappingURL=errorMaps.js.map
|