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