@aws-amplify/datastore 3.12.6-next.13 → 3.12.6-next.32
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/CHANGELOG.md +58 -0
- package/lib/authModeStrategies/multiAuthStrategy.js +17 -64
- package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/lib/datastore/datastore.js +682 -469
- package/lib/datastore/datastore.js.map +1 -1
- package/lib/index.js +2 -4
- package/lib/index.js.map +1 -1
- package/lib/predicates/index.js +12 -2
- package/lib/predicates/index.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +393 -298
- package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageDatabase.js +97 -122
- package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib/storage/adapter/InMemoryStore.js +16 -67
- package/lib/storage/adapter/InMemoryStore.js.map +1 -1
- package/lib/storage/adapter/InMemoryStore.native.js +2 -4
- package/lib/storage/adapter/InMemoryStore.native.js.map +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js +497 -404
- package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib/storage/adapter/getDefaultAdapter/index.js +3 -5
- package/lib/storage/adapter/getDefaultAdapter/index.js.map +1 -1
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -4
- package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
- package/lib/storage/storage.js +129 -151
- package/lib/storage/storage.js.map +1 -1
- package/lib/sync/datastoreConnectivity.js +13 -17
- package/lib/sync/datastoreConnectivity.js.map +1 -1
- package/lib/sync/datastoreReachability/index.native.js +2 -4
- package/lib/sync/datastoreReachability/index.native.js.map +1 -1
- package/lib/sync/index.js +544 -488
- package/lib/sync/index.js.map +1 -1
- package/lib/sync/merger.js +21 -80
- package/lib/sync/merger.js.map +1 -1
- package/lib/sync/outbox.js +95 -162
- package/lib/sync/outbox.js.map +1 -1
- package/lib/sync/processors/errorMaps.js +4 -34
- package/lib/sync/processors/errorMaps.js.map +1 -1
- package/lib/sync/processors/mutation.js +285 -312
- package/lib/sync/processors/mutation.js.map +1 -1
- package/lib/sync/processors/subscription.js +218 -259
- package/lib/sync/processors/subscription.js.map +1 -1
- package/lib/sync/processors/sync.js +141 -212
- package/lib/sync/processors/sync.js.map +1 -1
- package/lib/sync/utils.js +50 -61
- package/lib/sync/utils.js.map +1 -1
- package/lib/types.js +13 -39
- package/lib/types.js.map +1 -1
- package/lib/util.js +429 -242
- package/lib/util.js.map +1 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.d.ts +11 -0
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +13 -57
- package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/lib-esm/datastore/datastore.d.ts +107 -17
- package/lib-esm/datastore/datastore.js +649 -433
- package/lib-esm/datastore/datastore.js.map +1 -1
- package/lib-esm/index.d.ts +3 -19
- package/lib-esm/predicates/index.d.ts +3 -2
- package/lib-esm/predicates/index.js +13 -3
- package/lib-esm/predicates/index.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +4 -3
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +356 -258
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageDatabase.d.ts +14 -4
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +67 -92
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib-esm/storage/adapter/InMemoryStore.js +1 -52
- package/lib-esm/storage/adapter/InMemoryStore.js.map +1 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +26 -4
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +446 -346
- package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib-esm/storage/adapter/index.d.ts +1 -1
- package/lib-esm/storage/storage.d.ts +1 -1
- package/lib-esm/storage/storage.js +94 -113
- package/lib-esm/storage/storage.js.map +1 -1
- package/lib-esm/sync/datastoreConnectivity.d.ts +1 -0
- package/lib-esm/sync/datastoreConnectivity.js +10 -11
- package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
- package/lib-esm/sync/index.d.ts +31 -5
- package/lib-esm/sync/index.js +525 -466
- package/lib-esm/sync/index.js.map +1 -1
- package/lib-esm/sync/merger.d.ts +9 -3
- package/lib-esm/sync/merger.js +14 -73
- package/lib-esm/sync/merger.js.map +1 -1
- package/lib-esm/sync/outbox.d.ts +2 -2
- package/lib-esm/sync/outbox.js +79 -146
- package/lib-esm/sync/outbox.js.map +1 -1
- package/lib-esm/sync/processors/errorMaps.js +1 -31
- package/lib-esm/sync/processors/errorMaps.js.map +1 -1
- package/lib-esm/sync/processors/mutation.d.ts +2 -0
- package/lib-esm/sync/processors/mutation.js +271 -295
- package/lib-esm/sync/processors/mutation.js.map +1 -1
- package/lib-esm/sync/processors/subscription.d.ts +2 -0
- package/lib-esm/sync/processors/subscription.js +214 -245
- package/lib-esm/sync/processors/subscription.js.map +1 -1
- package/lib-esm/sync/processors/sync.d.ts +2 -1
- package/lib-esm/sync/processors/sync.js +127 -195
- package/lib-esm/sync/processors/sync.js.map +1 -1
- package/lib-esm/sync/utils.d.ts +3 -2
- package/lib-esm/sync/utils.js +45 -57
- package/lib-esm/sync/utils.js.map +1 -1
- package/lib-esm/types.d.ts +65 -26
- package/lib-esm/types.js +10 -38
- package/lib-esm/types.js.map +1 -1
- package/lib-esm/util.d.ts +67 -24
- package/lib-esm/util.js +420 -233
- package/lib-esm/util.js.map +1 -1
- package/package.json +14 -7
- package/src/authModeStrategies/multiAuthStrategy.ts +12 -1
- package/src/datastore/datastore.ts +798 -397
- package/src/predicates/index.ts +32 -10
- package/src/storage/adapter/AsyncStorageAdapter.ts +309 -93
- package/src/storage/adapter/AsyncStorageDatabase.ts +74 -26
- package/src/storage/adapter/IndexedDBAdapter.ts +358 -134
- package/src/storage/adapter/index.ts +1 -1
- package/src/storage/storage.ts +69 -22
- package/src/sync/datastoreConnectivity.ts +6 -0
- package/src/sync/index.ts +521 -412
- package/src/sync/merger.ts +20 -4
- package/src/sync/outbox.ts +22 -9
- package/src/sync/processors/mutation.ts +188 -150
- package/src/sync/processors/subscription.ts +289 -253
- package/src/sync/processors/sync.ts +151 -138
- package/src/sync/utils.ts +67 -12
- package/src/types.ts +182 -30
- package/src/util.ts +505 -176
- package/build.js +0 -5
- package/dist/aws-amplify-datastore.js +0 -98255
- package/dist/aws-amplify-datastore.js.map +0 -1
- package/dist/aws-amplify-datastore.min.js +0 -66
- package/dist/aws-amplify-datastore.min.js.map +0 -1
- package/index.js +0 -7
- package/lib/authModeStrategies/defaultAuthStrategy.d.ts +0 -2
- package/lib/authModeStrategies/index.d.ts +0 -2
- package/lib/authModeStrategies/multiAuthStrategy.d.ts +0 -2
- package/lib/datastore/datastore.d.ts +0 -66
- package/lib/index.d.ts +0 -31
- package/lib/predicates/index.d.ts +0 -15
- package/lib/predicates/sort.d.ts +0 -8
- package/lib/ssr/index.d.ts +0 -3
- package/lib/storage/adapter/AsyncStorageAdapter.d.ts +0 -40
- package/lib/storage/adapter/AsyncStorageDatabase.d.ts +0 -29
- package/lib/storage/adapter/InMemoryStore.d.ts +0 -11
- package/lib/storage/adapter/InMemoryStore.native.d.ts +0 -1
- package/lib/storage/adapter/IndexedDBAdapter.d.ts +0 -37
- package/lib/storage/adapter/getDefaultAdapter/index.d.ts +0 -3
- package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +0 -3
- package/lib/storage/adapter/index.d.ts +0 -9
- package/lib/storage/storage.d.ts +0 -49
- package/lib/sync/datastoreConnectivity.d.ts +0 -15
- package/lib/sync/datastoreReachability/index.d.ts +0 -3
- package/lib/sync/datastoreReachability/index.native.d.ts +0 -3
- package/lib/sync/index.d.ts +0 -63
- package/lib/sync/merger.d.ts +0 -11
- package/lib/sync/outbox.d.ts +0 -27
- package/lib/sync/processors/errorMaps.d.ts +0 -17
- package/lib/sync/processors/mutation.d.ts +0 -56
- package/lib/sync/processors/subscription.d.ts +0 -31
- package/lib/sync/processors/sync.d.ts +0 -27
- package/lib/sync/utils.d.ts +0 -41
- package/lib/types.d.ts +0 -462
- package/lib/util.d.ts +0 -113
- package/webpack.config.dev.js +0 -6
|
@@ -6,7 +6,11 @@ import {
|
|
|
6
6
|
PersistentModel,
|
|
7
7
|
QueryOne,
|
|
8
8
|
} from '../../types';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR,
|
|
11
|
+
indexNameFromKeys,
|
|
12
|
+
monotonicUlidFactory,
|
|
13
|
+
} from '../../util';
|
|
10
14
|
import { createInMemoryStore } from './InMemoryStore';
|
|
11
15
|
|
|
12
16
|
const DB_NAME = '@AmplifyDatastore';
|
|
@@ -23,6 +27,11 @@ class AsyncStorageDatabase {
|
|
|
23
27
|
|
|
24
28
|
private storage = createInMemoryStore();
|
|
25
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Collection index is map of stores (i.e. sync, metadata, mutation event, and data)
|
|
32
|
+
* @param storeName {string} - Name of the store
|
|
33
|
+
* @returns Map of ulid->id
|
|
34
|
+
*/
|
|
26
35
|
private getCollectionIndex(storeName: string) {
|
|
27
36
|
if (!this._collectionInMemoryIndex.has(storeName)) {
|
|
28
37
|
this._collectionInMemoryIndex.set(storeName, new Map());
|
|
@@ -31,6 +40,11 @@ class AsyncStorageDatabase {
|
|
|
31
40
|
return this._collectionInMemoryIndex.get(storeName);
|
|
32
41
|
}
|
|
33
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Return ULID for store if it exists, otherwise create a new one
|
|
45
|
+
* @param storeName {string} - Name of the store
|
|
46
|
+
* @returns ulid
|
|
47
|
+
*/
|
|
34
48
|
private getMonotonicFactory(storeName: string): ULID {
|
|
35
49
|
if (!monotonicFactoriesMap.has(storeName)) {
|
|
36
50
|
monotonicFactoriesMap.set(storeName, monotonicUlidFactory());
|
|
@@ -85,39 +99,63 @@ class AsyncStorageDatabase {
|
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
|
|
88
|
-
async save<T extends PersistentModel>(
|
|
102
|
+
async save<T extends PersistentModel>(
|
|
103
|
+
item: T,
|
|
104
|
+
storeName: string,
|
|
105
|
+
keys: string[],
|
|
106
|
+
keyValuesPath: string
|
|
107
|
+
) {
|
|
108
|
+
const idxName = indexNameFromKeys(keys);
|
|
109
|
+
|
|
89
110
|
const ulid =
|
|
90
|
-
this.getCollectionIndex(storeName).get(
|
|
111
|
+
this.getCollectionIndex(storeName).get(idxName) ||
|
|
91
112
|
this.getMonotonicFactory(storeName)();
|
|
92
113
|
|
|
93
|
-
|
|
114
|
+
// Retrieve db key for item
|
|
115
|
+
const itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
|
|
94
116
|
|
|
95
|
-
|
|
117
|
+
// Set key in collection index
|
|
118
|
+
this.getCollectionIndex(storeName).set(keyValuesPath, ulid);
|
|
96
119
|
|
|
120
|
+
// Save item in db
|
|
97
121
|
await this.storage.setItem(itemKey, JSON.stringify(item));
|
|
98
122
|
}
|
|
99
123
|
|
|
100
124
|
async batchSave<T extends PersistentModel>(
|
|
101
125
|
storeName: string,
|
|
102
|
-
items: ModelInstanceMetadata[]
|
|
126
|
+
items: ModelInstanceMetadata[],
|
|
127
|
+
keys: string[]
|
|
103
128
|
): Promise<[T, OpType][]> {
|
|
104
129
|
if (items.length === 0) {
|
|
105
130
|
return [];
|
|
106
131
|
}
|
|
107
132
|
|
|
108
133
|
const result: [T, OpType][] = [];
|
|
109
|
-
|
|
110
134
|
const collection = this.getCollectionIndex(storeName);
|
|
111
135
|
|
|
112
136
|
const keysToDelete = new Set<string>();
|
|
113
137
|
const keysToSave = new Set<string>();
|
|
114
138
|
const allItemsKeys = [];
|
|
115
139
|
const itemsMap: Record<string, { ulid: string; model: T }> = {};
|
|
140
|
+
|
|
141
|
+
/* Populate allItemKeys, keysToDelete, and keysToSave */
|
|
116
142
|
for (const item of items) {
|
|
117
|
-
|
|
118
|
-
const
|
|
143
|
+
// Extract keys from concatenated key path, map to item values
|
|
144
|
+
const keyValues = keys.map(field => item[field]);
|
|
119
145
|
|
|
120
|
-
const
|
|
146
|
+
const { _deleted } = item;
|
|
147
|
+
|
|
148
|
+
// If id is in the store, retrieve, otherwise generate new ULID
|
|
149
|
+
const ulid =
|
|
150
|
+
collection.get(keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR)) ||
|
|
151
|
+
this.getMonotonicFactory(storeName)();
|
|
152
|
+
|
|
153
|
+
// Generate the "longer key" for the item
|
|
154
|
+
const key = this.getKeyForItem(
|
|
155
|
+
storeName,
|
|
156
|
+
keyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR),
|
|
157
|
+
ulid
|
|
158
|
+
);
|
|
121
159
|
|
|
122
160
|
allItemsKeys.push(key);
|
|
123
161
|
itemsMap[key] = { ulid, model: <T>(<unknown>item) };
|
|
@@ -136,6 +174,7 @@ class AsyncStorageDatabase {
|
|
|
136
174
|
.filter(([, v]) => !!v)
|
|
137
175
|
.reduce((set, [k]) => set.add(k), new Set<string>());
|
|
138
176
|
|
|
177
|
+
// Delete
|
|
139
178
|
await new Promise((resolve, reject) => {
|
|
140
179
|
if (keysToDelete.size === 0) {
|
|
141
180
|
resolve();
|
|
@@ -144,9 +183,15 @@ class AsyncStorageDatabase {
|
|
|
144
183
|
|
|
145
184
|
const keysToDeleteArray = Array.from(keysToDelete);
|
|
146
185
|
|
|
147
|
-
keysToDeleteArray.forEach(key =>
|
|
148
|
-
|
|
149
|
-
|
|
186
|
+
keysToDeleteArray.forEach(key => {
|
|
187
|
+
// key: full db key
|
|
188
|
+
// keys: PK and/or SK keys
|
|
189
|
+
const primaryKeyValues: string = keys
|
|
190
|
+
.map(field => itemsMap[key].model[field])
|
|
191
|
+
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
192
|
+
|
|
193
|
+
collection.delete(primaryKeyValues);
|
|
194
|
+
});
|
|
150
195
|
|
|
151
196
|
this.storage.multiRemove(keysToDeleteArray, (errors?: Error[]) => {
|
|
152
197
|
if (errors && errors.length > 0) {
|
|
@@ -157,6 +202,7 @@ class AsyncStorageDatabase {
|
|
|
157
202
|
});
|
|
158
203
|
});
|
|
159
204
|
|
|
205
|
+
// Save
|
|
160
206
|
await new Promise((resolve, reject) => {
|
|
161
207
|
if (keysToSave.size === 0) {
|
|
162
208
|
resolve();
|
|
@@ -169,12 +215,14 @@ class AsyncStorageDatabase {
|
|
|
169
215
|
]);
|
|
170
216
|
|
|
171
217
|
keysToSave.forEach(key => {
|
|
172
|
-
const {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
218
|
+
const { model, ulid } = itemsMap[key];
|
|
219
|
+
|
|
220
|
+
// Retrieve values from model, use as key for collection index
|
|
221
|
+
const keyValues: string = keys
|
|
222
|
+
.map(field => model[field])
|
|
223
|
+
.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
176
224
|
|
|
177
|
-
collection.set(
|
|
225
|
+
collection.set(keyValues, ulid);
|
|
178
226
|
});
|
|
179
227
|
|
|
180
228
|
this.storage.multiSet(entriesToSet, (errors?: Error[]) => {
|
|
@@ -201,11 +249,11 @@ class AsyncStorageDatabase {
|
|
|
201
249
|
}
|
|
202
250
|
|
|
203
251
|
async get<T extends PersistentModel>(
|
|
204
|
-
|
|
252
|
+
keyValuePath: string,
|
|
205
253
|
storeName: string
|
|
206
254
|
): Promise<T> {
|
|
207
|
-
const ulid = this.getCollectionIndex(storeName).get(
|
|
208
|
-
const itemKey = this.getKeyForItem(storeName,
|
|
255
|
+
const ulid = this.getCollectionIndex(storeName).get(keyValuePath);
|
|
256
|
+
const itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
|
|
209
257
|
const recordAsString = await this.storage.getItem(itemKey);
|
|
210
258
|
const record = recordAsString && JSON.parse(recordAsString);
|
|
211
259
|
return record;
|
|
@@ -227,6 +275,7 @@ class AsyncStorageDatabase {
|
|
|
227
275
|
return [id, ulid];
|
|
228
276
|
})();
|
|
229
277
|
const itemKey = this.getKeyForItem(storeName, itemId, ulid);
|
|
278
|
+
|
|
230
279
|
const itemString = itemKey && (await this.storage.getItem(itemKey));
|
|
231
280
|
|
|
232
281
|
const result = itemString ? JSON.parse(itemString) || undefined : undefined;
|
|
@@ -272,11 +321,10 @@ class AsyncStorageDatabase {
|
|
|
272
321
|
return records;
|
|
273
322
|
}
|
|
274
323
|
|
|
275
|
-
async delete(
|
|
276
|
-
const ulid = this.getCollectionIndex(storeName).get(
|
|
277
|
-
const itemKey = this.getKeyForItem(storeName,
|
|
278
|
-
|
|
279
|
-
this.getCollectionIndex(storeName).delete(id);
|
|
324
|
+
async delete(key: string, storeName: string) {
|
|
325
|
+
const ulid = this.getCollectionIndex(storeName).get(key);
|
|
326
|
+
const itemKey = this.getKeyForItem(storeName, key, ulid);
|
|
327
|
+
this.getCollectionIndex(storeName).delete(key);
|
|
280
328
|
await this.storage.removeItem(itemKey);
|
|
281
329
|
}
|
|
282
330
|
|