@aws-amplify/datastore 5.0.1-api-v6-models.c1977f8.0 → 5.0.1-api-v6-models.891fe0d.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 (70) hide show
  1. package/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
  2. package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
  3. package/lib/datastore/datastore.js +887 -1370
  4. package/lib/index.js +1 -1
  5. package/lib/predicates/index.js +53 -102
  6. package/lib/predicates/next.js +310 -557
  7. package/lib/predicates/sort.js +24 -27
  8. package/lib/ssr/index.js +1 -1
  9. package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
  10. package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
  11. package/lib/storage/adapter/InMemoryStore.js +27 -101
  12. package/lib/storage/adapter/InMemoryStore.native.js +1 -1
  13. package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
  14. package/lib/storage/adapter/StorageAdapterBase.js +177 -396
  15. package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
  16. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
  17. package/lib/storage/relationship.js +174 -260
  18. package/lib/storage/storage.js +244 -507
  19. package/lib/sync/datastoreConnectivity.js +29 -84
  20. package/lib/sync/datastoreReachability/index.js +1 -1
  21. package/lib/sync/datastoreReachability/index.native.js +2 -2
  22. package/lib/sync/index.js +512 -885
  23. package/lib/sync/merger.js +30 -133
  24. package/lib/sync/outbox.js +147 -302
  25. package/lib/sync/processors/errorMaps.js +30 -80
  26. package/lib/sync/processors/mutation.js +331 -579
  27. package/lib/sync/processors/subscription.js +268 -428
  28. package/lib/sync/processors/sync.js +276 -464
  29. package/lib/sync/utils.js +248 -393
  30. package/lib/tsconfig.tsbuildinfo +1 -1
  31. package/lib/types.js +16 -58
  32. package/lib/util.js +335 -575
  33. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
  34. package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
  35. package/lib-esm/datastore/datastore.js +874 -1359
  36. package/lib-esm/index.js +6 -6
  37. package/lib-esm/predicates/index.js +54 -104
  38. package/lib-esm/predicates/next.js +306 -555
  39. package/lib-esm/predicates/sort.js +24 -27
  40. package/lib-esm/ssr/index.js +1 -1
  41. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
  42. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
  43. package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
  44. package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
  45. package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
  46. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
  47. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
  48. package/lib-esm/storage/relationship.js +173 -260
  49. package/lib-esm/storage/storage.js +236 -499
  50. package/lib-esm/sync/datastoreConnectivity.js +26 -82
  51. package/lib-esm/sync/datastoreReachability/index.js +1 -1
  52. package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
  53. package/lib-esm/sync/index.js +498 -872
  54. package/lib-esm/sync/merger.js +28 -131
  55. package/lib-esm/sync/outbox.js +143 -298
  56. package/lib-esm/sync/processors/errorMaps.js +32 -82
  57. package/lib-esm/sync/processors/mutation.js +324 -572
  58. package/lib-esm/sync/processors/subscription.js +258 -418
  59. package/lib-esm/sync/processors/sync.js +269 -457
  60. package/lib-esm/sync/utils.js +245 -390
  61. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  62. package/lib-esm/types.js +16 -59
  63. package/lib-esm/util.js +335 -577
  64. package/package.json +12 -12
  65. package/src/datastore/datastore.ts +4 -3
  66. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
  67. package/src/sync/processors/mutation.ts +1 -1
  68. package/src/sync/processors/sync.ts +1 -1
  69. package/src/sync/utils.ts +1 -1
  70. package/src/util.ts +44 -6
@@ -1,73 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- var __asyncValues = (this && this.__asyncValues) || function (o) {
54
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
55
- var m = o[Symbol.asyncIterator], i;
56
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
57
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
58
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
59
- };
60
- var __values = (this && this.__values) || function(o) {
61
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
62
- if (m) return m.call(o);
63
- if (o && typeof o.length === "number") return {
64
- next: function () {
65
- if (o && i >= o.length) o = void 0;
66
- return { value: o && o[i++], done: !o };
67
- }
68
- };
69
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
70
- };
71
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
72
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
73
4
  };
@@ -75,26 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
75
6
  exports.AsyncStorageAdapter = void 0;
76
7
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
77
8
  // SPDX-License-Identifier: Apache-2.0
78
- var AsyncStorageDatabase_1 = __importDefault(require("./AsyncStorageDatabase"));
79
- var types_1 = require("../../types");
80
- var util_1 = require("../../util");
81
- var StorageAdapterBase_1 = require("./StorageAdapterBase");
82
- var AsyncStorageAdapter = /** @class */ (function (_super) {
83
- __extends(AsyncStorageAdapter, _super);
84
- function AsyncStorageAdapter() {
85
- return _super !== null && _super.apply(this, arguments) || this;
86
- }
9
+ const AsyncStorageDatabase_1 = __importDefault(require("./AsyncStorageDatabase"));
10
+ const types_1 = require("../../types");
11
+ const util_1 = require("../../util");
12
+ const StorageAdapterBase_1 = require("./StorageAdapterBase");
13
+ class AsyncStorageAdapter extends StorageAdapterBase_1.StorageAdapterBase {
87
14
  // no-ops for this adapter
88
- AsyncStorageAdapter.prototype.preSetUpChecks = function () {
89
- return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
90
- return [2 /*return*/];
91
- }); });
92
- };
93
- AsyncStorageAdapter.prototype.preOpCheck = function () {
94
- return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
95
- return [2 /*return*/];
96
- }); });
97
- };
15
+ async preSetUpChecks() { }
16
+ async preOpCheck() { }
98
17
  /**
99
18
  * Open AsyncStorage database
100
19
  * Create new DB if one doesn't exist
@@ -103,366 +22,114 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
103
22
  *
104
23
  * @returns AsyncStorageDatabase instance
105
24
  */
106
- AsyncStorageAdapter.prototype.initDb = function () {
107
- return __awaiter(this, void 0, void 0, function () {
108
- var db;
109
- return __generator(this, function (_a) {
110
- switch (_a.label) {
111
- case 0:
112
- db = new AsyncStorageDatabase_1.default();
113
- return [4 /*yield*/, db.init()];
114
- case 1:
115
- _a.sent();
116
- return [2 /*return*/, db];
117
- }
118
- });
119
- });
120
- };
121
- AsyncStorageAdapter.prototype.clear = function () {
122
- return __awaiter(this, void 0, void 0, function () {
123
- return __generator(this, function (_a) {
124
- switch (_a.label) {
125
- case 0: return [4 /*yield*/, this.db.clear()];
126
- case 1:
127
- _a.sent();
128
- this.db = undefined;
129
- this.initPromise = undefined;
130
- return [2 /*return*/];
131
- }
132
- });
133
- });
134
- };
135
- AsyncStorageAdapter.prototype.batchSave = function (modelConstructor, items) {
136
- return __awaiter(this, void 0, void 0, function () {
137
- var modelName, namespaceName, storeName, keys, batch, _loop_1, this_1, items_1, items_1_1, item;
138
- var e_1, _a;
139
- var _this = this;
140
- return __generator(this, function (_b) {
141
- switch (_b.label) {
142
- case 0:
143
- if (items.length === 0) {
144
- return [2 /*return*/, []];
145
- }
146
- modelName = modelConstructor.name;
147
- namespaceName = this.namespaceResolver(modelConstructor);
148
- storeName = (0, util_1.getStorename)(namespaceName, modelName);
149
- keys = (0, util_1.getIndexKeys)(this.schema.namespaces[namespaceName], modelName);
150
- batch = [];
151
- _loop_1 = function (item) {
152
- var model = this_1.modelInstanceCreator(modelConstructor, item);
153
- var connectedModels = (0, util_1.traverseModel)(modelName, model, this_1.schema.namespaces[namespaceName], this_1.modelInstanceCreator, this_1.getModelConstructorByModelName);
154
- var keyValuesPath = this_1.getIndexKeyValuesPath(model);
155
- var instance = connectedModels.find(function (_a) {
156
- var instance = _a.instance;
157
- var instanceKeyValuesPath = _this.getIndexKeyValuesPath(instance);
158
- return (0, util_1.keysEqual)([instanceKeyValuesPath], [keyValuesPath]);
159
- }).instance;
160
- batch.push(instance);
161
- };
162
- this_1 = this;
163
- try {
164
- for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
165
- item = items_1_1.value;
166
- _loop_1(item);
167
- }
168
- }
169
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
170
- finally {
171
- try {
172
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
173
- }
174
- finally { if (e_1) throw e_1.error; }
175
- }
176
- return [4 /*yield*/, this.db.batchSave(storeName, batch, keys)];
177
- case 1: return [2 /*return*/, _b.sent()];
178
- }
179
- });
180
- });
181
- };
182
- AsyncStorageAdapter.prototype._get = function (storeName, keyArr) {
183
- return __awaiter(this, void 0, void 0, function () {
184
- var itemKeyValuesPath;
185
- return __generator(this, function (_a) {
186
- switch (_a.label) {
187
- case 0:
188
- itemKeyValuesPath = keyArr.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
189
- return [4 /*yield*/, this.db.get(itemKeyValuesPath, storeName)];
190
- case 1: return [2 /*return*/, _a.sent()];
191
- }
192
- });
193
- });
194
- };
195
- AsyncStorageAdapter.prototype.save = function (model, condition) {
196
- var _a, e_2, _b, _c;
197
- return __awaiter(this, void 0, void 0, function () {
198
- var _d, storeName, connectionStoreNames, modelKeyValues, fromDB, result, _loop_2, this_2, _e, connectionStoreNames_1, connectionStoreNames_1_1, e_2_1;
199
- return __generator(this, function (_f) {
200
- switch (_f.label) {
201
- case 0:
202
- _d = this.saveMetadata(model), storeName = _d.storeName, connectionStoreNames = _d.connectionStoreNames, modelKeyValues = _d.modelKeyValues;
203
- return [4 /*yield*/, this._get(storeName, modelKeyValues)];
204
- case 1:
205
- fromDB = _f.sent();
206
- this.validateSaveCondition(condition, fromDB);
207
- result = [];
208
- _f.label = 2;
209
- case 2:
210
- _f.trys.push([2, 8, 9, 14]);
211
- _loop_2 = function () {
212
- var resItem, storeName_1, item, instance, keys, itemKeyValues, fromDB_1, opType;
213
- return __generator(this, function (_g) {
214
- switch (_g.label) {
215
- case 0:
216
- _c = connectionStoreNames_1_1.value;
217
- _e = false;
218
- _g.label = 1;
219
- case 1:
220
- _g.trys.push([1, , 5, 6]);
221
- resItem = _c;
222
- storeName_1 = resItem.storeName, item = resItem.item, instance = resItem.instance, keys = resItem.keys;
223
- itemKeyValues = keys.map(function (key) { return item[key]; });
224
- return [4 /*yield*/, this_2._get(storeName_1, itemKeyValues)];
225
- case 2:
226
- fromDB_1 = _g.sent();
227
- opType = fromDB_1 ? types_1.OpType.UPDATE : types_1.OpType.INSERT;
228
- if (!((0, util_1.keysEqual)(itemKeyValues, modelKeyValues) ||
229
- opType === types_1.OpType.INSERT)) return [3 /*break*/, 4];
230
- return [4 /*yield*/, this_2.db.save(item, storeName_1, keys, itemKeyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR))];
231
- case 3:
232
- _g.sent();
233
- result.push([instance, opType]);
234
- _g.label = 4;
235
- case 4: return [3 /*break*/, 6];
236
- case 5:
237
- _e = true;
238
- return [7 /*endfinally*/];
239
- case 6: return [2 /*return*/];
240
- }
241
- });
242
- };
243
- this_2 = this;
244
- _e = true, connectionStoreNames_1 = __asyncValues(connectionStoreNames);
245
- _f.label = 3;
246
- case 3: return [4 /*yield*/, connectionStoreNames_1.next()];
247
- case 4:
248
- if (!(connectionStoreNames_1_1 = _f.sent(), _a = connectionStoreNames_1_1.done, !_a)) return [3 /*break*/, 7];
249
- return [5 /*yield**/, _loop_2()];
250
- case 5:
251
- _f.sent();
252
- _f.label = 6;
253
- case 6: return [3 /*break*/, 3];
254
- case 7: return [3 /*break*/, 14];
255
- case 8:
256
- e_2_1 = _f.sent();
257
- e_2 = { error: e_2_1 };
258
- return [3 /*break*/, 14];
259
- case 9:
260
- _f.trys.push([9, , 12, 13]);
261
- if (!(!_e && !_a && (_b = connectionStoreNames_1.return))) return [3 /*break*/, 11];
262
- return [4 /*yield*/, _b.call(connectionStoreNames_1)];
263
- case 10:
264
- _f.sent();
265
- _f.label = 11;
266
- case 11: return [3 /*break*/, 13];
267
- case 12:
268
- if (e_2) throw e_2.error;
269
- return [7 /*endfinally*/];
270
- case 13: return [7 /*endfinally*/];
271
- case 14: return [2 /*return*/, result];
272
- }
273
- });
274
- });
275
- };
276
- AsyncStorageAdapter.prototype.query = function (modelConstructor, predicate, pagination) {
277
- return __awaiter(this, void 0, void 0, function () {
278
- var _a, storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, records;
279
- var _this = this;
280
- return __generator(this, function (_b) {
281
- switch (_b.label) {
282
- case 0:
283
- _a = this.queryMetadata(modelConstructor, predicate, pagination), storeName = _a.storeName, namespaceName = _a.namespaceName, queryByKey = _a.queryByKey, predicates = _a.predicates, hasSort = _a.hasSort, hasPagination = _a.hasPagination;
284
- return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
285
- var keyValues, record, filtered, all;
286
- return __generator(this, function (_a) {
287
- switch (_a.label) {
288
- case 0:
289
- if (!queryByKey) return [3 /*break*/, 2];
290
- keyValues = queryByKey.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
291
- return [4 /*yield*/, this.getByKey(storeName, keyValues)];
292
- case 1:
293
- record = _a.sent();
294
- return [2 /*return*/, record ? [record] : []];
295
- case 2:
296
- if (!predicates) return [3 /*break*/, 4];
297
- return [4 /*yield*/, this.filterOnPredicate(storeName, predicates)];
298
- case 3:
299
- filtered = _a.sent();
300
- return [2 /*return*/, this.inMemoryPagination(filtered, pagination)];
301
- case 4:
302
- if (!(hasSort || hasPagination)) return [3 /*break*/, 6];
303
- return [4 /*yield*/, this.getAll(storeName)];
304
- case 5:
305
- all = _a.sent();
306
- return [2 /*return*/, this.inMemoryPagination(all, pagination)];
307
- case 6: return [2 /*return*/, this.getAll(storeName)];
308
- }
309
- });
310
- }); })()];
311
- case 1:
312
- records = (_b.sent());
313
- return [4 /*yield*/, this.load(namespaceName, modelConstructor.name, records)];
314
- case 2: return [2 /*return*/, _b.sent()];
315
- }
316
- });
317
- });
318
- };
319
- AsyncStorageAdapter.prototype.getByKey = function (storeName, keyValuePath) {
320
- return __awaiter(this, void 0, void 0, function () {
321
- return __generator(this, function (_a) {
322
- switch (_a.label) {
323
- case 0: return [4 /*yield*/, this.db.get(keyValuePath, storeName)];
324
- case 1: return [2 /*return*/, _a.sent()];
325
- }
326
- });
327
- });
328
- };
329
- AsyncStorageAdapter.prototype.getAll = function (storeName) {
330
- return __awaiter(this, void 0, void 0, function () {
331
- return __generator(this, function (_a) {
332
- switch (_a.label) {
333
- case 0: return [4 /*yield*/, this.db.getAll(storeName)];
334
- case 1: return [2 /*return*/, _a.sent()];
335
- }
336
- });
337
- });
338
- };
339
- AsyncStorageAdapter.prototype.filterOnPredicate = function (storeName, predicates) {
340
- return __awaiter(this, void 0, void 0, function () {
341
- var predicateObjs, type, all, filtered;
342
- return __generator(this, function (_a) {
343
- switch (_a.label) {
344
- case 0:
345
- predicateObjs = predicates.predicates, type = predicates.type;
346
- return [4 /*yield*/, this.getAll(storeName)];
347
- case 1:
348
- all = _a.sent();
349
- filtered = predicateObjs
350
- ? all.filter(function (m) { return (0, util_1.validatePredicate)(m, type, predicateObjs); })
351
- : all;
352
- return [2 /*return*/, filtered];
353
- }
25
+ async initDb() {
26
+ const db = new AsyncStorageDatabase_1.default();
27
+ await db.init();
28
+ return db;
29
+ }
30
+ async clear() {
31
+ await this.db.clear();
32
+ this.db = undefined;
33
+ this.initPromise = undefined;
34
+ }
35
+ async batchSave(modelConstructor, items) {
36
+ if (items.length === 0) {
37
+ return [];
38
+ }
39
+ const modelName = modelConstructor.name;
40
+ const namespaceName = this.namespaceResolver(modelConstructor);
41
+ const storeName = (0, util_1.getStorename)(namespaceName, modelName);
42
+ const keys = (0, util_1.getIndexKeys)(this.schema.namespaces[namespaceName], modelName);
43
+ const batch = [];
44
+ for (const item of items) {
45
+ const model = this.modelInstanceCreator(modelConstructor, item);
46
+ const connectedModels = (0, util_1.traverseModel)(modelName, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
47
+ const keyValuesPath = this.getIndexKeyValuesPath(model);
48
+ const { instance } = connectedModels.find(({ instance }) => {
49
+ const instanceKeyValuesPath = this.getIndexKeyValuesPath(instance);
50
+ return (0, util_1.keysEqual)([instanceKeyValuesPath], [keyValuesPath]);
354
51
  });
355
- });
356
- };
357
- AsyncStorageAdapter.prototype.inMemoryPagination = function (records, pagination) {
52
+ batch.push(instance);
53
+ }
54
+ return await this.db.batchSave(storeName, batch, keys);
55
+ }
56
+ async _get(storeName, keyArr) {
57
+ const itemKeyValuesPath = keyArr.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
58
+ return await this.db.get(itemKeyValuesPath, storeName);
59
+ }
60
+ async save(model, condition) {
61
+ const { storeName, connectionStoreNames, modelKeyValues } = this.saveMetadata(model);
62
+ const fromDB = await this._get(storeName, modelKeyValues);
63
+ this.validateSaveCondition(condition, fromDB);
64
+ const result = [];
65
+ for await (const resItem of connectionStoreNames) {
66
+ const { storeName, item, instance, keys } = resItem;
67
+ const itemKeyValues = keys.map(key => item[key]);
68
+ const fromDB = await this._get(storeName, itemKeyValues);
69
+ const opType = fromDB ? types_1.OpType.UPDATE : types_1.OpType.INSERT;
70
+ if ((0, util_1.keysEqual)(itemKeyValues, modelKeyValues) ||
71
+ opType === types_1.OpType.INSERT) {
72
+ await this.db.save(item, storeName, keys, itemKeyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR));
73
+ result.push([instance, opType]);
74
+ }
75
+ }
76
+ return result;
77
+ }
78
+ async query(modelConstructor, predicate, pagination) {
79
+ const { storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, } = this.queryMetadata(modelConstructor, predicate, pagination);
80
+ const records = (await (async () => {
81
+ if (queryByKey) {
82
+ const keyValues = queryByKey.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
83
+ const record = await this.getByKey(storeName, keyValues);
84
+ return record ? [record] : [];
85
+ }
86
+ if (predicates) {
87
+ const filtered = await this.filterOnPredicate(storeName, predicates);
88
+ return this.inMemoryPagination(filtered, pagination);
89
+ }
90
+ if (hasSort || hasPagination) {
91
+ const all = await this.getAll(storeName);
92
+ return this.inMemoryPagination(all, pagination);
93
+ }
94
+ return this.getAll(storeName);
95
+ })());
96
+ return await this.load(namespaceName, modelConstructor.name, records);
97
+ }
98
+ async getByKey(storeName, keyValuePath) {
99
+ return await this.db.get(keyValuePath, storeName);
100
+ }
101
+ async getAll(storeName) {
102
+ return await this.db.getAll(storeName);
103
+ }
104
+ async filterOnPredicate(storeName, predicates) {
105
+ const { predicates: predicateObjs, type } = predicates;
106
+ const all = await this.getAll(storeName);
107
+ const filtered = predicateObjs
108
+ ? all.filter(m => (0, util_1.validatePredicate)(m, type, predicateObjs))
109
+ : all;
110
+ return filtered;
111
+ }
112
+ inMemoryPagination(records, pagination) {
358
113
  return (0, util_1.inMemoryPagination)(records, pagination);
359
- };
360
- AsyncStorageAdapter.prototype.queryOne = function (modelConstructor, firstOrLast) {
361
- if (firstOrLast === void 0) { firstOrLast = types_1.QueryOne.FIRST; }
362
- return __awaiter(this, void 0, void 0, function () {
363
- var storeName, result;
364
- return __generator(this, function (_a) {
365
- switch (_a.label) {
366
- case 0:
367
- storeName = this.getStorenameForModel(modelConstructor);
368
- return [4 /*yield*/, this.db.getOne(firstOrLast, storeName)];
369
- case 1:
370
- result = _a.sent();
371
- return [2 /*return*/, result && this.modelInstanceCreator(modelConstructor, result)];
372
- }
373
- });
374
- });
375
- };
376
- AsyncStorageAdapter.prototype.deleteItem = function (deleteQueue) {
377
- var _a, e_3, _b, _c, _d, e_4, _e, _f;
378
- return __awaiter(this, void 0, void 0, function () {
379
- var _g, _h, _j, deleteItem, storeName, items, _k, items_2, items_2_1, item, keyValuesPath, e_4_1, e_3_1;
380
- return __generator(this, function (_l) {
381
- switch (_l.label) {
382
- case 0:
383
- _l.trys.push([0, 23, 24, 29]);
384
- _g = true, _h = __asyncValues(deleteQueue);
385
- _l.label = 1;
386
- case 1: return [4 /*yield*/, _h.next()];
387
- case 2:
388
- if (!(_j = _l.sent(), _a = _j.done, !_a)) return [3 /*break*/, 22];
389
- _c = _j.value;
390
- _g = false;
391
- _l.label = 3;
392
- case 3:
393
- _l.trys.push([3, , 20, 21]);
394
- deleteItem = _c;
395
- storeName = deleteItem.storeName, items = deleteItem.items;
396
- _l.label = 4;
397
- case 4:
398
- _l.trys.push([4, 13, 14, 19]);
399
- _k = true, items_2 = (e_4 = void 0, __asyncValues(items));
400
- _l.label = 5;
401
- case 5: return [4 /*yield*/, items_2.next()];
402
- case 6:
403
- if (!(items_2_1 = _l.sent(), _d = items_2_1.done, !_d)) return [3 /*break*/, 12];
404
- _f = items_2_1.value;
405
- _k = false;
406
- _l.label = 7;
407
- case 7:
408
- _l.trys.push([7, , 10, 11]);
409
- item = _f;
410
- if (!item) return [3 /*break*/, 9];
411
- if (!(typeof item === 'object')) return [3 /*break*/, 9];
412
- keyValuesPath = this.getIndexKeyValuesPath(item);
413
- return [4 /*yield*/, this.db.delete(keyValuesPath, storeName)];
414
- case 8:
415
- _l.sent();
416
- _l.label = 9;
417
- case 9: return [3 /*break*/, 11];
418
- case 10:
419
- _k = true;
420
- return [7 /*endfinally*/];
421
- case 11: return [3 /*break*/, 5];
422
- case 12: return [3 /*break*/, 19];
423
- case 13:
424
- e_4_1 = _l.sent();
425
- e_4 = { error: e_4_1 };
426
- return [3 /*break*/, 19];
427
- case 14:
428
- _l.trys.push([14, , 17, 18]);
429
- if (!(!_k && !_d && (_e = items_2.return))) return [3 /*break*/, 16];
430
- return [4 /*yield*/, _e.call(items_2)];
431
- case 15:
432
- _l.sent();
433
- _l.label = 16;
434
- case 16: return [3 /*break*/, 18];
435
- case 17:
436
- if (e_4) throw e_4.error;
437
- return [7 /*endfinally*/];
438
- case 18: return [7 /*endfinally*/];
439
- case 19: return [3 /*break*/, 21];
440
- case 20:
441
- _g = true;
442
- return [7 /*endfinally*/];
443
- case 21: return [3 /*break*/, 1];
444
- case 22: return [3 /*break*/, 29];
445
- case 23:
446
- e_3_1 = _l.sent();
447
- e_3 = { error: e_3_1 };
448
- return [3 /*break*/, 29];
449
- case 24:
450
- _l.trys.push([24, , 27, 28]);
451
- if (!(!_g && !_a && (_b = _h.return))) return [3 /*break*/, 26];
452
- return [4 /*yield*/, _b.call(_h)];
453
- case 25:
454
- _l.sent();
455
- _l.label = 26;
456
- case 26: return [3 /*break*/, 28];
457
- case 27:
458
- if (e_3) throw e_3.error;
459
- return [7 /*endfinally*/];
460
- case 28: return [7 /*endfinally*/];
461
- case 29: return [2 /*return*/];
114
+ }
115
+ async queryOne(modelConstructor, firstOrLast = types_1.QueryOne.FIRST) {
116
+ const storeName = this.getStorenameForModel(modelConstructor);
117
+ const result = await this.db.getOne(firstOrLast, storeName);
118
+ return result && this.modelInstanceCreator(modelConstructor, result);
119
+ }
120
+ async deleteItem(deleteQueue) {
121
+ for await (const deleteItem of deleteQueue) {
122
+ const { storeName, items } = deleteItem;
123
+ for await (const item of items) {
124
+ if (item) {
125
+ if (typeof item === 'object') {
126
+ const keyValuesPath = this.getIndexKeyValuesPath(item);
127
+ await this.db.delete(keyValuesPath, storeName);
128
+ }
462
129
  }
463
- });
464
- });
465
- };
130
+ }
131
+ }
132
+ }
466
133
  //#region platform-specific helper methods
467
134
  /**
468
135
  * Retrieves concatenated primary key values from a model
@@ -470,10 +137,9 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
470
137
  * @param model
471
138
  * @returns
472
139
  */
473
- AsyncStorageAdapter.prototype.getIndexKeyValuesPath = function (model) {
140
+ getIndexKeyValuesPath(model) {
474
141
  return this.getIndexKeyValuesFromModel(model).join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
475
- };
476
- return AsyncStorageAdapter;
477
- }(StorageAdapterBase_1.StorageAdapterBase));
142
+ }
143
+ }
478
144
  exports.AsyncStorageAdapter = AsyncStorageAdapter;
479
145
  exports.default = new AsyncStorageAdapter();