@aws-amplify/datastore 5.0.1-api-v6-models.c1977f8.0 → 5.0.1-api-v6-models.891fe0d.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/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
- package/lib/datastore/datastore.js +887 -1370
- package/lib/index.js +1 -1
- package/lib/predicates/index.js +53 -102
- package/lib/predicates/next.js +310 -557
- package/lib/predicates/sort.js +24 -27
- package/lib/ssr/index.js +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
- package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
- package/lib/storage/adapter/InMemoryStore.js +27 -101
- package/lib/storage/adapter/InMemoryStore.native.js +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
- package/lib/storage/adapter/StorageAdapterBase.js +177 -396
- package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
- package/lib/storage/relationship.js +174 -260
- package/lib/storage/storage.js +244 -507
- package/lib/sync/datastoreConnectivity.js +29 -84
- package/lib/sync/datastoreReachability/index.js +1 -1
- package/lib/sync/datastoreReachability/index.native.js +2 -2
- package/lib/sync/index.js +512 -885
- package/lib/sync/merger.js +30 -133
- package/lib/sync/outbox.js +147 -302
- package/lib/sync/processors/errorMaps.js +30 -80
- package/lib/sync/processors/mutation.js +331 -579
- package/lib/sync/processors/subscription.js +268 -428
- package/lib/sync/processors/sync.js +276 -464
- package/lib/sync/utils.js +248 -393
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.js +16 -58
- package/lib/util.js +335 -575
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
- package/lib-esm/datastore/datastore.js +874 -1359
- package/lib-esm/index.js +6 -6
- package/lib-esm/predicates/index.js +54 -104
- package/lib-esm/predicates/next.js +306 -555
- package/lib-esm/predicates/sort.js +24 -27
- package/lib-esm/ssr/index.js +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
- package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
- package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
- package/lib-esm/storage/relationship.js +173 -260
- package/lib-esm/storage/storage.js +236 -499
- package/lib-esm/sync/datastoreConnectivity.js +26 -82
- package/lib-esm/sync/datastoreReachability/index.js +1 -1
- package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
- package/lib-esm/sync/index.js +498 -872
- package/lib-esm/sync/merger.js +28 -131
- package/lib-esm/sync/outbox.js +143 -298
- package/lib-esm/sync/processors/errorMaps.js +32 -82
- package/lib-esm/sync/processors/mutation.js +324 -572
- package/lib-esm/sync/processors/subscription.js +258 -418
- package/lib-esm/sync/processors/sync.js +269 -457
- package/lib-esm/sync/utils.js +245 -390
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types.js +16 -59
- package/lib-esm/util.js +335 -577
- package/package.json +12 -12
- package/src/datastore/datastore.ts +4 -3
- package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
- package/src/sync/processors/mutation.ts +1 -1
- package/src/sync/processors/sync.ts +1 -1
- package/src/sync/utils.ts +1 -1
- package/src/util.ts +44 -6
|
@@ -1,69 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
39
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
40
|
-
var m = o[Symbol.asyncIterator], i;
|
|
41
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
42
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
43
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
44
|
-
};
|
|
45
|
-
var __values = (this && this.__values) || function(o) {
|
|
46
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
47
|
-
if (m) return m.call(o);
|
|
48
|
-
if (o && typeof o.length === "number") return {
|
|
49
|
-
next: function () {
|
|
50
|
-
if (o && i >= o.length) o = void 0;
|
|
51
|
-
return { value: o && o[i++], done: !o };
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
55
|
-
};
|
|
56
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
3
|
exports.StorageAdapterBase = void 0;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
4
|
+
const predicates_1 = require("../../predicates");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
const util_1 = require("../../util");
|
|
7
|
+
const relationship_1 = require("../relationship");
|
|
8
|
+
const core_1 = require("@aws-amplify/core");
|
|
9
|
+
const logger = new core_1.ConsoleLogger('DataStore');
|
|
10
|
+
const DB_NAME = 'amplify-datastore';
|
|
11
|
+
class StorageAdapterBase {
|
|
12
|
+
constructor() {
|
|
67
13
|
this.dbName = DB_NAME;
|
|
68
14
|
}
|
|
69
15
|
/**
|
|
@@ -75,117 +21,97 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
75
21
|
* @param getModelConstructorByModelName
|
|
76
22
|
* @param sessionId
|
|
77
23
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
case 0: return [4 /*yield*/, this.preSetUpChecks()];
|
|
85
|
-
case 1:
|
|
86
|
-
_b.sent();
|
|
87
|
-
if (!!this.initPromise) return [3 /*break*/, 2];
|
|
88
|
-
this.initPromise = new Promise(function (res, rej) {
|
|
89
|
-
_this.resolve = res;
|
|
90
|
-
_this.reject = rej;
|
|
91
|
-
});
|
|
92
|
-
return [3 /*break*/, 4];
|
|
93
|
-
case 2: return [4 /*yield*/, this.initPromise];
|
|
94
|
-
case 3:
|
|
95
|
-
_b.sent();
|
|
96
|
-
return [2 /*return*/];
|
|
97
|
-
case 4:
|
|
98
|
-
if (sessionId) {
|
|
99
|
-
this.dbName = "".concat(DB_NAME, "-").concat(sessionId);
|
|
100
|
-
}
|
|
101
|
-
this.schema = theSchema;
|
|
102
|
-
this.namespaceResolver = namespaceResolver;
|
|
103
|
-
this.modelInstanceCreator = modelInstanceCreator;
|
|
104
|
-
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
105
|
-
_b.label = 5;
|
|
106
|
-
case 5:
|
|
107
|
-
_b.trys.push([5, 8, , 9]);
|
|
108
|
-
if (!!this.db) return [3 /*break*/, 7];
|
|
109
|
-
_a = this;
|
|
110
|
-
return [4 /*yield*/, this.initDb()];
|
|
111
|
-
case 6:
|
|
112
|
-
_a.db = _b.sent();
|
|
113
|
-
this.resolve();
|
|
114
|
-
_b.label = 7;
|
|
115
|
-
case 7: return [3 /*break*/, 9];
|
|
116
|
-
case 8:
|
|
117
|
-
error_1 = _b.sent();
|
|
118
|
-
this.reject(error_1);
|
|
119
|
-
return [3 /*break*/, 9];
|
|
120
|
-
case 9: return [2 /*return*/];
|
|
121
|
-
}
|
|
24
|
+
async setUp(theSchema, namespaceResolver, modelInstanceCreator, getModelConstructorByModelName, sessionId) {
|
|
25
|
+
await this.preSetUpChecks();
|
|
26
|
+
if (!this.initPromise) {
|
|
27
|
+
this.initPromise = new Promise((res, rej) => {
|
|
28
|
+
this.resolve = res;
|
|
29
|
+
this.reject = rej;
|
|
122
30
|
});
|
|
123
|
-
}
|
|
124
|
-
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
await this.initPromise;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (sessionId) {
|
|
37
|
+
this.dbName = `${DB_NAME}-${sessionId}`;
|
|
38
|
+
}
|
|
39
|
+
this.schema = theSchema;
|
|
40
|
+
this.namespaceResolver = namespaceResolver;
|
|
41
|
+
this.modelInstanceCreator = modelInstanceCreator;
|
|
42
|
+
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
43
|
+
try {
|
|
44
|
+
if (!this.db) {
|
|
45
|
+
this.db = await this.initDb();
|
|
46
|
+
this.resolve();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
this.reject(error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
125
53
|
/**
|
|
126
54
|
* @param modelConstructor
|
|
127
55
|
* @returns local DB table name
|
|
128
56
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
57
|
+
getStorenameForModel(modelConstructor) {
|
|
58
|
+
const namespace = this.namespaceResolver(modelConstructor);
|
|
59
|
+
const { name: modelName } = modelConstructor;
|
|
132
60
|
return (0, util_1.getStorename)(namespace, modelName);
|
|
133
|
-
}
|
|
61
|
+
}
|
|
134
62
|
/**
|
|
135
63
|
*
|
|
136
64
|
* @param model - instantiated model record
|
|
137
65
|
* @returns the record's primary key values
|
|
138
66
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
67
|
+
getIndexKeyValuesFromModel(model) {
|
|
68
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
141
69
|
.constructor;
|
|
142
|
-
|
|
143
|
-
|
|
70
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
71
|
+
const keys = (0, util_1.getIndexKeys)(this.schema.namespaces[namespaceName], modelConstructor.name);
|
|
144
72
|
return (0, util_1.extractPrimaryKeyValues)(model, keys);
|
|
145
|
-
}
|
|
73
|
+
}
|
|
146
74
|
/**
|
|
147
75
|
* Common metadata for `save` operation
|
|
148
76
|
* used by individual storage adapters
|
|
149
77
|
*
|
|
150
78
|
* @param model
|
|
151
79
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
var modelConstructor = Object.getPrototypeOf(model)
|
|
80
|
+
saveMetadata(model) {
|
|
81
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
155
82
|
.constructor;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
var storeName = (0, util_1.getStorename)(namespaceName, modelName);
|
|
83
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
84
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
85
|
+
const connectedModels = (0, util_1.traverseModel)(modelConstructor.name, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
|
|
86
|
+
const set = new Set();
|
|
87
|
+
const connectionStoreNames = Object.values(connectedModels).map(({ modelName, item, instance }) => {
|
|
88
|
+
const storeName = (0, util_1.getStorename)(namespaceName, modelName);
|
|
163
89
|
set.add(storeName);
|
|
164
|
-
|
|
165
|
-
return { storeName
|
|
90
|
+
const keys = (0, util_1.getIndexKeys)(this.schema.namespaces[namespaceName], modelName);
|
|
91
|
+
return { storeName, item, instance, keys };
|
|
166
92
|
});
|
|
167
|
-
|
|
168
|
-
return { storeName
|
|
169
|
-
}
|
|
93
|
+
const modelKeyValues = this.getIndexKeyValuesFromModel(model);
|
|
94
|
+
return { storeName, set, connectionStoreNames, modelKeyValues };
|
|
95
|
+
}
|
|
170
96
|
/**
|
|
171
97
|
* Enforces conditional save. Throws if condition is not met.
|
|
172
98
|
* used by individual storage adapters
|
|
173
99
|
*
|
|
174
100
|
* @param model
|
|
175
101
|
*/
|
|
176
|
-
|
|
102
|
+
validateSaveCondition(condition, fromDB) {
|
|
177
103
|
if (!(condition && fromDB)) {
|
|
178
104
|
return;
|
|
179
105
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
106
|
+
const predicates = predicates_1.ModelPredicateCreator.getPredicates(condition);
|
|
107
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
108
|
+
const isValid = (0, util_1.validatePredicate)(fromDB, type, predicateObjs);
|
|
183
109
|
if (!isValid) {
|
|
184
|
-
|
|
110
|
+
const msg = 'Conditional update failed';
|
|
185
111
|
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
186
112
|
throw new Error(msg);
|
|
187
113
|
}
|
|
188
|
-
}
|
|
114
|
+
}
|
|
189
115
|
/**
|
|
190
116
|
* Instantiate models from POJO records returned from the database
|
|
191
117
|
*
|
|
@@ -194,29 +120,18 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
194
120
|
* @param records - array of uninstantiated records
|
|
195
121
|
* @returns
|
|
196
122
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return
|
|
202
|
-
namespace = this.schema.namespaces[namespaceName];
|
|
203
|
-
relations = namespace.relationships[srcModelName].relationTypes;
|
|
204
|
-
connectionStoreNames = relations.map(function (_a) {
|
|
205
|
-
var modelName = _a.modelName;
|
|
206
|
-
return (0, util_1.getStorename)(namespaceName, modelName);
|
|
207
|
-
});
|
|
208
|
-
modelConstructor = this.getModelConstructorByModelName(namespaceName, srcModelName);
|
|
209
|
-
if (connectionStoreNames.length === 0) {
|
|
210
|
-
return [2 /*return*/, records.map(function (record) {
|
|
211
|
-
return _this.modelInstanceCreator(modelConstructor, record);
|
|
212
|
-
})];
|
|
213
|
-
}
|
|
214
|
-
return [2 /*return*/, records.map(function (record) {
|
|
215
|
-
return _this.modelInstanceCreator(modelConstructor, record);
|
|
216
|
-
})];
|
|
217
|
-
});
|
|
123
|
+
async load(namespaceName, srcModelName, records) {
|
|
124
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
125
|
+
const relations = namespace.relationships[srcModelName].relationTypes;
|
|
126
|
+
const connectionStoreNames = relations.map(({ modelName }) => {
|
|
127
|
+
return (0, util_1.getStorename)(namespaceName, modelName);
|
|
218
128
|
});
|
|
219
|
-
|
|
129
|
+
const modelConstructor = this.getModelConstructorByModelName(namespaceName, srcModelName);
|
|
130
|
+
if (connectionStoreNames.length === 0) {
|
|
131
|
+
return records.map(record => this.modelInstanceCreator(modelConstructor, record));
|
|
132
|
+
}
|
|
133
|
+
return records.map(record => this.modelInstanceCreator(modelConstructor, record));
|
|
134
|
+
}
|
|
220
135
|
/**
|
|
221
136
|
* Extracts operands from a predicate group into an array of key values
|
|
222
137
|
* Used in the query method
|
|
@@ -234,40 +149,25 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
234
149
|
* ['abc', 'def']
|
|
235
150
|
* ```
|
|
236
151
|
*/
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
var predicateObjs = predicates.predicates;
|
|
152
|
+
keyValueFromPredicate(predicates, keyPath) {
|
|
153
|
+
const { predicates: predicateObjs } = predicates;
|
|
240
154
|
if (predicateObjs.length !== keyPath.length) {
|
|
241
155
|
return;
|
|
242
156
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
});
|
|
157
|
+
const keyValues = [];
|
|
158
|
+
for (const key of keyPath) {
|
|
159
|
+
const predicateObj = predicateObjs.find(p =>
|
|
160
|
+
// it's a relevant predicate object only if it's an equality
|
|
161
|
+
// operation for a key field from the key:
|
|
162
|
+
(0, types_1.isPredicateObj)(p) &&
|
|
163
|
+
p.field === key &&
|
|
164
|
+
p.operator === 'eq' &&
|
|
165
|
+
p.operand !== null &&
|
|
166
|
+
p.operand !== undefined);
|
|
254
167
|
predicateObj && keyValues.push(predicateObj.operand);
|
|
255
|
-
};
|
|
256
|
-
try {
|
|
257
|
-
for (var keyPath_1 = __values(keyPath), keyPath_1_1 = keyPath_1.next(); !keyPath_1_1.done; keyPath_1_1 = keyPath_1.next()) {
|
|
258
|
-
var key = keyPath_1_1.value;
|
|
259
|
-
_loop_1(key);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
263
|
-
finally {
|
|
264
|
-
try {
|
|
265
|
-
if (keyPath_1_1 && !keyPath_1_1.done && (_a = keyPath_1.return)) _a.call(keyPath_1);
|
|
266
|
-
}
|
|
267
|
-
finally { if (e_1) throw e_1.error; }
|
|
268
168
|
}
|
|
269
169
|
return keyValues.length === keyPath.length ? keyValues : undefined;
|
|
270
|
-
}
|
|
170
|
+
}
|
|
271
171
|
/**
|
|
272
172
|
* Common metadata for `query` operation
|
|
273
173
|
* used by individual storage adapters
|
|
@@ -276,23 +176,23 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
276
176
|
* @param predicate
|
|
277
177
|
* @param pagination
|
|
278
178
|
*/
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
179
|
+
queryMetadata(modelConstructor, predicate, pagination) {
|
|
180
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
181
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
182
|
+
const predicates = predicate && predicates_1.ModelPredicateCreator.getPredicates(predicate);
|
|
183
|
+
const keyPath = (0, util_1.getIndexKeys)(this.schema.namespaces[namespaceName], modelConstructor.name);
|
|
184
|
+
const queryByKey = predicates && this.keyValueFromPredicate(predicates, keyPath);
|
|
185
|
+
const hasSort = pagination && pagination.sort;
|
|
186
|
+
const hasPagination = pagination && pagination.limit;
|
|
287
187
|
return {
|
|
288
|
-
storeName
|
|
289
|
-
namespaceName
|
|
290
|
-
queryByKey
|
|
291
|
-
predicates
|
|
292
|
-
hasSort
|
|
293
|
-
hasPagination
|
|
188
|
+
storeName,
|
|
189
|
+
namespaceName,
|
|
190
|
+
queryByKey,
|
|
191
|
+
predicates,
|
|
192
|
+
hasSort,
|
|
193
|
+
hasPagination,
|
|
294
194
|
};
|
|
295
|
-
}
|
|
195
|
+
}
|
|
296
196
|
/**
|
|
297
197
|
* Delete record
|
|
298
198
|
* Cascades to related records (for Has One and Has Many relationships)
|
|
@@ -301,90 +201,58 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
301
201
|
* @param condition
|
|
302
202
|
* @returns
|
|
303
203
|
*/
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
deletedModels = deleteQueue.reduce(function (acc, _a) {
|
|
338
|
-
var items = _a.items;
|
|
339
|
-
return acc.concat(items);
|
|
340
|
-
}, []);
|
|
341
|
-
return [2 /*return*/, [models, deletedModels]];
|
|
342
|
-
case 8: return [3 /*break*/, 16];
|
|
343
|
-
case 9:
|
|
344
|
-
model = modelOrModelConstructor;
|
|
345
|
-
modelConstructor = Object.getPrototypeOf(model)
|
|
346
|
-
.constructor;
|
|
347
|
-
namespaceName = this.namespaceResolver(modelConstructor);
|
|
348
|
-
storeName = this.getStorenameForModel(modelConstructor);
|
|
349
|
-
if (!condition) return [3 /*break*/, 12];
|
|
350
|
-
keyValues = this.getIndexKeyValuesFromModel(model);
|
|
351
|
-
return [4 /*yield*/, this._get(storeName, keyValues)];
|
|
352
|
-
case 10:
|
|
353
|
-
fromDB = _b.sent();
|
|
354
|
-
if (fromDB === undefined) {
|
|
355
|
-
msg = 'Model instance not found in storage';
|
|
356
|
-
logger.warn(msg, { model: model });
|
|
357
|
-
return [2 /*return*/, [[model], []]];
|
|
358
|
-
}
|
|
359
|
-
predicates = predicates_1.ModelPredicateCreator.getPredicates(condition);
|
|
360
|
-
_a = predicates, predicateObjs = _a.predicates, type = _a.type;
|
|
361
|
-
isValid = (0, util_1.validatePredicate)(fromDB, type, predicateObjs);
|
|
362
|
-
if (!isValid) {
|
|
363
|
-
msg = 'Conditional update failed';
|
|
364
|
-
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
365
|
-
throw new Error(msg);
|
|
366
|
-
}
|
|
367
|
-
return [4 /*yield*/, this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue)];
|
|
368
|
-
case 11:
|
|
369
|
-
_b.sent();
|
|
370
|
-
return [3 /*break*/, 14];
|
|
371
|
-
case 12: return [4 /*yield*/, this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue)];
|
|
372
|
-
case 13:
|
|
373
|
-
_b.sent();
|
|
374
|
-
_b.label = 14;
|
|
375
|
-
case 14: return [4 /*yield*/, this.deleteItem(deleteQueue)];
|
|
376
|
-
case 15:
|
|
377
|
-
_b.sent();
|
|
378
|
-
deletedModels = deleteQueue.reduce(function (acc, _a) {
|
|
379
|
-
var items = _a.items;
|
|
380
|
-
return acc.concat(items);
|
|
381
|
-
}, []);
|
|
382
|
-
return [2 /*return*/, [[model], deletedModels]];
|
|
383
|
-
case 16: return [2 /*return*/];
|
|
204
|
+
async delete(modelOrModelConstructor, condition) {
|
|
205
|
+
await this.preOpCheck();
|
|
206
|
+
const deleteQueue = [];
|
|
207
|
+
if ((0, util_1.isModelConstructor)(modelOrModelConstructor)) {
|
|
208
|
+
const modelConstructor = modelOrModelConstructor;
|
|
209
|
+
const namespace = this.namespaceResolver(modelConstructor);
|
|
210
|
+
const models = await this.query(modelConstructor, condition);
|
|
211
|
+
if (condition !== undefined) {
|
|
212
|
+
await this.deleteTraverse(models, modelConstructor, namespace, deleteQueue);
|
|
213
|
+
await this.deleteItem(deleteQueue);
|
|
214
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
215
|
+
return [models, deletedModels];
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
await this.deleteTraverse(models, modelConstructor, namespace, deleteQueue);
|
|
219
|
+
await this.deleteItem(deleteQueue);
|
|
220
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
221
|
+
return [models, deletedModels];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const model = modelOrModelConstructor;
|
|
226
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
227
|
+
.constructor;
|
|
228
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
229
|
+
const storeName = this.getStorenameForModel(modelConstructor);
|
|
230
|
+
if (condition) {
|
|
231
|
+
const keyValues = this.getIndexKeyValuesFromModel(model);
|
|
232
|
+
const fromDB = await this._get(storeName, keyValues);
|
|
233
|
+
if (fromDB === undefined) {
|
|
234
|
+
const msg = 'Model instance not found in storage';
|
|
235
|
+
logger.warn(msg, { model });
|
|
236
|
+
return [[model], []];
|
|
384
237
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
238
|
+
const predicates = predicates_1.ModelPredicateCreator.getPredicates(condition);
|
|
239
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
240
|
+
const isValid = (0, util_1.validatePredicate)(fromDB, type, predicateObjs);
|
|
241
|
+
if (!isValid) {
|
|
242
|
+
const msg = 'Conditional update failed';
|
|
243
|
+
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
244
|
+
throw new Error(msg);
|
|
245
|
+
}
|
|
246
|
+
await this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
await this.deleteTraverse([model], modelConstructor, namespaceName, deleteQueue);
|
|
250
|
+
}
|
|
251
|
+
await this.deleteItem(deleteQueue);
|
|
252
|
+
const deletedModels = deleteQueue.reduce((acc, { items }) => acc.concat(items), []);
|
|
253
|
+
return [[model], deletedModels];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
388
256
|
/**
|
|
389
257
|
* Recursively traverse relationship graph and add
|
|
390
258
|
* all Has One and Has Many relations to `deleteQueue` param
|
|
@@ -396,115 +264,28 @@ var StorageAdapterBase = /** @class */ (function () {
|
|
|
396
264
|
* @param namespace
|
|
397
265
|
* @param deleteQueue
|
|
398
266
|
*/
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
case 3:
|
|
415
|
-
if (!(models_1_1 = _j.sent(), _b = models_1_1.done, !_b)) return [3 /*break*/, 24];
|
|
416
|
-
_d = models_1_1.value;
|
|
417
|
-
_a = false;
|
|
418
|
-
_j.label = 4;
|
|
419
|
-
case 4:
|
|
420
|
-
_j.trys.push([4, , 22, 23]);
|
|
421
|
-
model = _d;
|
|
422
|
-
modelDefinition = this.schema.namespaces[namespace].models[modelConstructor.name];
|
|
423
|
-
modelMeta = {
|
|
424
|
-
builder: modelConstructor,
|
|
425
|
-
schema: modelDefinition,
|
|
426
|
-
pkField: (0, util_1.extractPrimaryKeyFieldNames)(modelDefinition),
|
|
427
|
-
};
|
|
428
|
-
relationships = relationship_1.ModelRelationship.allFrom(modelMeta).filter(function (r) {
|
|
429
|
-
return cascadingRelationTypes.includes(r.type);
|
|
430
|
-
});
|
|
431
|
-
_j.label = 5;
|
|
432
|
-
case 5:
|
|
433
|
-
_j.trys.push([5, 15, 16, 21]);
|
|
434
|
-
_h = true, relationships_1 = (e_3 = void 0, __asyncValues(relationships));
|
|
435
|
-
_j.label = 6;
|
|
436
|
-
case 6: return [4 /*yield*/, relationships_1.next()];
|
|
437
|
-
case 7:
|
|
438
|
-
if (!(relationships_1_1 = _j.sent(), _e = relationships_1_1.done, !_e)) return [3 /*break*/, 14];
|
|
439
|
-
_g = relationships_1_1.value;
|
|
440
|
-
_h = false;
|
|
441
|
-
_j.label = 8;
|
|
442
|
-
case 8:
|
|
443
|
-
_j.trys.push([8, , 12, 13]);
|
|
444
|
-
r = _g;
|
|
445
|
-
queryObject = r.createRemoteQueryObject(model);
|
|
446
|
-
if (!(queryObject !== null)) return [3 /*break*/, 11];
|
|
447
|
-
return [4 /*yield*/, this.query(r.remoteModelConstructor, predicates_1.ModelPredicateCreator.createFromFlatEqualities(r.remoteDefinition, queryObject))];
|
|
448
|
-
case 9:
|
|
449
|
-
relatedRecords = _j.sent();
|
|
450
|
-
return [4 /*yield*/, this.deleteTraverse(relatedRecords, r.remoteModelConstructor, namespace, deleteQueue)];
|
|
451
|
-
case 10:
|
|
452
|
-
_j.sent();
|
|
453
|
-
_j.label = 11;
|
|
454
|
-
case 11: return [3 /*break*/, 13];
|
|
455
|
-
case 12:
|
|
456
|
-
_h = true;
|
|
457
|
-
return [7 /*endfinally*/];
|
|
458
|
-
case 13: return [3 /*break*/, 6];
|
|
459
|
-
case 14: return [3 /*break*/, 21];
|
|
460
|
-
case 15:
|
|
461
|
-
e_3_1 = _j.sent();
|
|
462
|
-
e_3 = { error: e_3_1 };
|
|
463
|
-
return [3 /*break*/, 21];
|
|
464
|
-
case 16:
|
|
465
|
-
_j.trys.push([16, , 19, 20]);
|
|
466
|
-
if (!(!_h && !_e && (_f = relationships_1.return))) return [3 /*break*/, 18];
|
|
467
|
-
return [4 /*yield*/, _f.call(relationships_1)];
|
|
468
|
-
case 17:
|
|
469
|
-
_j.sent();
|
|
470
|
-
_j.label = 18;
|
|
471
|
-
case 18: return [3 /*break*/, 20];
|
|
472
|
-
case 19:
|
|
473
|
-
if (e_3) throw e_3.error;
|
|
474
|
-
return [7 /*endfinally*/];
|
|
475
|
-
case 20: return [7 /*endfinally*/];
|
|
476
|
-
case 21: return [3 /*break*/, 23];
|
|
477
|
-
case 22:
|
|
478
|
-
_a = true;
|
|
479
|
-
return [7 /*endfinally*/];
|
|
480
|
-
case 23: return [3 /*break*/, 2];
|
|
481
|
-
case 24: return [3 /*break*/, 31];
|
|
482
|
-
case 25:
|
|
483
|
-
e_2_1 = _j.sent();
|
|
484
|
-
e_2 = { error: e_2_1 };
|
|
485
|
-
return [3 /*break*/, 31];
|
|
486
|
-
case 26:
|
|
487
|
-
_j.trys.push([26, , 29, 30]);
|
|
488
|
-
if (!(!_a && !_b && (_c = models_1.return))) return [3 /*break*/, 28];
|
|
489
|
-
return [4 /*yield*/, _c.call(models_1)];
|
|
490
|
-
case 27:
|
|
491
|
-
_j.sent();
|
|
492
|
-
_j.label = 28;
|
|
493
|
-
case 28: return [3 /*break*/, 30];
|
|
494
|
-
case 29:
|
|
495
|
-
if (e_2) throw e_2.error;
|
|
496
|
-
return [7 /*endfinally*/];
|
|
497
|
-
case 30: return [7 /*endfinally*/];
|
|
498
|
-
case 31:
|
|
499
|
-
deleteQueue.push({
|
|
500
|
-
storeName: (0, util_1.getStorename)(namespace, modelConstructor.name),
|
|
501
|
-
items: models,
|
|
502
|
-
});
|
|
503
|
-
return [2 /*return*/];
|
|
267
|
+
async deleteTraverse(models, modelConstructor, namespace, deleteQueue) {
|
|
268
|
+
const cascadingRelationTypes = ['HAS_ONE', 'HAS_MANY'];
|
|
269
|
+
for await (const model of models) {
|
|
270
|
+
const modelDefinition = this.schema.namespaces[namespace].models[modelConstructor.name];
|
|
271
|
+
const modelMeta = {
|
|
272
|
+
builder: modelConstructor,
|
|
273
|
+
schema: modelDefinition,
|
|
274
|
+
pkField: (0, util_1.extractPrimaryKeyFieldNames)(modelDefinition),
|
|
275
|
+
};
|
|
276
|
+
const relationships = relationship_1.ModelRelationship.allFrom(modelMeta).filter(r => cascadingRelationTypes.includes(r.type));
|
|
277
|
+
for await (const r of relationships) {
|
|
278
|
+
const queryObject = r.createRemoteQueryObject(model);
|
|
279
|
+
if (queryObject !== null) {
|
|
280
|
+
const relatedRecords = await this.query(r.remoteModelConstructor, predicates_1.ModelPredicateCreator.createFromFlatEqualities(r.remoteDefinition, queryObject));
|
|
281
|
+
await this.deleteTraverse(relatedRecords, r.remoteModelConstructor, namespace, deleteQueue);
|
|
504
282
|
}
|
|
505
|
-
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
deleteQueue.push({
|
|
286
|
+
storeName: (0, util_1.getStorename)(namespace, modelConstructor.name),
|
|
287
|
+
items: models,
|
|
506
288
|
});
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
}());
|
|
289
|
+
}
|
|
290
|
+
}
|
|
510
291
|
exports.StorageAdapterBase = StorageAdapterBase;
|