@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,75 +1,12 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
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;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var __values = (this && this.__values) || function(o) {
|
|
38
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
39
|
-
if (m) return m.call(o);
|
|
40
|
-
if (o && typeof o.length === "number") return {
|
|
41
|
-
next: function () {
|
|
42
|
-
if (o && i >= o.length) o = void 0;
|
|
43
|
-
return { value: o && o[i++], done: !o };
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
47
|
-
};
|
|
48
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
49
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
50
|
-
if (!m) return o;
|
|
51
|
-
var i = m.call(o), r, ar = [], e;
|
|
52
|
-
try {
|
|
53
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
54
|
-
}
|
|
55
|
-
catch (error) { e = { error: error }; }
|
|
56
|
-
finally {
|
|
57
|
-
try {
|
|
58
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
59
|
-
}
|
|
60
|
-
finally { if (e) throw e.error; }
|
|
61
|
-
}
|
|
62
|
-
return ar;
|
|
63
|
-
};
|
|
64
1
|
import { OpType, QueryOne, } from '../../types';
|
|
65
2
|
import { DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR, indexNameFromKeys, monotonicUlidFactory, } from '../../util';
|
|
66
3
|
import { createInMemoryStore } from './InMemoryStore';
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
4
|
+
const DB_NAME = '@AmplifyDatastore';
|
|
5
|
+
const COLLECTION = 'Collection';
|
|
6
|
+
const DATA = 'Data';
|
|
7
|
+
const monotonicFactoriesMap = new Map();
|
|
8
|
+
class AsyncStorageDatabase {
|
|
9
|
+
constructor() {
|
|
73
10
|
/**
|
|
74
11
|
* Maps storeNames to a map of ulid->id
|
|
75
12
|
*/
|
|
@@ -81,444 +18,242 @@ var AsyncStorageDatabase = /** @class */ (function () {
|
|
|
81
18
|
* @param storeName {string} - Name of the store
|
|
82
19
|
* @returns Map of ulid->id
|
|
83
20
|
*/
|
|
84
|
-
|
|
21
|
+
getCollectionIndex(storeName) {
|
|
85
22
|
if (!this._collectionInMemoryIndex.has(storeName)) {
|
|
86
23
|
this._collectionInMemoryIndex.set(storeName, new Map());
|
|
87
24
|
}
|
|
88
25
|
return this._collectionInMemoryIndex.get(storeName);
|
|
89
|
-
}
|
|
26
|
+
}
|
|
90
27
|
/**
|
|
91
28
|
* Return ULID for store if it exists, otherwise create a new one
|
|
92
29
|
* @param storeName {string} - Name of the store
|
|
93
30
|
* @returns ulid
|
|
94
31
|
*/
|
|
95
|
-
|
|
32
|
+
getMonotonicFactory(storeName) {
|
|
96
33
|
if (!monotonicFactoriesMap.has(storeName)) {
|
|
97
34
|
monotonicFactoriesMap.set(storeName, monotonicUlidFactory());
|
|
98
35
|
}
|
|
99
36
|
return monotonicFactoriesMap.get(storeName);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (!!allKeys_1_1.done) return [3 /*break*/, 11];
|
|
120
|
-
key = allKeys_1_1.value;
|
|
121
|
-
_a = __read(key.split('::'), 5), dbName = _a[0], storeName = _a[1], recordType = _a[2], ulidOrId = _a[3], id = _a[4];
|
|
122
|
-
if (!(dbName === DB_NAME)) return [3 /*break*/, 10];
|
|
123
|
-
if (!(recordType === DATA)) return [3 /*break*/, 9];
|
|
124
|
-
ulid = void 0;
|
|
125
|
-
if (!(id === undefined)) return [3 /*break*/, 7];
|
|
126
|
-
id_1 = ulidOrId;
|
|
127
|
-
newUlid = this.getMonotonicFactory(storeName)();
|
|
128
|
-
oldKey = this.getLegacyKeyForItem(storeName, id_1);
|
|
129
|
-
newKey = this.getKeyForItem(storeName, id_1, newUlid);
|
|
130
|
-
return [4 /*yield*/, this.storage.getItem(oldKey)];
|
|
131
|
-
case 4:
|
|
132
|
-
item = _c.sent();
|
|
133
|
-
return [4 /*yield*/, this.storage.setItem(newKey, item)];
|
|
134
|
-
case 5:
|
|
135
|
-
_c.sent();
|
|
136
|
-
return [4 /*yield*/, this.storage.removeItem(oldKey)];
|
|
137
|
-
case 6:
|
|
138
|
-
_c.sent();
|
|
37
|
+
}
|
|
38
|
+
async init() {
|
|
39
|
+
this._collectionInMemoryIndex.clear();
|
|
40
|
+
const allKeys = await this.storage.getAllKeys();
|
|
41
|
+
const keysForCollectionEntries = [];
|
|
42
|
+
for (const key of allKeys) {
|
|
43
|
+
const [dbName, storeName, recordType, ulidOrId, id] = key.split('::');
|
|
44
|
+
if (dbName === DB_NAME) {
|
|
45
|
+
if (recordType === DATA) {
|
|
46
|
+
let ulid;
|
|
47
|
+
if (id === undefined) {
|
|
48
|
+
// It is an old entry (without ulid). Need to migrate to new key format
|
|
49
|
+
const id = ulidOrId;
|
|
50
|
+
const newUlid = this.getMonotonicFactory(storeName)();
|
|
51
|
+
const oldKey = this.getLegacyKeyForItem(storeName, id);
|
|
52
|
+
const newKey = this.getKeyForItem(storeName, id, newUlid);
|
|
53
|
+
const item = await this.storage.getItem(oldKey);
|
|
54
|
+
await this.storage.setItem(newKey, item);
|
|
55
|
+
await this.storage.removeItem(oldKey);
|
|
139
56
|
ulid = newUlid;
|
|
140
|
-
|
|
141
|
-
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
142
59
|
ulid = ulidOrId;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.getCollectionIndex(storeName).set(id, ulid);
|
|
146
|
-
return [3 /*break*/, 10];
|
|
147
|
-
case 9:
|
|
148
|
-
if (recordType === COLLECTION) {
|
|
149
|
-
keysForCollectionEntries.push(key);
|
|
150
|
-
}
|
|
151
|
-
_c.label = 10;
|
|
152
|
-
case 10:
|
|
153
|
-
allKeys_1_1 = allKeys_1.next();
|
|
154
|
-
return [3 /*break*/, 3];
|
|
155
|
-
case 11: return [3 /*break*/, 14];
|
|
156
|
-
case 12:
|
|
157
|
-
e_1_1 = _c.sent();
|
|
158
|
-
e_1 = { error: e_1_1 };
|
|
159
|
-
return [3 /*break*/, 14];
|
|
160
|
-
case 13:
|
|
161
|
-
try {
|
|
162
|
-
if (allKeys_1_1 && !allKeys_1_1.done && (_b = allKeys_1.return)) _b.call(allKeys_1);
|
|
163
|
-
}
|
|
164
|
-
finally { if (e_1) throw e_1.error; }
|
|
165
|
-
return [7 /*endfinally*/];
|
|
166
|
-
case 14:
|
|
167
|
-
if (!(keysForCollectionEntries.length > 0)) return [3 /*break*/, 16];
|
|
168
|
-
return [4 /*yield*/, this.storage.multiRemove(keysForCollectionEntries)];
|
|
169
|
-
case 15:
|
|
170
|
-
_c.sent();
|
|
171
|
-
_c.label = 16;
|
|
172
|
-
case 16: return [2 /*return*/];
|
|
60
|
+
}
|
|
61
|
+
this.getCollectionIndex(storeName).set(id, ulid);
|
|
173
62
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
};
|
|
177
|
-
AsyncStorageDatabase.prototype.save = function (item, storeName, keys, keyValuesPath) {
|
|
178
|
-
var _a, _b;
|
|
179
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
180
|
-
var idxName, ulid, itemKey;
|
|
181
|
-
return __generator(this, function (_c) {
|
|
182
|
-
switch (_c.label) {
|
|
183
|
-
case 0:
|
|
184
|
-
idxName = indexNameFromKeys(keys);
|
|
185
|
-
ulid = ((_a = this.getCollectionIndex(storeName)) === null || _a === void 0 ? void 0 : _a.get(idxName)) ||
|
|
186
|
-
this.getMonotonicFactory(storeName)();
|
|
187
|
-
itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
|
|
188
|
-
// Set key in collection index
|
|
189
|
-
(_b = this.getCollectionIndex(storeName)) === null || _b === void 0 ? void 0 : _b.set(keyValuesPath, ulid);
|
|
190
|
-
// Save item in db
|
|
191
|
-
return [4 /*yield*/, this.storage.setItem(itemKey, JSON.stringify(item))];
|
|
192
|
-
case 1:
|
|
193
|
-
// Save item in db
|
|
194
|
-
_c.sent();
|
|
195
|
-
return [2 /*return*/];
|
|
63
|
+
else if (recordType === COLLECTION) {
|
|
64
|
+
keysForCollectionEntries.push(key);
|
|
196
65
|
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (keysForCollectionEntries.length > 0) {
|
|
69
|
+
await this.storage.multiRemove(keysForCollectionEntries);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async save(item, storeName, keys, keyValuesPath) {
|
|
73
|
+
const idxName = indexNameFromKeys(keys);
|
|
74
|
+
const ulid = this.getCollectionIndex(storeName)?.get(idxName) ||
|
|
75
|
+
this.getMonotonicFactory(storeName)();
|
|
76
|
+
// Retrieve db key for item
|
|
77
|
+
const itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
|
|
78
|
+
// Set key in collection index
|
|
79
|
+
this.getCollectionIndex(storeName)?.set(keyValuesPath, ulid);
|
|
80
|
+
// Save item in db
|
|
81
|
+
await this.storage.setItem(itemKey, JSON.stringify(item));
|
|
82
|
+
}
|
|
83
|
+
async batchSave(storeName, items, keys) {
|
|
84
|
+
if (items.length === 0) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
const result = [];
|
|
88
|
+
const collection = this.getCollectionIndex(storeName);
|
|
89
|
+
const keysToDelete = new Set();
|
|
90
|
+
const keysToSave = new Set();
|
|
91
|
+
const allItemsKeys = [];
|
|
92
|
+
const itemsMap = {};
|
|
93
|
+
/* Populate allItemKeys, keysToDelete, and keysToSave */
|
|
94
|
+
for (const item of items) {
|
|
95
|
+
// Extract keys from concatenated key path, map to item values
|
|
96
|
+
const keyValues = keys.map(field => item[field]);
|
|
97
|
+
const { _deleted } = item;
|
|
98
|
+
// If id is in the store, retrieve, otherwise generate new ULID
|
|
99
|
+
const ulid = collection.get(keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
|
|
100
|
+
this.getMonotonicFactory(storeName)();
|
|
101
|
+
// Generate the "longer key" for the item
|
|
102
|
+
const key = this.getKeyForItem(storeName, keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR), ulid);
|
|
103
|
+
allItemsKeys.push(key);
|
|
104
|
+
itemsMap[key] = { ulid, model: item };
|
|
105
|
+
if (_deleted) {
|
|
106
|
+
keysToDelete.add(key);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
keysToSave.add(key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const existingRecordsMap = await this.storage.multiGet(allItemsKeys);
|
|
113
|
+
const existingRecordsKeys = existingRecordsMap
|
|
114
|
+
.filter(([, v]) => !!v)
|
|
115
|
+
.reduce((set, [k]) => set.add(k), new Set());
|
|
116
|
+
// Delete
|
|
117
|
+
await new Promise((resolve, reject) => {
|
|
118
|
+
if (keysToDelete.size === 0) {
|
|
119
|
+
resolve();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const keysToDeleteArray = Array.from(keysToDelete);
|
|
123
|
+
keysToDeleteArray.forEach(key => {
|
|
124
|
+
// key: full db key
|
|
125
|
+
// keys: PK and/or SK keys
|
|
126
|
+
const primaryKeyValues = keys
|
|
127
|
+
.map(field => itemsMap[key].model[field])
|
|
128
|
+
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
129
|
+
collection.delete(primaryKeyValues);
|
|
197
130
|
});
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
202
|
-
var result, collection, keysToDelete, keysToSave, allItemsKeys, itemsMap, _loop_1, this_1, items_1, items_1_1, item, existingRecordsMap, existingRecordsKeys, allItemsKeys_1, allItemsKeys_1_1, key;
|
|
203
|
-
var e_2, _a, e_3, _b;
|
|
204
|
-
var _this = this;
|
|
205
|
-
return __generator(this, function (_c) {
|
|
206
|
-
switch (_c.label) {
|
|
207
|
-
case 0:
|
|
208
|
-
if (items.length === 0) {
|
|
209
|
-
return [2 /*return*/, []];
|
|
210
|
-
}
|
|
211
|
-
result = [];
|
|
212
|
-
collection = this.getCollectionIndex(storeName);
|
|
213
|
-
keysToDelete = new Set();
|
|
214
|
-
keysToSave = new Set();
|
|
215
|
-
allItemsKeys = [];
|
|
216
|
-
itemsMap = {};
|
|
217
|
-
_loop_1 = function (item) {
|
|
218
|
-
// Extract keys from concatenated key path, map to item values
|
|
219
|
-
var keyValues = keys.map(function (field) { return item[field]; });
|
|
220
|
-
var _deleted = item._deleted;
|
|
221
|
-
// If id is in the store, retrieve, otherwise generate new ULID
|
|
222
|
-
var ulid = collection.get(keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
|
|
223
|
-
this_1.getMonotonicFactory(storeName)();
|
|
224
|
-
// Generate the "longer key" for the item
|
|
225
|
-
var key = this_1.getKeyForItem(storeName, keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR), ulid);
|
|
226
|
-
allItemsKeys.push(key);
|
|
227
|
-
itemsMap[key] = { ulid: ulid, model: item };
|
|
228
|
-
if (_deleted) {
|
|
229
|
-
keysToDelete.add(key);
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
keysToSave.add(key);
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
this_1 = this;
|
|
236
|
-
try {
|
|
237
|
-
/* Populate allItemKeys, keysToDelete, and keysToSave */
|
|
238
|
-
for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
239
|
-
item = items_1_1.value;
|
|
240
|
-
_loop_1(item);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
244
|
-
finally {
|
|
245
|
-
try {
|
|
246
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
247
|
-
}
|
|
248
|
-
finally { if (e_2) throw e_2.error; }
|
|
249
|
-
}
|
|
250
|
-
return [4 /*yield*/, this.storage.multiGet(allItemsKeys)];
|
|
251
|
-
case 1:
|
|
252
|
-
existingRecordsMap = _c.sent();
|
|
253
|
-
existingRecordsKeys = existingRecordsMap
|
|
254
|
-
.filter(function (_a) {
|
|
255
|
-
var _b = __read(_a, 2), v = _b[1];
|
|
256
|
-
return !!v;
|
|
257
|
-
})
|
|
258
|
-
.reduce(function (set, _a) {
|
|
259
|
-
var _b = __read(_a, 1), k = _b[0];
|
|
260
|
-
return set.add(k);
|
|
261
|
-
}, new Set());
|
|
262
|
-
// Delete
|
|
263
|
-
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
264
|
-
if (keysToDelete.size === 0) {
|
|
265
|
-
resolve();
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
var keysToDeleteArray = Array.from(keysToDelete);
|
|
269
|
-
keysToDeleteArray.forEach(function (key) {
|
|
270
|
-
// key: full db key
|
|
271
|
-
// keys: PK and/or SK keys
|
|
272
|
-
var primaryKeyValues = keys
|
|
273
|
-
.map(function (field) { return itemsMap[key].model[field]; })
|
|
274
|
-
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
275
|
-
collection.delete(primaryKeyValues);
|
|
276
|
-
});
|
|
277
|
-
_this.storage.multiRemove(keysToDeleteArray, function (errors) {
|
|
278
|
-
if (errors && errors.length > 0) {
|
|
279
|
-
reject(errors);
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
resolve();
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
})];
|
|
286
|
-
case 2:
|
|
287
|
-
// Delete
|
|
288
|
-
_c.sent();
|
|
289
|
-
// Save
|
|
290
|
-
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
291
|
-
if (keysToSave.size === 0) {
|
|
292
|
-
resolve();
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
var entriesToSet = Array.from(keysToSave).map(function (key) { return [
|
|
296
|
-
key,
|
|
297
|
-
JSON.stringify(itemsMap[key].model),
|
|
298
|
-
]; });
|
|
299
|
-
keysToSave.forEach(function (key) {
|
|
300
|
-
var _a = itemsMap[key], model = _a.model, ulid = _a.ulid;
|
|
301
|
-
// Retrieve values from model, use as key for collection index
|
|
302
|
-
var keyValues = keys
|
|
303
|
-
.map(function (field) { return model[field]; })
|
|
304
|
-
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
305
|
-
collection.set(keyValues, ulid);
|
|
306
|
-
});
|
|
307
|
-
_this.storage.multiSet(entriesToSet, function (errors) {
|
|
308
|
-
if (errors && errors.length > 0) {
|
|
309
|
-
reject(errors);
|
|
310
|
-
}
|
|
311
|
-
else {
|
|
312
|
-
resolve();
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
})];
|
|
316
|
-
case 3:
|
|
317
|
-
// Save
|
|
318
|
-
_c.sent();
|
|
319
|
-
try {
|
|
320
|
-
for (allItemsKeys_1 = __values(allItemsKeys), allItemsKeys_1_1 = allItemsKeys_1.next(); !allItemsKeys_1_1.done; allItemsKeys_1_1 = allItemsKeys_1.next()) {
|
|
321
|
-
key = allItemsKeys_1_1.value;
|
|
322
|
-
if (keysToDelete.has(key) && existingRecordsKeys.has(key)) {
|
|
323
|
-
result.push([itemsMap[key].model, OpType.DELETE]);
|
|
324
|
-
}
|
|
325
|
-
else if (keysToSave.has(key)) {
|
|
326
|
-
result.push([
|
|
327
|
-
itemsMap[key].model,
|
|
328
|
-
existingRecordsKeys.has(key) ? OpType.UPDATE : OpType.INSERT,
|
|
329
|
-
]);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
334
|
-
finally {
|
|
335
|
-
try {
|
|
336
|
-
if (allItemsKeys_1_1 && !allItemsKeys_1_1.done && (_b = allItemsKeys_1.return)) _b.call(allItemsKeys_1);
|
|
337
|
-
}
|
|
338
|
-
finally { if (e_3) throw e_3.error; }
|
|
339
|
-
}
|
|
340
|
-
return [2 /*return*/, result];
|
|
131
|
+
this.storage.multiRemove(keysToDeleteArray, (errors) => {
|
|
132
|
+
if (errors && errors.length > 0) {
|
|
133
|
+
reject(errors);
|
|
341
134
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
};
|
|
345
|
-
AsyncStorageDatabase.prototype.get = function (keyValuePath, storeName) {
|
|
346
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
347
|
-
var ulid, itemKey, recordAsString, record;
|
|
348
|
-
return __generator(this, function (_a) {
|
|
349
|
-
switch (_a.label) {
|
|
350
|
-
case 0:
|
|
351
|
-
ulid = this.getCollectionIndex(storeName).get(keyValuePath);
|
|
352
|
-
itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
|
|
353
|
-
return [4 /*yield*/, this.storage.getItem(itemKey)];
|
|
354
|
-
case 1:
|
|
355
|
-
recordAsString = _a.sent();
|
|
356
|
-
record = recordAsString && JSON.parse(recordAsString);
|
|
357
|
-
return [2 /*return*/, record];
|
|
135
|
+
else {
|
|
136
|
+
resolve();
|
|
358
137
|
}
|
|
359
138
|
});
|
|
360
139
|
});
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
return [id, ulid];
|
|
387
|
-
})()
|
|
388
|
-
: (function () {
|
|
389
|
-
var e_5, _a, _b;
|
|
390
|
-
var id, ulid;
|
|
391
|
-
try {
|
|
392
|
-
for (var collection_2 = __values(collection), collection_2_1 = collection_2.next(); !collection_2_1.done; collection_2_1 = collection_2.next()) {
|
|
393
|
-
_b = __read(collection_2_1.value, 2), id = _b[0], ulid = _b[1];
|
|
394
|
-
;
|
|
395
|
-
} // Get last element of the set
|
|
396
|
-
}
|
|
397
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
398
|
-
finally {
|
|
399
|
-
try {
|
|
400
|
-
if (collection_2_1 && !collection_2_1.done && (_a = collection_2.return)) _a.call(collection_2);
|
|
401
|
-
}
|
|
402
|
-
finally { if (e_5) throw e_5.error; }
|
|
403
|
-
}
|
|
404
|
-
return [id, ulid];
|
|
405
|
-
})(), 2), itemId = _a[0], ulid = _a[1];
|
|
406
|
-
itemKey = this.getKeyForItem(storeName, itemId, ulid);
|
|
407
|
-
_b = itemKey;
|
|
408
|
-
if (!_b) return [3 /*break*/, 2];
|
|
409
|
-
return [4 /*yield*/, this.storage.getItem(itemKey)];
|
|
410
|
-
case 1:
|
|
411
|
-
_b = (_c.sent());
|
|
412
|
-
_c.label = 2;
|
|
413
|
-
case 2:
|
|
414
|
-
itemString = _b;
|
|
415
|
-
result = itemString ? JSON.parse(itemString) || undefined : undefined;
|
|
416
|
-
return [2 /*return*/, result];
|
|
140
|
+
// Save
|
|
141
|
+
await new Promise((resolve, reject) => {
|
|
142
|
+
if (keysToSave.size === 0) {
|
|
143
|
+
resolve();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const entriesToSet = Array.from(keysToSave).map(key => [
|
|
147
|
+
key,
|
|
148
|
+
JSON.stringify(itemsMap[key].model),
|
|
149
|
+
]);
|
|
150
|
+
keysToSave.forEach(key => {
|
|
151
|
+
const { model, ulid } = itemsMap[key];
|
|
152
|
+
// Retrieve values from model, use as key for collection index
|
|
153
|
+
const keyValues = keys
|
|
154
|
+
.map(field => model[field])
|
|
155
|
+
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
156
|
+
collection.set(keyValues, ulid);
|
|
157
|
+
});
|
|
158
|
+
this.storage.multiSet(entriesToSet, (errors) => {
|
|
159
|
+
if (errors && errors.length > 0) {
|
|
160
|
+
reject(errors);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
resolve();
|
|
417
164
|
}
|
|
418
165
|
});
|
|
419
166
|
});
|
|
420
|
-
|
|
167
|
+
for (const key of allItemsKeys) {
|
|
168
|
+
if (keysToDelete.has(key) && existingRecordsKeys.has(key)) {
|
|
169
|
+
result.push([itemsMap[key].model, OpType.DELETE]);
|
|
170
|
+
}
|
|
171
|
+
else if (keysToSave.has(key)) {
|
|
172
|
+
result.push([
|
|
173
|
+
itemsMap[key].model,
|
|
174
|
+
existingRecordsKeys.has(key) ? OpType.UPDATE : OpType.INSERT,
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
async get(keyValuePath, storeName) {
|
|
181
|
+
const ulid = this.getCollectionIndex(storeName).get(keyValuePath);
|
|
182
|
+
const itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
|
|
183
|
+
const recordAsString = await this.storage.getItem(itemKey);
|
|
184
|
+
const record = recordAsString && JSON.parse(recordAsString);
|
|
185
|
+
return record;
|
|
186
|
+
}
|
|
187
|
+
async getOne(firstOrLast, storeName) {
|
|
188
|
+
const collection = this.getCollectionIndex(storeName);
|
|
189
|
+
const [itemId, ulid] = firstOrLast === QueryOne.FIRST
|
|
190
|
+
? (() => {
|
|
191
|
+
let id, ulid;
|
|
192
|
+
for ([id, ulid] of collection)
|
|
193
|
+
break; // Get first element of the set
|
|
194
|
+
return [id, ulid];
|
|
195
|
+
})()
|
|
196
|
+
: (() => {
|
|
197
|
+
let id, ulid;
|
|
198
|
+
for ([id, ulid] of collection)
|
|
199
|
+
; // Get last element of the set
|
|
200
|
+
return [id, ulid];
|
|
201
|
+
})();
|
|
202
|
+
const itemKey = this.getKeyForItem(storeName, itemId, ulid);
|
|
203
|
+
const itemString = itemKey && (await this.storage.getItem(itemKey));
|
|
204
|
+
const result = itemString ? JSON.parse(itemString) || undefined : undefined;
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
421
207
|
/**
|
|
422
208
|
* This function gets all the records stored in async storage for a particular storeName
|
|
423
209
|
* It then loads all the records for that filtered set of keys using multiGet()
|
|
424
210
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (collection_3_1 && !collection_3_1.done && (_e = collection_3.return)) _e.call(collection_3);
|
|
455
|
-
}
|
|
456
|
-
finally { if (e_6) throw e_6.error; }
|
|
457
|
-
}
|
|
458
|
-
return [4 /*yield*/, this.storage.multiGet(keysForStore)];
|
|
459
|
-
case 1:
|
|
460
|
-
storeRecordStrings = _f.sent();
|
|
461
|
-
records = storeRecordStrings
|
|
462
|
-
.filter(function (_a) {
|
|
463
|
-
var _b = __read(_a, 2), value = _b[1];
|
|
464
|
-
return value;
|
|
465
|
-
})
|
|
466
|
-
.map(function (_a) {
|
|
467
|
-
var _b = __read(_a, 2), value = _b[1];
|
|
468
|
-
return JSON.parse(value);
|
|
469
|
-
});
|
|
470
|
-
return [2 /*return*/, records];
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
});
|
|
474
|
-
};
|
|
475
|
-
AsyncStorageDatabase.prototype.delete = function (key, storeName) {
|
|
476
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
477
|
-
var ulid, itemKey;
|
|
478
|
-
return __generator(this, function (_a) {
|
|
479
|
-
switch (_a.label) {
|
|
480
|
-
case 0:
|
|
481
|
-
ulid = this.getCollectionIndex(storeName).get(key);
|
|
482
|
-
itemKey = this.getKeyForItem(storeName, key, ulid);
|
|
483
|
-
this.getCollectionIndex(storeName).delete(key);
|
|
484
|
-
return [4 /*yield*/, this.storage.removeItem(itemKey)];
|
|
485
|
-
case 1:
|
|
486
|
-
_a.sent();
|
|
487
|
-
return [2 /*return*/];
|
|
488
|
-
}
|
|
489
|
-
});
|
|
490
|
-
});
|
|
491
|
-
};
|
|
211
|
+
async getAll(storeName, pagination) {
|
|
212
|
+
const collection = this.getCollectionIndex(storeName);
|
|
213
|
+
const { page = 0, limit = 0 } = pagination || {};
|
|
214
|
+
const start = Math.max(0, page * limit) || 0;
|
|
215
|
+
const end = limit > 0 ? start + limit : undefined;
|
|
216
|
+
const keysForStore = [];
|
|
217
|
+
let count = 0;
|
|
218
|
+
for (const [id, ulid] of collection) {
|
|
219
|
+
count++;
|
|
220
|
+
if (count <= start) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
keysForStore.push(this.getKeyForItem(storeName, id, ulid));
|
|
224
|
+
if (count === end) {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const storeRecordStrings = await this.storage.multiGet(keysForStore);
|
|
229
|
+
const records = storeRecordStrings
|
|
230
|
+
.filter(([, value]) => value)
|
|
231
|
+
.map(([, value]) => JSON.parse(value));
|
|
232
|
+
return records;
|
|
233
|
+
}
|
|
234
|
+
async delete(key, storeName) {
|
|
235
|
+
const ulid = this.getCollectionIndex(storeName).get(key);
|
|
236
|
+
const itemKey = this.getKeyForItem(storeName, key, ulid);
|
|
237
|
+
this.getCollectionIndex(storeName).delete(key);
|
|
238
|
+
await this.storage.removeItem(itemKey);
|
|
239
|
+
}
|
|
492
240
|
/**
|
|
493
241
|
* Clear the AsyncStorage of all DataStore entries
|
|
494
242
|
*/
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
});
|
|
512
|
-
};
|
|
513
|
-
AsyncStorageDatabase.prototype.getKeyForItem = function (storeName, id, ulid) {
|
|
514
|
-
return "".concat(this.getKeyPrefixForStoreItems(storeName), "::").concat(ulid, "::").concat(id);
|
|
515
|
-
};
|
|
516
|
-
AsyncStorageDatabase.prototype.getLegacyKeyForItem = function (storeName, id) {
|
|
517
|
-
return "".concat(this.getKeyPrefixForStoreItems(storeName), "::").concat(id);
|
|
518
|
-
};
|
|
519
|
-
AsyncStorageDatabase.prototype.getKeyPrefixForStoreItems = function (storeName) {
|
|
520
|
-
return "".concat(DB_NAME, "::").concat(storeName, "::").concat(DATA);
|
|
521
|
-
};
|
|
522
|
-
return AsyncStorageDatabase;
|
|
523
|
-
}());
|
|
243
|
+
async clear() {
|
|
244
|
+
const allKeys = await this.storage.getAllKeys();
|
|
245
|
+
const allDataStoreKeys = allKeys.filter(key => key.startsWith(DB_NAME));
|
|
246
|
+
await this.storage.multiRemove(allDataStoreKeys);
|
|
247
|
+
this._collectionInMemoryIndex.clear();
|
|
248
|
+
}
|
|
249
|
+
getKeyForItem(storeName, id, ulid) {
|
|
250
|
+
return `${this.getKeyPrefixForStoreItems(storeName)}::${ulid}::${id}`;
|
|
251
|
+
}
|
|
252
|
+
getLegacyKeyForItem(storeName, id) {
|
|
253
|
+
return `${this.getKeyPrefixForStoreItems(storeName)}::${id}`;
|
|
254
|
+
}
|
|
255
|
+
getKeyPrefixForStoreItems(storeName) {
|
|
256
|
+
return `${DB_NAME}::${storeName}::${DATA}`;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
524
259
|
export default AsyncStorageDatabase;
|