@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
package/lib/storage/storage.js
CHANGED
|
@@ -1,89 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
-
if (!m) return o;
|
|
63
|
-
var i = m.call(o), r, ar = [], e;
|
|
64
|
-
try {
|
|
65
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
-
}
|
|
67
|
-
catch (error) { e = { error: error }; }
|
|
68
|
-
finally {
|
|
69
|
-
try {
|
|
70
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
-
}
|
|
72
|
-
finally { if (e) throw e.error; }
|
|
73
|
-
}
|
|
74
|
-
return ar;
|
|
75
|
-
};
|
|
76
|
-
var __values = (this && this.__values) || function(o) {
|
|
77
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
78
|
-
if (m) return m.call(o);
|
|
79
|
-
if (o && typeof o.length === "number") return {
|
|
80
|
-
next: function () {
|
|
81
|
-
if (o && i >= o.length) o = void 0;
|
|
82
|
-
return { value: o && o[i++], done: !o };
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
86
|
-
};
|
|
87
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
88
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
89
4
|
};
|
|
@@ -91,17 +6,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
91
6
|
exports.ExclusiveStorage = void 0;
|
|
92
7
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
93
8
|
// SPDX-License-Identifier: Apache-2.0
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
9
|
+
const rxjs_1 = require("rxjs");
|
|
10
|
+
const predicates_1 = require("../predicates");
|
|
11
|
+
const types_1 = require("../types");
|
|
12
|
+
const util_1 = require("../util");
|
|
13
|
+
const utils_1 = require("../sync/utils");
|
|
14
|
+
const getDefaultAdapter_1 = __importDefault(require("./adapter/getDefaultAdapter"));
|
|
15
|
+
const utils_2 = require("@aws-amplify/core/internals/utils");
|
|
16
|
+
const core_1 = require("@aws-amplify/core");
|
|
17
|
+
const logger = new core_1.ConsoleLogger('DataStore');
|
|
18
|
+
class StorageClass {
|
|
19
|
+
constructor(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
|
|
105
20
|
this.schema = schema;
|
|
106
21
|
this.namespaceResolver = namespaceResolver;
|
|
107
22
|
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
@@ -111,8 +26,8 @@ var StorageClass = /** @class */ (function () {
|
|
|
111
26
|
this.adapter = this.adapter || (0, getDefaultAdapter_1.default)();
|
|
112
27
|
this.pushStream = new rxjs_1.Subject();
|
|
113
28
|
}
|
|
114
|
-
|
|
115
|
-
|
|
29
|
+
static getNamespace() {
|
|
30
|
+
const namespace = {
|
|
116
31
|
name: util_1.STORAGE,
|
|
117
32
|
relationships: {},
|
|
118
33
|
enums: {},
|
|
@@ -120,196 +35,138 @@ var StorageClass = /** @class */ (function () {
|
|
|
120
35
|
nonModels: {},
|
|
121
36
|
};
|
|
122
37
|
return namespace;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
case 2:
|
|
136
|
-
logger.debug('Starting Storage');
|
|
137
|
-
this.initialized = new Promise(function (res, rej) {
|
|
138
|
-
resolve = res;
|
|
139
|
-
reject = rej;
|
|
140
|
-
});
|
|
141
|
-
this.adapter.setUp(this.schema, this.namespaceResolver, this.modelInstanceCreator, this.getModelConstructorByModelName, this.sessionId).then(resolve, reject);
|
|
142
|
-
return [4 /*yield*/, this.initialized];
|
|
143
|
-
case 3:
|
|
144
|
-
_a.sent();
|
|
145
|
-
return [2 /*return*/];
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
StorageClass.prototype.save = function (model, condition, mutator, patchesTuple) {
|
|
151
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
152
|
-
var result;
|
|
153
|
-
var _this = this;
|
|
154
|
-
return __generator(this, function (_a) {
|
|
155
|
-
switch (_a.label) {
|
|
156
|
-
case 0: return [4 /*yield*/, this.init()];
|
|
157
|
-
case 1:
|
|
158
|
-
_a.sent();
|
|
159
|
-
if (!this.adapter) {
|
|
160
|
-
throw new Error('Storage adapter is missing');
|
|
161
|
-
}
|
|
162
|
-
return [4 /*yield*/, this.adapter.save(model, condition)];
|
|
163
|
-
case 2:
|
|
164
|
-
result = _a.sent();
|
|
165
|
-
result.forEach(function (r) {
|
|
166
|
-
var _a = __read(r, 2), savedElement = _a[0], opType = _a[1];
|
|
167
|
-
// truthy when save is called by the Merger
|
|
168
|
-
var syncResponse = !!mutator;
|
|
169
|
-
var updateMutationInput;
|
|
170
|
-
// don't attempt to calc mutation input when storage.save
|
|
171
|
-
// is called by Merger, i.e., when processing an AppSync response
|
|
172
|
-
if ((opType === types_1.OpType.UPDATE || opType === types_1.OpType.INSERT) &&
|
|
173
|
-
!syncResponse) {
|
|
174
|
-
//
|
|
175
|
-
// TODO: LOOK!!!
|
|
176
|
-
// the `model` used here is in effect regardless of what model
|
|
177
|
-
// comes back from adapter.save().
|
|
178
|
-
// Prior to fix, SQLite adapter had been returning two models
|
|
179
|
-
// of different types, resulting in invalid outbox entries.
|
|
180
|
-
//
|
|
181
|
-
// the bug is essentially fixed in SQLite adapter.
|
|
182
|
-
// leaving as-is, because it's currently unclear whether anything
|
|
183
|
-
// depends on this remaining as-is.
|
|
184
|
-
//
|
|
185
|
-
updateMutationInput = _this.getChangedFieldsInput(model, savedElement, patchesTuple);
|
|
186
|
-
// // an update without changed user fields
|
|
187
|
-
// => don't create mutationEvent
|
|
188
|
-
if (updateMutationInput === null) {
|
|
189
|
-
return result;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
var element = updateMutationInput || savedElement;
|
|
193
|
-
var modelConstructor = Object.getPrototypeOf(savedElement)
|
|
194
|
-
.constructor;
|
|
195
|
-
_this.pushStream.next({
|
|
196
|
-
model: modelConstructor,
|
|
197
|
-
opType: opType,
|
|
198
|
-
element: element,
|
|
199
|
-
mutator: mutator,
|
|
200
|
-
condition: (condition &&
|
|
201
|
-
predicates_1.ModelPredicateCreator.getPredicates(condition, false)) ||
|
|
202
|
-
null,
|
|
203
|
-
savedElement: savedElement,
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
return [2 /*return*/, result];
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
StorageClass.prototype.delete = function (modelOrModelConstructor, condition, mutator) {
|
|
212
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
213
|
-
var models, deleted, modelConstructor, namespaceName, modelDefinition, modelIds;
|
|
214
|
-
var _a;
|
|
215
|
-
var _this = this;
|
|
216
|
-
return __generator(this, function (_b) {
|
|
217
|
-
switch (_b.label) {
|
|
218
|
-
case 0: return [4 /*yield*/, this.init()];
|
|
219
|
-
case 1:
|
|
220
|
-
_b.sent();
|
|
221
|
-
if (!this.adapter) {
|
|
222
|
-
throw new Error('Storage adapter is missing');
|
|
223
|
-
}
|
|
224
|
-
return [4 /*yield*/, this.adapter.delete(modelOrModelConstructor, condition)];
|
|
225
|
-
case 2:
|
|
226
|
-
_a = __read.apply(void 0, [_b.sent(), 2]), models = _a[0], deleted = _a[1];
|
|
227
|
-
modelConstructor = (0, util_1.isModelConstructor)(modelOrModelConstructor)
|
|
228
|
-
? modelOrModelConstructor
|
|
229
|
-
: Object.getPrototypeOf(modelOrModelConstructor || {})
|
|
230
|
-
.constructor;
|
|
231
|
-
namespaceName = this.namespaceResolver(modelConstructor);
|
|
232
|
-
modelDefinition = this.schema.namespaces[namespaceName].models[modelConstructor.name];
|
|
233
|
-
modelIds = new Set(models.map(function (model) {
|
|
234
|
-
var modelId = (0, utils_1.getIdentifierValue)(modelDefinition, model);
|
|
235
|
-
return modelId;
|
|
236
|
-
}));
|
|
237
|
-
if (!(0, util_1.isModelConstructor)(modelOrModelConstructor) &&
|
|
238
|
-
!Array.isArray(deleted)) {
|
|
239
|
-
deleted = [deleted];
|
|
240
|
-
}
|
|
241
|
-
deleted.forEach(function (model) {
|
|
242
|
-
var modelConstructor = Object.getPrototypeOf(model)
|
|
243
|
-
.constructor;
|
|
244
|
-
var theCondition;
|
|
245
|
-
if (!(0, util_1.isModelConstructor)(modelOrModelConstructor)) {
|
|
246
|
-
var modelId = (0, utils_1.getIdentifierValue)(modelDefinition, model);
|
|
247
|
-
theCondition = modelIds.has(modelId)
|
|
248
|
-
? predicates_1.ModelPredicateCreator.getPredicates(condition, false)
|
|
249
|
-
: undefined;
|
|
250
|
-
}
|
|
251
|
-
_this.pushStream.next({
|
|
252
|
-
model: modelConstructor,
|
|
253
|
-
opType: types_1.OpType.DELETE,
|
|
254
|
-
element: model,
|
|
255
|
-
mutator: mutator,
|
|
256
|
-
condition: theCondition || null,
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
return [2 /*return*/, [models, deleted]];
|
|
260
|
-
}
|
|
261
|
-
});
|
|
38
|
+
}
|
|
39
|
+
async init() {
|
|
40
|
+
if (this.initialized !== undefined) {
|
|
41
|
+
await this.initialized;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
logger.debug('Starting Storage');
|
|
45
|
+
let resolve;
|
|
46
|
+
let reject;
|
|
47
|
+
this.initialized = new Promise((res, rej) => {
|
|
48
|
+
resolve = res;
|
|
49
|
+
reject = rej;
|
|
262
50
|
});
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
51
|
+
this.adapter.setUp(this.schema, this.namespaceResolver, this.modelInstanceCreator, this.getModelConstructorByModelName, this.sessionId).then(resolve, reject);
|
|
52
|
+
await this.initialized;
|
|
53
|
+
}
|
|
54
|
+
async save(model, condition, mutator, patchesTuple) {
|
|
55
|
+
await this.init();
|
|
56
|
+
if (!this.adapter) {
|
|
57
|
+
throw new Error('Storage adapter is missing');
|
|
58
|
+
}
|
|
59
|
+
const result = await this.adapter.save(model, condition);
|
|
60
|
+
result.forEach(r => {
|
|
61
|
+
const [savedElement, opType] = r;
|
|
62
|
+
// truthy when save is called by the Merger
|
|
63
|
+
const syncResponse = !!mutator;
|
|
64
|
+
let updateMutationInput;
|
|
65
|
+
// don't attempt to calc mutation input when storage.save
|
|
66
|
+
// is called by Merger, i.e., when processing an AppSync response
|
|
67
|
+
if ((opType === types_1.OpType.UPDATE || opType === types_1.OpType.INSERT) &&
|
|
68
|
+
!syncResponse) {
|
|
69
|
+
//
|
|
70
|
+
// TODO: LOOK!!!
|
|
71
|
+
// the `model` used here is in effect regardless of what model
|
|
72
|
+
// comes back from adapter.save().
|
|
73
|
+
// Prior to fix, SQLite adapter had been returning two models
|
|
74
|
+
// of different types, resulting in invalid outbox entries.
|
|
75
|
+
//
|
|
76
|
+
// the bug is essentially fixed in SQLite adapter.
|
|
77
|
+
// leaving as-is, because it's currently unclear whether anything
|
|
78
|
+
// depends on this remaining as-is.
|
|
79
|
+
//
|
|
80
|
+
updateMutationInput = this.getChangedFieldsInput(model, savedElement, patchesTuple);
|
|
81
|
+
// // an update without changed user fields
|
|
82
|
+
// => don't create mutationEvent
|
|
83
|
+
if (updateMutationInput === null) {
|
|
84
|
+
return result;
|
|
276
85
|
}
|
|
86
|
+
}
|
|
87
|
+
const element = updateMutationInput || savedElement;
|
|
88
|
+
const modelConstructor = Object.getPrototypeOf(savedElement)
|
|
89
|
+
.constructor;
|
|
90
|
+
this.pushStream.next({
|
|
91
|
+
model: modelConstructor,
|
|
92
|
+
opType,
|
|
93
|
+
element,
|
|
94
|
+
mutator,
|
|
95
|
+
condition: (condition &&
|
|
96
|
+
predicates_1.ModelPredicateCreator.getPredicates(condition, false)) ||
|
|
97
|
+
null,
|
|
98
|
+
savedElement,
|
|
277
99
|
});
|
|
278
100
|
});
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
async delete(modelOrModelConstructor, condition, mutator) {
|
|
104
|
+
await this.init();
|
|
105
|
+
if (!this.adapter) {
|
|
106
|
+
throw new Error('Storage adapter is missing');
|
|
107
|
+
}
|
|
108
|
+
let models;
|
|
109
|
+
let deleted;
|
|
110
|
+
[models, deleted] = await this.adapter.delete(modelOrModelConstructor, condition);
|
|
111
|
+
const modelConstructor = (0, util_1.isModelConstructor)(modelOrModelConstructor)
|
|
112
|
+
? modelOrModelConstructor
|
|
113
|
+
: Object.getPrototypeOf(modelOrModelConstructor || {})
|
|
114
|
+
.constructor;
|
|
115
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
116
|
+
const modelDefinition = this.schema.namespaces[namespaceName].models[modelConstructor.name];
|
|
117
|
+
const modelIds = new Set(models.map(model => {
|
|
118
|
+
const modelId = (0, utils_1.getIdentifierValue)(modelDefinition, model);
|
|
119
|
+
return modelId;
|
|
120
|
+
}));
|
|
121
|
+
if (!(0, util_1.isModelConstructor)(modelOrModelConstructor) &&
|
|
122
|
+
!Array.isArray(deleted)) {
|
|
123
|
+
deleted = [deleted];
|
|
124
|
+
}
|
|
125
|
+
deleted.forEach(model => {
|
|
126
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
127
|
+
.constructor;
|
|
128
|
+
let theCondition;
|
|
129
|
+
if (!(0, util_1.isModelConstructor)(modelOrModelConstructor)) {
|
|
130
|
+
const modelId = (0, utils_1.getIdentifierValue)(modelDefinition, model);
|
|
131
|
+
theCondition = modelIds.has(modelId)
|
|
132
|
+
? predicates_1.ModelPredicateCreator.getPredicates(condition, false)
|
|
133
|
+
: undefined;
|
|
134
|
+
}
|
|
135
|
+
this.pushStream.next({
|
|
136
|
+
model: modelConstructor,
|
|
137
|
+
opType: types_1.OpType.DELETE,
|
|
138
|
+
element: model,
|
|
139
|
+
mutator,
|
|
140
|
+
condition: theCondition || null,
|
|
294
141
|
});
|
|
295
142
|
});
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
143
|
+
return [models, deleted];
|
|
144
|
+
}
|
|
145
|
+
async query(modelConstructor, predicate, pagination) {
|
|
146
|
+
await this.init();
|
|
147
|
+
if (!this.adapter) {
|
|
148
|
+
throw new Error('Storage adapter is missing');
|
|
149
|
+
}
|
|
150
|
+
return await this.adapter.query(modelConstructor, predicate, pagination);
|
|
151
|
+
}
|
|
152
|
+
async queryOne(modelConstructor, firstOrLast = types_1.QueryOne.FIRST) {
|
|
153
|
+
await this.init();
|
|
154
|
+
if (!this.adapter) {
|
|
155
|
+
throw new Error('Storage adapter is missing');
|
|
156
|
+
}
|
|
157
|
+
return await this.adapter.queryOne(modelConstructor, firstOrLast);
|
|
158
|
+
}
|
|
159
|
+
observe(modelConstructor, predicate, skipOwn) {
|
|
160
|
+
const listenToAll = !modelConstructor;
|
|
161
|
+
const { predicates, type } = (predicate && predicates_1.ModelPredicateCreator.getPredicates(predicate, false)) ||
|
|
162
|
+
{};
|
|
163
|
+
let result = this.pushStream
|
|
164
|
+
.pipe((0, rxjs_1.filter)(({ mutator }) => {
|
|
304
165
|
return !skipOwn || mutator !== skipOwn;
|
|
305
166
|
}))
|
|
306
|
-
.pipe((0, rxjs_1.map)(
|
|
307
|
-
var _mutator = _a.mutator, message = __rest(_a, ["mutator"]);
|
|
308
|
-
return message;
|
|
309
|
-
}));
|
|
167
|
+
.pipe((0, rxjs_1.map)(({ mutator: _mutator, ...message }) => message));
|
|
310
168
|
if (!listenToAll) {
|
|
311
|
-
result = result.pipe((0, rxjs_1.filter)(
|
|
312
|
-
var model = _a.model, element = _a.element;
|
|
169
|
+
result = result.pipe((0, rxjs_1.filter)(({ model, element }) => {
|
|
313
170
|
if (modelConstructor !== model) {
|
|
314
171
|
return false;
|
|
315
172
|
}
|
|
@@ -320,171 +177,93 @@ var StorageClass = /** @class */ (function () {
|
|
|
320
177
|
}));
|
|
321
178
|
}
|
|
322
179
|
return result;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
var result;
|
|
348
|
-
var _this = this;
|
|
349
|
-
return __generator(this, function (_a) {
|
|
350
|
-
switch (_a.label) {
|
|
351
|
-
case 0: return [4 /*yield*/, this.init()];
|
|
352
|
-
case 1:
|
|
353
|
-
_a.sent();
|
|
354
|
-
if (!this.adapter) {
|
|
355
|
-
throw new Error('Storage adapter is missing');
|
|
356
|
-
}
|
|
357
|
-
return [4 /*yield*/, this.adapter.batchSave(modelConstructor, items)];
|
|
358
|
-
case 2:
|
|
359
|
-
result = _a.sent();
|
|
360
|
-
result.forEach(function (_a) {
|
|
361
|
-
var _b = __read(_a, 2), element = _b[0], opType = _b[1];
|
|
362
|
-
_this.pushStream.next({
|
|
363
|
-
model: modelConstructor,
|
|
364
|
-
opType: opType,
|
|
365
|
-
element: element,
|
|
366
|
-
mutator: mutator,
|
|
367
|
-
condition: null,
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
return [2 /*return*/, result];
|
|
371
|
-
}
|
|
180
|
+
}
|
|
181
|
+
async clear(completeObservable = true) {
|
|
182
|
+
this.initialized = undefined;
|
|
183
|
+
if (!this.adapter) {
|
|
184
|
+
throw new Error('Storage adapter is missing');
|
|
185
|
+
}
|
|
186
|
+
await this.adapter.clear();
|
|
187
|
+
if (completeObservable) {
|
|
188
|
+
this.pushStream.complete();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async batchSave(modelConstructor, items, mutator) {
|
|
192
|
+
await this.init();
|
|
193
|
+
if (!this.adapter) {
|
|
194
|
+
throw new Error('Storage adapter is missing');
|
|
195
|
+
}
|
|
196
|
+
const result = await this.adapter.batchSave(modelConstructor, items);
|
|
197
|
+
result.forEach(([element, opType]) => {
|
|
198
|
+
this.pushStream.next({
|
|
199
|
+
model: modelConstructor,
|
|
200
|
+
opType,
|
|
201
|
+
element,
|
|
202
|
+
mutator,
|
|
203
|
+
condition: null,
|
|
372
204
|
});
|
|
373
205
|
});
|
|
374
|
-
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
375
208
|
// returns null if no user fields were changed (determined by value comparison)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
var _b;
|
|
379
|
-
var containsPatches = patchesTuple && patchesTuple.length;
|
|
209
|
+
getChangedFieldsInput(model, originalElement, patchesTuple) {
|
|
210
|
+
const containsPatches = patchesTuple && patchesTuple.length;
|
|
380
211
|
if (!containsPatches) {
|
|
381
212
|
return null;
|
|
382
213
|
}
|
|
383
|
-
|
|
384
|
-
|
|
214
|
+
const [patches, source] = patchesTuple;
|
|
215
|
+
const updatedElement = {};
|
|
385
216
|
// extract array of updated fields from patches
|
|
386
|
-
|
|
217
|
+
const updatedFields = (patches.map(patch => patch.path && patch.path[0]));
|
|
387
218
|
// check model def for association and replace with targetName if exists
|
|
388
|
-
|
|
219
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
389
220
|
.constructor;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
221
|
+
const namespace = this.namespaceResolver(modelConstructor);
|
|
222
|
+
const { fields } = this.schema.namespaces[namespace].models[modelConstructor.name];
|
|
223
|
+
const { primaryKey, compositeKeys = [] } = this.schema.namespaces[namespace].keys?.[modelConstructor.name] || {};
|
|
393
224
|
// set original values for these fields
|
|
394
|
-
updatedFields.forEach(
|
|
395
|
-
|
|
396
|
-
var _f;
|
|
397
|
-
var targetNames = (0, types_1.isTargetNameAssociation)((_f = fields[field]) === null || _f === void 0 ? void 0 : _f.association);
|
|
225
|
+
updatedFields.forEach((field) => {
|
|
226
|
+
const targetNames = (0, types_1.isTargetNameAssociation)(fields[field]?.association);
|
|
398
227
|
if (Array.isArray(targetNames)) {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
originalElement[targetName]
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
for (
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (fieldSet.has(targetName)) {
|
|
415
|
-
try {
|
|
416
|
-
for (var fieldSet_1 = (e_4 = void 0, __values(fieldSet)), fieldSet_1_1 = fieldSet_1.next(); !fieldSet_1_1.done; fieldSet_1_1 = fieldSet_1.next()) {
|
|
417
|
-
var compositeField = fieldSet_1_1.value;
|
|
418
|
-
updatedElement[compositeField] =
|
|
419
|
-
originalElement[compositeField];
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
423
|
-
finally {
|
|
424
|
-
try {
|
|
425
|
-
if (fieldSet_1_1 && !fieldSet_1_1.done && (_c = fieldSet_1.return)) _c.call(fieldSet_1);
|
|
426
|
-
}
|
|
427
|
-
finally { if (e_4) throw e_4.error; }
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
433
|
-
finally {
|
|
434
|
-
try {
|
|
435
|
-
if (compositeKeys_1_1 && !compositeKeys_1_1.done && (_b = compositeKeys_1.return)) _b.call(compositeKeys_1);
|
|
228
|
+
// if field refers to a belongsTo relation, use the target field instead
|
|
229
|
+
for (const targetName of targetNames) {
|
|
230
|
+
// check field values by value. Ignore unchanged fields
|
|
231
|
+
if (!(0, util_1.valuesEqual)(source[targetName], originalElement[targetName])) {
|
|
232
|
+
// if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
|
|
233
|
+
updatedElement[targetName] =
|
|
234
|
+
originalElement[targetName] === undefined
|
|
235
|
+
? null
|
|
236
|
+
: originalElement[targetName];
|
|
237
|
+
for (const fieldSet of compositeKeys) {
|
|
238
|
+
// include all of the fields that comprise the composite key
|
|
239
|
+
if (fieldSet.has(targetName)) {
|
|
240
|
+
for (const compositeField of fieldSet) {
|
|
241
|
+
updatedElement[compositeField] =
|
|
242
|
+
originalElement[compositeField];
|
|
436
243
|
}
|
|
437
|
-
finally { if (e_3) throw e_3.error; }
|
|
438
244
|
}
|
|
439
245
|
}
|
|
440
246
|
}
|
|
441
247
|
}
|
|
442
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
443
|
-
finally {
|
|
444
|
-
try {
|
|
445
|
-
if (targetNames_1_1 && !targetNames_1_1.done && (_a = targetNames_1.return)) _a.call(targetNames_1);
|
|
446
|
-
}
|
|
447
|
-
finally { if (e_2) throw e_2.error; }
|
|
448
|
-
}
|
|
449
248
|
}
|
|
450
249
|
else {
|
|
451
250
|
// Backwards compatibility pre-CPK
|
|
452
251
|
// if field refers to a belongsTo relation, use the target field instead
|
|
453
|
-
|
|
252
|
+
const key = targetNames || field;
|
|
454
253
|
// check field values by value. Ignore unchanged fields
|
|
455
254
|
if (!(0, util_1.valuesEqual)(source[key], originalElement[key])) {
|
|
456
255
|
// if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
|
|
457
256
|
updatedElement[key] =
|
|
458
257
|
originalElement[key] === undefined ? null : originalElement[key];
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
for (var fieldSet_2 = (e_6 = void 0, __values(fieldSet)), fieldSet_2_1 = fieldSet_2.next(); !fieldSet_2_1.done; fieldSet_2_1 = fieldSet_2.next()) {
|
|
466
|
-
var compositeField = fieldSet_2_1.value;
|
|
467
|
-
updatedElement[compositeField] =
|
|
468
|
-
originalElement[compositeField];
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
472
|
-
finally {
|
|
473
|
-
try {
|
|
474
|
-
if (fieldSet_2_1 && !fieldSet_2_1.done && (_e = fieldSet_2.return)) _e.call(fieldSet_2);
|
|
475
|
-
}
|
|
476
|
-
finally { if (e_6) throw e_6.error; }
|
|
477
|
-
}
|
|
258
|
+
for (const fieldSet of compositeKeys) {
|
|
259
|
+
// include all of the fields that comprise the composite key
|
|
260
|
+
if (fieldSet.has(key)) {
|
|
261
|
+
for (const compositeField of fieldSet) {
|
|
262
|
+
updatedElement[compositeField] =
|
|
263
|
+
originalElement[compositeField];
|
|
478
264
|
}
|
|
479
265
|
}
|
|
480
266
|
}
|
|
481
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
482
|
-
finally {
|
|
483
|
-
try {
|
|
484
|
-
if (compositeKeys_2_1 && !compositeKeys_2_1.done && (_d = compositeKeys_2.return)) _d.call(compositeKeys_2);
|
|
485
|
-
}
|
|
486
|
-
finally { if (e_5) throw e_5.error; }
|
|
487
|
-
}
|
|
488
267
|
}
|
|
489
268
|
}
|
|
490
269
|
});
|
|
@@ -494,107 +273,65 @@ var StorageClass = /** @class */ (function () {
|
|
|
494
273
|
}
|
|
495
274
|
// include field(s) from custom PK if one is specified for the model
|
|
496
275
|
if (primaryKey && primaryKey.length) {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
var pkField = primaryKey_1_1.value;
|
|
500
|
-
updatedElement[pkField] = originalElement[pkField];
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
504
|
-
finally {
|
|
505
|
-
try {
|
|
506
|
-
if (primaryKey_1_1 && !primaryKey_1_1.done && (_a = primaryKey_1.return)) _a.call(primaryKey_1);
|
|
507
|
-
}
|
|
508
|
-
finally { if (e_1) throw e_1.error; }
|
|
276
|
+
for (const pkField of primaryKey) {
|
|
277
|
+
updatedElement[pkField] = originalElement[pkField];
|
|
509
278
|
}
|
|
510
279
|
}
|
|
511
|
-
|
|
280
|
+
const { id, _version, _lastChangedAt, _deleted } = originalElement;
|
|
512
281
|
// For update mutations we only want to send fields with changes
|
|
513
282
|
// and the required internal fields
|
|
514
|
-
return
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
283
|
+
return {
|
|
284
|
+
...updatedElement,
|
|
285
|
+
id,
|
|
286
|
+
_version,
|
|
287
|
+
_lastChangedAt,
|
|
288
|
+
_deleted,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
class ExclusiveStorage {
|
|
293
|
+
constructor(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
|
|
520
294
|
this.mutex = new utils_2.Mutex();
|
|
521
295
|
this.storage = new StorageClass(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId);
|
|
522
296
|
}
|
|
523
|
-
|
|
297
|
+
runExclusive(fn) {
|
|
524
298
|
return this.mutex.runExclusive(fn.bind(this, this.storage));
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
return
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
if ((0, util_1.isModelConstructor)(modelOrModelConstructor)) {
|
|
540
|
-
var modelConstructor = modelOrModelConstructor;
|
|
541
|
-
return storage.delete(modelConstructor, condition, mutator);
|
|
542
|
-
}
|
|
543
|
-
else {
|
|
544
|
-
var model = modelOrModelConstructor;
|
|
545
|
-
return storage.delete(model, condition, mutator);
|
|
546
|
-
}
|
|
547
|
-
})];
|
|
548
|
-
});
|
|
549
|
-
});
|
|
550
|
-
};
|
|
551
|
-
ExclusiveStorage.prototype.query = function (modelConstructor, predicate, pagination) {
|
|
552
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
553
|
-
return __generator(this, function (_a) {
|
|
554
|
-
return [2 /*return*/, this.runExclusive(function (storage) {
|
|
555
|
-
return storage.query(modelConstructor, predicate, pagination);
|
|
556
|
-
})];
|
|
557
|
-
});
|
|
558
|
-
});
|
|
559
|
-
};
|
|
560
|
-
ExclusiveStorage.prototype.queryOne = function (modelConstructor, firstOrLast) {
|
|
561
|
-
if (firstOrLast === void 0) { firstOrLast = types_1.QueryOne.FIRST; }
|
|
562
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
563
|
-
return __generator(this, function (_a) {
|
|
564
|
-
return [2 /*return*/, this.runExclusive(function (storage) {
|
|
565
|
-
return storage.queryOne(modelConstructor, firstOrLast);
|
|
566
|
-
})];
|
|
567
|
-
});
|
|
299
|
+
}
|
|
300
|
+
async save(model, condition, mutator, patchesTuple) {
|
|
301
|
+
return this.runExclusive(storage => storage.save(model, condition, mutator, patchesTuple));
|
|
302
|
+
}
|
|
303
|
+
async delete(modelOrModelConstructor, condition, mutator) {
|
|
304
|
+
return this.runExclusive(storage => {
|
|
305
|
+
if ((0, util_1.isModelConstructor)(modelOrModelConstructor)) {
|
|
306
|
+
const modelConstructor = modelOrModelConstructor;
|
|
307
|
+
return storage.delete(modelConstructor, condition, mutator);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
const model = modelOrModelConstructor;
|
|
311
|
+
return storage.delete(model, condition, mutator);
|
|
312
|
+
}
|
|
568
313
|
});
|
|
569
|
-
}
|
|
570
|
-
|
|
314
|
+
}
|
|
315
|
+
async query(modelConstructor, predicate, pagination) {
|
|
316
|
+
return this.runExclusive(storage => storage.query(modelConstructor, predicate, pagination));
|
|
317
|
+
}
|
|
318
|
+
async queryOne(modelConstructor, firstOrLast = types_1.QueryOne.FIRST) {
|
|
319
|
+
return this.runExclusive(storage => storage.queryOne(modelConstructor, firstOrLast));
|
|
320
|
+
}
|
|
321
|
+
static getNamespace() {
|
|
571
322
|
return StorageClass.getNamespace();
|
|
572
|
-
}
|
|
573
|
-
|
|
323
|
+
}
|
|
324
|
+
observe(modelConstructor, predicate, skipOwn) {
|
|
574
325
|
return this.storage.observe(modelConstructor, predicate, skipOwn);
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
case 0: return [4 /*yield*/, this.runExclusive(function (storage) { return storage.clear(); })];
|
|
581
|
-
case 1:
|
|
582
|
-
_a.sent();
|
|
583
|
-
return [2 /*return*/];
|
|
584
|
-
}
|
|
585
|
-
});
|
|
586
|
-
});
|
|
587
|
-
};
|
|
588
|
-
ExclusiveStorage.prototype.batchSave = function (modelConstructor, items) {
|
|
326
|
+
}
|
|
327
|
+
async clear() {
|
|
328
|
+
await this.runExclusive(storage => storage.clear());
|
|
329
|
+
}
|
|
330
|
+
batchSave(modelConstructor, items) {
|
|
589
331
|
return this.storage.batchSave(modelConstructor, items);
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
});
|
|
596
|
-
});
|
|
597
|
-
};
|
|
598
|
-
return ExclusiveStorage;
|
|
599
|
-
}());
|
|
332
|
+
}
|
|
333
|
+
async init() {
|
|
334
|
+
return this.storage.init();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
600
337
|
exports.ExclusiveStorage = ExclusiveStorage;
|