@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.
Files changed (187) hide show
  1. package/README.md +4 -0
  2. package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
  3. package/lib/authModeStrategies/index.js +3 -3
  4. package/lib/authModeStrategies/multiAuthStrategy.js +38 -53
  5. package/lib/datastore/datastore.d.ts +4 -5
  6. package/lib/datastore/datastore.js +929 -1284
  7. package/lib/index.d.ts +1 -1
  8. package/lib/index.js +26 -13
  9. package/lib/predicates/index.js +54 -69
  10. package/lib/predicates/next.d.ts +2 -2
  11. package/lib/predicates/next.js +313 -462
  12. package/lib/predicates/sort.js +24 -28
  13. package/lib/ssr/index.js +2 -2
  14. package/lib/storage/adapter/AsyncStorageAdapter.js +120 -342
  15. package/lib/storage/adapter/AsyncStorageDatabase.js +217 -421
  16. package/lib/storage/adapter/InMemoryStore.js +28 -51
  17. package/lib/storage/adapter/InMemoryStore.native.js +5 -3
  18. package/lib/storage/adapter/IndexedDBAdapter.js +466 -871
  19. package/lib/storage/adapter/StorageAdapterBase.js +180 -330
  20. package/lib/storage/adapter/getDefaultAdapter/index.js +8 -10
  21. package/lib/storage/adapter/getDefaultAdapter/index.native.js +5 -4
  22. package/lib/storage/adapter/index.js +0 -1
  23. package/lib/storage/relationship.js +177 -253
  24. package/lib/storage/storage.d.ts +4 -4
  25. package/lib/storage/storage.js +255 -433
  26. package/lib/sync/datastoreConnectivity.d.ts +2 -2
  27. package/lib/sync/datastoreConnectivity.js +29 -39
  28. package/lib/sync/datastoreReachability/index.d.ts +1 -3
  29. package/lib/sync/datastoreReachability/index.js +3 -3
  30. package/lib/sync/datastoreReachability/index.native.d.ts +1 -3
  31. package/lib/sync/datastoreReachability/index.native.js +4 -5
  32. package/lib/sync/index.d.ts +2 -2
  33. package/lib/sync/index.js +522 -827
  34. package/lib/sync/merger.js +31 -63
  35. package/lib/sync/outbox.js +148 -232
  36. package/lib/sync/processors/errorMaps.d.ts +1 -1
  37. package/lib/sync/processors/errorMaps.js +30 -47
  38. package/lib/sync/processors/mutation.d.ts +2 -2
  39. package/lib/sync/processors/mutation.js +343 -502
  40. package/lib/sync/processors/subscription.d.ts +5 -2
  41. package/lib/sync/processors/subscription.js +283 -437
  42. package/lib/sync/processors/sync.d.ts +2 -2
  43. package/lib/sync/processors/sync.js +279 -404
  44. package/lib/sync/utils.d.ts +5 -4
  45. package/lib/sync/utils.js +267 -320
  46. package/lib/tsconfig.tsbuildinfo +1 -0
  47. package/lib/types.d.ts +138 -140
  48. package/lib/types.js +17 -24
  49. package/lib/util.d.ts +9 -17
  50. package/lib/util.js +387 -511
  51. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -2
  52. package/lib-esm/authModeStrategies/index.js +0 -1
  53. package/lib-esm/authModeStrategies/multiAuthStrategy.js +35 -52
  54. package/lib-esm/datastore/datastore.d.ts +4 -5
  55. package/lib-esm/datastore/datastore.js +888 -1247
  56. package/lib-esm/index.d.ts +1 -1
  57. package/lib-esm/index.js +6 -7
  58. package/lib-esm/predicates/index.js +53 -70
  59. package/lib-esm/predicates/next.d.ts +2 -2
  60. package/lib-esm/predicates/next.js +306 -459
  61. package/lib-esm/predicates/sort.js +23 -28
  62. package/lib-esm/ssr/index.js +1 -2
  63. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -338
  64. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -416
  65. package/lib-esm/storage/adapter/InMemoryStore.js +27 -52
  66. package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
  67. package/lib-esm/storage/adapter/IndexedDBAdapter.js +438 -866
  68. package/lib-esm/storage/adapter/StorageAdapterBase.js +173 -325
  69. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -6
  70. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -2
  71. package/lib-esm/storage/adapter/index.js +1 -1
  72. package/lib-esm/storage/relationship.js +173 -251
  73. package/lib-esm/storage/storage.d.ts +4 -4
  74. package/lib-esm/storage/storage.js +242 -424
  75. package/lib-esm/sync/datastoreConnectivity.d.ts +2 -2
  76. package/lib-esm/sync/datastoreConnectivity.js +28 -39
  77. package/lib-esm/sync/datastoreReachability/index.d.ts +1 -3
  78. package/lib-esm/sync/datastoreReachability/index.js +2 -3
  79. package/lib-esm/sync/datastoreReachability/index.native.d.ts +1 -3
  80. package/lib-esm/sync/datastoreReachability/index.native.js +3 -4
  81. package/lib-esm/sync/index.d.ts +2 -2
  82. package/lib-esm/sync/index.js +502 -812
  83. package/lib-esm/sync/merger.js +28 -61
  84. package/lib-esm/sync/outbox.js +143 -228
  85. package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
  86. package/lib-esm/sync/processors/errorMaps.js +32 -50
  87. package/lib-esm/sync/processors/mutation.d.ts +2 -2
  88. package/lib-esm/sync/processors/mutation.js +329 -490
  89. package/lib-esm/sync/processors/subscription.d.ts +5 -2
  90. package/lib-esm/sync/processors/subscription.js +266 -421
  91. package/lib-esm/sync/processors/sync.d.ts +2 -2
  92. package/lib-esm/sync/processors/sync.js +271 -397
  93. package/lib-esm/sync/utils.d.ts +5 -4
  94. package/lib-esm/sync/utils.js +252 -307
  95. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  96. package/lib-esm/types.d.ts +138 -140
  97. package/lib-esm/types.js +16 -25
  98. package/lib-esm/util.d.ts +9 -17
  99. package/lib-esm/util.js +335 -497
  100. package/package.json +31 -26
  101. package/src/authModeStrategies/multiAuthStrategy.ts +15 -12
  102. package/src/datastore/datastore.ts +36 -35
  103. package/src/predicates/sort.ts +3 -1
  104. package/src/storage/adapter/InMemoryStore.ts +1 -1
  105. package/src/storage/adapter/IndexedDBAdapter.ts +2 -2
  106. package/src/storage/adapter/StorageAdapterBase.ts +2 -2
  107. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -4
  108. package/src/storage/storage.ts +29 -24
  109. package/src/sync/datastoreConnectivity.ts +6 -6
  110. package/src/sync/datastoreReachability/index.native.ts +5 -3
  111. package/src/sync/datastoreReachability/index.ts +1 -1
  112. package/src/sync/index.ts +79 -89
  113. package/src/sync/processors/errorMaps.ts +7 -7
  114. package/src/sync/processors/mutation.ts +19 -13
  115. package/src/sync/processors/subscription.ts +221 -295
  116. package/src/sync/processors/sync.ts +11 -8
  117. package/src/sync/utils.ts +30 -15
  118. package/src/types.ts +4 -8
  119. package/src/util.ts +46 -9
  120. package/lib/.tsbuildinfo +0 -3
  121. package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  122. package/lib/authModeStrategies/index.js.map +0 -1
  123. package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
  124. package/lib/datastore/datastore.js.map +0 -1
  125. package/lib/index.js.map +0 -1
  126. package/lib/predicates/index.js.map +0 -1
  127. package/lib/predicates/next.js.map +0 -1
  128. package/lib/predicates/sort.js.map +0 -1
  129. package/lib/ssr/index.js.map +0 -1
  130. package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  131. package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  132. package/lib/storage/adapter/InMemoryStore.js.map +0 -1
  133. package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
  134. package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
  135. package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
  136. package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  137. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  138. package/lib/storage/adapter/index.js.map +0 -1
  139. package/lib/storage/relationship.js.map +0 -1
  140. package/lib/storage/storage.js.map +0 -1
  141. package/lib/sync/datastoreConnectivity.js.map +0 -1
  142. package/lib/sync/datastoreReachability/index.js.map +0 -1
  143. package/lib/sync/datastoreReachability/index.native.js.map +0 -1
  144. package/lib/sync/index.js.map +0 -1
  145. package/lib/sync/merger.js.map +0 -1
  146. package/lib/sync/outbox.js.map +0 -1
  147. package/lib/sync/processors/errorMaps.js.map +0 -1
  148. package/lib/sync/processors/mutation.js.map +0 -1
  149. package/lib/sync/processors/subscription.js.map +0 -1
  150. package/lib/sync/processors/sync.js.map +0 -1
  151. package/lib/sync/utils.js.map +0 -1
  152. package/lib/types.js.map +0 -1
  153. package/lib/util.js.map +0 -1
  154. package/lib-esm/.tsbuildinfo +0 -3
  155. package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  156. package/lib-esm/authModeStrategies/index.js.map +0 -1
  157. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
  158. package/lib-esm/datastore/datastore.js.map +0 -1
  159. package/lib-esm/index.js.map +0 -1
  160. package/lib-esm/predicates/index.js.map +0 -1
  161. package/lib-esm/predicates/next.js.map +0 -1
  162. package/lib-esm/predicates/sort.js.map +0 -1
  163. package/lib-esm/ssr/index.js.map +0 -1
  164. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  165. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  166. package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
  167. package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
  168. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
  169. package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
  170. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  171. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  172. package/lib-esm/storage/adapter/index.js.map +0 -1
  173. package/lib-esm/storage/relationship.js.map +0 -1
  174. package/lib-esm/storage/storage.js.map +0 -1
  175. package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
  176. package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
  177. package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
  178. package/lib-esm/sync/index.js.map +0 -1
  179. package/lib-esm/sync/merger.js.map +0 -1
  180. package/lib-esm/sync/outbox.js.map +0 -1
  181. package/lib-esm/sync/processors/errorMaps.js.map +0 -1
  182. package/lib-esm/sync/processors/mutation.js.map +0 -1
  183. package/lib-esm/sync/processors/subscription.js.map +0 -1
  184. package/lib-esm/sync/processors/sync.js.map +0 -1
  185. package/lib-esm/sync/utils.js.map +0 -1
  186. package/lib-esm/types.js.map +0 -1
  187. 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
- var logger = new Logger('DataStore');
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
- var MULTI_OR_CONDITION_SCAN_BREAKPOINT = 7;
30
+ const MULTI_OR_CONDITION_SCAN_BREAKPOINT = 7;
32
31
  //
33
- var DB_VERSION = 3;
34
- var IndexedDBAdapter = /** @class */ (function (_super) {
35
- __extends(IndexedDBAdapter, _super);
36
- function IndexedDBAdapter() {
37
- var _this = _super !== null && _super.apply(this, arguments) || this;
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
- _this.canonicalKeyPath = function (keyArr) {
48
- if (_this.safariCompatabilityMode) {
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
- IndexedDBAdapter.prototype.preSetUpChecks = function () {
58
- return __awaiter(this, void 0, void 0, function () {
59
- return __generator(this, function (_a) {
60
- switch (_a.label) {
61
- case 0: return [4 /*yield*/, this.checkPrivate()];
62
- case 1:
63
- _a.sent();
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
- IndexedDBAdapter.prototype.initDb = function () {
94
- return __awaiter(this, void 0, void 0, function () {
95
- var _this = this;
96
- return __generator(this, function (_a) {
97
- switch (_a.label) {
98
- case 0: return [4 /*yield*/, idb.openDB(this.dbName, DB_VERSION, {
99
- upgrade: function (db, oldVersion, newVersion, txn) { return __awaiter(_this, void 0, void 0, function () {
100
- var _a, _b, storeName, origStore, tmpName, _c, namespaceName, modelName, modelInCurrentSchema, newStore, cursor, count, e_1_1, error_1;
101
- var e_1, _d;
102
- var _this = this;
103
- return __generator(this, function (_e) {
104
- switch (_e.label) {
105
- case 0:
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
- IndexedDBAdapter.prototype.batchSave = function (modelConstructor, items) {
396
- return __awaiter(this, void 0, void 0, function () {
397
- var modelName, namespaceName, storeName, result, txn, store, _loop_1, this_1, items_1, items_1_1, item, e_3_1;
398
- var e_3, _a;
399
- var _this = this;
400
- return __generator(this, function (_b) {
401
- switch (_b.label) {
402
- case 0: return [4 /*yield*/, this.checkPrivate()];
403
- case 1:
404
- _b.sent();
405
- if (items.length === 0) {
406
- return [2 /*return*/, []];
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
- modelName = modelConstructor.name;
409
- namespaceName = this.namespaceResolver(modelConstructor);
410
- storeName = this.getStorenameForModel(modelConstructor);
411
- result = [];
412
- txn = this.db.transaction(storeName, 'readwrite');
413
- store = txn.store;
414
- _loop_1 = function (item) {
415
- var model, connectedModels, keyValues, _deleted, index, key, instance;
416
- return __generator(this, function (_a) {
417
- switch (_a.label) {
418
- case 0:
419
- model = this_1.modelInstanceCreator(modelConstructor, item);
420
- connectedModels = traverseModel(modelName, model, this_1.schema.namespaces[namespaceName], this_1.modelInstanceCreator, this_1.getModelConstructorByModelName);
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
- this_1 = this;
453
- _b.label = 2;
454
- case 2:
455
- _b.trys.push([2, 7, 8, 9]);
456
- items_1 = __values(items), items_1_1 = items_1.next();
457
- _b.label = 3;
458
- case 3:
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
- IndexedDBAdapter.prototype.deleteItem = function (deleteQueue) {
488
- var e_4, _a, e_5, _b;
489
- return __awaiter(this, void 0, void 0, function () {
490
- var connectionStoreNames, tx, _c, _d, deleteItem, storeName, items, store, items_2, items_2_1, item, key, keyValues, itemKey, e_5_1, e_4_1;
491
- return __generator(this, function (_e) {
492
- switch (_e.label) {
493
- case 0:
494
- connectionStoreNames = deleteQueue.map(function (_a) {
495
- var storeName = _a.storeName;
496
- return storeName;
497
- });
498
- tx = this.db.transaction(__spread(connectionStoreNames), 'readwrite');
499
- _e.label = 1;
500
- case 1:
501
- _e.trys.push([1, 22, 23, 28]);
502
- _c = __asyncValues(deleteQueue);
503
- _e.label = 2;
504
- case 2: return [4 /*yield*/, _c.next()];
505
- case 3:
506
- if (!(_d = _e.sent(), !_d.done)) return [3 /*break*/, 21];
507
- deleteItem = _d.value;
508
- storeName = deleteItem.storeName, items = deleteItem.items;
509
- store = tx.objectStore(storeName);
510
- _e.label = 4;
511
- case 4:
512
- _e.trys.push([4, 14, 15, 20]);
513
- items_2 = __asyncValues(items);
514
- _e.label = 5;
515
- case 5: return [4 /*yield*/, items_2.next()];
516
- case 6:
517
- if (!(items_2_1 = _e.sent(), !items_2_1.done)) return [3 /*break*/, 13];
518
- item = items_2_1.value;
519
- if (!item) return [3 /*break*/, 12];
520
- key = void 0;
521
- if (!(typeof item === 'object')) return [3 /*break*/, 8];
522
- keyValues = this.getIndexKeyValuesFromModel(item);
523
- return [4 /*yield*/, store
524
- .index('byPk')
525
- .getKey(this.canonicalKeyPath(keyValues))];
526
- case 7:
527
- key = _e.sent();
528
- return [3 /*break*/, 10];
529
- case 8:
530
- itemKey = item.toString();
531
- return [4 /*yield*/, store.index('byPk').getKey(itemKey)];
532
- case 9:
533
- key = _e.sent();
534
- _e.label = 10;
535
- case 10:
536
- if (!(key !== undefined)) return [3 /*break*/, 12];
537
- return [4 /*yield*/, store.delete(key)];
538
- case 11:
539
- _e.sent();
540
- _e.label = 12;
541
- case 12: return [3 /*break*/, 5];
542
- case 13: return [3 /*break*/, 20];
543
- case 14:
544
- e_5_1 = _e.sent();
545
- e_5 = { error: e_5_1 };
546
- return [3 /*break*/, 20];
547
- case 15:
548
- _e.trys.push([15, , 18, 19]);
549
- if (!(items_2_1 && !items_2_1.done && (_b = items_2.return))) return [3 /*break*/, 17];
550
- return [4 /*yield*/, _b.call(items_2)];
551
- case 16:
552
- _e.sent();
553
- _e.label = 17;
554
- case 17: return [3 /*break*/, 19];
555
- case 18:
556
- if (e_5) throw e_5.error;
557
- return [7 /*endfinally*/];
558
- case 19: return [7 /*endfinally*/];
559
- case 20: return [3 /*break*/, 2];
560
- case 21: return [3 /*break*/, 28];
561
- case 22:
562
- e_4_1 = _e.sent();
563
- e_4 = { error: e_4_1 };
564
- return [3 /*break*/, 28];
565
- case 23:
566
- _e.trys.push([23, , 26, 27]);
567
- if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 25];
568
- return [4 /*yield*/, _a.call(_c)];
569
- case 24:
570
- _e.sent();
571
- _e.label = 25;
572
- case 25: return [3 /*break*/, 27];
573
- case 26:
574
- if (e_4) throw e_4.error;
575
- return [7 /*endfinally*/];
576
- case 27: return [7 /*endfinally*/];
577
- case 28: return [2 /*return*/];
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
- //#region platform-specific helper methods
583
- IndexedDBAdapter.prototype.checkPrivate = function () {
584
- return __awaiter(this, void 0, void 0, function () {
585
- var isPrivate;
586
- return __generator(this, function (_a) {
587
- switch (_a.label) {
588
- case 0: return [4 /*yield*/, isPrivateMode().then(function (isPrivate) {
589
- return isPrivate;
590
- })];
591
- case 1:
592
- isPrivate = _a.sent();
593
- if (isPrivate) {
594
- logger.error("IndexedDB not supported in this browser's private mode");
595
- return [2 /*return*/, Promise.reject("IndexedDB not supported in this browser's private mode")];
596
- }
597
- else {
598
- return [2 /*return*/, Promise.resolve()];
599
- }
600
- return [2 /*return*/];
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
- IndexedDBAdapter.prototype.setSafariCompatabilityMode = function () {
616
- return __awaiter(this, void 0, void 0, function () {
617
- var _a;
618
- return __generator(this, function (_b) {
619
- switch (_b.label) {
620
- case 0:
621
- _a = this;
622
- return [4 /*yield*/, isSafariCompatabilityMode()];
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: namespaceName,
328
+ namespaceName,
637
329
  modelName: modelNameArr.join('_'),
638
330
  };
639
- };
640
- IndexedDBAdapter.prototype.createObjectStoreForModel = function (db, namespaceName, storeName, modelName) {
641
- var store = db.createObjectStore(storeName, {
331
+ }
332
+ createObjectStoreForModel(db, namespaceName, storeName, modelName) {
333
+ const store = db.createObjectStore(storeName, {
642
334
  autoIncrement: true,
643
335
  });
644
- var indexes = this.schema.namespaces[namespaceName].relationships[modelName].indexes;
645
- indexes.forEach(function (_a) {
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
- IndexedDBAdapter.prototype.getByKey = function (storeName, keyValue) {
652
- return __awaiter(this, void 0, void 0, function () {
653
- return __generator(this, function (_a) {
654
- switch (_a.label) {
655
- case 0: return [4 /*yield*/, this._get(storeName, keyValue)];
656
- case 1: return [2 /*return*/, _a.sent()];
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
- IndexedDBAdapter.prototype.matchingIndexQueries = function (storeName, predicates, transaction) {
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
- var queries = [];
685
- var predicateIndex = new Map();
686
- try {
687
- for (var predicates_1 = __values(predicates), predicates_1_1 = predicates_1.next(); !predicates_1_1.done; predicates_1_1 = predicates_1.next()) {
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
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
693
- finally {
694
- try {
695
- if (predicates_1_1 && !predicates_1_1.done && (_a = predicates_1.return)) _a.call(predicates_1);
696
- }
697
- finally { if (e_6) throw e_6.error; }
698
- }
699
- var store = transaction.objectStore(storeName);
700
- var _loop_2 = function (name_1) {
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
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
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(function () {
730
- return _this.db
731
- .transaction(storeName)
732
- .objectStore(storeName)
733
- .index(name_1)
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
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
745
- finally {
746
- try {
747
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
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
- return queries;
752
- };
753
- IndexedDBAdapter.prototype.baseQueryIndex = function (storeName, predicates, transaction) {
754
- return __awaiter(this, void 0, void 0, function () {
755
- var predicateObjs, type, fieldPredicates, txn, result, groupQueries, objectQueries, indexedQueries;
756
- var _this = this;
757
- return __generator(this, function (_a) {
758
- switch (_a.label) {
759
- case 0:
760
- predicateObjs = predicates.predicates, type = predicates.type;
761
- // the predicate objects we care about tend to be nested at least
762
- // one level down: `{and: {or: {and: { <the predicates we want> }}}}`
763
- // so, we unpack and/or groups until we find a group with more than 1
764
- // child OR a child that is not a group (and is therefore a predicate "object").
765
- while (predicateObjs.length === 1 &&
766
- isPredicateGroup(predicateObjs[0]) &&
767
- predicateObjs[0].type !== 'not') {
768
- type = predicateObjs[0].type;
769
- predicateObjs = predicateObjs[0].predicates;
770
- }
771
- fieldPredicates = predicateObjs.filter(function (p) { return isPredicateObj(p) && p.operator === 'eq'; });
772
- txn = transaction || this.db.transaction(storeName);
773
- result = {};
774
- if (!(type === 'or')) return [3 /*break*/, 2];
775
- return [4 /*yield*/, Promise.all(predicateObjs
776
- .filter(function (o) { return isPredicateGroup(o) && o.type === 'and'; })
777
- .map(function (o) {
778
- return _this.baseQueryIndex(storeName, o, txn);
779
- })).then(function (queries) {
780
- return queries
781
- .filter(function (q) { return q.indexedQueries.length === 1; })
782
- .map(function (i) { return i.indexedQueries; });
783
- })];
784
- case 1:
785
- groupQueries = _a.sent();
786
- objectQueries = predicateObjs
787
- .filter(function (o) { return isPredicateObj(o); })
788
- .map(function (o) {
789
- return _this.matchingIndexQueries(storeName, [o], txn);
790
- });
791
- indexedQueries = __spread(groupQueries, objectQueries).map(function (q) { return q[0]; })
792
- .filter(function (i) { return i; });
793
- // if, after hunting for base queries, we don't have exactly 1 base query
794
- // for each child group + object, stop trying to optimize. we're not dealing
795
- // with a simple query that fits the intended optimization path.
796
- if (predicateObjs.length > indexedQueries.length) {
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
- IndexedDBAdapter.prototype.inMemoryPagination = function (records, pagination) {
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
- IndexedDBAdapter.prototype.enginePagination = function (storeName, pagination) {
924
- return __awaiter(this, void 0, void 0, function () {
925
- var result, _a, page, _b, limit, initialRecord, cursor, pageResults, hasLimit;
926
- return __generator(this, function (_c) {
927
- switch (_c.label) {
928
- case 0:
929
- if (!pagination) return [3 /*break*/, 7];
930
- _a = pagination.page, page = _a === void 0 ? 0 : _a, _b = pagination.limit, limit = _b === void 0 ? 0 : _b;
931
- initialRecord = Math.max(0, page * limit) || 0;
932
- return [4 /*yield*/, this.db
933
- .transaction(storeName)
934
- .objectStore(storeName)
935
- .openCursor()];
936
- case 1:
937
- cursor = _c.sent();
938
- if (!(cursor && initialRecord > 0)) return [3 /*break*/, 3];
939
- return [4 /*yield*/, cursor.advance(initialRecord)];
940
- case 2:
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
- return IndexedDBAdapter;
970
- }(StorageAdapterBase));
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