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