@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
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
get: function (_target, propertyKey, receiver) {
|
|
10
|
-
var field = propertyKey;
|
|
1
|
+
class ModelSortPredicateCreator {
|
|
2
|
+
static createPredicateBuilder(modelDefinition) {
|
|
3
|
+
const { name: modelName } = modelDefinition;
|
|
4
|
+
const fieldNames = new Set(Object.keys(modelDefinition.fields));
|
|
5
|
+
let handler;
|
|
6
|
+
const predicate = new Proxy({}, (handler = {
|
|
7
|
+
get(_target, propertyKey, receiver) {
|
|
8
|
+
const field = propertyKey;
|
|
11
9
|
if (!fieldNames.has(field)) {
|
|
12
|
-
throw new Error(
|
|
10
|
+
throw new Error(`Invalid field for model. field: ${String(field)}, model: ${modelName}`);
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
const result = (sortDirection) => {
|
|
13
|
+
ModelSortPredicateCreator.sortPredicateGroupsMap
|
|
14
|
+
.get(receiver)
|
|
15
|
+
?.push({ field, sortDirection });
|
|
18
16
|
return receiver;
|
|
19
17
|
};
|
|
20
18
|
return result;
|
|
@@ -22,33 +20,30 @@ var ModelSortPredicateCreator = /** @class */ (function () {
|
|
|
22
20
|
}));
|
|
23
21
|
ModelSortPredicateCreator.sortPredicateGroupsMap.set(predicate, []);
|
|
24
22
|
return predicate;
|
|
25
|
-
}
|
|
26
|
-
|
|
23
|
+
}
|
|
24
|
+
static isValidPredicate(predicate) {
|
|
27
25
|
return ModelSortPredicateCreator.sortPredicateGroupsMap.has(predicate);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (throwOnInvalid === void 0) { throwOnInvalid = true; }
|
|
26
|
+
}
|
|
27
|
+
static getPredicates(predicate, throwOnInvalid = true) {
|
|
31
28
|
if (throwOnInvalid &&
|
|
32
29
|
!ModelSortPredicateCreator.isValidPredicate(predicate)) {
|
|
33
30
|
throw new Error('The predicate is not valid');
|
|
34
31
|
}
|
|
35
|
-
|
|
32
|
+
const predicateGroup = ModelSortPredicateCreator.sortPredicateGroupsMap.get(predicate);
|
|
36
33
|
if (predicateGroup) {
|
|
37
34
|
return predicateGroup;
|
|
38
35
|
}
|
|
39
36
|
else {
|
|
40
37
|
throw new Error('Predicate group not found');
|
|
41
38
|
}
|
|
42
|
-
}
|
|
39
|
+
}
|
|
43
40
|
// transforms cb-style predicate into Proxy
|
|
44
|
-
|
|
41
|
+
static createFromExisting(modelDefinition, existing) {
|
|
45
42
|
if (!existing || !modelDefinition) {
|
|
46
43
|
return undefined;
|
|
47
44
|
}
|
|
48
45
|
return existing(ModelSortPredicateCreator.createPredicateBuilder(modelDefinition));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
ModelSortPredicateCreator.sortPredicateGroupsMap = new WeakMap();
|
|
53
49
|
export { ModelSortPredicateCreator };
|
|
54
|
-
//# sourceMappingURL=sort.js.map
|
package/lib-esm/ssr/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Helper for converting JSON back into DataStore models (while respecting IDs)
|
|
2
2
|
export function deserializeModel(Model, init) {
|
|
3
3
|
if (Array.isArray(init)) {
|
|
4
|
-
return init.map(
|
|
4
|
+
return init.map(init => deserializeModel(Model, init));
|
|
5
5
|
}
|
|
6
6
|
// `fromJSON` is intentionally hidden from types as a "private" method (though it exists on the instance)
|
|
7
7
|
// @ts-ignore Property 'fromJSON' does not exist on type 'PersistentModelConstructor<T>'.ts(2339)
|
|
@@ -11,4 +11,3 @@ export function deserializeModel(Model, init) {
|
|
|
11
11
|
export function serializeModel(model) {
|
|
12
12
|
return JSON.parse(JSON.stringify(model));
|
|
13
13
|
}
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,26 +1,13 @@
|
|
|
1
|
-
import { __asyncValues, __awaiter, __extends, __generator, __values } from "tslib";
|
|
2
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
3
|
import AsyncStorageDatabase from './AsyncStorageDatabase';
|
|
5
4
|
import { OpType, QueryOne, } from '../../types';
|
|
6
5
|
import { DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR, traverseModel, validatePredicate, inMemoryPagination, keysEqual, getStorename, getIndexKeys, } from '../../util';
|
|
7
6
|
import { StorageAdapterBase } from './StorageAdapterBase';
|
|
8
|
-
|
|
9
|
-
__extends(AsyncStorageAdapter, _super);
|
|
10
|
-
function AsyncStorageAdapter() {
|
|
11
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
12
|
-
}
|
|
7
|
+
export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
13
8
|
// no-ops for this adapter
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return [2 /*return*/];
|
|
17
|
-
}); });
|
|
18
|
-
};
|
|
19
|
-
AsyncStorageAdapter.prototype.preOpCheck = function () {
|
|
20
|
-
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
21
|
-
return [2 /*return*/];
|
|
22
|
-
}); });
|
|
23
|
-
};
|
|
9
|
+
async preSetUpChecks() { }
|
|
10
|
+
async preOpCheck() { }
|
|
24
11
|
/**
|
|
25
12
|
* Open AsyncStorage database
|
|
26
13
|
* Create new DB if one doesn't exist
|
|
@@ -29,325 +16,114 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
|
|
|
29
16
|
*
|
|
30
17
|
* @returns AsyncStorageDatabase instance
|
|
31
18
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
AsyncStorageAdapter.prototype.batchSave = function (modelConstructor, items) {
|
|
62
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
-
var modelName, namespaceName, storeName, keys, batch, _loop_1, this_1, items_1, items_1_1, item;
|
|
64
|
-
var e_1, _a;
|
|
65
|
-
var _this = this;
|
|
66
|
-
return __generator(this, function (_b) {
|
|
67
|
-
switch (_b.label) {
|
|
68
|
-
case 0:
|
|
69
|
-
if (items.length === 0) {
|
|
70
|
-
return [2 /*return*/, []];
|
|
71
|
-
}
|
|
72
|
-
modelName = modelConstructor.name;
|
|
73
|
-
namespaceName = this.namespaceResolver(modelConstructor);
|
|
74
|
-
storeName = getStorename(namespaceName, modelName);
|
|
75
|
-
keys = getIndexKeys(this.schema.namespaces[namespaceName], modelName);
|
|
76
|
-
batch = [];
|
|
77
|
-
_loop_1 = function (item) {
|
|
78
|
-
var model = this_1.modelInstanceCreator(modelConstructor, item);
|
|
79
|
-
var connectedModels = traverseModel(modelName, model, this_1.schema.namespaces[namespaceName], this_1.modelInstanceCreator, this_1.getModelConstructorByModelName);
|
|
80
|
-
var keyValuesPath = this_1.getIndexKeyValuesPath(model);
|
|
81
|
-
var instance = connectedModels.find(function (_a) {
|
|
82
|
-
var instance = _a.instance;
|
|
83
|
-
var instanceKeyValuesPath = _this.getIndexKeyValuesPath(instance);
|
|
84
|
-
return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
|
|
85
|
-
}).instance;
|
|
86
|
-
batch.push(instance);
|
|
87
|
-
};
|
|
88
|
-
this_1 = this;
|
|
89
|
-
try {
|
|
90
|
-
for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
91
|
-
item = items_1_1.value;
|
|
92
|
-
_loop_1(item);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
96
|
-
finally {
|
|
97
|
-
try {
|
|
98
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
99
|
-
}
|
|
100
|
-
finally { if (e_1) throw e_1.error; }
|
|
101
|
-
}
|
|
102
|
-
return [4 /*yield*/, this.db.batchSave(storeName, batch, keys)];
|
|
103
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
AsyncStorageAdapter.prototype._get = function (storeName, keyArr) {
|
|
109
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
-
var itemKeyValuesPath;
|
|
111
|
-
return __generator(this, function (_a) {
|
|
112
|
-
switch (_a.label) {
|
|
113
|
-
case 0:
|
|
114
|
-
itemKeyValuesPath = keyArr.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
115
|
-
return [4 /*yield*/, this.db.get(itemKeyValuesPath, storeName)];
|
|
116
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
AsyncStorageAdapter.prototype.save = function (model, condition) {
|
|
122
|
-
var e_2, _a;
|
|
123
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
-
var _b, storeName, connectionStoreNames, modelKeyValues, fromDB, result, connectionStoreNames_1, connectionStoreNames_1_1, resItem, storeName_1, item, instance, keys, itemKeyValues, fromDB_1, opType, e_2_1;
|
|
125
|
-
return __generator(this, function (_c) {
|
|
126
|
-
switch (_c.label) {
|
|
127
|
-
case 0:
|
|
128
|
-
_b = this.saveMetadata(model), storeName = _b.storeName, connectionStoreNames = _b.connectionStoreNames, modelKeyValues = _b.modelKeyValues;
|
|
129
|
-
return [4 /*yield*/, this._get(storeName, modelKeyValues)];
|
|
130
|
-
case 1:
|
|
131
|
-
fromDB = _c.sent();
|
|
132
|
-
this.validateSaveCondition(condition, fromDB);
|
|
133
|
-
result = [];
|
|
134
|
-
_c.label = 2;
|
|
135
|
-
case 2:
|
|
136
|
-
_c.trys.push([2, 9, 10, 15]);
|
|
137
|
-
connectionStoreNames_1 = __asyncValues(connectionStoreNames);
|
|
138
|
-
_c.label = 3;
|
|
139
|
-
case 3: return [4 /*yield*/, connectionStoreNames_1.next()];
|
|
140
|
-
case 4:
|
|
141
|
-
if (!(connectionStoreNames_1_1 = _c.sent(), !connectionStoreNames_1_1.done)) return [3 /*break*/, 8];
|
|
142
|
-
resItem = connectionStoreNames_1_1.value;
|
|
143
|
-
storeName_1 = resItem.storeName, item = resItem.item, instance = resItem.instance, keys = resItem.keys;
|
|
144
|
-
itemKeyValues = keys.map(function (key) { return item[key]; });
|
|
145
|
-
return [4 /*yield*/, this._get(storeName_1, itemKeyValues)];
|
|
146
|
-
case 5:
|
|
147
|
-
fromDB_1 = _c.sent();
|
|
148
|
-
opType = fromDB_1 ? OpType.UPDATE : OpType.INSERT;
|
|
149
|
-
if (!(keysEqual(itemKeyValues, modelKeyValues) ||
|
|
150
|
-
opType === OpType.INSERT)) return [3 /*break*/, 7];
|
|
151
|
-
return [4 /*yield*/, this.db.save(item, storeName_1, keys, itemKeyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR))];
|
|
152
|
-
case 6:
|
|
153
|
-
_c.sent();
|
|
154
|
-
result.push([instance, opType]);
|
|
155
|
-
_c.label = 7;
|
|
156
|
-
case 7: return [3 /*break*/, 3];
|
|
157
|
-
case 8: return [3 /*break*/, 15];
|
|
158
|
-
case 9:
|
|
159
|
-
e_2_1 = _c.sent();
|
|
160
|
-
e_2 = { error: e_2_1 };
|
|
161
|
-
return [3 /*break*/, 15];
|
|
162
|
-
case 10:
|
|
163
|
-
_c.trys.push([10, , 13, 14]);
|
|
164
|
-
if (!(connectionStoreNames_1_1 && !connectionStoreNames_1_1.done && (_a = connectionStoreNames_1.return))) return [3 /*break*/, 12];
|
|
165
|
-
return [4 /*yield*/, _a.call(connectionStoreNames_1)];
|
|
166
|
-
case 11:
|
|
167
|
-
_c.sent();
|
|
168
|
-
_c.label = 12;
|
|
169
|
-
case 12: return [3 /*break*/, 14];
|
|
170
|
-
case 13:
|
|
171
|
-
if (e_2) throw e_2.error;
|
|
172
|
-
return [7 /*endfinally*/];
|
|
173
|
-
case 14: return [7 /*endfinally*/];
|
|
174
|
-
case 15: return [2 /*return*/, result];
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
};
|
|
179
|
-
AsyncStorageAdapter.prototype.query = function (modelConstructor, predicate, pagination) {
|
|
180
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
181
|
-
var _a, storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, records;
|
|
182
|
-
var _this = this;
|
|
183
|
-
return __generator(this, function (_b) {
|
|
184
|
-
switch (_b.label) {
|
|
185
|
-
case 0:
|
|
186
|
-
_a = this.queryMetadata(modelConstructor, predicate, pagination), storeName = _a.storeName, namespaceName = _a.namespaceName, queryByKey = _a.queryByKey, predicates = _a.predicates, hasSort = _a.hasSort, hasPagination = _a.hasPagination;
|
|
187
|
-
return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
188
|
-
var keyValues, record, filtered, all;
|
|
189
|
-
return __generator(this, function (_a) {
|
|
190
|
-
switch (_a.label) {
|
|
191
|
-
case 0:
|
|
192
|
-
if (!queryByKey) return [3 /*break*/, 2];
|
|
193
|
-
keyValues = queryByKey.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
194
|
-
return [4 /*yield*/, this.getByKey(storeName, keyValues)];
|
|
195
|
-
case 1:
|
|
196
|
-
record = _a.sent();
|
|
197
|
-
return [2 /*return*/, record ? [record] : []];
|
|
198
|
-
case 2:
|
|
199
|
-
if (!predicates) return [3 /*break*/, 4];
|
|
200
|
-
return [4 /*yield*/, this.filterOnPredicate(storeName, predicates)];
|
|
201
|
-
case 3:
|
|
202
|
-
filtered = _a.sent();
|
|
203
|
-
return [2 /*return*/, this.inMemoryPagination(filtered, pagination)];
|
|
204
|
-
case 4:
|
|
205
|
-
if (!(hasSort || hasPagination)) return [3 /*break*/, 6];
|
|
206
|
-
return [4 /*yield*/, this.getAll(storeName)];
|
|
207
|
-
case 5:
|
|
208
|
-
all = _a.sent();
|
|
209
|
-
return [2 /*return*/, this.inMemoryPagination(all, pagination)];
|
|
210
|
-
case 6: return [2 /*return*/, this.getAll(storeName)];
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
}); })()];
|
|
214
|
-
case 1:
|
|
215
|
-
records = (_b.sent());
|
|
216
|
-
return [4 /*yield*/, this.load(namespaceName, modelConstructor.name, records)];
|
|
217
|
-
case 2: return [2 /*return*/, _b.sent()];
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
};
|
|
222
|
-
AsyncStorageAdapter.prototype.getByKey = function (storeName, keyValuePath) {
|
|
223
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
224
|
-
return __generator(this, function (_a) {
|
|
225
|
-
switch (_a.label) {
|
|
226
|
-
case 0: return [4 /*yield*/, this.db.get(keyValuePath, storeName)];
|
|
227
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
AsyncStorageAdapter.prototype.getAll = function (storeName) {
|
|
233
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
234
|
-
return __generator(this, function (_a) {
|
|
235
|
-
switch (_a.label) {
|
|
236
|
-
case 0: return [4 /*yield*/, this.db.getAll(storeName)];
|
|
237
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
};
|
|
242
|
-
AsyncStorageAdapter.prototype.filterOnPredicate = function (storeName, predicates) {
|
|
243
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
244
|
-
var predicateObjs, type, all, filtered;
|
|
245
|
-
return __generator(this, function (_a) {
|
|
246
|
-
switch (_a.label) {
|
|
247
|
-
case 0:
|
|
248
|
-
predicateObjs = predicates.predicates, type = predicates.type;
|
|
249
|
-
return [4 /*yield*/, this.getAll(storeName)];
|
|
250
|
-
case 1:
|
|
251
|
-
all = _a.sent();
|
|
252
|
-
filtered = predicateObjs
|
|
253
|
-
? all.filter(function (m) { return validatePredicate(m, type, predicateObjs); })
|
|
254
|
-
: all;
|
|
255
|
-
return [2 /*return*/, filtered];
|
|
256
|
-
}
|
|
19
|
+
async initDb() {
|
|
20
|
+
const db = new AsyncStorageDatabase();
|
|
21
|
+
await db.init();
|
|
22
|
+
return db;
|
|
23
|
+
}
|
|
24
|
+
async clear() {
|
|
25
|
+
await this.db.clear();
|
|
26
|
+
this.db = undefined;
|
|
27
|
+
this.initPromise = undefined;
|
|
28
|
+
}
|
|
29
|
+
async batchSave(modelConstructor, items) {
|
|
30
|
+
if (items.length === 0) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const modelName = modelConstructor.name;
|
|
34
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
35
|
+
const storeName = getStorename(namespaceName, modelName);
|
|
36
|
+
const keys = getIndexKeys(this.schema.namespaces[namespaceName], modelName);
|
|
37
|
+
const batch = [];
|
|
38
|
+
for (const item of items) {
|
|
39
|
+
const model = this.modelInstanceCreator(modelConstructor, item);
|
|
40
|
+
const connectedModels = traverseModel(modelName, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
|
|
41
|
+
const keyValuesPath = this.getIndexKeyValuesPath(model);
|
|
42
|
+
const { instance } = connectedModels.find(({ instance }) => {
|
|
43
|
+
const instanceKeyValuesPath = this.getIndexKeyValuesPath(instance);
|
|
44
|
+
return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
|
|
257
45
|
});
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
46
|
+
batch.push(instance);
|
|
47
|
+
}
|
|
48
|
+
return await this.db.batchSave(storeName, batch, keys);
|
|
49
|
+
}
|
|
50
|
+
async _get(storeName, keyArr) {
|
|
51
|
+
const itemKeyValuesPath = keyArr.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
52
|
+
return await this.db.get(itemKeyValuesPath, storeName);
|
|
53
|
+
}
|
|
54
|
+
async save(model, condition) {
|
|
55
|
+
const { storeName, connectionStoreNames, modelKeyValues } = this.saveMetadata(model);
|
|
56
|
+
const fromDB = await this._get(storeName, modelKeyValues);
|
|
57
|
+
this.validateSaveCondition(condition, fromDB);
|
|
58
|
+
const result = [];
|
|
59
|
+
for await (const resItem of connectionStoreNames) {
|
|
60
|
+
const { storeName, item, instance, keys } = resItem;
|
|
61
|
+
const itemKeyValues = keys.map(key => item[key]);
|
|
62
|
+
const fromDB = await this._get(storeName, itemKeyValues);
|
|
63
|
+
const opType = fromDB ? OpType.UPDATE : OpType.INSERT;
|
|
64
|
+
if (keysEqual(itemKeyValues, modelKeyValues) ||
|
|
65
|
+
opType === OpType.INSERT) {
|
|
66
|
+
await this.db.save(item, storeName, keys, itemKeyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR));
|
|
67
|
+
result.push([instance, opType]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
async query(modelConstructor, predicate, pagination) {
|
|
73
|
+
const { storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, } = this.queryMetadata(modelConstructor, predicate, pagination);
|
|
74
|
+
const records = (await (async () => {
|
|
75
|
+
if (queryByKey) {
|
|
76
|
+
const keyValues = queryByKey.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
77
|
+
const record = await this.getByKey(storeName, keyValues);
|
|
78
|
+
return record ? [record] : [];
|
|
79
|
+
}
|
|
80
|
+
if (predicates) {
|
|
81
|
+
const filtered = await this.filterOnPredicate(storeName, predicates);
|
|
82
|
+
return this.inMemoryPagination(filtered, pagination);
|
|
83
|
+
}
|
|
84
|
+
if (hasSort || hasPagination) {
|
|
85
|
+
const all = await this.getAll(storeName);
|
|
86
|
+
return this.inMemoryPagination(all, pagination);
|
|
87
|
+
}
|
|
88
|
+
return this.getAll(storeName);
|
|
89
|
+
})());
|
|
90
|
+
return await this.load(namespaceName, modelConstructor.name, records);
|
|
91
|
+
}
|
|
92
|
+
async getByKey(storeName, keyValuePath) {
|
|
93
|
+
return await this.db.get(keyValuePath, storeName);
|
|
94
|
+
}
|
|
95
|
+
async getAll(storeName) {
|
|
96
|
+
return await this.db.getAll(storeName);
|
|
97
|
+
}
|
|
98
|
+
async filterOnPredicate(storeName, predicates) {
|
|
99
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
100
|
+
const all = await this.getAll(storeName);
|
|
101
|
+
const filtered = predicateObjs
|
|
102
|
+
? all.filter(m => validatePredicate(m, type, predicateObjs))
|
|
103
|
+
: all;
|
|
104
|
+
return filtered;
|
|
105
|
+
}
|
|
106
|
+
inMemoryPagination(records, pagination) {
|
|
261
107
|
return inMemoryPagination(records, pagination);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
};
|
|
279
|
-
AsyncStorageAdapter.prototype.deleteItem = function (deleteQueue) {
|
|
280
|
-
var e_3, _a, e_4, _b;
|
|
281
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
282
|
-
var _c, _d, deleteItem, storeName, items, items_2, items_2_1, item, keyValuesPath, e_4_1, e_3_1;
|
|
283
|
-
return __generator(this, function (_e) {
|
|
284
|
-
switch (_e.label) {
|
|
285
|
-
case 0:
|
|
286
|
-
_e.trys.push([0, 17, 18, 23]);
|
|
287
|
-
_c = __asyncValues(deleteQueue);
|
|
288
|
-
_e.label = 1;
|
|
289
|
-
case 1: return [4 /*yield*/, _c.next()];
|
|
290
|
-
case 2:
|
|
291
|
-
if (!(_d = _e.sent(), !_d.done)) return [3 /*break*/, 16];
|
|
292
|
-
deleteItem = _d.value;
|
|
293
|
-
storeName = deleteItem.storeName, items = deleteItem.items;
|
|
294
|
-
_e.label = 3;
|
|
295
|
-
case 3:
|
|
296
|
-
_e.trys.push([3, 9, 10, 15]);
|
|
297
|
-
items_2 = __asyncValues(items);
|
|
298
|
-
_e.label = 4;
|
|
299
|
-
case 4: return [4 /*yield*/, items_2.next()];
|
|
300
|
-
case 5:
|
|
301
|
-
if (!(items_2_1 = _e.sent(), !items_2_1.done)) return [3 /*break*/, 8];
|
|
302
|
-
item = items_2_1.value;
|
|
303
|
-
if (!item) return [3 /*break*/, 7];
|
|
304
|
-
if (!(typeof item === 'object')) return [3 /*break*/, 7];
|
|
305
|
-
keyValuesPath = this.getIndexKeyValuesPath(item);
|
|
306
|
-
return [4 /*yield*/, this.db.delete(keyValuesPath, storeName)];
|
|
307
|
-
case 6:
|
|
308
|
-
_e.sent();
|
|
309
|
-
_e.label = 7;
|
|
310
|
-
case 7: return [3 /*break*/, 4];
|
|
311
|
-
case 8: return [3 /*break*/, 15];
|
|
312
|
-
case 9:
|
|
313
|
-
e_4_1 = _e.sent();
|
|
314
|
-
e_4 = { error: e_4_1 };
|
|
315
|
-
return [3 /*break*/, 15];
|
|
316
|
-
case 10:
|
|
317
|
-
_e.trys.push([10, , 13, 14]);
|
|
318
|
-
if (!(items_2_1 && !items_2_1.done && (_b = items_2.return))) return [3 /*break*/, 12];
|
|
319
|
-
return [4 /*yield*/, _b.call(items_2)];
|
|
320
|
-
case 11:
|
|
321
|
-
_e.sent();
|
|
322
|
-
_e.label = 12;
|
|
323
|
-
case 12: return [3 /*break*/, 14];
|
|
324
|
-
case 13:
|
|
325
|
-
if (e_4) throw e_4.error;
|
|
326
|
-
return [7 /*endfinally*/];
|
|
327
|
-
case 14: return [7 /*endfinally*/];
|
|
328
|
-
case 15: return [3 /*break*/, 1];
|
|
329
|
-
case 16: return [3 /*break*/, 23];
|
|
330
|
-
case 17:
|
|
331
|
-
e_3_1 = _e.sent();
|
|
332
|
-
e_3 = { error: e_3_1 };
|
|
333
|
-
return [3 /*break*/, 23];
|
|
334
|
-
case 18:
|
|
335
|
-
_e.trys.push([18, , 21, 22]);
|
|
336
|
-
if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 20];
|
|
337
|
-
return [4 /*yield*/, _a.call(_c)];
|
|
338
|
-
case 19:
|
|
339
|
-
_e.sent();
|
|
340
|
-
_e.label = 20;
|
|
341
|
-
case 20: return [3 /*break*/, 22];
|
|
342
|
-
case 21:
|
|
343
|
-
if (e_3) throw e_3.error;
|
|
344
|
-
return [7 /*endfinally*/];
|
|
345
|
-
case 22: return [7 /*endfinally*/];
|
|
346
|
-
case 23: return [2 /*return*/];
|
|
108
|
+
}
|
|
109
|
+
async queryOne(modelConstructor, firstOrLast = QueryOne.FIRST) {
|
|
110
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
111
|
+
const result = await this.db.getOne(firstOrLast, storeName);
|
|
112
|
+
return result && this.modelInstanceCreator(modelConstructor, result);
|
|
113
|
+
}
|
|
114
|
+
async deleteItem(deleteQueue) {
|
|
115
|
+
for await (const deleteItem of deleteQueue) {
|
|
116
|
+
const { storeName, items } = deleteItem;
|
|
117
|
+
for await (const item of items) {
|
|
118
|
+
if (item) {
|
|
119
|
+
if (typeof item === 'object') {
|
|
120
|
+
const keyValuesPath = this.getIndexKeyValuesPath(item);
|
|
121
|
+
await this.db.delete(keyValuesPath, storeName);
|
|
122
|
+
}
|
|
347
123
|
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
351
127
|
//#region platform-specific helper methods
|
|
352
128
|
/**
|
|
353
129
|
* Retrieves concatenated primary key values from a model
|
|
@@ -355,11 +131,8 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
|
|
|
355
131
|
* @param model
|
|
356
132
|
* @returns
|
|
357
133
|
*/
|
|
358
|
-
|
|
134
|
+
getIndexKeyValuesPath(model) {
|
|
359
135
|
return this.getIndexKeyValuesFromModel(model).join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
}(StorageAdapterBase));
|
|
363
|
-
export { AsyncStorageAdapter };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
364
138
|
export default new AsyncStorageAdapter();
|
|
365
|
-
//# sourceMappingURL=AsyncStorageAdapter.js.map
|