@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.
- package/README.md +4 -0
- package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
- package/lib/authModeStrategies/index.js +3 -3
- package/lib/authModeStrategies/multiAuthStrategy.js +38 -53
- package/lib/datastore/datastore.d.ts +4 -5
- package/lib/datastore/datastore.js +929 -1284
- package/lib/index.d.ts +1 -1
- package/lib/index.js +26 -13
- package/lib/predicates/index.js +54 -69
- package/lib/predicates/next.d.ts +2 -2
- package/lib/predicates/next.js +313 -462
- package/lib/predicates/sort.js +24 -28
- package/lib/ssr/index.js +2 -2
- package/lib/storage/adapter/AsyncStorageAdapter.js +120 -342
- package/lib/storage/adapter/AsyncStorageDatabase.js +217 -421
- package/lib/storage/adapter/InMemoryStore.js +28 -51
- package/lib/storage/adapter/InMemoryStore.native.js +5 -3
- package/lib/storage/adapter/IndexedDBAdapter.js +466 -871
- package/lib/storage/adapter/StorageAdapterBase.js +180 -330
- package/lib/storage/adapter/getDefaultAdapter/index.js +8 -10
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +5 -4
- package/lib/storage/adapter/index.js +0 -1
- package/lib/storage/relationship.js +177 -253
- package/lib/storage/storage.d.ts +4 -4
- package/lib/storage/storage.js +255 -433
- package/lib/sync/datastoreConnectivity.d.ts +2 -2
- package/lib/sync/datastoreConnectivity.js +29 -39
- package/lib/sync/datastoreReachability/index.d.ts +1 -3
- package/lib/sync/datastoreReachability/index.js +3 -3
- package/lib/sync/datastoreReachability/index.native.d.ts +1 -3
- package/lib/sync/datastoreReachability/index.native.js +4 -5
- package/lib/sync/index.d.ts +2 -2
- package/lib/sync/index.js +522 -827
- package/lib/sync/merger.js +31 -63
- package/lib/sync/outbox.js +148 -232
- package/lib/sync/processors/errorMaps.d.ts +1 -1
- package/lib/sync/processors/errorMaps.js +30 -47
- package/lib/sync/processors/mutation.d.ts +2 -2
- package/lib/sync/processors/mutation.js +343 -502
- package/lib/sync/processors/subscription.d.ts +5 -2
- package/lib/sync/processors/subscription.js +283 -437
- package/lib/sync/processors/sync.d.ts +2 -2
- package/lib/sync/processors/sync.js +279 -404
- package/lib/sync/utils.d.ts +5 -4
- package/lib/sync/utils.js +267 -320
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types.d.ts +138 -140
- package/lib/types.js +17 -24
- package/lib/util.d.ts +9 -17
- package/lib/util.js +387 -511
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -2
- package/lib-esm/authModeStrategies/index.js +0 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +35 -52
- package/lib-esm/datastore/datastore.d.ts +4 -5
- package/lib-esm/datastore/datastore.js +888 -1247
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +6 -7
- package/lib-esm/predicates/index.js +53 -70
- package/lib-esm/predicates/next.d.ts +2 -2
- package/lib-esm/predicates/next.js +306 -459
- package/lib-esm/predicates/sort.js +23 -28
- package/lib-esm/ssr/index.js +1 -2
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -338
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -416
- package/lib-esm/storage/adapter/InMemoryStore.js +27 -52
- package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +438 -866
- package/lib-esm/storage/adapter/StorageAdapterBase.js +173 -325
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -6
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -2
- package/lib-esm/storage/adapter/index.js +1 -1
- package/lib-esm/storage/relationship.js +173 -251
- package/lib-esm/storage/storage.d.ts +4 -4
- package/lib-esm/storage/storage.js +242 -424
- package/lib-esm/sync/datastoreConnectivity.d.ts +2 -2
- package/lib-esm/sync/datastoreConnectivity.js +28 -39
- package/lib-esm/sync/datastoreReachability/index.d.ts +1 -3
- package/lib-esm/sync/datastoreReachability/index.js +2 -3
- package/lib-esm/sync/datastoreReachability/index.native.d.ts +1 -3
- package/lib-esm/sync/datastoreReachability/index.native.js +3 -4
- package/lib-esm/sync/index.d.ts +2 -2
- package/lib-esm/sync/index.js +502 -812
- package/lib-esm/sync/merger.js +28 -61
- package/lib-esm/sync/outbox.js +143 -228
- package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
- package/lib-esm/sync/processors/errorMaps.js +32 -50
- package/lib-esm/sync/processors/mutation.d.ts +2 -2
- package/lib-esm/sync/processors/mutation.js +329 -490
- package/lib-esm/sync/processors/subscription.d.ts +5 -2
- package/lib-esm/sync/processors/subscription.js +266 -421
- package/lib-esm/sync/processors/sync.d.ts +2 -2
- package/lib-esm/sync/processors/sync.js +271 -397
- package/lib-esm/sync/utils.d.ts +5 -4
- package/lib-esm/sync/utils.js +252 -307
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types.d.ts +138 -140
- package/lib-esm/types.js +16 -25
- package/lib-esm/util.d.ts +9 -17
- package/lib-esm/util.js +335 -497
- package/package.json +31 -26
- package/src/authModeStrategies/multiAuthStrategy.ts +15 -12
- package/src/datastore/datastore.ts +36 -35
- package/src/predicates/sort.ts +3 -1
- package/src/storage/adapter/InMemoryStore.ts +1 -1
- package/src/storage/adapter/IndexedDBAdapter.ts +2 -2
- package/src/storage/adapter/StorageAdapterBase.ts +2 -2
- package/src/storage/adapter/getDefaultAdapter/index.ts +1 -4
- package/src/storage/storage.ts +29 -24
- package/src/sync/datastoreConnectivity.ts +6 -6
- package/src/sync/datastoreReachability/index.native.ts +5 -3
- package/src/sync/datastoreReachability/index.ts +1 -1
- package/src/sync/index.ts +79 -89
- package/src/sync/processors/errorMaps.ts +7 -7
- package/src/sync/processors/mutation.ts +19 -13
- package/src/sync/processors/subscription.ts +221 -295
- package/src/sync/processors/sync.ts +11 -8
- package/src/sync/utils.ts +30 -15
- package/src/types.ts +4 -8
- package/src/util.ts +46 -9
- package/lib/.tsbuildinfo +0 -3
- package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
- package/lib/authModeStrategies/index.js.map +0 -1
- package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
- package/lib/datastore/datastore.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/predicates/index.js.map +0 -1
- package/lib/predicates/next.js.map +0 -1
- package/lib/predicates/sort.js.map +0 -1
- package/lib/ssr/index.js.map +0 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
- package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
- package/lib/storage/adapter/InMemoryStore.js.map +0 -1
- package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
- package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
- package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
- package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
- package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
- package/lib/storage/adapter/index.js.map +0 -1
- package/lib/storage/relationship.js.map +0 -1
- package/lib/storage/storage.js.map +0 -1
- package/lib/sync/datastoreConnectivity.js.map +0 -1
- package/lib/sync/datastoreReachability/index.js.map +0 -1
- package/lib/sync/datastoreReachability/index.native.js.map +0 -1
- package/lib/sync/index.js.map +0 -1
- package/lib/sync/merger.js.map +0 -1
- package/lib/sync/outbox.js.map +0 -1
- package/lib/sync/processors/errorMaps.js.map +0 -1
- package/lib/sync/processors/mutation.js.map +0 -1
- package/lib/sync/processors/subscription.js.map +0 -1
- package/lib/sync/processors/sync.js.map +0 -1
- package/lib/sync/utils.js.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/util.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
- package/lib-esm/authModeStrategies/index.js.map +0 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
- package/lib-esm/datastore/datastore.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/predicates/index.js.map +0 -1
- package/lib-esm/predicates/next.js.map +0 -1
- package/lib-esm/predicates/sort.js.map +0 -1
- package/lib-esm/ssr/index.js.map +0 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
- package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
- package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
- package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
- package/lib-esm/storage/adapter/index.js.map +0 -1
- package/lib-esm/storage/relationship.js.map +0 -1
- package/lib-esm/storage/storage.js.map +0 -1
- package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
- package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
- package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
- package/lib-esm/sync/index.js.map +0 -1
- package/lib-esm/sync/merger.js.map +0 -1
- package/lib-esm/sync/outbox.js.map +0 -1
- package/lib-esm/sync/processors/errorMaps.js.map +0 -1
- package/lib-esm/sync/processors/mutation.js.map +0 -1
- package/lib-esm/sync/processors/subscription.js.map +0 -1
- package/lib-esm/sync/processors/sync.js.map +0 -1
- package/lib-esm/sync/utils.js.map +0 -1
- package/lib-esm/types.js.map +0 -1
- package/lib-esm/util.js.map +0 -1
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { __asyncValues, __awaiter, __generator, __values } from "tslib";
|
|
2
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
5
1
|
import { ModelPredicateCreator } from '../../predicates';
|
|
6
2
|
import { isPredicateObj, } from '../../types';
|
|
7
3
|
import { getStorename, getIndexKeys, extractPrimaryKeyValues, traverseModel, validatePredicate, isModelConstructor, extractPrimaryKeyFieldNames, } from '../../util';
|
|
8
4
|
import { ModelRelationship } from '../relationship';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
6
|
+
const logger = new ConsoleLogger('DataStore');
|
|
7
|
+
const DB_NAME = 'amplify-datastore';
|
|
8
|
+
export class StorageAdapterBase {
|
|
9
|
+
constructor() {
|
|
13
10
|
this.dbName = DB_NAME;
|
|
14
11
|
}
|
|
15
12
|
/**
|
|
@@ -21,117 +18,97 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
21
18
|
* @param getModelConstructorByModelName
|
|
22
19
|
* @param sessionId
|
|
23
20
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
case 0: return [4 /*yield*/, this.preSetUpChecks()];
|
|
31
|
-
case 1:
|
|
32
|
-
_b.sent();
|
|
33
|
-
if (!!this.initPromise) return [3 /*break*/, 2];
|
|
34
|
-
this.initPromise = new Promise(function (res, rej) {
|
|
35
|
-
_this.resolve = res;
|
|
36
|
-
_this.reject = rej;
|
|
37
|
-
});
|
|
38
|
-
return [3 /*break*/, 4];
|
|
39
|
-
case 2: return [4 /*yield*/, this.initPromise];
|
|
40
|
-
case 3:
|
|
41
|
-
_b.sent();
|
|
42
|
-
return [2 /*return*/];
|
|
43
|
-
case 4:
|
|
44
|
-
if (sessionId) {
|
|
45
|
-
this.dbName = DB_NAME + "-" + sessionId;
|
|
46
|
-
}
|
|
47
|
-
this.schema = theSchema;
|
|
48
|
-
this.namespaceResolver = namespaceResolver;
|
|
49
|
-
this.modelInstanceCreator = modelInstanceCreator;
|
|
50
|
-
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
51
|
-
_b.label = 5;
|
|
52
|
-
case 5:
|
|
53
|
-
_b.trys.push([5, 8, , 9]);
|
|
54
|
-
if (!!this.db) return [3 /*break*/, 7];
|
|
55
|
-
_a = this;
|
|
56
|
-
return [4 /*yield*/, this.initDb()];
|
|
57
|
-
case 6:
|
|
58
|
-
_a.db = _b.sent();
|
|
59
|
-
this.resolve();
|
|
60
|
-
_b.label = 7;
|
|
61
|
-
case 7: return [3 /*break*/, 9];
|
|
62
|
-
case 8:
|
|
63
|
-
error_1 = _b.sent();
|
|
64
|
-
this.reject(error_1);
|
|
65
|
-
return [3 /*break*/, 9];
|
|
66
|
-
case 9: return [2 /*return*/];
|
|
67
|
-
}
|
|
21
|
+
async setUp(theSchema, namespaceResolver, modelInstanceCreator, getModelConstructorByModelName, sessionId) {
|
|
22
|
+
await this.preSetUpChecks();
|
|
23
|
+
if (!this.initPromise) {
|
|
24
|
+
this.initPromise = new Promise((res, rej) => {
|
|
25
|
+
this.resolve = res;
|
|
26
|
+
this.reject = rej;
|
|
68
27
|
});
|
|
69
|
-
}
|
|
70
|
-
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
await this.initPromise;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (sessionId) {
|
|
34
|
+
this.dbName = `${DB_NAME}-${sessionId}`;
|
|
35
|
+
}
|
|
36
|
+
this.schema = theSchema;
|
|
37
|
+
this.namespaceResolver = namespaceResolver;
|
|
38
|
+
this.modelInstanceCreator = modelInstanceCreator;
|
|
39
|
+
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
40
|
+
try {
|
|
41
|
+
if (!this.db) {
|
|
42
|
+
this.db = await this.initDb();
|
|
43
|
+
this.resolve();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
this.reject(error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
71
50
|
/**
|
|
72
51
|
* @param modelConstructor
|
|
73
52
|
* @returns local DB table name
|
|
74
53
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
54
|
+
getStorenameForModel(modelConstructor) {
|
|
55
|
+
const namespace = this.namespaceResolver(modelConstructor);
|
|
56
|
+
const { name: modelName } = modelConstructor;
|
|
78
57
|
return getStorename(namespace, modelName);
|
|
79
|
-
}
|
|
58
|
+
}
|
|
80
59
|
/**
|
|
81
60
|
*
|
|
82
61
|
* @param model - instantiated model record
|
|
83
62
|
* @returns the record's primary key values
|
|
84
63
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
64
|
+
getIndexKeyValuesFromModel(model) {
|
|
65
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
87
66
|
.constructor;
|
|
88
|
-
|
|
89
|
-
|
|
67
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
68
|
+
const keys = getIndexKeys(this.schema.namespaces[namespaceName], modelConstructor.name);
|
|
90
69
|
return extractPrimaryKeyValues(model, keys);
|
|
91
|
-
}
|
|
70
|
+
}
|
|
92
71
|
/**
|
|
93
72
|
* Common metadata for `save` operation
|
|
94
73
|
* used by individual storage adapters
|
|
95
74
|
*
|
|
96
75
|
* @param model
|
|
97
76
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var modelConstructor = Object.getPrototypeOf(model)
|
|
77
|
+
saveMetadata(model) {
|
|
78
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
101
79
|
.constructor;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
var storeName = getStorename(namespaceName, modelName);
|
|
80
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
81
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
82
|
+
const connectedModels = traverseModel(modelConstructor.name, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
|
|
83
|
+
const set = new Set();
|
|
84
|
+
const connectionStoreNames = Object.values(connectedModels).map(({ modelName, item, instance }) => {
|
|
85
|
+
const storeName = getStorename(namespaceName, modelName);
|
|
109
86
|
set.add(storeName);
|
|
110
|
-
|
|
111
|
-
return { storeName
|
|
87
|
+
const keys = getIndexKeys(this.schema.namespaces[namespaceName], modelName);
|
|
88
|
+
return { storeName, item, instance, keys };
|
|
112
89
|
});
|
|
113
|
-
|
|
114
|
-
return { storeName
|
|
115
|
-
}
|
|
90
|
+
const modelKeyValues = this.getIndexKeyValuesFromModel(model);
|
|
91
|
+
return { storeName, set, connectionStoreNames, modelKeyValues };
|
|
92
|
+
}
|
|
116
93
|
/**
|
|
117
94
|
* Enforces conditional save. Throws if condition is not met.
|
|
118
95
|
* used by individual storage adapters
|
|
119
96
|
*
|
|
120
97
|
* @param model
|
|
121
98
|
*/
|
|
122
|
-
|
|
99
|
+
validateSaveCondition(condition, fromDB) {
|
|
123
100
|
if (!(condition && fromDB)) {
|
|
124
101
|
return;
|
|
125
102
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
103
|
+
const predicates = ModelPredicateCreator.getPredicates(condition);
|
|
104
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
105
|
+
const isValid = validatePredicate(fromDB, type, predicateObjs);
|
|
129
106
|
if (!isValid) {
|
|
130
|
-
|
|
107
|
+
const msg = 'Conditional update failed';
|
|
131
108
|
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
132
109
|
throw new Error(msg);
|
|
133
110
|
}
|
|
134
|
-
}
|
|
111
|
+
}
|
|
135
112
|
/**
|
|
136
113
|
* Instantiate models from POJO records returned from the database
|
|
137
114
|
*
|
|
@@ -140,29 +117,18 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
140
117
|
* @param records - array of uninstantiated records
|
|
141
118
|
* @returns
|
|
142
119
|
*/
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return
|
|
148
|
-
namespace = this.schema.namespaces[namespaceName];
|
|
149
|
-
relations = namespace.relationships[srcModelName].relationTypes;
|
|
150
|
-
connectionStoreNames = relations.map(function (_a) {
|
|
151
|
-
var modelName = _a.modelName;
|
|
152
|
-
return getStorename(namespaceName, modelName);
|
|
153
|
-
});
|
|
154
|
-
modelConstructor = this.getModelConstructorByModelName(namespaceName, srcModelName);
|
|
155
|
-
if (connectionStoreNames.length === 0) {
|
|
156
|
-
return [2 /*return*/, records.map(function (record) {
|
|
157
|
-
return _this.modelInstanceCreator(modelConstructor, record);
|
|
158
|
-
})];
|
|
159
|
-
}
|
|
160
|
-
return [2 /*return*/, records.map(function (record) {
|
|
161
|
-
return _this.modelInstanceCreator(modelConstructor, record);
|
|
162
|
-
})];
|
|
163
|
-
});
|
|
120
|
+
async load(namespaceName, srcModelName, records) {
|
|
121
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
122
|
+
const relations = namespace.relationships[srcModelName].relationTypes;
|
|
123
|
+
const connectionStoreNames = relations.map(({ modelName }) => {
|
|
124
|
+
return getStorename(namespaceName, modelName);
|
|
164
125
|
});
|
|
165
|
-
|
|
126
|
+
const modelConstructor = this.getModelConstructorByModelName(namespaceName, srcModelName);
|
|
127
|
+
if (connectionStoreNames.length === 0) {
|
|
128
|
+
return records.map(record => this.modelInstanceCreator(modelConstructor, record));
|
|
129
|
+
}
|
|
130
|
+
return records.map(record => this.modelInstanceCreator(modelConstructor, record));
|
|
131
|
+
}
|
|
166
132
|
/**
|
|
167
133
|
* Extracts operands from a predicate group into an array of key values
|
|
168
134
|
* Used in the query method
|
|
@@ -180,40 +146,25 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
180
146
|
* ['abc', 'def']
|
|
181
147
|
* ```
|
|
182
148
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var predicateObjs = predicates.predicates;
|
|
149
|
+
keyValueFromPredicate(predicates, keyPath) {
|
|
150
|
+
const { predicates: predicateObjs } = predicates;
|
|
186
151
|
if (predicateObjs.length !== keyPath.length) {
|
|
187
152
|
return;
|
|
188
153
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
});
|
|
154
|
+
const keyValues = [];
|
|
155
|
+
for (const key of keyPath) {
|
|
156
|
+
const predicateObj = predicateObjs.find(p =>
|
|
157
|
+
// it's a relevant predicate object only if it's an equality
|
|
158
|
+
// operation for a key field from the key:
|
|
159
|
+
isPredicateObj(p) &&
|
|
160
|
+
p.field === key &&
|
|
161
|
+
p.operator === 'eq' &&
|
|
162
|
+
p.operand !== null &&
|
|
163
|
+
p.operand !== undefined);
|
|
200
164
|
predicateObj && keyValues.push(predicateObj.operand);
|
|
201
|
-
};
|
|
202
|
-
try {
|
|
203
|
-
for (var keyPath_1 = __values(keyPath), keyPath_1_1 = keyPath_1.next(); !keyPath_1_1.done; keyPath_1_1 = keyPath_1.next()) {
|
|
204
|
-
var key = keyPath_1_1.value;
|
|
205
|
-
_loop_1(key);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
209
|
-
finally {
|
|
210
|
-
try {
|
|
211
|
-
if (keyPath_1_1 && !keyPath_1_1.done && (_a = keyPath_1.return)) _a.call(keyPath_1);
|
|
212
|
-
}
|
|
213
|
-
finally { if (e_1) throw e_1.error; }
|
|
214
165
|
}
|
|
215
166
|
return keyValues.length === keyPath.length ? keyValues : undefined;
|
|
216
|
-
}
|
|
167
|
+
}
|
|
217
168
|
/**
|
|
218
169
|
* Common metadata for `query` operation
|
|
219
170
|
* used by individual storage adapters
|
|
@@ -222,23 +173,23 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
222
173
|
* @param predicate
|
|
223
174
|
* @param pagination
|
|
224
175
|
*/
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
176
|
+
queryMetadata(modelConstructor, predicate, pagination) {
|
|
177
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
178
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
179
|
+
const predicates = predicate && ModelPredicateCreator.getPredicates(predicate);
|
|
180
|
+
const keyPath = getIndexKeys(this.schema.namespaces[namespaceName], modelConstructor.name);
|
|
181
|
+
const queryByKey = predicates && this.keyValueFromPredicate(predicates, keyPath);
|
|
182
|
+
const hasSort = pagination && pagination.sort;
|
|
183
|
+
const hasPagination = pagination && pagination.limit;
|
|
233
184
|
return {
|
|
234
|
-
storeName
|
|
235
|
-
namespaceName
|
|
236
|
-
queryByKey
|
|
237
|
-
predicates
|
|
238
|
-
hasSort
|
|
239
|
-
hasPagination
|
|
185
|
+
storeName,
|
|
186
|
+
namespaceName,
|
|
187
|
+
queryByKey,
|
|
188
|
+
predicates,
|
|
189
|
+
hasSort,
|
|
190
|
+
hasPagination,
|
|
240
191
|
};
|
|
241
|
-
}
|
|
192
|
+
}
|
|
242
193
|
/**
|
|
243
194
|
* Delete record
|
|
244
195
|
* Cascades to related records (for Has One and Has Many relationships)
|
|
@@ -247,90 +198,58 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
247
198
|
* @param condition
|
|
248
199
|
* @returns
|
|
249
200
|
*/
|
|
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
|
-
deletedModels = deleteQueue.reduce(function (acc, _a) {
|
|
284
|
-
var items = _a.items;
|
|
285
|
-
return acc.concat(items);
|
|
286
|
-
}, []);
|
|
287
|
-
return [2 /*return*/, [models, deletedModels]];
|
|
288
|
-
case 8: return [3 /*break*/, 16];
|
|
289
|
-
case 9:
|
|
290
|
-
model = modelOrModelConstructor;
|
|
291
|
-
modelConstructor = Object.getPrototypeOf(model)
|
|
292
|
-
.constructor;
|
|
293
|
-
namespaceName = this.namespaceResolver(modelConstructor);
|
|
294
|
-
storeName = this.getStorenameForModel(modelConstructor);
|
|
295
|
-
if (!condition) return [3 /*break*/, 12];
|
|
296
|
-
keyValues = this.getIndexKeyValuesFromModel(model);
|
|
297
|
-
return [4 /*yield*/, this._get(storeName, keyValues)];
|
|
298
|
-
case 10:
|
|
299
|
-
fromDB = _b.sent();
|
|
300
|
-
if (fromDB === undefined) {
|
|
301
|
-
msg = 'Model instance not found in storage';
|
|
302
|
-
logger.warn(msg, { model: model });
|
|
303
|
-
return [2 /*return*/, [[model], []]];
|
|
304
|
-
}
|
|
305
|
-
predicates = ModelPredicateCreator.getPredicates(condition);
|
|
306
|
-
_a = predicates, predicateObjs = _a.predicates, type = _a.type;
|
|
307
|
-
isValid = validatePredicate(fromDB, type, predicateObjs);
|
|
308
|
-
if (!isValid) {
|
|
309
|
-
msg = 'Conditional update failed';
|
|
310
|
-
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
311
|
-
throw new Error(msg);
|
|
312
|
-
}
|
|
313
|
-
return [4 /*yield*/, this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue)];
|
|
314
|
-
case 11:
|
|
315
|
-
_b.sent();
|
|
316
|
-
return [3 /*break*/, 14];
|
|
317
|
-
case 12: return [4 /*yield*/, this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue)];
|
|
318
|
-
case 13:
|
|
319
|
-
_b.sent();
|
|
320
|
-
_b.label = 14;
|
|
321
|
-
case 14: return [4 /*yield*/, this.deleteItem(deleteQueue)];
|
|
322
|
-
case 15:
|
|
323
|
-
_b.sent();
|
|
324
|
-
deletedModels = deleteQueue.reduce(function (acc, _a) {
|
|
325
|
-
var items = _a.items;
|
|
326
|
-
return acc.concat(items);
|
|
327
|
-
}, []);
|
|
328
|
-
return [2 /*return*/, [[model], deletedModels]];
|
|
329
|
-
case 16: return [2 /*return*/];
|
|
201
|
+
async delete(modelOrModelConstructor, condition) {
|
|
202
|
+
await this.preOpCheck();
|
|
203
|
+
const deleteQueue = [];
|
|
204
|
+
if (isModelConstructor(modelOrModelConstructor)) {
|
|
205
|
+
const modelConstructor = modelOrModelConstructor;
|
|
206
|
+
const namespace = this.namespaceResolver(modelConstructor);
|
|
207
|
+
const models = await this.query(modelConstructor, condition);
|
|
208
|
+
if (condition !== undefined) {
|
|
209
|
+
await this.deleteTraverse(models, modelConstructor, namespace, deleteQueue);
|
|
210
|
+
await this.deleteItem(deleteQueue);
|
|
211
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
212
|
+
return [models, deletedModels];
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
await this.deleteTraverse(models, modelConstructor, namespace, deleteQueue);
|
|
216
|
+
await this.deleteItem(deleteQueue);
|
|
217
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
218
|
+
return [models, deletedModels];
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
const model = modelOrModelConstructor;
|
|
223
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
224
|
+
.constructor;
|
|
225
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
226
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
227
|
+
if (condition) {
|
|
228
|
+
const keyValues = this.getIndexKeyValuesFromModel(model);
|
|
229
|
+
const fromDB = await this._get(storeName, keyValues);
|
|
230
|
+
if (fromDB === undefined) {
|
|
231
|
+
const msg = 'Model instance not found in storage';
|
|
232
|
+
logger.warn(msg, { model });
|
|
233
|
+
return [[model], []];
|
|
330
234
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
235
|
+
const predicates = ModelPredicateCreator.getPredicates(condition);
|
|
236
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
237
|
+
const isValid = validatePredicate(fromDB, type, predicateObjs);
|
|
238
|
+
if (!isValid) {
|
|
239
|
+
const msg = 'Conditional update failed';
|
|
240
|
+
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
241
|
+
throw new Error(msg);
|
|
242
|
+
}
|
|
243
|
+
await this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
await this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue);
|
|
247
|
+
}
|
|
248
|
+
await this.deleteItem(deleteQueue);
|
|
249
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
250
|
+
return [[model], deletedModels];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
334
253
|
/**
|
|
335
254
|
* Recursively traverse relationship graph and add
|
|
336
255
|
* all Has One and Has Many relations to `deleteQueue` param
|
|
@@ -342,98 +261,27 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
342
261
|
* @param namespace
|
|
343
262
|
* @param deleteQueue
|
|
344
263
|
*/
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
case 3:
|
|
361
|
-
if (!(models_1_1 = _c.sent(), !models_1_1.done)) return [3 /*break*/, 18];
|
|
362
|
-
model = models_1_1.value;
|
|
363
|
-
modelDefinition = this.schema.namespaces[namespace].models[modelConstructor.name];
|
|
364
|
-
modelMeta = {
|
|
365
|
-
builder: modelConstructor,
|
|
366
|
-
schema: modelDefinition,
|
|
367
|
-
pkField: extractPrimaryKeyFieldNames(modelDefinition),
|
|
368
|
-
};
|
|
369
|
-
relationships = ModelRelationship.allFrom(modelMeta).filter(function (r) {
|
|
370
|
-
return cascadingRelationTypes.includes(r.type);
|
|
371
|
-
});
|
|
372
|
-
_c.label = 4;
|
|
373
|
-
case 4:
|
|
374
|
-
_c.trys.push([4, 11, 12, 17]);
|
|
375
|
-
relationships_1 = __asyncValues(relationships);
|
|
376
|
-
_c.label = 5;
|
|
377
|
-
case 5: return [4 /*yield*/, relationships_1.next()];
|
|
378
|
-
case 6:
|
|
379
|
-
if (!(relationships_1_1 = _c.sent(), !relationships_1_1.done)) return [3 /*break*/, 10];
|
|
380
|
-
r = relationships_1_1.value;
|
|
381
|
-
queryObject = r.createRemoteQueryObject(model);
|
|
382
|
-
if (!(queryObject !== null)) return [3 /*break*/, 9];
|
|
383
|
-
return [4 /*yield*/, this.query(r.remoteModelConstructor, ModelPredicateCreator.createFromFlatEqualities(r.remoteDefinition, queryObject))];
|
|
384
|
-
case 7:
|
|
385
|
-
relatedRecords = _c.sent();
|
|
386
|
-
return [4 /*yield*/, this.deleteTraverse(relatedRecords, r.remoteModelConstructor, namespace, deleteQueue)];
|
|
387
|
-
case 8:
|
|
388
|
-
_c.sent();
|
|
389
|
-
_c.label = 9;
|
|
390
|
-
case 9: return [3 /*break*/, 5];
|
|
391
|
-
case 10: return [3 /*break*/, 17];
|
|
392
|
-
case 11:
|
|
393
|
-
e_3_1 = _c.sent();
|
|
394
|
-
e_3 = { error: e_3_1 };
|
|
395
|
-
return [3 /*break*/, 17];
|
|
396
|
-
case 12:
|
|
397
|
-
_c.trys.push([12, , 15, 16]);
|
|
398
|
-
if (!(relationships_1_1 && !relationships_1_1.done && (_b = relationships_1.return))) return [3 /*break*/, 14];
|
|
399
|
-
return [4 /*yield*/, _b.call(relationships_1)];
|
|
400
|
-
case 13:
|
|
401
|
-
_c.sent();
|
|
402
|
-
_c.label = 14;
|
|
403
|
-
case 14: return [3 /*break*/, 16];
|
|
404
|
-
case 15:
|
|
405
|
-
if (e_3) throw e_3.error;
|
|
406
|
-
return [7 /*endfinally*/];
|
|
407
|
-
case 16: return [7 /*endfinally*/];
|
|
408
|
-
case 17: return [3 /*break*/, 2];
|
|
409
|
-
case 18: return [3 /*break*/, 25];
|
|
410
|
-
case 19:
|
|
411
|
-
e_2_1 = _c.sent();
|
|
412
|
-
e_2 = { error: e_2_1 };
|
|
413
|
-
return [3 /*break*/, 25];
|
|
414
|
-
case 20:
|
|
415
|
-
_c.trys.push([20, , 23, 24]);
|
|
416
|
-
if (!(models_1_1 && !models_1_1.done && (_a = models_1.return))) return [3 /*break*/, 22];
|
|
417
|
-
return [4 /*yield*/, _a.call(models_1)];
|
|
418
|
-
case 21:
|
|
419
|
-
_c.sent();
|
|
420
|
-
_c.label = 22;
|
|
421
|
-
case 22: return [3 /*break*/, 24];
|
|
422
|
-
case 23:
|
|
423
|
-
if (e_2) throw e_2.error;
|
|
424
|
-
return [7 /*endfinally*/];
|
|
425
|
-
case 24: return [7 /*endfinally*/];
|
|
426
|
-
case 25:
|
|
427
|
-
deleteQueue.push({
|
|
428
|
-
storeName: getStorename(namespace, modelConstructor.name),
|
|
429
|
-
items: models,
|
|
430
|
-
});
|
|
431
|
-
return [2 /*return*/];
|
|
264
|
+
async deleteTraverse(models, modelConstructor, namespace, deleteQueue) {
|
|
265
|
+
const cascadingRelationTypes = ['HAS_ONE', 'HAS_MANY'];
|
|
266
|
+
for await (const model of models) {
|
|
267
|
+
const modelDefinition = this.schema.namespaces[namespace].models[modelConstructor.name];
|
|
268
|
+
const modelMeta = {
|
|
269
|
+
builder: modelConstructor,
|
|
270
|
+
schema: modelDefinition,
|
|
271
|
+
pkField: extractPrimaryKeyFieldNames(modelDefinition),
|
|
272
|
+
};
|
|
273
|
+
const relationships = ModelRelationship.allFrom(modelMeta).filter(r => cascadingRelationTypes.includes(r.type));
|
|
274
|
+
for await (const r of relationships) {
|
|
275
|
+
const queryObject = r.createRemoteQueryObject(model);
|
|
276
|
+
if (queryObject !== null) {
|
|
277
|
+
const relatedRecords = await this.query(r.remoteModelConstructor, ModelPredicateCreator.createFromFlatEqualities(r.remoteDefinition, queryObject));
|
|
278
|
+
await this.deleteTraverse(relatedRecords, r.remoteModelConstructor, namespace, deleteQueue);
|
|
432
279
|
}
|
|
433
|
-
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
deleteQueue.push({
|
|
283
|
+
storeName: getStorename(namespace, modelConstructor.name),
|
|
284
|
+
items: models,
|
|
434
285
|
});
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
}());
|
|
438
|
-
export { StorageAdapterBase };
|
|
439
|
-
//# sourceMappingURL=StorageAdapterBase.js.map
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { browserOrNode, isWebWorker } from '@aws-amplify/core';
|
|
4
1
|
import IndexedDBAdapter from '../IndexedDBAdapter';
|
|
5
2
|
import AsyncStorageAdapter from '../AsyncStorageAdapter';
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { isWebWorker, isBrowser } from '@aws-amplify/core/internals/utils';
|
|
4
|
+
const getDefaultAdapter = () => {
|
|
8
5
|
if ((isBrowser && window.indexedDB) || (isWebWorker() && self.indexedDB)) {
|
|
9
6
|
return IndexedDBAdapter;
|
|
10
7
|
}
|
|
11
8
|
return AsyncStorageAdapter;
|
|
12
9
|
};
|
|
13
10
|
export default getDefaultAdapter;
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|