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