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