@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,12 +1,11 @@
|
|
|
1
|
-
import { __asyncValues, __awaiter, __extends, __generator, __read, __spread, __values } from "tslib";
|
|
2
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
5
3
|
import * as idb from 'idb';
|
|
6
4
|
import { isPredicateObj, isPredicateGroup, OpType, QueryOne, } from '../../types';
|
|
7
5
|
import { isPrivateMode, traverseModel, validatePredicate, inMemoryPagination, keysEqual, getStorename, isSafariCompatabilityMode, } from '../../util';
|
|
8
6
|
import { StorageAdapterBase } from './StorageAdapterBase';
|
|
9
|
-
|
|
7
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
8
|
+
const logger = new ConsoleLogger('DataStore');
|
|
10
9
|
/**
|
|
11
10
|
* The point after which queries composed of multiple simple OR conditions
|
|
12
11
|
* should scan-and-filter instead of individual queries for each condition.
|
|
@@ -28,14 +27,13 @@ var logger = new Logger('DataStore');
|
|
|
28
27
|
* etc...
|
|
29
28
|
*
|
|
30
29
|
*/
|
|
31
|
-
|
|
30
|
+
const MULTI_OR_CONDITION_SCAN_BREAKPOINT = 7;
|
|
32
31
|
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
_this.safariCompatabilityMode = false;
|
|
32
|
+
const DB_VERSION = 3;
|
|
33
|
+
class IndexedDBAdapter extends StorageAdapterBase {
|
|
34
|
+
constructor() {
|
|
35
|
+
super(...arguments);
|
|
36
|
+
this.safariCompatabilityMode = false;
|
|
39
37
|
/**
|
|
40
38
|
* Checks the given path against the browser's IndexedDB implementation for
|
|
41
39
|
* necessary compatibility transformations, applying those transforms if needed.
|
|
@@ -44,43 +42,22 @@ var IndexedDBAdapter = /** @class */ (function (_super) {
|
|
|
44
42
|
* @returns An array or string, depending on and given key,
|
|
45
43
|
* that is ensured to be compatible with the IndexedDB implementation's nuances.
|
|
46
44
|
*/
|
|
47
|
-
|
|
48
|
-
if (
|
|
45
|
+
this.canonicalKeyPath = (keyArr) => {
|
|
46
|
+
if (this.safariCompatabilityMode) {
|
|
49
47
|
return keyArr.length > 1 ? keyArr : keyArr[0];
|
|
50
48
|
}
|
|
51
49
|
return keyArr;
|
|
52
50
|
};
|
|
53
|
-
return _this;
|
|
54
51
|
//#endregion
|
|
55
52
|
}
|
|
56
53
|
// checks are called by StorageAdapterBase class
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return [4 /*yield*/, this.setSafariCompatabilityMode()];
|
|
65
|
-
case 2:
|
|
66
|
-
_a.sent();
|
|
67
|
-
return [2 /*return*/];
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
IndexedDBAdapter.prototype.preOpCheck = function () {
|
|
73
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
-
return __generator(this, function (_a) {
|
|
75
|
-
switch (_a.label) {
|
|
76
|
-
case 0: return [4 /*yield*/, this.checkPrivate()];
|
|
77
|
-
case 1:
|
|
78
|
-
_a.sent();
|
|
79
|
-
return [2 /*return*/];
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
};
|
|
54
|
+
async preSetUpChecks() {
|
|
55
|
+
await this.checkPrivate();
|
|
56
|
+
await this.setSafariCompatabilityMode();
|
|
57
|
+
}
|
|
58
|
+
async preOpCheck() {
|
|
59
|
+
await this.checkPrivate();
|
|
60
|
+
}
|
|
84
61
|
/**
|
|
85
62
|
* Initialize IndexedDB database
|
|
86
63
|
* Create new DB if one doesn't exist
|
|
@@ -90,518 +67,245 @@ var IndexedDBAdapter = /** @class */ (function (_super) {
|
|
|
90
67
|
*
|
|
91
68
|
* @returns IDB Database instance
|
|
92
69
|
*/
|
|
93
|
-
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// create new database
|
|
107
|
-
if (oldVersion === 0) {
|
|
108
|
-
Object.keys(this.schema.namespaces).forEach(function (namespaceName) {
|
|
109
|
-
var namespace = _this.schema.namespaces[namespaceName];
|
|
110
|
-
Object.keys(namespace.models).forEach(function (modelName) {
|
|
111
|
-
var storeName = getStorename(namespaceName, modelName);
|
|
112
|
-
_this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
return [2 /*return*/];
|
|
116
|
-
}
|
|
117
|
-
if (!((oldVersion === 1 || oldVersion === 2) && newVersion === 3)) return [3 /*break*/, 16];
|
|
118
|
-
_e.label = 1;
|
|
119
|
-
case 1:
|
|
120
|
-
_e.trys.push([1, 14, , 15]);
|
|
121
|
-
_e.label = 2;
|
|
122
|
-
case 2:
|
|
123
|
-
_e.trys.push([2, 11, 12, 13]);
|
|
124
|
-
_a = __values(txn.objectStoreNames), _b = _a.next();
|
|
125
|
-
_e.label = 3;
|
|
126
|
-
case 3:
|
|
127
|
-
if (!!_b.done) return [3 /*break*/, 10];
|
|
128
|
-
storeName = _b.value;
|
|
129
|
-
origStore = txn.objectStore(storeName);
|
|
130
|
-
tmpName = "tmp_" + storeName;
|
|
131
|
-
origStore.name = tmpName;
|
|
132
|
-
_c = this.getNamespaceAndModelFromStorename(storeName), namespaceName = _c.namespaceName, modelName = _c.modelName;
|
|
133
|
-
modelInCurrentSchema = modelName in this.schema.namespaces[namespaceName].models;
|
|
134
|
-
if (!modelInCurrentSchema) {
|
|
135
|
-
// delete original
|
|
136
|
-
db.deleteObjectStore(tmpName);
|
|
137
|
-
return [3 /*break*/, 9];
|
|
138
|
-
}
|
|
139
|
-
newStore = this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
140
|
-
return [4 /*yield*/, origStore.openCursor()];
|
|
141
|
-
case 4:
|
|
142
|
-
cursor = _e.sent();
|
|
143
|
-
count = 0;
|
|
144
|
-
_e.label = 5;
|
|
145
|
-
case 5:
|
|
146
|
-
if (!(cursor && cursor.value)) return [3 /*break*/, 8];
|
|
147
|
-
// we don't pass key, since they are all new entries in the new store
|
|
148
|
-
return [4 /*yield*/, newStore.put(cursor.value)];
|
|
149
|
-
case 6:
|
|
150
|
-
// we don't pass key, since they are all new entries in the new store
|
|
151
|
-
_e.sent();
|
|
152
|
-
return [4 /*yield*/, cursor.continue()];
|
|
153
|
-
case 7:
|
|
154
|
-
cursor = _e.sent();
|
|
155
|
-
count++;
|
|
156
|
-
return [3 /*break*/, 5];
|
|
157
|
-
case 8:
|
|
158
|
-
// delete original
|
|
159
|
-
db.deleteObjectStore(tmpName);
|
|
160
|
-
logger.debug(count + " " + storeName + " records migrated");
|
|
161
|
-
_e.label = 9;
|
|
162
|
-
case 9:
|
|
163
|
-
_b = _a.next();
|
|
164
|
-
return [3 /*break*/, 3];
|
|
165
|
-
case 10: return [3 /*break*/, 13];
|
|
166
|
-
case 11:
|
|
167
|
-
e_1_1 = _e.sent();
|
|
168
|
-
e_1 = { error: e_1_1 };
|
|
169
|
-
return [3 /*break*/, 13];
|
|
170
|
-
case 12:
|
|
171
|
-
try {
|
|
172
|
-
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
173
|
-
}
|
|
174
|
-
finally { if (e_1) throw e_1.error; }
|
|
175
|
-
return [7 /*endfinally*/];
|
|
176
|
-
case 13:
|
|
177
|
-
// add new models created after IndexedDB, but before migration
|
|
178
|
-
// this case may happen when a user has not opened an app for
|
|
179
|
-
// some time and a new model is added during that time
|
|
180
|
-
Object.keys(this.schema.namespaces).forEach(function (namespaceName) {
|
|
181
|
-
var namespace = _this.schema.namespaces[namespaceName];
|
|
182
|
-
var objectStoreNames = new Set(txn.objectStoreNames);
|
|
183
|
-
Object.keys(namespace.models)
|
|
184
|
-
.map(function (modelName) {
|
|
185
|
-
return [modelName, getStorename(namespaceName, modelName)];
|
|
186
|
-
})
|
|
187
|
-
.filter(function (_a) {
|
|
188
|
-
var _b = __read(_a, 2), storeName = _b[1];
|
|
189
|
-
return !objectStoreNames.has(storeName);
|
|
190
|
-
})
|
|
191
|
-
.forEach(function (_a) {
|
|
192
|
-
var _b = __read(_a, 2), modelName = _b[0], storeName = _b[1];
|
|
193
|
-
_this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
return [3 /*break*/, 15];
|
|
197
|
-
case 14:
|
|
198
|
-
error_1 = _e.sent();
|
|
199
|
-
logger.error('Error migrating IndexedDB data', error_1);
|
|
200
|
-
txn.abort();
|
|
201
|
-
throw error_1;
|
|
202
|
-
case 15: return [2 /*return*/];
|
|
203
|
-
case 16: return [2 /*return*/];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}); },
|
|
207
|
-
})];
|
|
208
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
};
|
|
213
|
-
IndexedDBAdapter.prototype._get = function (storeOrStoreName, keyArr) {
|
|
214
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
215
|
-
var index, storeName, store, result;
|
|
216
|
-
return __generator(this, function (_a) {
|
|
217
|
-
switch (_a.label) {
|
|
218
|
-
case 0:
|
|
219
|
-
if (typeof storeOrStoreName === 'string') {
|
|
220
|
-
storeName = storeOrStoreName;
|
|
221
|
-
index = this.db.transaction(storeName, 'readonly').store.index('byPk');
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
store = storeOrStoreName;
|
|
225
|
-
index = store.index('byPk');
|
|
226
|
-
}
|
|
227
|
-
return [4 /*yield*/, index.get(this.canonicalKeyPath(keyArr))];
|
|
228
|
-
case 1:
|
|
229
|
-
result = _a.sent();
|
|
230
|
-
return [2 /*return*/, result];
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
IndexedDBAdapter.prototype.clear = function () {
|
|
236
|
-
var _a;
|
|
237
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
238
|
-
return __generator(this, function (_b) {
|
|
239
|
-
switch (_b.label) {
|
|
240
|
-
case 0: return [4 /*yield*/, this.checkPrivate()];
|
|
241
|
-
case 1:
|
|
242
|
-
_b.sent();
|
|
243
|
-
(_a = this.db) === null || _a === void 0 ? void 0 : _a.close();
|
|
244
|
-
return [4 /*yield*/, idb.deleteDB(this.dbName)];
|
|
245
|
-
case 2:
|
|
246
|
-
_b.sent();
|
|
247
|
-
this.db = undefined;
|
|
248
|
-
this.initPromise = undefined;
|
|
249
|
-
return [2 /*return*/];
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
};
|
|
254
|
-
IndexedDBAdapter.prototype.save = function (model, condition) {
|
|
255
|
-
var e_2, _a;
|
|
256
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
257
|
-
var _b, storeName, set, connectionStoreNames, modelKeyValues, tx, store, fromDB, result, connectionStoreNames_1, connectionStoreNames_1_1, resItem, storeName_1, item, instance, keys, store_1, itemKeyValues, fromDB_1, opType, key, e_2_1;
|
|
258
|
-
return __generator(this, function (_c) {
|
|
259
|
-
switch (_c.label) {
|
|
260
|
-
case 0: return [4 /*yield*/, this.checkPrivate()];
|
|
261
|
-
case 1:
|
|
262
|
-
_c.sent();
|
|
263
|
-
_b = this.saveMetadata(model), storeName = _b.storeName, set = _b.set, connectionStoreNames = _b.connectionStoreNames, modelKeyValues = _b.modelKeyValues;
|
|
264
|
-
tx = this.db.transaction(__spread([storeName], Array.from(set.values())), 'readwrite');
|
|
265
|
-
store = tx.objectStore(storeName);
|
|
266
|
-
return [4 /*yield*/, this._get(store, modelKeyValues)];
|
|
267
|
-
case 2:
|
|
268
|
-
fromDB = _c.sent();
|
|
269
|
-
this.validateSaveCondition(condition, fromDB);
|
|
270
|
-
result = [];
|
|
271
|
-
_c.label = 3;
|
|
272
|
-
case 3:
|
|
273
|
-
_c.trys.push([3, 11, 12, 17]);
|
|
274
|
-
connectionStoreNames_1 = __asyncValues(connectionStoreNames);
|
|
275
|
-
_c.label = 4;
|
|
276
|
-
case 4: return [4 /*yield*/, connectionStoreNames_1.next()];
|
|
277
|
-
case 5:
|
|
278
|
-
if (!(connectionStoreNames_1_1 = _c.sent(), !connectionStoreNames_1_1.done)) return [3 /*break*/, 10];
|
|
279
|
-
resItem = connectionStoreNames_1_1.value;
|
|
280
|
-
storeName_1 = resItem.storeName, item = resItem.item, instance = resItem.instance, keys = resItem.keys;
|
|
281
|
-
store_1 = tx.objectStore(storeName_1);
|
|
282
|
-
itemKeyValues = keys.map(function (key) { return item[key]; });
|
|
283
|
-
return [4 /*yield*/, this._get(store_1, itemKeyValues)];
|
|
284
|
-
case 6:
|
|
285
|
-
fromDB_1 = _c.sent();
|
|
286
|
-
opType = fromDB_1 ? OpType.UPDATE : OpType.INSERT;
|
|
287
|
-
if (!(keysEqual(itemKeyValues, modelKeyValues) ||
|
|
288
|
-
opType === OpType.INSERT)) return [3 /*break*/, 9];
|
|
289
|
-
return [4 /*yield*/, store_1
|
|
290
|
-
.index('byPk')
|
|
291
|
-
.getKey(this.canonicalKeyPath(itemKeyValues))];
|
|
292
|
-
case 7:
|
|
293
|
-
key = _c.sent();
|
|
294
|
-
return [4 /*yield*/, store_1.put(item, key)];
|
|
295
|
-
case 8:
|
|
296
|
-
_c.sent();
|
|
297
|
-
result.push([instance, opType]);
|
|
298
|
-
_c.label = 9;
|
|
299
|
-
case 9: return [3 /*break*/, 4];
|
|
300
|
-
case 10: return [3 /*break*/, 17];
|
|
301
|
-
case 11:
|
|
302
|
-
e_2_1 = _c.sent();
|
|
303
|
-
e_2 = { error: e_2_1 };
|
|
304
|
-
return [3 /*break*/, 17];
|
|
305
|
-
case 12:
|
|
306
|
-
_c.trys.push([12, , 15, 16]);
|
|
307
|
-
if (!(connectionStoreNames_1_1 && !connectionStoreNames_1_1.done && (_a = connectionStoreNames_1.return))) return [3 /*break*/, 14];
|
|
308
|
-
return [4 /*yield*/, _a.call(connectionStoreNames_1)];
|
|
309
|
-
case 13:
|
|
310
|
-
_c.sent();
|
|
311
|
-
_c.label = 14;
|
|
312
|
-
case 14: return [3 /*break*/, 16];
|
|
313
|
-
case 15:
|
|
314
|
-
if (e_2) throw e_2.error;
|
|
315
|
-
return [7 /*endfinally*/];
|
|
316
|
-
case 16: return [7 /*endfinally*/];
|
|
317
|
-
case 17: return [4 /*yield*/, tx.done];
|
|
318
|
-
case 18:
|
|
319
|
-
_c.sent();
|
|
320
|
-
return [2 /*return*/, result];
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
};
|
|
325
|
-
IndexedDBAdapter.prototype.query = function (modelConstructor, predicate, pagination) {
|
|
326
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
327
|
-
var _a, storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, records;
|
|
328
|
-
var _this = this;
|
|
329
|
-
return __generator(this, function (_b) {
|
|
330
|
-
switch (_b.label) {
|
|
331
|
-
case 0: return [4 /*yield*/, this.checkPrivate()];
|
|
332
|
-
case 1:
|
|
333
|
-
_b.sent();
|
|
334
|
-
_a = this.queryMetadata(modelConstructor, predicate, pagination), storeName = _a.storeName, namespaceName = _a.namespaceName, queryByKey = _a.queryByKey, predicates = _a.predicates, hasSort = _a.hasSort, hasPagination = _a.hasPagination;
|
|
335
|
-
return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
336
|
-
var record, filtered, all;
|
|
337
|
-
return __generator(this, function (_a) {
|
|
338
|
-
switch (_a.label) {
|
|
339
|
-
case 0:
|
|
340
|
-
if (!queryByKey) return [3 /*break*/, 2];
|
|
341
|
-
return [4 /*yield*/, this.getByKey(storeName, queryByKey)];
|
|
342
|
-
case 1:
|
|
343
|
-
record = _a.sent();
|
|
344
|
-
return [2 /*return*/, record ? [record] : []];
|
|
345
|
-
case 2:
|
|
346
|
-
if (!predicates) return [3 /*break*/, 4];
|
|
347
|
-
return [4 /*yield*/, this.filterOnPredicate(storeName, predicates)];
|
|
348
|
-
case 3:
|
|
349
|
-
filtered = _a.sent();
|
|
350
|
-
return [2 /*return*/, this.inMemoryPagination(filtered, pagination)];
|
|
351
|
-
case 4:
|
|
352
|
-
if (!hasSort) return [3 /*break*/, 6];
|
|
353
|
-
return [4 /*yield*/, this.getAll(storeName)];
|
|
354
|
-
case 5:
|
|
355
|
-
all = _a.sent();
|
|
356
|
-
return [2 /*return*/, this.inMemoryPagination(all, pagination)];
|
|
357
|
-
case 6:
|
|
358
|
-
if (hasPagination) {
|
|
359
|
-
return [2 /*return*/, this.enginePagination(storeName, pagination)];
|
|
360
|
-
}
|
|
361
|
-
return [2 /*return*/, this.getAll(storeName)];
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
}); })()];
|
|
365
|
-
case 2:
|
|
366
|
-
records = (_b.sent());
|
|
367
|
-
return [4 /*yield*/, this.load(namespaceName, modelConstructor.name, records)];
|
|
368
|
-
case 3: return [2 /*return*/, _b.sent()];
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
};
|
|
373
|
-
IndexedDBAdapter.prototype.queryOne = function (modelConstructor, firstOrLast) {
|
|
374
|
-
if (firstOrLast === void 0) { firstOrLast = QueryOne.FIRST; }
|
|
375
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
376
|
-
var storeName, cursor, result;
|
|
377
|
-
return __generator(this, function (_a) {
|
|
378
|
-
switch (_a.label) {
|
|
379
|
-
case 0: return [4 /*yield*/, this.checkPrivate()];
|
|
380
|
-
case 1:
|
|
381
|
-
_a.sent();
|
|
382
|
-
storeName = this.getStorenameForModel(modelConstructor);
|
|
383
|
-
return [4 /*yield*/, this.db
|
|
384
|
-
.transaction([storeName], 'readonly')
|
|
385
|
-
.objectStore(storeName)
|
|
386
|
-
.openCursor(undefined, firstOrLast === QueryOne.FIRST ? 'next' : 'prev')];
|
|
387
|
-
case 2:
|
|
388
|
-
cursor = _a.sent();
|
|
389
|
-
result = cursor ? cursor.value : undefined;
|
|
390
|
-
return [2 /*return*/, result && this.modelInstanceCreator(modelConstructor, result)];
|
|
70
|
+
async initDb() {
|
|
71
|
+
return await idb.openDB(this.dbName, DB_VERSION, {
|
|
72
|
+
upgrade: async (db, oldVersion, newVersion, txn) => {
|
|
73
|
+
// create new database
|
|
74
|
+
if (oldVersion === 0) {
|
|
75
|
+
Object.keys(this.schema.namespaces).forEach(namespaceName => {
|
|
76
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
77
|
+
Object.keys(namespace.models).forEach(modelName => {
|
|
78
|
+
const storeName = getStorename(namespaceName, modelName);
|
|
79
|
+
this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
391
83
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
84
|
+
// migrate existing database to latest schema
|
|
85
|
+
if ((oldVersion === 1 || oldVersion === 2) && newVersion === 3) {
|
|
86
|
+
try {
|
|
87
|
+
for (const storeName of txn.objectStoreNames) {
|
|
88
|
+
const origStore = txn.objectStore(storeName);
|
|
89
|
+
// rename original store
|
|
90
|
+
const tmpName = `tmp_${storeName}`;
|
|
91
|
+
origStore.name = tmpName;
|
|
92
|
+
const { namespaceName, modelName } = this.getNamespaceAndModelFromStorename(storeName);
|
|
93
|
+
const modelInCurrentSchema = modelName in this.schema.namespaces[namespaceName].models;
|
|
94
|
+
if (!modelInCurrentSchema) {
|
|
95
|
+
// delete original
|
|
96
|
+
db.deleteObjectStore(tmpName);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const newStore = this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
100
|
+
let cursor = await origStore.openCursor();
|
|
101
|
+
let count = 0;
|
|
102
|
+
// Copy data from original to new
|
|
103
|
+
while (cursor && cursor.value) {
|
|
104
|
+
// we don't pass key, since they are all new entries in the new store
|
|
105
|
+
await newStore.put(cursor.value);
|
|
106
|
+
cursor = await cursor.continue();
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
// delete original
|
|
110
|
+
db.deleteObjectStore(tmpName);
|
|
111
|
+
logger.debug(`${count} ${storeName} records migrated`);
|
|
407
112
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
keyValues = this_1.getIndexKeyValuesFromModel(model);
|
|
422
|
-
_deleted = item._deleted;
|
|
423
|
-
index = store.index('byPk');
|
|
424
|
-
return [4 /*yield*/, index.getKey(this_1.canonicalKeyPath(keyValues))];
|
|
425
|
-
case 1:
|
|
426
|
-
key = _a.sent();
|
|
427
|
-
if (!!_deleted) return [3 /*break*/, 3];
|
|
428
|
-
instance = connectedModels.find(function (_a) {
|
|
429
|
-
var instance = _a.instance;
|
|
430
|
-
var instanceKeyValues = _this.getIndexKeyValuesFromModel(instance);
|
|
431
|
-
return keysEqual(instanceKeyValues, keyValues);
|
|
432
|
-
}).instance;
|
|
433
|
-
result.push([
|
|
434
|
-
instance,
|
|
435
|
-
key ? OpType.UPDATE : OpType.INSERT,
|
|
436
|
-
]);
|
|
437
|
-
return [4 /*yield*/, store.put(instance, key)];
|
|
438
|
-
case 2:
|
|
439
|
-
_a.sent();
|
|
440
|
-
return [3 /*break*/, 5];
|
|
441
|
-
case 3:
|
|
442
|
-
result.push([item, OpType.DELETE]);
|
|
443
|
-
if (!key) return [3 /*break*/, 5];
|
|
444
|
-
return [4 /*yield*/, store.delete(key)];
|
|
445
|
-
case 4:
|
|
446
|
-
_a.sent();
|
|
447
|
-
_a.label = 5;
|
|
448
|
-
case 5: return [2 /*return*/];
|
|
449
|
-
}
|
|
113
|
+
// add new models created after IndexedDB, but before migration
|
|
114
|
+
// this case may happen when a user has not opened an app for
|
|
115
|
+
// some time and a new model is added during that time
|
|
116
|
+
Object.keys(this.schema.namespaces).forEach(namespaceName => {
|
|
117
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
118
|
+
const objectStoreNames = new Set(txn.objectStoreNames);
|
|
119
|
+
Object.keys(namespace.models)
|
|
120
|
+
.map(modelName => {
|
|
121
|
+
return [modelName, getStorename(namespaceName, modelName)];
|
|
122
|
+
})
|
|
123
|
+
.filter(([, storeName]) => !objectStoreNames.has(storeName))
|
|
124
|
+
.forEach(([modelName, storeName]) => {
|
|
125
|
+
this.createObjectStoreForModel(db, namespaceName, storeName, modelName);
|
|
450
126
|
});
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
if (!!items_1_1.done) return [3 /*break*/, 6];
|
|
460
|
-
item = items_1_1.value;
|
|
461
|
-
return [5 /*yield**/, _loop_1(item)];
|
|
462
|
-
case 4:
|
|
463
|
-
_b.sent();
|
|
464
|
-
_b.label = 5;
|
|
465
|
-
case 5:
|
|
466
|
-
items_1_1 = items_1.next();
|
|
467
|
-
return [3 /*break*/, 3];
|
|
468
|
-
case 6: return [3 /*break*/, 9];
|
|
469
|
-
case 7:
|
|
470
|
-
e_3_1 = _b.sent();
|
|
471
|
-
e_3 = { error: e_3_1 };
|
|
472
|
-
return [3 /*break*/, 9];
|
|
473
|
-
case 8:
|
|
474
|
-
try {
|
|
475
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
476
|
-
}
|
|
477
|
-
finally { if (e_3) throw e_3.error; }
|
|
478
|
-
return [7 /*endfinally*/];
|
|
479
|
-
case 9: return [4 /*yield*/, txn.done];
|
|
480
|
-
case 10:
|
|
481
|
-
_b.sent();
|
|
482
|
-
return [2 /*return*/, result];
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
logger.error('Error migrating IndexedDB data', error);
|
|
131
|
+
txn.abort();
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
483
135
|
}
|
|
484
|
-
}
|
|
136
|
+
},
|
|
485
137
|
});
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
138
|
+
}
|
|
139
|
+
async _get(storeOrStoreName, keyArr) {
|
|
140
|
+
let index;
|
|
141
|
+
if (typeof storeOrStoreName === 'string') {
|
|
142
|
+
const storeName = storeOrStoreName;
|
|
143
|
+
index = this.db.transaction(storeName, 'readonly').store.index('byPk');
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const store = storeOrStoreName;
|
|
147
|
+
index = store.index('byPk');
|
|
148
|
+
}
|
|
149
|
+
const result = await index.get(this.canonicalKeyPath(keyArr));
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
async clear() {
|
|
153
|
+
await this.checkPrivate();
|
|
154
|
+
this.db?.close();
|
|
155
|
+
await idb.deleteDB(this.dbName);
|
|
156
|
+
this.db = undefined;
|
|
157
|
+
this.initPromise = undefined;
|
|
158
|
+
}
|
|
159
|
+
async save(model, condition) {
|
|
160
|
+
await this.checkPrivate();
|
|
161
|
+
const { storeName, set, connectionStoreNames, modelKeyValues } = this.saveMetadata(model);
|
|
162
|
+
const tx = this.db.transaction([storeName, ...Array.from(set.values())], 'readwrite');
|
|
163
|
+
const store = tx.objectStore(storeName);
|
|
164
|
+
const fromDB = await this._get(store, modelKeyValues);
|
|
165
|
+
this.validateSaveCondition(condition, fromDB);
|
|
166
|
+
const result = [];
|
|
167
|
+
for await (const resItem of connectionStoreNames) {
|
|
168
|
+
const { storeName, item, instance, keys } = resItem;
|
|
169
|
+
const store = tx.objectStore(storeName);
|
|
170
|
+
const itemKeyValues = keys.map(key => item[key]);
|
|
171
|
+
const fromDB = await this._get(store, itemKeyValues);
|
|
172
|
+
const opType = fromDB ? OpType.UPDATE : OpType.INSERT;
|
|
173
|
+
if (keysEqual(itemKeyValues, modelKeyValues) ||
|
|
174
|
+
opType === OpType.INSERT) {
|
|
175
|
+
const key = await store
|
|
176
|
+
.index('byPk')
|
|
177
|
+
.getKey(this.canonicalKeyPath(itemKeyValues));
|
|
178
|
+
await store.put(item, key);
|
|
179
|
+
result.push([instance, opType]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
await tx.done;
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
async query(modelConstructor, predicate, pagination) {
|
|
186
|
+
await this.checkPrivate();
|
|
187
|
+
const { storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, } = this.queryMetadata(modelConstructor, predicate, pagination);
|
|
188
|
+
const records = (await (async () => {
|
|
189
|
+
//
|
|
190
|
+
// NOTE: @svidgen explored removing this and letting query() take care of automatic
|
|
191
|
+
// index leveraging. This would eliminate some amount of very similar code.
|
|
192
|
+
// But, getAll is slightly slower than get()
|
|
193
|
+
//
|
|
194
|
+
// On Chrome:
|
|
195
|
+
// ~700ms vs ~1175ms per 10k reads.
|
|
196
|
+
//
|
|
197
|
+
// You can (and should) check my work here:
|
|
198
|
+
// https://gist.github.com/svidgen/74e55d573b19c3e5432b1b5bdf0f4d96
|
|
199
|
+
//
|
|
200
|
+
if (queryByKey) {
|
|
201
|
+
const record = await this.getByKey(storeName, queryByKey);
|
|
202
|
+
return record ? [record] : [];
|
|
203
|
+
}
|
|
204
|
+
if (predicates) {
|
|
205
|
+
const filtered = await this.filterOnPredicate(storeName, predicates);
|
|
206
|
+
return this.inMemoryPagination(filtered, pagination);
|
|
207
|
+
}
|
|
208
|
+
if (hasSort) {
|
|
209
|
+
const all = await this.getAll(storeName);
|
|
210
|
+
return this.inMemoryPagination(all, pagination);
|
|
211
|
+
}
|
|
212
|
+
if (hasPagination) {
|
|
213
|
+
return this.enginePagination(storeName, pagination);
|
|
214
|
+
}
|
|
215
|
+
return this.getAll(storeName);
|
|
216
|
+
})());
|
|
217
|
+
return await this.load(namespaceName, modelConstructor.name, records);
|
|
218
|
+
}
|
|
219
|
+
async queryOne(modelConstructor, firstOrLast = QueryOne.FIRST) {
|
|
220
|
+
await this.checkPrivate();
|
|
221
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
222
|
+
const cursor = await this.db
|
|
223
|
+
.transaction([storeName], 'readonly')
|
|
224
|
+
.objectStore(storeName)
|
|
225
|
+
.openCursor(undefined, firstOrLast === QueryOne.FIRST ? 'next' : 'prev');
|
|
226
|
+
const result = cursor ? cursor.value : undefined;
|
|
227
|
+
return result && this.modelInstanceCreator(modelConstructor, result);
|
|
228
|
+
}
|
|
229
|
+
async batchSave(modelConstructor, items) {
|
|
230
|
+
await this.checkPrivate();
|
|
231
|
+
if (items.length === 0) {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
const modelName = modelConstructor.name;
|
|
235
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
236
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
237
|
+
const result = [];
|
|
238
|
+
const txn = this.db.transaction(storeName, 'readwrite');
|
|
239
|
+
const store = txn.store;
|
|
240
|
+
for (const item of items) {
|
|
241
|
+
const model = this.modelInstanceCreator(modelConstructor, item);
|
|
242
|
+
const connectedModels = traverseModel(modelName, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
|
|
243
|
+
const keyValues = this.getIndexKeyValuesFromModel(model);
|
|
244
|
+
const { _deleted } = item;
|
|
245
|
+
const index = store.index('byPk');
|
|
246
|
+
const key = await index.getKey(this.canonicalKeyPath(keyValues));
|
|
247
|
+
if (!_deleted) {
|
|
248
|
+
const { instance } = connectedModels.find(({ instance }) => {
|
|
249
|
+
const instanceKeyValues = this.getIndexKeyValuesFromModel(instance);
|
|
250
|
+
return keysEqual(instanceKeyValues, keyValues);
|
|
251
|
+
});
|
|
252
|
+
result.push([
|
|
253
|
+
instance,
|
|
254
|
+
key ? OpType.UPDATE : OpType.INSERT,
|
|
255
|
+
]);
|
|
256
|
+
await store.put(instance, key);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
result.push([item, OpType.DELETE]);
|
|
260
|
+
if (key) {
|
|
261
|
+
await store.delete(key);
|
|
578
262
|
}
|
|
579
|
-
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
await txn.done;
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
async deleteItem(deleteQueue) {
|
|
269
|
+
const connectionStoreNames = deleteQueue.map(({ storeName }) => {
|
|
270
|
+
return storeName;
|
|
580
271
|
});
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
272
|
+
const tx = this.db.transaction([...connectionStoreNames], 'readwrite');
|
|
273
|
+
for await (const deleteItem of deleteQueue) {
|
|
274
|
+
const { storeName, items } = deleteItem;
|
|
275
|
+
const store = tx.objectStore(storeName);
|
|
276
|
+
for await (const item of items) {
|
|
277
|
+
if (item) {
|
|
278
|
+
let key;
|
|
279
|
+
if (typeof item === 'object') {
|
|
280
|
+
const keyValues = this.getIndexKeyValuesFromModel(item);
|
|
281
|
+
key = await store
|
|
282
|
+
.index('byPk')
|
|
283
|
+
.getKey(this.canonicalKeyPath(keyValues));
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
const itemKey = item.toString();
|
|
287
|
+
key = await store.index('byPk').getKey(itemKey);
|
|
288
|
+
}
|
|
289
|
+
if (key !== undefined) {
|
|
290
|
+
await store.delete(key);
|
|
291
|
+
}
|
|
601
292
|
}
|
|
602
|
-
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
//#region platform-specific helper methods
|
|
297
|
+
async checkPrivate() {
|
|
298
|
+
const isPrivate = await isPrivateMode().then(isPrivate => {
|
|
299
|
+
return isPrivate;
|
|
603
300
|
});
|
|
604
|
-
|
|
301
|
+
if (isPrivate) {
|
|
302
|
+
logger.error("IndexedDB not supported in this browser's private mode");
|
|
303
|
+
return Promise.reject("IndexedDB not supported in this browser's private mode");
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
return Promise.resolve();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
605
309
|
/**
|
|
606
310
|
* Whether the browser's implementation of IndexedDB is coercing single-field
|
|
607
311
|
* indexes to a scalar key.
|
|
@@ -612,62 +316,35 @@ var IndexedDBAdapter = /** @class */ (function (_super) {
|
|
|
612
316
|
* See PR description for reference:
|
|
613
317
|
* https://github.com/aws-amplify/amplify-js/pull/10527
|
|
614
318
|
*/
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
case 1:
|
|
624
|
-
_a.safariCompatabilityMode = _b.sent();
|
|
625
|
-
if (this.safariCompatabilityMode === true) {
|
|
626
|
-
logger.debug('IndexedDB Adapter is running in Safari Compatability Mode');
|
|
627
|
-
}
|
|
628
|
-
return [2 /*return*/];
|
|
629
|
-
}
|
|
630
|
-
});
|
|
631
|
-
});
|
|
632
|
-
};
|
|
633
|
-
IndexedDBAdapter.prototype.getNamespaceAndModelFromStorename = function (storeName) {
|
|
634
|
-
var _a = __read(storeName.split('_')), namespaceName = _a[0], modelNameArr = _a.slice(1);
|
|
319
|
+
async setSafariCompatabilityMode() {
|
|
320
|
+
this.safariCompatabilityMode = await isSafariCompatabilityMode();
|
|
321
|
+
if (this.safariCompatabilityMode === true) {
|
|
322
|
+
logger.debug('IndexedDB Adapter is running in Safari Compatability Mode');
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
getNamespaceAndModelFromStorename(storeName) {
|
|
326
|
+
const [namespaceName, ...modelNameArr] = storeName.split('_');
|
|
635
327
|
return {
|
|
636
|
-
namespaceName
|
|
328
|
+
namespaceName,
|
|
637
329
|
modelName: modelNameArr.join('_'),
|
|
638
330
|
};
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
|
|
331
|
+
}
|
|
332
|
+
createObjectStoreForModel(db, namespaceName, storeName, modelName) {
|
|
333
|
+
const store = db.createObjectStore(storeName, {
|
|
642
334
|
autoIncrement: true,
|
|
643
335
|
});
|
|
644
|
-
|
|
645
|
-
indexes.forEach(
|
|
646
|
-
var _b = __read(_a, 3), idxName = _b[0], keyPath = _b[1], options = _b[2];
|
|
336
|
+
const { indexes } = this.schema.namespaces[namespaceName].relationships[modelName];
|
|
337
|
+
indexes.forEach(([idxName, keyPath, options]) => {
|
|
647
338
|
store.createIndex(idxName, keyPath, options);
|
|
648
339
|
});
|
|
649
340
|
return store;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
return
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
});
|
|
659
|
-
});
|
|
660
|
-
};
|
|
661
|
-
IndexedDBAdapter.prototype.getAll = function (storeName) {
|
|
662
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
663
|
-
return __generator(this, function (_a) {
|
|
664
|
-
switch (_a.label) {
|
|
665
|
-
case 0: return [4 /*yield*/, this.db.getAll(storeName)];
|
|
666
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
667
|
-
}
|
|
668
|
-
});
|
|
669
|
-
});
|
|
670
|
-
};
|
|
341
|
+
}
|
|
342
|
+
async getByKey(storeName, keyValue) {
|
|
343
|
+
return await this._get(storeName, keyValue);
|
|
344
|
+
}
|
|
345
|
+
async getAll(storeName) {
|
|
346
|
+
return await this.db.getAll(storeName);
|
|
347
|
+
}
|
|
671
348
|
/**
|
|
672
349
|
* Tries to generate an index fetcher for the given predicates. Assumes
|
|
673
350
|
* that the given predicate conditions are contained by an AND group and
|
|
@@ -677,296 +354,191 @@ var IndexedDBAdapter = /** @class */ (function (_super) {
|
|
|
677
354
|
* @param predicates The predicates to try to AND together.
|
|
678
355
|
* @param transaction
|
|
679
356
|
*/
|
|
680
|
-
|
|
681
|
-
var e_6, _a, e_7, _b;
|
|
682
|
-
var _this = this;
|
|
357
|
+
matchingIndexQueries(storeName, predicates, transaction) {
|
|
683
358
|
// could be expanded later to include `exec()` and a `cardinality` estimate?
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
var predicate = predicates_1_1.value;
|
|
689
|
-
predicateIndex.set(String(predicate.field), predicate);
|
|
690
|
-
}
|
|
359
|
+
const queries = [];
|
|
360
|
+
const predicateIndex = new Map();
|
|
361
|
+
for (const predicate of predicates) {
|
|
362
|
+
predicateIndex.set(String(predicate.field), predicate);
|
|
691
363
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
var e_8, _a;
|
|
702
|
-
var idx = store.index(name_1);
|
|
703
|
-
var keypath = Array.isArray(idx.keyPath) ? idx.keyPath : [idx.keyPath];
|
|
704
|
-
var matchingPredicateValues = [];
|
|
705
|
-
try {
|
|
706
|
-
for (var keypath_1 = (e_8 = void 0, __values(keypath)), keypath_1_1 = keypath_1.next(); !keypath_1_1.done; keypath_1_1 = keypath_1.next()) {
|
|
707
|
-
var field = keypath_1_1.value;
|
|
708
|
-
var p = predicateIndex.get(field);
|
|
709
|
-
if (p && p.operand !== null && p.operand !== undefined) {
|
|
710
|
-
matchingPredicateValues.push(p.operand);
|
|
711
|
-
}
|
|
712
|
-
else {
|
|
713
|
-
break;
|
|
714
|
-
}
|
|
364
|
+
const store = transaction.objectStore(storeName);
|
|
365
|
+
for (const name of store.indexNames) {
|
|
366
|
+
const idx = store.index(name);
|
|
367
|
+
const keypath = Array.isArray(idx.keyPath) ? idx.keyPath : [idx.keyPath];
|
|
368
|
+
const matchingPredicateValues = [];
|
|
369
|
+
for (const field of keypath) {
|
|
370
|
+
const p = predicateIndex.get(field);
|
|
371
|
+
if (p && p.operand !== null && p.operand !== undefined) {
|
|
372
|
+
matchingPredicateValues.push(p.operand);
|
|
715
373
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
finally {
|
|
719
|
-
try {
|
|
720
|
-
if (keypath_1_1 && !keypath_1_1.done && (_a = keypath_1.return)) _a.call(keypath_1);
|
|
374
|
+
else {
|
|
375
|
+
break;
|
|
721
376
|
}
|
|
722
|
-
finally { if (e_8) throw e_8.error; }
|
|
723
377
|
}
|
|
724
378
|
// if we have a matching predicate field for each component of this index,
|
|
725
379
|
// we can build a query for it. otherwise, we can't.
|
|
726
380
|
if (matchingPredicateValues.length === keypath.length) {
|
|
727
381
|
// re-create a transaction, because the transaction used to fetch the
|
|
728
382
|
// indexes may no longer be active.
|
|
729
|
-
queries.push(
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
.getAll(_this.canonicalKeyPath(matchingPredicateValues));
|
|
735
|
-
});
|
|
736
|
-
}
|
|
737
|
-
};
|
|
738
|
-
try {
|
|
739
|
-
for (var _c = __values(store.indexNames), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
740
|
-
var name_1 = _d.value;
|
|
741
|
-
_loop_2(name_1);
|
|
383
|
+
queries.push(() => this.db
|
|
384
|
+
.transaction(storeName)
|
|
385
|
+
.objectStore(storeName)
|
|
386
|
+
.index(name)
|
|
387
|
+
.getAll(this.canonicalKeyPath(matchingPredicateValues)));
|
|
742
388
|
}
|
|
743
389
|
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
390
|
+
return queries;
|
|
391
|
+
}
|
|
392
|
+
async baseQueryIndex(storeName, predicates, transaction) {
|
|
393
|
+
let { predicates: predicateObjs, type } = predicates;
|
|
394
|
+
// the predicate objects we care about tend to be nested at least
|
|
395
|
+
// one level down: `{and: {or: {and: { <the predicates we want> }}}}`
|
|
396
|
+
// so, we unpack and/or groups until we find a group with more than 1
|
|
397
|
+
// child OR a child that is not a group (and is therefore a predicate "object").
|
|
398
|
+
while (predicateObjs.length === 1 &&
|
|
399
|
+
isPredicateGroup(predicateObjs[0]) &&
|
|
400
|
+
predicateObjs[0].type !== 'not') {
|
|
401
|
+
type = predicateObjs[0].type;
|
|
402
|
+
predicateObjs = predicateObjs[0].predicates;
|
|
403
|
+
}
|
|
404
|
+
const fieldPredicates = predicateObjs.filter(p => isPredicateObj(p) && p.operator === 'eq');
|
|
405
|
+
// several sub-queries could occur here. explicitly start a txn here to avoid
|
|
406
|
+
// opening/closing multiple txns.
|
|
407
|
+
const txn = transaction || this.db.transaction(storeName);
|
|
408
|
+
let result = {};
|
|
409
|
+
// `or` conditions, if usable, need to generate multiple queries. this is unlike
|
|
410
|
+
// `and` conditions, which should just be combined.
|
|
411
|
+
if (type === 'or') {
|
|
412
|
+
/**
|
|
413
|
+
* Base queries for each child group.
|
|
414
|
+
*
|
|
415
|
+
* For each child group, if it's an AND condition that results in a single
|
|
416
|
+
* subordinate "base query", we can use it. if it's any more complicated
|
|
417
|
+
* than that, it's not a simple join condition we want to use.
|
|
418
|
+
*/
|
|
419
|
+
const groupQueries = await Promise.all(predicateObjs
|
|
420
|
+
.filter(o => isPredicateGroup(o) && o.type === 'and')
|
|
421
|
+
.map(o => this.baseQueryIndex(storeName, o, txn))).then(queries => queries
|
|
422
|
+
.filter(q => q.indexedQueries.length === 1)
|
|
423
|
+
.map(i => i.indexedQueries));
|
|
424
|
+
/**
|
|
425
|
+
* Base queries for each simple child "object" (field condition).
|
|
426
|
+
*/
|
|
427
|
+
const objectQueries = predicateObjs
|
|
428
|
+
.filter(o => isPredicateObj(o))
|
|
429
|
+
.map(o => this.matchingIndexQueries(storeName, [o], txn));
|
|
430
|
+
const indexedQueries = [...groupQueries, ...objectQueries]
|
|
431
|
+
.map(q => q[0])
|
|
432
|
+
.filter(i => i);
|
|
433
|
+
// if, after hunting for base queries, we don't have exactly 1 base query
|
|
434
|
+
// for each child group + object, stop trying to optimize. we're not dealing
|
|
435
|
+
// with a simple query that fits the intended optimization path.
|
|
436
|
+
if (predicateObjs.length > indexedQueries.length) {
|
|
437
|
+
result = {
|
|
438
|
+
groupType: null,
|
|
439
|
+
indexedQueries: [],
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
result = {
|
|
444
|
+
groupType: 'or',
|
|
445
|
+
indexedQueries,
|
|
446
|
+
};
|
|
748
447
|
}
|
|
749
|
-
finally { if (e_7) throw e_7.error; }
|
|
750
448
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
result = {
|
|
798
|
-
groupType: null,
|
|
799
|
-
indexedQueries: [],
|
|
800
|
-
};
|
|
801
|
-
}
|
|
802
|
-
else {
|
|
803
|
-
result = {
|
|
804
|
-
groupType: 'or',
|
|
805
|
-
indexedQueries: indexedQueries,
|
|
806
|
-
};
|
|
807
|
-
}
|
|
808
|
-
return [3 /*break*/, 3];
|
|
809
|
-
case 2:
|
|
810
|
-
if (type === 'and') {
|
|
811
|
-
// our potential indexes or lacks thereof.
|
|
812
|
-
// note that we're only optimizing for `eq` right now.
|
|
813
|
-
result = {
|
|
814
|
-
groupType: type,
|
|
815
|
-
indexedQueries: this.matchingIndexQueries(storeName, fieldPredicates, txn),
|
|
816
|
-
};
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
result = {
|
|
820
|
-
groupType: null,
|
|
821
|
-
indexedQueries: [],
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
_a.label = 3;
|
|
825
|
-
case 3:
|
|
826
|
-
if (!!transaction) return [3 /*break*/, 5];
|
|
827
|
-
return [4 /*yield*/, txn.done];
|
|
828
|
-
case 4:
|
|
829
|
-
_a.sent();
|
|
830
|
-
_a.label = 5;
|
|
831
|
-
case 5: return [2 /*return*/, result];
|
|
832
|
-
}
|
|
833
|
-
});
|
|
834
|
-
});
|
|
835
|
-
};
|
|
836
|
-
IndexedDBAdapter.prototype.filterOnPredicate = function (storeName, predicates) {
|
|
837
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
838
|
-
var predicateObjs, type, _a, groupType, indexedQueries, candidateResults, distinctResults, indexedQueries_1, indexedQueries_1_1, query, resultGroup, resultGroup_1, resultGroup_1_1, item, distinctificationString, e_9_1, filtered;
|
|
839
|
-
var e_9, _b, e_10, _c;
|
|
840
|
-
return __generator(this, function (_d) {
|
|
841
|
-
switch (_d.label) {
|
|
842
|
-
case 0:
|
|
843
|
-
predicateObjs = predicates.predicates, type = predicates.type;
|
|
844
|
-
return [4 /*yield*/, this.baseQueryIndex(storeName, predicates)];
|
|
845
|
-
case 1:
|
|
846
|
-
_a = _d.sent(), groupType = _a.groupType, indexedQueries = _a.indexedQueries;
|
|
847
|
-
if (!(groupType === 'and' && indexedQueries.length > 0)) return [3 /*break*/, 3];
|
|
848
|
-
return [4 /*yield*/, indexedQueries[0]()];
|
|
849
|
-
case 2:
|
|
850
|
-
// each condition must be satsified, we can form a base set with any
|
|
851
|
-
// ONE of those conditions and then filter.
|
|
852
|
-
candidateResults = _d.sent();
|
|
853
|
-
return [3 /*break*/, 14];
|
|
854
|
-
case 3:
|
|
855
|
-
if (!(groupType === 'or' &&
|
|
856
|
-
indexedQueries.length > 0 &&
|
|
857
|
-
indexedQueries.length <= MULTI_OR_CONDITION_SCAN_BREAKPOINT)) return [3 /*break*/, 12];
|
|
858
|
-
distinctResults = new Map();
|
|
859
|
-
_d.label = 4;
|
|
860
|
-
case 4:
|
|
861
|
-
_d.trys.push([4, 9, 10, 11]);
|
|
862
|
-
indexedQueries_1 = __values(indexedQueries), indexedQueries_1_1 = indexedQueries_1.next();
|
|
863
|
-
_d.label = 5;
|
|
864
|
-
case 5:
|
|
865
|
-
if (!!indexedQueries_1_1.done) return [3 /*break*/, 8];
|
|
866
|
-
query = indexedQueries_1_1.value;
|
|
867
|
-
return [4 /*yield*/, query()];
|
|
868
|
-
case 6:
|
|
869
|
-
resultGroup = _d.sent();
|
|
870
|
-
try {
|
|
871
|
-
for (resultGroup_1 = (e_10 = void 0, __values(resultGroup)), resultGroup_1_1 = resultGroup_1.next(); !resultGroup_1_1.done; resultGroup_1_1 = resultGroup_1.next()) {
|
|
872
|
-
item = resultGroup_1_1.value;
|
|
873
|
-
distinctificationString = JSON.stringify(item);
|
|
874
|
-
distinctResults.set(distinctificationString, item);
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
878
|
-
finally {
|
|
879
|
-
try {
|
|
880
|
-
if (resultGroup_1_1 && !resultGroup_1_1.done && (_c = resultGroup_1.return)) _c.call(resultGroup_1);
|
|
881
|
-
}
|
|
882
|
-
finally { if (e_10) throw e_10.error; }
|
|
883
|
-
}
|
|
884
|
-
_d.label = 7;
|
|
885
|
-
case 7:
|
|
886
|
-
indexedQueries_1_1 = indexedQueries_1.next();
|
|
887
|
-
return [3 /*break*/, 5];
|
|
888
|
-
case 8: return [3 /*break*/, 11];
|
|
889
|
-
case 9:
|
|
890
|
-
e_9_1 = _d.sent();
|
|
891
|
-
e_9 = { error: e_9_1 };
|
|
892
|
-
return [3 /*break*/, 11];
|
|
893
|
-
case 10:
|
|
894
|
-
try {
|
|
895
|
-
if (indexedQueries_1_1 && !indexedQueries_1_1.done && (_b = indexedQueries_1.return)) _b.call(indexedQueries_1);
|
|
896
|
-
}
|
|
897
|
-
finally { if (e_9) throw e_9.error; }
|
|
898
|
-
return [7 /*endfinally*/];
|
|
899
|
-
case 11:
|
|
900
|
-
// we could conceivably check for special conditions and return early here.
|
|
901
|
-
// but, this is simpler and has not yet had a measurable performance impact.
|
|
902
|
-
candidateResults = Array.from(distinctResults.values());
|
|
903
|
-
return [3 /*break*/, 14];
|
|
904
|
-
case 12: return [4 /*yield*/, this.getAll(storeName)];
|
|
905
|
-
case 13:
|
|
906
|
-
// nothing intelligent we can do with `not` groups unless or until we start
|
|
907
|
-
// smashing comparison operators against indexes -- at which point we could
|
|
908
|
-
// perform some reversal here.
|
|
909
|
-
candidateResults = (_d.sent());
|
|
910
|
-
_d.label = 14;
|
|
911
|
-
case 14:
|
|
912
|
-
filtered = predicateObjs
|
|
913
|
-
? candidateResults.filter(function (m) { return validatePredicate(m, type, predicateObjs); })
|
|
914
|
-
: candidateResults;
|
|
915
|
-
return [2 /*return*/, filtered];
|
|
449
|
+
else if (type === 'and') {
|
|
450
|
+
// our potential indexes or lacks thereof.
|
|
451
|
+
// note that we're only optimizing for `eq` right now.
|
|
452
|
+
result = {
|
|
453
|
+
groupType: type,
|
|
454
|
+
indexedQueries: this.matchingIndexQueries(storeName, fieldPredicates, txn),
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
result = {
|
|
459
|
+
groupType: null,
|
|
460
|
+
indexedQueries: [],
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
// Explicitly wait for txns from index queries to complete before proceding.
|
|
464
|
+
// This helps ensure IndexedDB is in a stable, ready state. Else, subseqeuent
|
|
465
|
+
// qeuries can sometimes appear to deadlock (at least in FakeIndexedDB).
|
|
466
|
+
// (Unless we were *given* the transaction -- we'll assume the parent handles it.)
|
|
467
|
+
if (!transaction)
|
|
468
|
+
await txn.done;
|
|
469
|
+
return result;
|
|
470
|
+
}
|
|
471
|
+
async filterOnPredicate(storeName, predicates) {
|
|
472
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
473
|
+
const { groupType, indexedQueries } = await this.baseQueryIndex(storeName, predicates);
|
|
474
|
+
// where we'll accumulate candidate results, which will be filtered at the end.
|
|
475
|
+
let candidateResults;
|
|
476
|
+
// semi-naive implementation:
|
|
477
|
+
if (groupType === 'and' && indexedQueries.length > 0) {
|
|
478
|
+
// each condition must be satsified, we can form a base set with any
|
|
479
|
+
// ONE of those conditions and then filter.
|
|
480
|
+
candidateResults = await indexedQueries[0]();
|
|
481
|
+
}
|
|
482
|
+
else if (groupType === 'or' &&
|
|
483
|
+
indexedQueries.length > 0 &&
|
|
484
|
+
indexedQueries.length <= MULTI_OR_CONDITION_SCAN_BREAKPOINT) {
|
|
485
|
+
// NOTE: each condition implies a potentially distinct set. we only benefit
|
|
486
|
+
// from using indexes here if EVERY condition uses an index. if any one
|
|
487
|
+
// index requires a table scan, we gain nothing from the indexes.
|
|
488
|
+
// NOTE: results must be DISTINCT-ified if we leverage indexes.
|
|
489
|
+
const distinctResults = new Map();
|
|
490
|
+
for (const query of indexedQueries) {
|
|
491
|
+
const resultGroup = await query();
|
|
492
|
+
for (const item of resultGroup) {
|
|
493
|
+
const distinctificationString = JSON.stringify(item);
|
|
494
|
+
distinctResults.set(distinctificationString, item);
|
|
916
495
|
}
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
496
|
+
}
|
|
497
|
+
// we could conceivably check for special conditions and return early here.
|
|
498
|
+
// but, this is simpler and has not yet had a measurable performance impact.
|
|
499
|
+
candidateResults = Array.from(distinctResults.values());
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
// nothing intelligent we can do with `not` groups unless or until we start
|
|
503
|
+
// smashing comparison operators against indexes -- at which point we could
|
|
504
|
+
// perform some reversal here.
|
|
505
|
+
candidateResults = await this.getAll(storeName);
|
|
506
|
+
}
|
|
507
|
+
const filtered = predicateObjs
|
|
508
|
+
? candidateResults.filter(m => validatePredicate(m, type, predicateObjs))
|
|
509
|
+
: candidateResults;
|
|
510
|
+
return filtered;
|
|
511
|
+
}
|
|
512
|
+
inMemoryPagination(records, pagination) {
|
|
921
513
|
return inMemoryPagination(records, pagination);
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
_c.sent();
|
|
942
|
-
_c.label = 3;
|
|
943
|
-
case 3:
|
|
944
|
-
pageResults = [];
|
|
945
|
-
hasLimit = typeof limit === 'number' && limit > 0;
|
|
946
|
-
_c.label = 4;
|
|
947
|
-
case 4:
|
|
948
|
-
if (!(cursor && cursor.value)) return [3 /*break*/, 6];
|
|
949
|
-
pageResults.push(cursor.value);
|
|
950
|
-
if (hasLimit && pageResults.length === limit) {
|
|
951
|
-
return [3 /*break*/, 6];
|
|
952
|
-
}
|
|
953
|
-
return [4 /*yield*/, cursor.continue()];
|
|
954
|
-
case 5:
|
|
955
|
-
cursor = _c.sent();
|
|
956
|
-
return [3 /*break*/, 4];
|
|
957
|
-
case 6:
|
|
958
|
-
result = pageResults;
|
|
959
|
-
return [3 /*break*/, 9];
|
|
960
|
-
case 7: return [4 /*yield*/, this.db.getAll(storeName)];
|
|
961
|
-
case 8:
|
|
962
|
-
result = (_c.sent());
|
|
963
|
-
_c.label = 9;
|
|
964
|
-
case 9: return [2 /*return*/, result];
|
|
514
|
+
}
|
|
515
|
+
async enginePagination(storeName, pagination) {
|
|
516
|
+
let result;
|
|
517
|
+
if (pagination) {
|
|
518
|
+
const { page = 0, limit = 0 } = pagination;
|
|
519
|
+
const initialRecord = Math.max(0, page * limit) || 0;
|
|
520
|
+
let cursor = await this.db
|
|
521
|
+
.transaction(storeName)
|
|
522
|
+
.objectStore(storeName)
|
|
523
|
+
.openCursor();
|
|
524
|
+
if (cursor && initialRecord > 0) {
|
|
525
|
+
await cursor.advance(initialRecord);
|
|
526
|
+
}
|
|
527
|
+
const pageResults = [];
|
|
528
|
+
const hasLimit = typeof limit === 'number' && limit > 0;
|
|
529
|
+
while (cursor && cursor.value) {
|
|
530
|
+
pageResults.push(cursor.value);
|
|
531
|
+
if (hasLimit && pageResults.length === limit) {
|
|
532
|
+
break;
|
|
965
533
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
534
|
+
cursor = await cursor.continue();
|
|
535
|
+
}
|
|
536
|
+
result = pageResults;
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
result = await this.db.getAll(storeName);
|
|
540
|
+
}
|
|
541
|
+
return result;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
971
544
|
export default new IndexedDBAdapter();
|
|
972
|
-
//# sourceMappingURL=IndexedDBAdapter.js.map
|