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