-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAsyncStorageDatabase.ts
364 lines (286 loc) · 9.56 KB
/
AsyncStorageDatabase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ULID } from 'ulid';
import {
ModelInstanceMetadata,
OpType,
PaginationInput,
PersistentModel,
QueryOne,
} from '../../types';
import {
DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR,
indexNameFromKeys,
monotonicUlidFactory,
} from '../../util';
import { createInMemoryStore } from './InMemoryStore';
const DB_NAME = '@AmplifyDatastore';
const COLLECTION = 'Collection';
const DATA = 'Data';
const monotonicFactoriesMap = new Map<string, ULID>();
class AsyncStorageDatabase {
/**
* Maps storeNames to a map of ulid->id
*/
private _collectionInMemoryIndex = new Map<string, Map<string, string>>();
private storage = createInMemoryStore();
/**
* Collection index is map of stores (i.e. sync, metadata, mutation event, and data)
* @param storeName {string} - Name of the store
* @returns Map of ulid->id
*/
private getCollectionIndex(storeName: string) {
if (!this._collectionInMemoryIndex.has(storeName)) {
this._collectionInMemoryIndex.set(storeName, new Map());
}
return this._collectionInMemoryIndex.get(storeName);
}
/**
* Return ULID for store if it exists, otherwise create a new one
* @param storeName {string} - Name of the store
* @returns ulid
*/
private getMonotonicFactory(storeName: string): ULID {
if (!monotonicFactoriesMap.has(storeName)) {
monotonicFactoriesMap.set(storeName, monotonicUlidFactory());
}
return monotonicFactoriesMap.get(storeName)!;
}
async init(): Promise<void> {
this._collectionInMemoryIndex.clear();
const allKeys: string[] = await this.storage.getAllKeys();
const keysForCollectionEntries: string[] = [];
for (const key of allKeys) {
const [dbName, storeName, recordType, ulidOrId, id] = key.split('::');
if (dbName === DB_NAME) {
if (recordType === DATA) {
let ulid: string;
if (id === undefined) {
// It is an old entry (without ulid). Need to migrate to new key format
const resolvedId = ulidOrId;
const newUlid = this.getMonotonicFactory(storeName)();
const oldKey = this.getLegacyKeyForItem(storeName, resolvedId);
const newKey = this.getKeyForItem(storeName, resolvedId, newUlid);
const item = await this.storage.getItem(oldKey);
await this.storage.setItem(newKey, item!);
await this.storage.removeItem(oldKey);
ulid = newUlid;
} else {
ulid = ulidOrId;
}
this.getCollectionIndex(storeName)!.set(id, ulid);
} else if (recordType === COLLECTION) {
keysForCollectionEntries.push(key);
}
}
}
if (keysForCollectionEntries.length > 0) {
await this.storage.multiRemove(keysForCollectionEntries);
}
}
async save<T extends PersistentModel>(
item: T,
storeName: string,
keys: string[],
keyValuesPath: string,
) {
const idxName = indexNameFromKeys(keys);
const ulid =
this.getCollectionIndex(storeName)?.get(idxName) ||
this.getMonotonicFactory(storeName)();
// Retrieve db key for item
const itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
// Set key in collection index
this.getCollectionIndex(storeName)?.set(keyValuesPath, ulid);
// Save item in db
await this.storage.setItem(itemKey, JSON.stringify(item));
}
async batchSave<T extends PersistentModel>(
storeName: string,
items: ModelInstanceMetadata[],
keys: string[],
): Promise<[T, OpType][]> {
if (items.length === 0) {
return [];
}
const result: [T, OpType][] = [];
const collection = this.getCollectionIndex(storeName)!;
const keysToDelete = new Set<string>();
const keysToSave = new Set<string>();
const allItemsKeys: string[] = [];
const itemsMap: Record<string, { ulid: string; model: T }> = {};
/* Populate allItemKeys, keysToDelete, and keysToSave */
for (const item of items) {
// Extract keys from concatenated key path, map to item values
const keyValues = keys.map(field => item[field]);
const { _deleted } = item;
// If id is in the store, retrieve, otherwise generate new ULID
const ulid =
collection.get(keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
this.getMonotonicFactory(storeName)();
// Generate the "longer key" for the item
const key = this.getKeyForItem(
storeName,
keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR),
ulid,
);
allItemsKeys.push(key);
itemsMap[key] = { ulid, model: item as unknown as T };
if (_deleted) {
keysToDelete.add(key);
} else {
keysToSave.add(key);
}
}
const existingRecordsMap: [string, string][] =
await this.storage.multiGet(allItemsKeys);
const existingRecordsKeys = existingRecordsMap
.filter(([, v]) => !!v)
.reduce((set, [k]) => set.add(k), new Set<string>());
// Delete
await new Promise<void>((resolve, reject) => {
if (keysToDelete.size === 0) {
resolve();
return;
}
const keysToDeleteArray = Array.from(keysToDelete);
keysToDeleteArray.forEach(key => {
// key: full db key
// keys: PK and/or SK keys
const primaryKeyValues: string = keys
.map(field => itemsMap[key].model[field])
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
collection.delete(primaryKeyValues);
});
this.storage.multiRemove(keysToDeleteArray, (errors?: Error[]) => {
if (errors && errors.length > 0) {
reject(errors);
} else {
resolve();
}
});
});
// Save
await new Promise<void>((resolve, reject) => {
if (keysToSave.size === 0) {
resolve();
return;
}
const entriesToSet = Array.from(keysToSave).map(key => [
key,
JSON.stringify(itemsMap[key].model),
]);
keysToSave.forEach(key => {
const { model, ulid } = itemsMap[key];
// Retrieve values from model, use as key for collection index
const keyValues: string = keys
.map(field => model[field])
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
collection.set(keyValues, ulid);
});
this.storage.multiSet(entriesToSet, (errors?: Error[]) => {
if (errors && errors.length > 0) {
reject(errors);
} else {
resolve();
}
});
});
for (const key of allItemsKeys) {
if (keysToDelete.has(key) && existingRecordsKeys.has(key)) {
result.push([itemsMap[key].model, OpType.DELETE]);
} else if (keysToSave.has(key)) {
result.push([
itemsMap[key].model,
existingRecordsKeys.has(key) ? OpType.UPDATE : OpType.INSERT,
]);
}
}
return result;
}
async get<T extends PersistentModel>(
keyValuePath: string,
storeName: string,
): Promise<T> {
const ulid = this.getCollectionIndex(storeName)!.get(keyValuePath)!;
const itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
const recordAsString = await this.storage.getItem(itemKey);
const record = recordAsString && JSON.parse(recordAsString);
return record;
}
async getOne(firstOrLast: QueryOne, storeName: string) {
const collection = this.getCollectionIndex(storeName)!;
const [itemId, ulid] =
firstOrLast === QueryOne.FIRST
? (() => {
let resolvedId: string, resolvedUlid: string;
// eslint-disable-next-line no-unreachable-loop
for ([resolvedId, resolvedUlid] of collection) break; // Get first element of the set
return [resolvedId!, resolvedUlid!];
})()
: (() => {
let resolvedId: string, resolvedUlid: string;
for ([resolvedId, resolvedUlid] of collection); // Get last element of the set
return [resolvedId!, resolvedUlid!];
})();
const itemKey = this.getKeyForItem(storeName, itemId, ulid);
const itemString = itemKey && (await this.storage.getItem(itemKey));
const result = itemString ? JSON.parse(itemString) || undefined : undefined;
return result;
}
/**
* This function gets all the records stored in async storage for a particular storeName
* It then loads all the records for that filtered set of keys using multiGet()
*/
async getAll<T extends PersistentModel>(
storeName: string,
pagination?: PaginationInput<T>,
): Promise<T[]> {
const collection = this.getCollectionIndex(storeName)!;
const { page = 0, limit = 0 } = pagination || {};
const start = Math.max(0, page * limit) || 0;
const end = limit > 0 ? start + limit : undefined;
const keysForStore: string[] = [];
let count = 0;
for (const [id, ulid] of collection) {
count++;
if (count <= start) {
continue;
}
keysForStore.push(this.getKeyForItem(storeName, id, ulid));
if (count === end) {
break;
}
}
const storeRecordStrings = await this.storage.multiGet(keysForStore);
const records = storeRecordStrings
.filter(([, value]) => value)
.map(([, value]) => JSON.parse(value));
return records;
}
async delete(key: string, storeName: string) {
const ulid = this.getCollectionIndex(storeName)!.get(key)!;
const itemKey = this.getKeyForItem(storeName, key, ulid);
this.getCollectionIndex(storeName)!.delete(key);
await this.storage.removeItem(itemKey);
}
/**
* Clear the AsyncStorage of all DataStore entries
*/
async clear() {
const allKeys = await this.storage.getAllKeys();
const allDataStoreKeys = allKeys.filter(key => key.startsWith(DB_NAME));
await this.storage.multiRemove(allDataStoreKeys);
this._collectionInMemoryIndex.clear();
}
private getKeyForItem(storeName: string, id: string, ulid: string): string {
return `${this.getKeyPrefixForStoreItems(storeName)}::${ulid}::${id}`;
}
private getLegacyKeyForItem(storeName: string, id: string): string {
return `${this.getKeyPrefixForStoreItems(storeName)}::${id}`;
}
private getKeyPrefixForStoreItems(storeName: string): string {
return `${DB_NAME}::${storeName}::${DATA}`;
}
}
export default AsyncStorageDatabase;