@aws-amplify/datastore 4.7.6-api-v6-models.b3abc9b.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +4 -0
  2. package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
  3. package/lib/authModeStrategies/index.js +3 -3
  4. package/lib/authModeStrategies/multiAuthStrategy.js +38 -53
  5. package/lib/datastore/datastore.d.ts +4 -5
  6. package/lib/datastore/datastore.js +929 -1284
  7. package/lib/index.d.ts +1 -1
  8. package/lib/index.js +26 -13
  9. package/lib/predicates/index.js +54 -69
  10. package/lib/predicates/next.d.ts +2 -2
  11. package/lib/predicates/next.js +313 -462
  12. package/lib/predicates/sort.js +24 -28
  13. package/lib/ssr/index.js +2 -2
  14. package/lib/storage/adapter/AsyncStorageAdapter.js +120 -342
  15. package/lib/storage/adapter/AsyncStorageDatabase.js +217 -421
  16. package/lib/storage/adapter/InMemoryStore.js +28 -51
  17. package/lib/storage/adapter/InMemoryStore.native.js +5 -3
  18. package/lib/storage/adapter/IndexedDBAdapter.js +466 -871
  19. package/lib/storage/adapter/StorageAdapterBase.js +180 -330
  20. package/lib/storage/adapter/getDefaultAdapter/index.js +8 -10
  21. package/lib/storage/adapter/getDefaultAdapter/index.native.js +5 -4
  22. package/lib/storage/adapter/index.js +0 -1
  23. package/lib/storage/relationship.js +177 -253
  24. package/lib/storage/storage.d.ts +4 -4
  25. package/lib/storage/storage.js +255 -433
  26. package/lib/sync/datastoreConnectivity.d.ts +2 -2
  27. package/lib/sync/datastoreConnectivity.js +29 -39
  28. package/lib/sync/datastoreReachability/index.d.ts +1 -3
  29. package/lib/sync/datastoreReachability/index.js +3 -3
  30. package/lib/sync/datastoreReachability/index.native.d.ts +1 -3
  31. package/lib/sync/datastoreReachability/index.native.js +4 -5
  32. package/lib/sync/index.d.ts +2 -2
  33. package/lib/sync/index.js +522 -827
  34. package/lib/sync/merger.js +31 -63
  35. package/lib/sync/outbox.js +148 -232
  36. package/lib/sync/processors/errorMaps.d.ts +1 -1
  37. package/lib/sync/processors/errorMaps.js +30 -47
  38. package/lib/sync/processors/mutation.d.ts +2 -2
  39. package/lib/sync/processors/mutation.js +343 -502
  40. package/lib/sync/processors/subscription.d.ts +5 -2
  41. package/lib/sync/processors/subscription.js +283 -437
  42. package/lib/sync/processors/sync.d.ts +2 -2
  43. package/lib/sync/processors/sync.js +279 -404
  44. package/lib/sync/utils.d.ts +5 -4
  45. package/lib/sync/utils.js +267 -320
  46. package/lib/tsconfig.tsbuildinfo +1 -0
  47. package/lib/types.d.ts +138 -140
  48. package/lib/types.js +17 -24
  49. package/lib/util.d.ts +9 -17
  50. package/lib/util.js +387 -511
  51. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -2
  52. package/lib-esm/authModeStrategies/index.js +0 -1
  53. package/lib-esm/authModeStrategies/multiAuthStrategy.js +35 -52
  54. package/lib-esm/datastore/datastore.d.ts +4 -5
  55. package/lib-esm/datastore/datastore.js +888 -1247
  56. package/lib-esm/index.d.ts +1 -1
  57. package/lib-esm/index.js +6 -7
  58. package/lib-esm/predicates/index.js +53 -70
  59. package/lib-esm/predicates/next.d.ts +2 -2
  60. package/lib-esm/predicates/next.js +306 -459
  61. package/lib-esm/predicates/sort.js +23 -28
  62. package/lib-esm/ssr/index.js +1 -2
  63. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -338
  64. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -416
  65. package/lib-esm/storage/adapter/InMemoryStore.js +27 -52
  66. package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
  67. package/lib-esm/storage/adapter/IndexedDBAdapter.js +438 -866
  68. package/lib-esm/storage/adapter/StorageAdapterBase.js +173 -325
  69. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -6
  70. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -2
  71. package/lib-esm/storage/adapter/index.js +1 -1
  72. package/lib-esm/storage/relationship.js +173 -251
  73. package/lib-esm/storage/storage.d.ts +4 -4
  74. package/lib-esm/storage/storage.js +242 -424
  75. package/lib-esm/sync/datastoreConnectivity.d.ts +2 -2
  76. package/lib-esm/sync/datastoreConnectivity.js +28 -39
  77. package/lib-esm/sync/datastoreReachability/index.d.ts +1 -3
  78. package/lib-esm/sync/datastoreReachability/index.js +2 -3
  79. package/lib-esm/sync/datastoreReachability/index.native.d.ts +1 -3
  80. package/lib-esm/sync/datastoreReachability/index.native.js +3 -4
  81. package/lib-esm/sync/index.d.ts +2 -2
  82. package/lib-esm/sync/index.js +502 -812
  83. package/lib-esm/sync/merger.js +28 -61
  84. package/lib-esm/sync/outbox.js +143 -228
  85. package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
  86. package/lib-esm/sync/processors/errorMaps.js +32 -50
  87. package/lib-esm/sync/processors/mutation.d.ts +2 -2
  88. package/lib-esm/sync/processors/mutation.js +329 -490
  89. package/lib-esm/sync/processors/subscription.d.ts +5 -2
  90. package/lib-esm/sync/processors/subscription.js +266 -421
  91. package/lib-esm/sync/processors/sync.d.ts +2 -2
  92. package/lib-esm/sync/processors/sync.js +271 -397
  93. package/lib-esm/sync/utils.d.ts +5 -4
  94. package/lib-esm/sync/utils.js +252 -307
  95. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  96. package/lib-esm/types.d.ts +138 -140
  97. package/lib-esm/types.js +16 -25
  98. package/lib-esm/util.d.ts +9 -17
  99. package/lib-esm/util.js +335 -497
  100. package/package.json +31 -26
  101. package/src/authModeStrategies/multiAuthStrategy.ts +15 -12
  102. package/src/datastore/datastore.ts +36 -35
  103. package/src/predicates/sort.ts +3 -1
  104. package/src/storage/adapter/InMemoryStore.ts +1 -1
  105. package/src/storage/adapter/IndexedDBAdapter.ts +2 -2
  106. package/src/storage/adapter/StorageAdapterBase.ts +2 -2
  107. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -4
  108. package/src/storage/storage.ts +29 -24
  109. package/src/sync/datastoreConnectivity.ts +6 -6
  110. package/src/sync/datastoreReachability/index.native.ts +5 -3
  111. package/src/sync/datastoreReachability/index.ts +1 -1
  112. package/src/sync/index.ts +79 -89
  113. package/src/sync/processors/errorMaps.ts +7 -7
  114. package/src/sync/processors/mutation.ts +19 -13
  115. package/src/sync/processors/subscription.ts +221 -295
  116. package/src/sync/processors/sync.ts +11 -8
  117. package/src/sync/utils.ts +30 -15
  118. package/src/types.ts +4 -8
  119. package/src/util.ts +46 -9
  120. package/lib/.tsbuildinfo +0 -3
  121. package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  122. package/lib/authModeStrategies/index.js.map +0 -1
  123. package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
  124. package/lib/datastore/datastore.js.map +0 -1
  125. package/lib/index.js.map +0 -1
  126. package/lib/predicates/index.js.map +0 -1
  127. package/lib/predicates/next.js.map +0 -1
  128. package/lib/predicates/sort.js.map +0 -1
  129. package/lib/ssr/index.js.map +0 -1
  130. package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  131. package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  132. package/lib/storage/adapter/InMemoryStore.js.map +0 -1
  133. package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
  134. package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
  135. package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
  136. package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  137. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  138. package/lib/storage/adapter/index.js.map +0 -1
  139. package/lib/storage/relationship.js.map +0 -1
  140. package/lib/storage/storage.js.map +0 -1
  141. package/lib/sync/datastoreConnectivity.js.map +0 -1
  142. package/lib/sync/datastoreReachability/index.js.map +0 -1
  143. package/lib/sync/datastoreReachability/index.native.js.map +0 -1
  144. package/lib/sync/index.js.map +0 -1
  145. package/lib/sync/merger.js.map +0 -1
  146. package/lib/sync/outbox.js.map +0 -1
  147. package/lib/sync/processors/errorMaps.js.map +0 -1
  148. package/lib/sync/processors/mutation.js.map +0 -1
  149. package/lib/sync/processors/subscription.js.map +0 -1
  150. package/lib/sync/processors/sync.js.map +0 -1
  151. package/lib/sync/utils.js.map +0 -1
  152. package/lib/types.js.map +0 -1
  153. package/lib/util.js.map +0 -1
  154. package/lib-esm/.tsbuildinfo +0 -3
  155. package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  156. package/lib-esm/authModeStrategies/index.js.map +0 -1
  157. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
  158. package/lib-esm/datastore/datastore.js.map +0 -1
  159. package/lib-esm/index.js.map +0 -1
  160. package/lib-esm/predicates/index.js.map +0 -1
  161. package/lib-esm/predicates/next.js.map +0 -1
  162. package/lib-esm/predicates/sort.js.map +0 -1
  163. package/lib-esm/ssr/index.js.map +0 -1
  164. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  165. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  166. package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
  167. package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
  168. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
  169. package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
  170. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  171. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  172. package/lib-esm/storage/adapter/index.js.map +0 -1
  173. package/lib-esm/storage/relationship.js.map +0 -1
  174. package/lib-esm/storage/storage.js.map +0 -1
  175. package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
  176. package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
  177. package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
  178. package/lib-esm/sync/index.js.map +0 -1
  179. package/lib-esm/sync/merger.js.map +0 -1
  180. package/lib-esm/sync/outbox.js.map +0 -1
  181. package/lib-esm/sync/processors/errorMaps.js.map +0 -1
  182. package/lib-esm/sync/processors/mutation.js.map +0 -1
  183. package/lib-esm/sync/processors/subscription.js.map +0 -1
  184. package/lib-esm/sync/processors/sync.js.map +0 -1
  185. package/lib-esm/sync/utils.js.map +0 -1
  186. package/lib-esm/types.js.map +0 -1
  187. package/lib-esm/util.js.map +0 -1
@@ -1,465 +1,261 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var tslib_1 = require("tslib");
4
- var types_1 = require("../../types");
5
- var util_1 = require("../../util");
6
- var InMemoryStore_1 = require("./InMemoryStore");
7
- var DB_NAME = '@AmplifyDatastore';
8
- var COLLECTION = 'Collection';
9
- var DATA = 'Data';
10
- var monotonicFactoriesMap = new Map();
11
- var AsyncStorageDatabase = /** @class */ (function () {
12
- function AsyncStorageDatabase() {
3
+ const types_1 = require("../../types");
4
+ const util_1 = require("../../util");
5
+ const InMemoryStore_1 = require("./InMemoryStore");
6
+ const DB_NAME = '@AmplifyDatastore';
7
+ const COLLECTION = 'Collection';
8
+ const DATA = 'Data';
9
+ const monotonicFactoriesMap = new Map();
10
+ class AsyncStorageDatabase {
11
+ constructor() {
13
12
  /**
14
13
  * Maps storeNames to a map of ulid->id
15
14
  */
16
15
  this._collectionInMemoryIndex = new Map();
17
- this.storage = InMemoryStore_1.createInMemoryStore();
16
+ this.storage = (0, InMemoryStore_1.createInMemoryStore)();
18
17
  }
19
18
  /**
20
19
  * Collection index is map of stores (i.e. sync, metadata, mutation event, and data)
21
20
  * @param storeName {string} - Name of the store
22
21
  * @returns Map of ulid->id
23
22
  */
24
- AsyncStorageDatabase.prototype.getCollectionIndex = function (storeName) {
23
+ getCollectionIndex(storeName) {
25
24
  if (!this._collectionInMemoryIndex.has(storeName)) {
26
25
  this._collectionInMemoryIndex.set(storeName, new Map());
27
26
  }
28
27
  return this._collectionInMemoryIndex.get(storeName);
29
- };
28
+ }
30
29
  /**
31
30
  * Return ULID for store if it exists, otherwise create a new one
32
31
  * @param storeName {string} - Name of the store
33
32
  * @returns ulid
34
33
  */
35
- AsyncStorageDatabase.prototype.getMonotonicFactory = function (storeName) {
34
+ getMonotonicFactory(storeName) {
36
35
  if (!monotonicFactoriesMap.has(storeName)) {
37
- monotonicFactoriesMap.set(storeName, util_1.monotonicUlidFactory());
36
+ monotonicFactoriesMap.set(storeName, (0, util_1.monotonicUlidFactory)());
38
37
  }
39
38
  return monotonicFactoriesMap.get(storeName);
40
- };
41
- AsyncStorageDatabase.prototype.init = function () {
42
- return tslib_1.__awaiter(this, void 0, void 0, function () {
43
- var allKeys, keysForCollectionEntries, allKeys_1, allKeys_1_1, key, _a, dbName, storeName, recordType, ulidOrId, id, ulid, id_1, newUlid, oldKey, newKey, item, e_1_1;
44
- var e_1, _b;
45
- return tslib_1.__generator(this, function (_c) {
46
- switch (_c.label) {
47
- case 0:
48
- this._collectionInMemoryIndex.clear();
49
- return [4 /*yield*/, this.storage.getAllKeys()];
50
- case 1:
51
- allKeys = _c.sent();
52
- keysForCollectionEntries = [];
53
- _c.label = 2;
54
- case 2:
55
- _c.trys.push([2, 12, 13, 14]);
56
- allKeys_1 = tslib_1.__values(allKeys), allKeys_1_1 = allKeys_1.next();
57
- _c.label = 3;
58
- case 3:
59
- if (!!allKeys_1_1.done) return [3 /*break*/, 11];
60
- key = allKeys_1_1.value;
61
- _a = tslib_1.__read(key.split('::'), 5), dbName = _a[0], storeName = _a[1], recordType = _a[2], ulidOrId = _a[3], id = _a[4];
62
- if (!(dbName === DB_NAME)) return [3 /*break*/, 10];
63
- if (!(recordType === DATA)) return [3 /*break*/, 9];
64
- ulid = void 0;
65
- if (!(id === undefined)) return [3 /*break*/, 7];
66
- id_1 = ulidOrId;
67
- newUlid = this.getMonotonicFactory(storeName)();
68
- oldKey = this.getLegacyKeyForItem(storeName, id_1);
69
- newKey = this.getKeyForItem(storeName, id_1, newUlid);
70
- return [4 /*yield*/, this.storage.getItem(oldKey)];
71
- case 4:
72
- item = _c.sent();
73
- return [4 /*yield*/, this.storage.setItem(newKey, item)];
74
- case 5:
75
- _c.sent();
76
- return [4 /*yield*/, this.storage.removeItem(oldKey)];
77
- case 6:
78
- _c.sent();
39
+ }
40
+ async init() {
41
+ this._collectionInMemoryIndex.clear();
42
+ const allKeys = await this.storage.getAllKeys();
43
+ const keysForCollectionEntries = [];
44
+ for (const key of allKeys) {
45
+ const [dbName, storeName, recordType, ulidOrId, id] = key.split('::');
46
+ if (dbName === DB_NAME) {
47
+ if (recordType === DATA) {
48
+ let ulid;
49
+ if (id === undefined) {
50
+ // It is an old entry (without ulid). Need to migrate to new key format
51
+ const id = ulidOrId;
52
+ const newUlid = this.getMonotonicFactory(storeName)();
53
+ const oldKey = this.getLegacyKeyForItem(storeName, id);
54
+ const newKey = this.getKeyForItem(storeName, id, newUlid);
55
+ const item = await this.storage.getItem(oldKey);
56
+ await this.storage.setItem(newKey, item);
57
+ await this.storage.removeItem(oldKey);
79
58
  ulid = newUlid;
80
- return [3 /*break*/, 8];
81
- case 7:
59
+ }
60
+ else {
82
61
  ulid = ulidOrId;
83
- _c.label = 8;
84
- case 8:
85
- this.getCollectionIndex(storeName).set(id, ulid);
86
- return [3 /*break*/, 10];
87
- case 9:
88
- if (recordType === COLLECTION) {
89
- keysForCollectionEntries.push(key);
90
- }
91
- _c.label = 10;
92
- case 10:
93
- allKeys_1_1 = allKeys_1.next();
94
- return [3 /*break*/, 3];
95
- case 11: return [3 /*break*/, 14];
96
- case 12:
97
- e_1_1 = _c.sent();
98
- e_1 = { error: e_1_1 };
99
- return [3 /*break*/, 14];
100
- case 13:
101
- try {
102
- if (allKeys_1_1 && !allKeys_1_1.done && (_b = allKeys_1.return)) _b.call(allKeys_1);
103
- }
104
- finally { if (e_1) throw e_1.error; }
105
- return [7 /*endfinally*/];
106
- case 14:
107
- if (!(keysForCollectionEntries.length > 0)) return [3 /*break*/, 16];
108
- return [4 /*yield*/, this.storage.multiRemove(keysForCollectionEntries)];
109
- case 15:
110
- _c.sent();
111
- _c.label = 16;
112
- case 16: return [2 /*return*/];
62
+ }
63
+ this.getCollectionIndex(storeName).set(id, ulid);
113
64
  }
114
- });
115
- });
116
- };
117
- AsyncStorageDatabase.prototype.save = function (item, storeName, keys, keyValuesPath) {
118
- var _a, _b;
119
- return tslib_1.__awaiter(this, void 0, void 0, function () {
120
- var idxName, ulid, itemKey;
121
- return tslib_1.__generator(this, function (_c) {
122
- switch (_c.label) {
123
- case 0:
124
- idxName = util_1.indexNameFromKeys(keys);
125
- ulid = ((_a = this.getCollectionIndex(storeName)) === null || _a === void 0 ? void 0 : _a.get(idxName)) ||
126
- this.getMonotonicFactory(storeName)();
127
- itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
128
- // Set key in collection index
129
- (_b = this.getCollectionIndex(storeName)) === null || _b === void 0 ? void 0 : _b.set(keyValuesPath, ulid);
130
- // Save item in db
131
- return [4 /*yield*/, this.storage.setItem(itemKey, JSON.stringify(item))];
132
- case 1:
133
- // Save item in db
134
- _c.sent();
135
- return [2 /*return*/];
65
+ else if (recordType === COLLECTION) {
66
+ keysForCollectionEntries.push(key);
136
67
  }
68
+ }
69
+ }
70
+ if (keysForCollectionEntries.length > 0) {
71
+ await this.storage.multiRemove(keysForCollectionEntries);
72
+ }
73
+ }
74
+ async save(item, storeName, keys, keyValuesPath) {
75
+ const idxName = (0, util_1.indexNameFromKeys)(keys);
76
+ const ulid = this.getCollectionIndex(storeName)?.get(idxName) ||
77
+ this.getMonotonicFactory(storeName)();
78
+ // Retrieve db key for item
79
+ const itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
80
+ // Set key in collection index
81
+ this.getCollectionIndex(storeName)?.set(keyValuesPath, ulid);
82
+ // Save item in db
83
+ await this.storage.setItem(itemKey, JSON.stringify(item));
84
+ }
85
+ async batchSave(storeName, items, keys) {
86
+ if (items.length === 0) {
87
+ return [];
88
+ }
89
+ const result = [];
90
+ const collection = this.getCollectionIndex(storeName);
91
+ const keysToDelete = new Set();
92
+ const keysToSave = new Set();
93
+ const allItemsKeys = [];
94
+ const itemsMap = {};
95
+ /* Populate allItemKeys, keysToDelete, and keysToSave */
96
+ for (const item of items) {
97
+ // Extract keys from concatenated key path, map to item values
98
+ const keyValues = keys.map(field => item[field]);
99
+ const { _deleted } = item;
100
+ // If id is in the store, retrieve, otherwise generate new ULID
101
+ const ulid = collection.get(keyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
102
+ this.getMonotonicFactory(storeName)();
103
+ // Generate the "longer key" for the item
104
+ const key = this.getKeyForItem(storeName, keyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR), ulid);
105
+ allItemsKeys.push(key);
106
+ itemsMap[key] = { ulid, model: item };
107
+ if (_deleted) {
108
+ keysToDelete.add(key);
109
+ }
110
+ else {
111
+ keysToSave.add(key);
112
+ }
113
+ }
114
+ const existingRecordsMap = await this.storage.multiGet(allItemsKeys);
115
+ const existingRecordsKeys = existingRecordsMap
116
+ .filter(([, v]) => !!v)
117
+ .reduce((set, [k]) => set.add(k), new Set());
118
+ // Delete
119
+ await new Promise((resolve, reject) => {
120
+ if (keysToDelete.size === 0) {
121
+ resolve();
122
+ return;
123
+ }
124
+ const keysToDeleteArray = Array.from(keysToDelete);
125
+ keysToDeleteArray.forEach(key => {
126
+ // key: full db key
127
+ // keys: PK and/or SK keys
128
+ const primaryKeyValues = keys
129
+ .map(field => itemsMap[key].model[field])
130
+ .join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
131
+ collection.delete(primaryKeyValues);
137
132
  });
138
- });
139
- };
140
- AsyncStorageDatabase.prototype.batchSave = function (storeName, items, keys) {
141
- return tslib_1.__awaiter(this, void 0, void 0, function () {
142
- 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;
143
- var e_2, _a, e_3, _b;
144
- var _this = this;
145
- return tslib_1.__generator(this, function (_c) {
146
- switch (_c.label) {
147
- case 0:
148
- if (items.length === 0) {
149
- return [2 /*return*/, []];
150
- }
151
- result = [];
152
- collection = this.getCollectionIndex(storeName);
153
- keysToDelete = new Set();
154
- keysToSave = new Set();
155
- allItemsKeys = [];
156
- itemsMap = {};
157
- _loop_1 = function (item) {
158
- // Extract keys from concatenated key path, map to item values
159
- var keyValues = keys.map(function (field) { return item[field]; });
160
- var _deleted = item._deleted;
161
- // If id is in the store, retrieve, otherwise generate new ULID
162
- var ulid = collection.get(keyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
163
- this_1.getMonotonicFactory(storeName)();
164
- // Generate the "longer key" for the item
165
- var key = this_1.getKeyForItem(storeName, keyValues.join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR), ulid);
166
- allItemsKeys.push(key);
167
- itemsMap[key] = { ulid: ulid, model: item };
168
- if (_deleted) {
169
- keysToDelete.add(key);
170
- }
171
- else {
172
- keysToSave.add(key);
173
- }
174
- };
175
- this_1 = this;
176
- try {
177
- /* Populate allItemKeys, keysToDelete, and keysToSave */
178
- for (items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
179
- item = items_1_1.value;
180
- _loop_1(item);
181
- }
182
- }
183
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
184
- finally {
185
- try {
186
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
187
- }
188
- finally { if (e_2) throw e_2.error; }
189
- }
190
- return [4 /*yield*/, this.storage.multiGet(allItemsKeys)];
191
- case 1:
192
- existingRecordsMap = _c.sent();
193
- existingRecordsKeys = existingRecordsMap
194
- .filter(function (_a) {
195
- var _b = tslib_1.__read(_a, 2), v = _b[1];
196
- return !!v;
197
- })
198
- .reduce(function (set, _a) {
199
- var _b = tslib_1.__read(_a, 1), k = _b[0];
200
- return set.add(k);
201
- }, new Set());
202
- // Delete
203
- return [4 /*yield*/, new Promise(function (resolve, reject) {
204
- if (keysToDelete.size === 0) {
205
- resolve();
206
- return;
207
- }
208
- var keysToDeleteArray = Array.from(keysToDelete);
209
- keysToDeleteArray.forEach(function (key) {
210
- // key: full db key
211
- // keys: PK and/or SK keys
212
- var primaryKeyValues = keys
213
- .map(function (field) { return itemsMap[key].model[field]; })
214
- .join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
215
- collection.delete(primaryKeyValues);
216
- });
217
- _this.storage.multiRemove(keysToDeleteArray, function (errors) {
218
- if (errors && errors.length > 0) {
219
- reject(errors);
220
- }
221
- else {
222
- resolve();
223
- }
224
- });
225
- })];
226
- case 2:
227
- // Delete
228
- _c.sent();
229
- // Save
230
- return [4 /*yield*/, new Promise(function (resolve, reject) {
231
- if (keysToSave.size === 0) {
232
- resolve();
233
- return;
234
- }
235
- var entriesToSet = Array.from(keysToSave).map(function (key) { return [
236
- key,
237
- JSON.stringify(itemsMap[key].model),
238
- ]; });
239
- keysToSave.forEach(function (key) {
240
- var _a = itemsMap[key], model = _a.model, ulid = _a.ulid;
241
- // Retrieve values from model, use as key for collection index
242
- var keyValues = keys
243
- .map(function (field) { return model[field]; })
244
- .join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
245
- collection.set(keyValues, ulid);
246
- });
247
- _this.storage.multiSet(entriesToSet, function (errors) {
248
- if (errors && errors.length > 0) {
249
- reject(errors);
250
- }
251
- else {
252
- resolve();
253
- }
254
- });
255
- })];
256
- case 3:
257
- // Save
258
- _c.sent();
259
- try {
260
- for (allItemsKeys_1 = tslib_1.__values(allItemsKeys), allItemsKeys_1_1 = allItemsKeys_1.next(); !allItemsKeys_1_1.done; allItemsKeys_1_1 = allItemsKeys_1.next()) {
261
- key = allItemsKeys_1_1.value;
262
- if (keysToDelete.has(key) && existingRecordsKeys.has(key)) {
263
- result.push([itemsMap[key].model, types_1.OpType.DELETE]);
264
- }
265
- else if (keysToSave.has(key)) {
266
- result.push([
267
- itemsMap[key].model,
268
- existingRecordsKeys.has(key) ? types_1.OpType.UPDATE : types_1.OpType.INSERT,
269
- ]);
270
- }
271
- }
272
- }
273
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
274
- finally {
275
- try {
276
- if (allItemsKeys_1_1 && !allItemsKeys_1_1.done && (_b = allItemsKeys_1.return)) _b.call(allItemsKeys_1);
277
- }
278
- finally { if (e_3) throw e_3.error; }
279
- }
280
- return [2 /*return*/, result];
133
+ this.storage.multiRemove(keysToDeleteArray, (errors) => {
134
+ if (errors && errors.length > 0) {
135
+ reject(errors);
281
136
  }
282
- });
283
- });
284
- };
285
- AsyncStorageDatabase.prototype.get = function (keyValuePath, storeName) {
286
- return tslib_1.__awaiter(this, void 0, void 0, function () {
287
- var ulid, itemKey, recordAsString, record;
288
- return tslib_1.__generator(this, function (_a) {
289
- switch (_a.label) {
290
- case 0:
291
- ulid = this.getCollectionIndex(storeName).get(keyValuePath);
292
- itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
293
- return [4 /*yield*/, this.storage.getItem(itemKey)];
294
- case 1:
295
- recordAsString = _a.sent();
296
- record = recordAsString && JSON.parse(recordAsString);
297
- return [2 /*return*/, record];
137
+ else {
138
+ resolve();
298
139
  }
299
140
  });
300
141
  });
301
- };
302
- AsyncStorageDatabase.prototype.getOne = function (firstOrLast, storeName) {
303
- return tslib_1.__awaiter(this, void 0, void 0, function () {
304
- var collection, _a, itemId, ulid, itemKey, itemString, _b, result;
305
- return tslib_1.__generator(this, function (_c) {
306
- switch (_c.label) {
307
- case 0:
308
- collection = this.getCollectionIndex(storeName);
309
- _a = tslib_1.__read(firstOrLast === types_1.QueryOne.FIRST
310
- ? (function () {
311
- var e_4, _a, _b;
312
- var id, ulid;
313
- try {
314
- for (var collection_1 = tslib_1.__values(collection), collection_1_1 = collection_1.next(); !collection_1_1.done; collection_1_1 = collection_1.next()) {
315
- _b = tslib_1.__read(collection_1_1.value, 2), id = _b[0], ulid = _b[1];
316
- break;
317
- } // Get first element of the set
318
- }
319
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
320
- finally {
321
- try {
322
- if (collection_1_1 && !collection_1_1.done && (_a = collection_1.return)) _a.call(collection_1);
323
- }
324
- finally { if (e_4) throw e_4.error; }
325
- }
326
- return [id, ulid];
327
- })()
328
- : (function () {
329
- var e_5, _a, _b;
330
- var id, ulid;
331
- try {
332
- for (var collection_2 = tslib_1.__values(collection), collection_2_1 = collection_2.next(); !collection_2_1.done; collection_2_1 = collection_2.next()) {
333
- _b = tslib_1.__read(collection_2_1.value, 2), id = _b[0], ulid = _b[1];
334
- ;
335
- } // Get last element of the set
336
- }
337
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
338
- finally {
339
- try {
340
- if (collection_2_1 && !collection_2_1.done && (_a = collection_2.return)) _a.call(collection_2);
341
- }
342
- finally { if (e_5) throw e_5.error; }
343
- }
344
- return [id, ulid];
345
- })(), 2), itemId = _a[0], ulid = _a[1];
346
- itemKey = this.getKeyForItem(storeName, itemId, ulid);
347
- _b = itemKey;
348
- if (!_b) return [3 /*break*/, 2];
349
- return [4 /*yield*/, this.storage.getItem(itemKey)];
350
- case 1:
351
- _b = (_c.sent());
352
- _c.label = 2;
353
- case 2:
354
- itemString = _b;
355
- result = itemString ? JSON.parse(itemString) || undefined : undefined;
356
- return [2 /*return*/, result];
142
+ // Save
143
+ await new Promise((resolve, reject) => {
144
+ if (keysToSave.size === 0) {
145
+ resolve();
146
+ return;
147
+ }
148
+ const entriesToSet = Array.from(keysToSave).map(key => [
149
+ key,
150
+ JSON.stringify(itemsMap[key].model),
151
+ ]);
152
+ keysToSave.forEach(key => {
153
+ const { model, ulid } = itemsMap[key];
154
+ // Retrieve values from model, use as key for collection index
155
+ const keyValues = keys
156
+ .map(field => model[field])
157
+ .join(util_1.DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
158
+ collection.set(keyValues, ulid);
159
+ });
160
+ this.storage.multiSet(entriesToSet, (errors) => {
161
+ if (errors && errors.length > 0) {
162
+ reject(errors);
163
+ }
164
+ else {
165
+ resolve();
357
166
  }
358
167
  });
359
168
  });
360
- };
169
+ for (const key of allItemsKeys) {
170
+ if (keysToDelete.has(key) && existingRecordsKeys.has(key)) {
171
+ result.push([itemsMap[key].model, types_1.OpType.DELETE]);
172
+ }
173
+ else if (keysToSave.has(key)) {
174
+ result.push([
175
+ itemsMap[key].model,
176
+ existingRecordsKeys.has(key) ? types_1.OpType.UPDATE : types_1.OpType.INSERT,
177
+ ]);
178
+ }
179
+ }
180
+ return result;
181
+ }
182
+ async get(keyValuePath, storeName) {
183
+ const ulid = this.getCollectionIndex(storeName).get(keyValuePath);
184
+ const itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
185
+ const recordAsString = await this.storage.getItem(itemKey);
186
+ const record = recordAsString && JSON.parse(recordAsString);
187
+ return record;
188
+ }
189
+ async getOne(firstOrLast, storeName) {
190
+ const collection = this.getCollectionIndex(storeName);
191
+ const [itemId, ulid] = firstOrLast === types_1.QueryOne.FIRST
192
+ ? (() => {
193
+ let id, ulid;
194
+ for ([id, ulid] of collection)
195
+ break; // Get first element of the set
196
+ return [id, ulid];
197
+ })()
198
+ : (() => {
199
+ let id, ulid;
200
+ for ([id, ulid] of collection)
201
+ ; // Get last element of the set
202
+ return [id, ulid];
203
+ })();
204
+ const itemKey = this.getKeyForItem(storeName, itemId, ulid);
205
+ const itemString = itemKey && (await this.storage.getItem(itemKey));
206
+ const result = itemString ? JSON.parse(itemString) || undefined : undefined;
207
+ return result;
208
+ }
361
209
  /**
362
210
  * This function gets all the records stored in async storage for a particular storeName
363
211
  * It then loads all the records for that filtered set of keys using multiGet()
364
212
  */
365
- AsyncStorageDatabase.prototype.getAll = function (storeName, pagination) {
366
- return tslib_1.__awaiter(this, void 0, void 0, function () {
367
- var collection, _a, _b, page, _c, limit, start, end, keysForStore, count, collection_3, collection_3_1, _d, id, ulid, storeRecordStrings, records;
368
- var e_6, _e;
369
- return tslib_1.__generator(this, function (_f) {
370
- switch (_f.label) {
371
- case 0:
372
- collection = this.getCollectionIndex(storeName);
373
- _a = pagination || {}, _b = _a.page, page = _b === void 0 ? 0 : _b, _c = _a.limit, limit = _c === void 0 ? 0 : _c;
374
- start = Math.max(0, page * limit) || 0;
375
- end = limit > 0 ? start + limit : undefined;
376
- keysForStore = [];
377
- count = 0;
378
- try {
379
- for (collection_3 = tslib_1.__values(collection), collection_3_1 = collection_3.next(); !collection_3_1.done; collection_3_1 = collection_3.next()) {
380
- _d = tslib_1.__read(collection_3_1.value, 2), id = _d[0], ulid = _d[1];
381
- count++;
382
- if (count <= start) {
383
- continue;
384
- }
385
- keysForStore.push(this.getKeyForItem(storeName, id, ulid));
386
- if (count === end) {
387
- break;
388
- }
389
- }
390
- }
391
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
392
- finally {
393
- try {
394
- if (collection_3_1 && !collection_3_1.done && (_e = collection_3.return)) _e.call(collection_3);
395
- }
396
- finally { if (e_6) throw e_6.error; }
397
- }
398
- return [4 /*yield*/, this.storage.multiGet(keysForStore)];
399
- case 1:
400
- storeRecordStrings = _f.sent();
401
- records = storeRecordStrings
402
- .filter(function (_a) {
403
- var _b = tslib_1.__read(_a, 2), value = _b[1];
404
- return value;
405
- })
406
- .map(function (_a) {
407
- var _b = tslib_1.__read(_a, 2), value = _b[1];
408
- return JSON.parse(value);
409
- });
410
- return [2 /*return*/, records];
411
- }
412
- });
413
- });
414
- };
415
- AsyncStorageDatabase.prototype.delete = function (key, storeName) {
416
- return tslib_1.__awaiter(this, void 0, void 0, function () {
417
- var ulid, itemKey;
418
- return tslib_1.__generator(this, function (_a) {
419
- switch (_a.label) {
420
- case 0:
421
- ulid = this.getCollectionIndex(storeName).get(key);
422
- itemKey = this.getKeyForItem(storeName, key, ulid);
423
- this.getCollectionIndex(storeName).delete(key);
424
- return [4 /*yield*/, this.storage.removeItem(itemKey)];
425
- case 1:
426
- _a.sent();
427
- return [2 /*return*/];
428
- }
429
- });
430
- });
431
- };
213
+ async getAll(storeName, pagination) {
214
+ const collection = this.getCollectionIndex(storeName);
215
+ const { page = 0, limit = 0 } = pagination || {};
216
+ const start = Math.max(0, page * limit) || 0;
217
+ const end = limit > 0 ? start + limit : undefined;
218
+ const keysForStore = [];
219
+ let count = 0;
220
+ for (const [id, ulid] of collection) {
221
+ count++;
222
+ if (count <= start) {
223
+ continue;
224
+ }
225
+ keysForStore.push(this.getKeyForItem(storeName, id, ulid));
226
+ if (count === end) {
227
+ break;
228
+ }
229
+ }
230
+ const storeRecordStrings = await this.storage.multiGet(keysForStore);
231
+ const records = storeRecordStrings
232
+ .filter(([, value]) => value)
233
+ .map(([, value]) => JSON.parse(value));
234
+ return records;
235
+ }
236
+ async delete(key, storeName) {
237
+ const ulid = this.getCollectionIndex(storeName).get(key);
238
+ const itemKey = this.getKeyForItem(storeName, key, ulid);
239
+ this.getCollectionIndex(storeName).delete(key);
240
+ await this.storage.removeItem(itemKey);
241
+ }
432
242
  /**
433
243
  * Clear the AsyncStorage of all DataStore entries
434
244
  */
435
- AsyncStorageDatabase.prototype.clear = function () {
436
- return tslib_1.__awaiter(this, void 0, void 0, function () {
437
- var allKeys, allDataStoreKeys;
438
- return tslib_1.__generator(this, function (_a) {
439
- switch (_a.label) {
440
- case 0: return [4 /*yield*/, this.storage.getAllKeys()];
441
- case 1:
442
- allKeys = _a.sent();
443
- allDataStoreKeys = allKeys.filter(function (key) { return key.startsWith(DB_NAME); });
444
- return [4 /*yield*/, this.storage.multiRemove(allDataStoreKeys)];
445
- case 2:
446
- _a.sent();
447
- this._collectionInMemoryIndex.clear();
448
- return [2 /*return*/];
449
- }
450
- });
451
- });
452
- };
453
- AsyncStorageDatabase.prototype.getKeyForItem = function (storeName, id, ulid) {
454
- return this.getKeyPrefixForStoreItems(storeName) + "::" + ulid + "::" + id;
455
- };
456
- AsyncStorageDatabase.prototype.getLegacyKeyForItem = function (storeName, id) {
457
- return this.getKeyPrefixForStoreItems(storeName) + "::" + id;
458
- };
459
- AsyncStorageDatabase.prototype.getKeyPrefixForStoreItems = function (storeName) {
460
- return DB_NAME + "::" + storeName + "::" + DATA;
461
- };
462
- return AsyncStorageDatabase;
463
- }());
245
+ async clear() {
246
+ const allKeys = await this.storage.getAllKeys();
247
+ const allDataStoreKeys = allKeys.filter(key => key.startsWith(DB_NAME));
248
+ await this.storage.multiRemove(allDataStoreKeys);
249
+ this._collectionInMemoryIndex.clear();
250
+ }
251
+ getKeyForItem(storeName, id, ulid) {
252
+ return `${this.getKeyPrefixForStoreItems(storeName)}::${ulid}::${id}`;
253
+ }
254
+ getLegacyKeyForItem(storeName, id) {
255
+ return `${this.getKeyPrefixForStoreItems(storeName)}::${id}`;
256
+ }
257
+ getKeyPrefixForStoreItems(storeName) {
258
+ return `${DB_NAME}::${storeName}::${DATA}`;
259
+ }
260
+ }
464
261
  exports.default = AsyncStorageDatabase;
465
- //# sourceMappingURL=AsyncStorageDatabase.js.map