@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-esm/util.js
CHANGED
|
@@ -1,94 +1,11 @@
|
|
|
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 __read = (this && this.__read) || function (o, n) {
|
|
49
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
50
|
-
if (!m) return o;
|
|
51
|
-
var i = m.call(o), r, ar = [], e;
|
|
52
|
-
try {
|
|
53
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
54
|
-
}
|
|
55
|
-
catch (error) { e = { error: error }; }
|
|
56
|
-
finally {
|
|
57
|
-
try {
|
|
58
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
59
|
-
}
|
|
60
|
-
finally { if (e) throw e.error; }
|
|
61
|
-
}
|
|
62
|
-
return ar;
|
|
63
|
-
};
|
|
64
|
-
var __values = (this && this.__values) || function(o) {
|
|
65
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
66
|
-
if (m) return m.call(o);
|
|
67
|
-
if (o && typeof o.length === "number") return {
|
|
68
|
-
next: function () {
|
|
69
|
-
if (o && i >= o.length) o = void 0;
|
|
70
|
-
return { value: o && o[i++], done: !o };
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
74
|
-
};
|
|
75
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
76
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
77
|
-
if (ar || !(i in from)) {
|
|
78
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
79
|
-
ar[i] = from[i];
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
83
|
-
};
|
|
84
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
85
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
86
3
|
import { monotonicFactory } from 'ulid';
|
|
87
|
-
import { amplifyUuid, AmplifyUrl } from '@aws-amplify/core/internals/utils';
|
|
4
|
+
import { amplifyUuid, AmplifyUrl, WordArray, } from '@aws-amplify/core/internals/utils';
|
|
88
5
|
import { produce, applyPatches } from 'immer';
|
|
89
6
|
import { isPredicateGroup, isPredicateObj, SortDirection, isModelAttributeKey, isModelAttributePrimaryKey, isModelAttributeCompositeKey, LimitTimerRaceResolvedValues, } from './types';
|
|
90
7
|
import { ModelSortPredicateCreator } from './predicates';
|
|
91
|
-
export
|
|
8
|
+
export const ID = 'id';
|
|
92
9
|
/**
|
|
93
10
|
* Used by the Async Storage Adapter to concatenate key values
|
|
94
11
|
* for a record. For instance, if a model has the following keys:
|
|
@@ -96,14 +13,14 @@ export var ID = 'id';
|
|
|
96
13
|
* we concatenate the `customId` and `createdAt` as:
|
|
97
14
|
* `12-234-5#2022-09-28T00:00:00.000Z`
|
|
98
15
|
*/
|
|
99
|
-
export
|
|
16
|
+
export const DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR = '#';
|
|
100
17
|
/**
|
|
101
18
|
* Used for generating spinal-cased index name from an array of
|
|
102
19
|
* key field names.
|
|
103
20
|
* E.g. for keys `[id, title]` => 'id-title'
|
|
104
21
|
*/
|
|
105
|
-
export
|
|
106
|
-
export
|
|
22
|
+
export const IDENTIFIER_KEY_SEPARATOR = '-';
|
|
23
|
+
export const errorMessages = {
|
|
107
24
|
idEmptyString: 'An index field cannot contain an empty string value',
|
|
108
25
|
queryByPkWithCompositeKeyPresent: 'Models with composite primary keys cannot be queried by a single key value. Use object literal syntax for composite keys instead: https://docs.amplify.aws/lib/datastore/advanced-workflows/q/platform/js/#querying-records-with-custom-primary-keys',
|
|
109
26
|
deleteByPkWithCompositeKeyPresent: 'Models with composite primary keys cannot be deleted by a single key value, unless using a predicate. Use object literal syntax for composite keys instead: https://docs.amplify.aws/lib/datastore/advanced-workflows/q/platform/js/#querying-records-with-custom-primary-keys',
|
|
@@ -116,23 +33,22 @@ export var NAMESPACES;
|
|
|
116
33
|
NAMESPACES["SYNC"] = "sync";
|
|
117
34
|
NAMESPACES["STORAGE"] = "storage";
|
|
118
35
|
})(NAMESPACES || (NAMESPACES = {}));
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
36
|
+
const DATASTORE = NAMESPACES.DATASTORE;
|
|
37
|
+
const USER = NAMESPACES.USER;
|
|
38
|
+
const SYNC = NAMESPACES.SYNC;
|
|
39
|
+
const STORAGE = NAMESPACES.STORAGE;
|
|
123
40
|
export { USER, SYNC, STORAGE, DATASTORE };
|
|
124
|
-
export
|
|
125
|
-
if (throwOnError === void 0) { throwOnError = true; }
|
|
41
|
+
export const exhaustiveCheck = (obj, throwOnError = true) => {
|
|
126
42
|
if (throwOnError) {
|
|
127
|
-
throw new Error(
|
|
43
|
+
throw new Error(`Invalid ${obj}`);
|
|
128
44
|
}
|
|
129
45
|
};
|
|
130
|
-
export
|
|
46
|
+
export const isNullOrUndefined = (val) => {
|
|
131
47
|
return typeof val === 'undefined' || val === undefined || val === null;
|
|
132
48
|
};
|
|
133
|
-
export
|
|
134
|
-
|
|
135
|
-
|
|
49
|
+
export const validatePredicate = (model, groupType, predicatesOrGroups) => {
|
|
50
|
+
let filterType;
|
|
51
|
+
let isNegation = false;
|
|
136
52
|
if (predicatesOrGroups.length === 0) {
|
|
137
53
|
return true;
|
|
138
54
|
}
|
|
@@ -148,23 +64,23 @@ export var validatePredicate = function (model, groupType, predicatesOrGroups) {
|
|
|
148
64
|
filterType = 'some';
|
|
149
65
|
break;
|
|
150
66
|
default:
|
|
151
|
-
throw new Error(
|
|
67
|
+
throw new Error(`Invalid ${groupType}`);
|
|
152
68
|
}
|
|
153
|
-
|
|
69
|
+
const result = predicatesOrGroups[filterType](predicateOrGroup => {
|
|
154
70
|
if (isPredicateObj(predicateOrGroup)) {
|
|
155
|
-
|
|
156
|
-
|
|
71
|
+
const { field, operator, operand } = predicateOrGroup;
|
|
72
|
+
const value = model[field];
|
|
157
73
|
return validatePredicateField(value, operator, operand);
|
|
158
74
|
}
|
|
159
75
|
if (isPredicateGroup(predicateOrGroup)) {
|
|
160
|
-
|
|
76
|
+
const { type, predicates } = predicateOrGroup;
|
|
161
77
|
return validatePredicate(model, type, predicates);
|
|
162
78
|
}
|
|
163
79
|
throw new Error('Not a predicate or group');
|
|
164
80
|
});
|
|
165
81
|
return isNegation ? !result : result;
|
|
166
82
|
};
|
|
167
|
-
export
|
|
83
|
+
export const validatePredicateField = (value, operator, operand) => {
|
|
168
84
|
switch (operator) {
|
|
169
85
|
case 'ne':
|
|
170
86
|
return value !== operand;
|
|
@@ -179,7 +95,7 @@ export var validatePredicateField = function (value, operator, operand) {
|
|
|
179
95
|
case 'gt':
|
|
180
96
|
return value > operand;
|
|
181
97
|
case 'between':
|
|
182
|
-
|
|
98
|
+
const [min, max] = operand;
|
|
183
99
|
return value >= min && value <= max;
|
|
184
100
|
case 'beginsWith':
|
|
185
101
|
return (!isNullOrUndefined(value) &&
|
|
@@ -194,21 +110,21 @@ export var validatePredicateField = function (value, operator, operand) {
|
|
|
194
110
|
return false;
|
|
195
111
|
}
|
|
196
112
|
};
|
|
197
|
-
export
|
|
113
|
+
export const isModelConstructor = (obj) => {
|
|
198
114
|
return (obj && typeof obj.copyOf === 'function');
|
|
199
115
|
};
|
|
200
|
-
|
|
116
|
+
const nonModelClasses = new WeakSet();
|
|
201
117
|
export function registerNonModelClass(clazz) {
|
|
202
118
|
nonModelClasses.add(clazz);
|
|
203
119
|
}
|
|
204
|
-
export
|
|
120
|
+
export const isNonModelConstructor = (obj) => {
|
|
205
121
|
return nonModelClasses.has(obj);
|
|
206
122
|
};
|
|
207
|
-
|
|
208
|
-
export
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
123
|
+
const topologicallySortedModels = new WeakMap();
|
|
124
|
+
export const traverseModel = (srcModelName, instance, namespace, modelInstanceCreator, getModelConstructorByModelName) => {
|
|
125
|
+
const modelConstructor = getModelConstructorByModelName(namespace.name, srcModelName);
|
|
126
|
+
const result = [];
|
|
127
|
+
const newInstance = modelConstructor.copyOf(instance, () => { });
|
|
212
128
|
result.unshift({
|
|
213
129
|
modelName: srcModelName,
|
|
214
130
|
item: newInstance,
|
|
@@ -217,38 +133,29 @@ export var traverseModel = function (srcModelName, instance, namespace, modelIns
|
|
|
217
133
|
if (!topologicallySortedModels.has(namespace)) {
|
|
218
134
|
topologicallySortedModels.set(namespace, Array.from(namespace.modelTopologicalOrdering.keys()));
|
|
219
135
|
}
|
|
220
|
-
|
|
221
|
-
result.sort(
|
|
136
|
+
const sortedModels = topologicallySortedModels.get(namespace);
|
|
137
|
+
result.sort((a, b) => {
|
|
222
138
|
return (sortedModels.indexOf(a.modelName) - sortedModels.indexOf(b.modelName));
|
|
223
139
|
});
|
|
224
140
|
return result;
|
|
225
141
|
};
|
|
226
|
-
|
|
227
|
-
export
|
|
228
|
-
return new Promise(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
142
|
+
let privateModeCheckResult;
|
|
143
|
+
export const isPrivateMode = () => {
|
|
144
|
+
return new Promise(resolve => {
|
|
145
|
+
const dbname = amplifyUuid();
|
|
146
|
+
let db;
|
|
147
|
+
const isPrivate = () => {
|
|
232
148
|
privateModeCheckResult = false;
|
|
233
149
|
resolve(true);
|
|
234
150
|
};
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
_c.label = 2;
|
|
244
|
-
case 2: return [4 /*yield*/, indexedDB.deleteDatabase(dbname)];
|
|
245
|
-
case 3:
|
|
246
|
-
_c.sent();
|
|
247
|
-
privateModeCheckResult = true;
|
|
248
|
-
return [2 /*return*/, resolve(false)];
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
}); };
|
|
151
|
+
const isNotPrivate = async () => {
|
|
152
|
+
if (db && db.result && typeof db.result.close === 'function') {
|
|
153
|
+
await db.result.close();
|
|
154
|
+
}
|
|
155
|
+
await indexedDB.deleteDatabase(dbname);
|
|
156
|
+
privateModeCheckResult = true;
|
|
157
|
+
return resolve(false);
|
|
158
|
+
};
|
|
252
159
|
if (privateModeCheckResult === true) {
|
|
253
160
|
return isNotPrivate();
|
|
254
161
|
}
|
|
@@ -262,7 +169,7 @@ export var isPrivateMode = function () {
|
|
|
262
169
|
db.onsuccess = isNotPrivate;
|
|
263
170
|
});
|
|
264
171
|
};
|
|
265
|
-
|
|
172
|
+
let safariCompatabilityModeResult;
|
|
266
173
|
/**
|
|
267
174
|
* Whether the browser's implementation of IndexedDB breaks on array lookups
|
|
268
175
|
* against composite indexes whose keypath contains a single column.
|
|
@@ -273,92 +180,99 @@ var safariCompatabilityModeResult;
|
|
|
273
180
|
* In all known, modern Safari browsers as of Q4 2022, the query against an index like
|
|
274
181
|
* this will *always* return `undefined`. So, the index needs to be created as a scalar.
|
|
275
182
|
*/
|
|
276
|
-
export
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
var _c;
|
|
299
|
-
var db = (_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.result;
|
|
300
|
-
db.onerror = function () { return resolve(false); };
|
|
301
|
-
var store = db.createObjectStore(storeName_1, {
|
|
302
|
-
autoIncrement: true,
|
|
303
|
-
});
|
|
304
|
-
store.createIndex(indexName_1, ['id']);
|
|
305
|
-
};
|
|
306
|
-
})];
|
|
307
|
-
case 1:
|
|
308
|
-
db_1 = _c.sent();
|
|
309
|
-
if (!db_1) {
|
|
310
|
-
throw new Error('Could not open probe DB');
|
|
311
|
-
}
|
|
312
|
-
rwTx = db_1.transaction(storeName_1, 'readwrite');
|
|
313
|
-
rwStore = rwTx.objectStore(storeName_1);
|
|
314
|
-
rwStore.add({
|
|
315
|
-
id: 1,
|
|
183
|
+
export const isSafariCompatabilityMode = async () => {
|
|
184
|
+
try {
|
|
185
|
+
const dbName = amplifyUuid();
|
|
186
|
+
const storeName = 'indexedDBFeatureProbeStore';
|
|
187
|
+
const indexName = 'idx';
|
|
188
|
+
if (indexedDB === null)
|
|
189
|
+
return false;
|
|
190
|
+
if (safariCompatabilityModeResult !== undefined) {
|
|
191
|
+
return safariCompatabilityModeResult;
|
|
192
|
+
}
|
|
193
|
+
const db = await new Promise(resolve => {
|
|
194
|
+
const dbOpenRequest = indexedDB.open(dbName);
|
|
195
|
+
dbOpenRequest.onerror = () => resolve(false);
|
|
196
|
+
dbOpenRequest.onsuccess = () => {
|
|
197
|
+
const db = dbOpenRequest.result;
|
|
198
|
+
resolve(db);
|
|
199
|
+
};
|
|
200
|
+
dbOpenRequest.onupgradeneeded = (event) => {
|
|
201
|
+
const db = event?.target?.result;
|
|
202
|
+
db.onerror = () => resolve(false);
|
|
203
|
+
const store = db.createObjectStore(storeName, {
|
|
204
|
+
autoIncrement: true,
|
|
316
205
|
});
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
var getRequest = index.get([1]);
|
|
323
|
-
getRequest.onerror = function () { return resolve(false); };
|
|
324
|
-
getRequest.onsuccess = function (event) {
|
|
325
|
-
var _c;
|
|
326
|
-
resolve((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.result);
|
|
327
|
-
};
|
|
328
|
-
})];
|
|
329
|
-
case 2:
|
|
330
|
-
result = _c.sent();
|
|
331
|
-
if (!(db_1 && typeof db_1.close === 'function')) return [3 /*break*/, 4];
|
|
332
|
-
return [4 /*yield*/, db_1.close()];
|
|
333
|
-
case 3:
|
|
334
|
-
_c.sent();
|
|
335
|
-
_c.label = 4;
|
|
336
|
-
case 4: return [4 /*yield*/, indexedDB.deleteDatabase(dbName_1)];
|
|
337
|
-
case 5:
|
|
338
|
-
_c.sent();
|
|
339
|
-
if (result === undefined) {
|
|
340
|
-
safariCompatabilityModeResult = true;
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
safariCompatabilityModeResult = false;
|
|
344
|
-
}
|
|
345
|
-
return [3 /*break*/, 7];
|
|
346
|
-
case 6:
|
|
347
|
-
error_1 = _c.sent();
|
|
348
|
-
safariCompatabilityModeResult = false;
|
|
349
|
-
return [3 /*break*/, 7];
|
|
350
|
-
case 7: return [2 /*return*/, safariCompatabilityModeResult];
|
|
206
|
+
store.createIndex(indexName, ['id']);
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
if (!db) {
|
|
210
|
+
throw new Error('Could not open probe DB');
|
|
351
211
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
212
|
+
const rwTx = db.transaction(storeName, 'readwrite');
|
|
213
|
+
const rwStore = rwTx.objectStore(storeName);
|
|
214
|
+
rwStore.add({
|
|
215
|
+
id: 1,
|
|
216
|
+
});
|
|
217
|
+
rwTx.commit();
|
|
218
|
+
const result = await new Promise(resolve => {
|
|
219
|
+
const tx = db.transaction(storeName, 'readonly');
|
|
220
|
+
const store = tx.objectStore(storeName);
|
|
221
|
+
const index = store.index(indexName);
|
|
222
|
+
const getRequest = index.get([1]);
|
|
223
|
+
getRequest.onerror = () => resolve(false);
|
|
224
|
+
getRequest.onsuccess = (event) => {
|
|
225
|
+
resolve(event?.target?.result);
|
|
226
|
+
};
|
|
227
|
+
});
|
|
228
|
+
if (db && typeof db.close === 'function') {
|
|
229
|
+
await db.close();
|
|
230
|
+
}
|
|
231
|
+
await indexedDB.deleteDatabase(dbName);
|
|
232
|
+
if (result === undefined) {
|
|
233
|
+
safariCompatabilityModeResult = true;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
safariCompatabilityModeResult = false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
safariCompatabilityModeResult = false;
|
|
241
|
+
}
|
|
242
|
+
return safariCompatabilityModeResult;
|
|
243
|
+
};
|
|
244
|
+
const HEX_TO_SHORT = {};
|
|
245
|
+
for (let i = 0; i < 256; i++) {
|
|
246
|
+
let encodedByte = i.toString(16).toLowerCase();
|
|
247
|
+
if (encodedByte.length === 1) {
|
|
248
|
+
encodedByte = `0${encodedByte}`;
|
|
249
|
+
}
|
|
250
|
+
HEX_TO_SHORT[encodedByte] = i;
|
|
251
|
+
}
|
|
252
|
+
const getBytesFromHex = (encoded) => {
|
|
253
|
+
if (encoded.length % 2 !== 0) {
|
|
254
|
+
throw new Error('Hex encoded strings must have an even number length');
|
|
255
|
+
}
|
|
256
|
+
const out = new Uint8Array(encoded.length / 2);
|
|
257
|
+
for (let i = 0; i < encoded.length; i += 2) {
|
|
258
|
+
const encodedByte = encoded.slice(i, i + 2).toLowerCase();
|
|
259
|
+
if (encodedByte in HEX_TO_SHORT) {
|
|
260
|
+
out[i / 2] = HEX_TO_SHORT[encodedByte];
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
throw new Error(`Cannot decode unrecognized sequence ${encodedByte} as hexadecimal`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return out;
|
|
267
|
+
};
|
|
268
|
+
const randomBytes = (nBytes) => {
|
|
269
|
+
const str = new WordArray().random(nBytes).toString();
|
|
270
|
+
return getBytesFromHex(str);
|
|
271
|
+
};
|
|
272
|
+
const prng = () => randomBytes(1)[0] / 0xff;
|
|
359
273
|
export function monotonicUlidFactory(seed) {
|
|
360
|
-
|
|
361
|
-
return
|
|
274
|
+
const ulid = monotonicFactory(prng);
|
|
275
|
+
return () => {
|
|
362
276
|
return ulid(seed);
|
|
363
277
|
};
|
|
364
278
|
}
|
|
@@ -386,29 +300,18 @@ export function getNow() {
|
|
|
386
300
|
}
|
|
387
301
|
export function sortCompareFunction(sortPredicates) {
|
|
388
302
|
return function compareFunction(a, b) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
var sortMultiplier = sortDirection === SortDirection.ASCENDING ? 1 : -1;
|
|
398
|
-
if (a[field] < b[field]) {
|
|
399
|
-
return -1 * sortMultiplier;
|
|
400
|
-
}
|
|
401
|
-
if (a[field] > b[field]) {
|
|
402
|
-
return 1 * sortMultiplier;
|
|
403
|
-
}
|
|
303
|
+
// enable multi-field sort by iterating over predicates until
|
|
304
|
+
// a comparison returns -1 or 1
|
|
305
|
+
for (const predicate of sortPredicates) {
|
|
306
|
+
const { field, sortDirection } = predicate;
|
|
307
|
+
// reverse result when direction is descending
|
|
308
|
+
const sortMultiplier = sortDirection === SortDirection.ASCENDING ? 1 : -1;
|
|
309
|
+
if (a[field] < b[field]) {
|
|
310
|
+
return -1 * sortMultiplier;
|
|
404
311
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
finally {
|
|
408
|
-
try {
|
|
409
|
-
if (sortPredicates_1_1 && !sortPredicates_1_1.done && (_c = sortPredicates_1.return)) _c.call(sortPredicates_1);
|
|
312
|
+
if (a[field] > b[field]) {
|
|
313
|
+
return 1 * sortMultiplier;
|
|
410
314
|
}
|
|
411
|
-
finally { if (e_1) throw e_1.error; }
|
|
412
315
|
}
|
|
413
316
|
return 0;
|
|
414
317
|
};
|
|
@@ -418,12 +321,10 @@ export function sortCompareFunction(sortPredicates) {
|
|
|
418
321
|
// returns true if equal by value
|
|
419
322
|
// if nullish is true, treat undefined and null values as equal
|
|
420
323
|
// to normalize for GQL response values for undefined fields
|
|
421
|
-
export function valuesEqual(valA, valB, nullish) {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
var b = valB;
|
|
426
|
-
var nullishCompare = function (_a, _b) {
|
|
324
|
+
export function valuesEqual(valA, valB, nullish = false) {
|
|
325
|
+
let a = valA;
|
|
326
|
+
let b = valB;
|
|
327
|
+
const nullishCompare = (_a, _b) => {
|
|
427
328
|
return ((_a === undefined || _a === null) && (_b === undefined || _b === null));
|
|
428
329
|
};
|
|
429
330
|
// if one of the values is a primitive and the other is an object
|
|
@@ -444,15 +345,15 @@ export function valuesEqual(valA, valB, nullish) {
|
|
|
444
345
|
return false;
|
|
445
346
|
}
|
|
446
347
|
if (a instanceof Set && b instanceof Set) {
|
|
447
|
-
a =
|
|
448
|
-
b =
|
|
348
|
+
a = [...a];
|
|
349
|
+
b = [...b];
|
|
449
350
|
}
|
|
450
351
|
if (a instanceof Map && b instanceof Map) {
|
|
451
352
|
a = Object.fromEntries(a);
|
|
452
353
|
b = Object.fromEntries(b);
|
|
453
354
|
}
|
|
454
|
-
|
|
455
|
-
|
|
355
|
+
const aKeys = Object.keys(a);
|
|
356
|
+
const bKeys = Object.keys(b);
|
|
456
357
|
// last condition is to ensure that [] !== [null] even if nullish. However [undefined] === [null] when nullish
|
|
457
358
|
if (aKeys.length !== bKeys.length && (!nullish || Array.isArray(a))) {
|
|
458
359
|
return false;
|
|
@@ -460,23 +361,13 @@ export function valuesEqual(valA, valB, nullish) {
|
|
|
460
361
|
// iterate through the longer set of keys
|
|
461
362
|
// e.g., for a nullish comparison of a={ a: 1 } and b={ a: 1, b: null }
|
|
462
363
|
// we want to iterate through bKeys
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
if (!valuesEqual(aVal, bVal, nullish)) {
|
|
470
|
-
return false;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
475
|
-
finally {
|
|
476
|
-
try {
|
|
477
|
-
if (keys_1_1 && !keys_1_1.done && (_c = keys_1.return)) _c.call(keys_1);
|
|
364
|
+
const keys = aKeys.length >= bKeys.length ? aKeys : bKeys;
|
|
365
|
+
for (const key of keys) {
|
|
366
|
+
const aVal = a[key];
|
|
367
|
+
const bVal = b[key];
|
|
368
|
+
if (!valuesEqual(aVal, bVal, nullish)) {
|
|
369
|
+
return false;
|
|
478
370
|
}
|
|
479
|
-
finally { if (e_2) throw e_2.error; }
|
|
480
371
|
}
|
|
481
372
|
return true;
|
|
482
373
|
}
|
|
@@ -490,15 +381,15 @@ export function valuesEqual(valA, valB, nullish) {
|
|
|
490
381
|
export function inMemoryPagination(records, pagination) {
|
|
491
382
|
if (pagination && records.length > 1) {
|
|
492
383
|
if (pagination.sort) {
|
|
493
|
-
|
|
384
|
+
const sortPredicates = ModelSortPredicateCreator.getPredicates(pagination.sort);
|
|
494
385
|
if (sortPredicates.length) {
|
|
495
|
-
|
|
386
|
+
const compareFn = sortCompareFunction(sortPredicates);
|
|
496
387
|
records.sort(compareFn);
|
|
497
388
|
}
|
|
498
389
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
390
|
+
const { page = 0, limit = 0 } = pagination;
|
|
391
|
+
const start = Math.max(0, page * limit) || 0;
|
|
392
|
+
const end = limit > 0 ? start + limit : records.length;
|
|
502
393
|
return records.slice(start, end);
|
|
503
394
|
}
|
|
504
395
|
return records;
|
|
@@ -510,43 +401,13 @@ export function inMemoryPagination(records, pagination) {
|
|
|
510
401
|
* return Promise<boolean>: `true` for a matching item, `false` otherwise.
|
|
511
402
|
* @returns A `Promise<boolean>`, `true` if "some" items match; `false` otherwise.
|
|
512
403
|
*/
|
|
513
|
-
export function asyncSome(items, matches) {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
_d.trys.push([0, 5, 6, 7]);
|
|
521
|
-
items_1 = __values(items), items_1_1 = items_1.next();
|
|
522
|
-
_d.label = 1;
|
|
523
|
-
case 1:
|
|
524
|
-
if (!!items_1_1.done) return [3 /*break*/, 4];
|
|
525
|
-
item = items_1_1.value;
|
|
526
|
-
return [4 /*yield*/, matches(item)];
|
|
527
|
-
case 2:
|
|
528
|
-
if (_d.sent()) {
|
|
529
|
-
return [2 /*return*/, true];
|
|
530
|
-
}
|
|
531
|
-
_d.label = 3;
|
|
532
|
-
case 3:
|
|
533
|
-
items_1_1 = items_1.next();
|
|
534
|
-
return [3 /*break*/, 1];
|
|
535
|
-
case 4: return [3 /*break*/, 7];
|
|
536
|
-
case 5:
|
|
537
|
-
e_3_1 = _d.sent();
|
|
538
|
-
e_3 = { error: e_3_1 };
|
|
539
|
-
return [3 /*break*/, 7];
|
|
540
|
-
case 6:
|
|
541
|
-
try {
|
|
542
|
-
if (items_1_1 && !items_1_1.done && (_c = items_1.return)) _c.call(items_1);
|
|
543
|
-
}
|
|
544
|
-
finally { if (e_3) throw e_3.error; }
|
|
545
|
-
return [7 /*endfinally*/];
|
|
546
|
-
case 7: return [2 /*return*/, false];
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
});
|
|
404
|
+
export async function asyncSome(items, matches) {
|
|
405
|
+
for (const item of items) {
|
|
406
|
+
if (await matches(item)) {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return false;
|
|
550
411
|
}
|
|
551
412
|
/**
|
|
552
413
|
* An `aysnc` implementation of `Array.every()`. Returns as soon as a non-match is found.
|
|
@@ -555,43 +416,13 @@ export function asyncSome(items, matches) {
|
|
|
555
416
|
* return Promise<boolean>: `true` for a matching item, `false` otherwise.
|
|
556
417
|
* @returns A `Promise<boolean>`, `true` if every item matches; `false` otherwise.
|
|
557
418
|
*/
|
|
558
|
-
export function asyncEvery(items, matches) {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
_d.trys.push([0, 5, 6, 7]);
|
|
566
|
-
items_2 = __values(items), items_2_1 = items_2.next();
|
|
567
|
-
_d.label = 1;
|
|
568
|
-
case 1:
|
|
569
|
-
if (!!items_2_1.done) return [3 /*break*/, 4];
|
|
570
|
-
item = items_2_1.value;
|
|
571
|
-
return [4 /*yield*/, matches(item)];
|
|
572
|
-
case 2:
|
|
573
|
-
if (!(_d.sent())) {
|
|
574
|
-
return [2 /*return*/, false];
|
|
575
|
-
}
|
|
576
|
-
_d.label = 3;
|
|
577
|
-
case 3:
|
|
578
|
-
items_2_1 = items_2.next();
|
|
579
|
-
return [3 /*break*/, 1];
|
|
580
|
-
case 4: return [3 /*break*/, 7];
|
|
581
|
-
case 5:
|
|
582
|
-
e_4_1 = _d.sent();
|
|
583
|
-
e_4 = { error: e_4_1 };
|
|
584
|
-
return [3 /*break*/, 7];
|
|
585
|
-
case 6:
|
|
586
|
-
try {
|
|
587
|
-
if (items_2_1 && !items_2_1.done && (_c = items_2.return)) _c.call(items_2);
|
|
588
|
-
}
|
|
589
|
-
finally { if (e_4) throw e_4.error; }
|
|
590
|
-
return [7 /*endfinally*/];
|
|
591
|
-
case 7: return [2 /*return*/, true];
|
|
592
|
-
}
|
|
593
|
-
});
|
|
594
|
-
});
|
|
419
|
+
export async function asyncEvery(items, matches) {
|
|
420
|
+
for (const item of items) {
|
|
421
|
+
if (!(await matches(item))) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return true;
|
|
595
426
|
}
|
|
596
427
|
/**
|
|
597
428
|
* An `async` implementation of `Array.filter()`. Returns after all items have been filtered.
|
|
@@ -601,162 +432,114 @@ export function asyncEvery(items, matches) {
|
|
|
601
432
|
* return Promise<boolean>: `true` for a matching item, `false` otherwise.
|
|
602
433
|
* @returns A `Promise<T>` of matching items.
|
|
603
434
|
*/
|
|
604
|
-
export function asyncFilter(items, matches) {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
_d.label = 1;
|
|
613
|
-
case 1:
|
|
614
|
-
_d.trys.push([1, 6, 7, 8]);
|
|
615
|
-
items_3 = __values(items), items_3_1 = items_3.next();
|
|
616
|
-
_d.label = 2;
|
|
617
|
-
case 2:
|
|
618
|
-
if (!!items_3_1.done) return [3 /*break*/, 5];
|
|
619
|
-
item = items_3_1.value;
|
|
620
|
-
return [4 /*yield*/, matches(item)];
|
|
621
|
-
case 3:
|
|
622
|
-
if (_d.sent()) {
|
|
623
|
-
results.push(item);
|
|
624
|
-
}
|
|
625
|
-
_d.label = 4;
|
|
626
|
-
case 4:
|
|
627
|
-
items_3_1 = items_3.next();
|
|
628
|
-
return [3 /*break*/, 2];
|
|
629
|
-
case 5: return [3 /*break*/, 8];
|
|
630
|
-
case 6:
|
|
631
|
-
e_5_1 = _d.sent();
|
|
632
|
-
e_5 = { error: e_5_1 };
|
|
633
|
-
return [3 /*break*/, 8];
|
|
634
|
-
case 7:
|
|
635
|
-
try {
|
|
636
|
-
if (items_3_1 && !items_3_1.done && (_c = items_3.return)) _c.call(items_3);
|
|
637
|
-
}
|
|
638
|
-
finally { if (e_5) throw e_5.error; }
|
|
639
|
-
return [7 /*endfinally*/];
|
|
640
|
-
case 8: return [2 /*return*/, results];
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
});
|
|
435
|
+
export async function asyncFilter(items, matches) {
|
|
436
|
+
const results = [];
|
|
437
|
+
for (const item of items) {
|
|
438
|
+
if (await matches(item)) {
|
|
439
|
+
results.push(item);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return results;
|
|
644
443
|
}
|
|
645
|
-
export
|
|
444
|
+
export const isAWSDate = (val) => {
|
|
646
445
|
return !!/^\d{4}-\d{2}-\d{2}(Z|[+-]\d{2}:\d{2}($|:\d{2}))?$/.exec(val);
|
|
647
446
|
};
|
|
648
|
-
export
|
|
447
|
+
export const isAWSTime = (val) => {
|
|
649
448
|
return !!/^\d{2}:\d{2}(:\d{2}(.\d+)?)?(Z|[+-]\d{2}:\d{2}($|:\d{2}))?$/.exec(val);
|
|
650
449
|
};
|
|
651
|
-
export
|
|
450
|
+
export const isAWSDateTime = (val) => {
|
|
652
451
|
return !!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(.\d+)?)?(Z|[+-]\d{2}:\d{2}($|:\d{2}))?$/.exec(val);
|
|
653
452
|
};
|
|
654
|
-
export
|
|
453
|
+
export const isAWSTimestamp = (val) => {
|
|
655
454
|
return !!/^\d+$/.exec(String(val));
|
|
656
455
|
};
|
|
657
|
-
export
|
|
456
|
+
export const isAWSEmail = (val) => {
|
|
658
457
|
return !!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.exec(val);
|
|
659
458
|
};
|
|
660
|
-
export
|
|
459
|
+
export const isAWSJSON = (val) => {
|
|
661
460
|
try {
|
|
662
461
|
JSON.parse(val);
|
|
663
462
|
return true;
|
|
664
463
|
}
|
|
665
|
-
catch
|
|
464
|
+
catch {
|
|
666
465
|
return false;
|
|
667
466
|
}
|
|
668
467
|
};
|
|
669
|
-
export
|
|
468
|
+
export const isAWSURL = (val) => {
|
|
670
469
|
try {
|
|
671
470
|
return !!new AmplifyUrl(val);
|
|
672
471
|
}
|
|
673
|
-
catch
|
|
472
|
+
catch {
|
|
674
473
|
return false;
|
|
675
474
|
}
|
|
676
475
|
};
|
|
677
|
-
export
|
|
476
|
+
export const isAWSPhone = (val) => {
|
|
678
477
|
return !!/^\+?\d[\d\s-]+$/.exec(val);
|
|
679
478
|
};
|
|
680
|
-
export
|
|
479
|
+
export const isAWSIPAddress = (val) => {
|
|
681
480
|
return !!/((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?$))$/.exec(val);
|
|
682
481
|
};
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
this.promise = new Promise(
|
|
482
|
+
export class DeferredPromise {
|
|
483
|
+
constructor() {
|
|
484
|
+
const self = this;
|
|
485
|
+
this.promise = new Promise((resolve, reject) => {
|
|
687
486
|
self.resolve = resolve;
|
|
688
487
|
self.reject = reject;
|
|
689
488
|
});
|
|
690
489
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
var DeferredCallbackResolver = /** @class */ (function () {
|
|
695
|
-
function DeferredCallbackResolver(options) {
|
|
490
|
+
}
|
|
491
|
+
export class DeferredCallbackResolver {
|
|
492
|
+
constructor(options) {
|
|
696
493
|
this.limitPromise = new DeferredPromise();
|
|
697
494
|
this.raceInFlight = false;
|
|
698
|
-
this.callback =
|
|
699
|
-
this.defaultErrorHandler =
|
|
700
|
-
if (msg === void 0) { msg = 'DeferredCallbackResolver error'; }
|
|
495
|
+
this.callback = () => { };
|
|
496
|
+
this.defaultErrorHandler = (msg = 'DeferredCallbackResolver error') => {
|
|
701
497
|
throw new Error(msg);
|
|
702
498
|
};
|
|
703
499
|
this.callback = options.callback;
|
|
704
500
|
this.errorHandler = options.errorHandler || this.defaultErrorHandler;
|
|
705
501
|
this.maxInterval = options.maxInterval || 2000;
|
|
706
502
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
_this.timer = setTimeout(function () {
|
|
503
|
+
startTimer() {
|
|
504
|
+
this.timerPromise = new Promise((resolve, reject) => {
|
|
505
|
+
this.timer = setTimeout(() => {
|
|
711
506
|
resolve(LimitTimerRaceResolvedValues.TIMER);
|
|
712
|
-
},
|
|
507
|
+
}, this.maxInterval);
|
|
713
508
|
});
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
this.clear();
|
|
739
|
-
this.raceInFlight = false;
|
|
740
|
-
this.limitPromise = new DeferredPromise();
|
|
741
|
-
return [2 /*return*/, winner];
|
|
742
|
-
case 4: return [2 /*return*/];
|
|
743
|
-
}
|
|
744
|
-
});
|
|
745
|
-
});
|
|
746
|
-
};
|
|
747
|
-
DeferredCallbackResolver.prototype.start = function () {
|
|
509
|
+
}
|
|
510
|
+
async racePromises() {
|
|
511
|
+
let winner;
|
|
512
|
+
try {
|
|
513
|
+
this.raceInFlight = true;
|
|
514
|
+
this.startTimer();
|
|
515
|
+
winner = await Promise.race([
|
|
516
|
+
this.timerPromise,
|
|
517
|
+
this.limitPromise.promise,
|
|
518
|
+
]);
|
|
519
|
+
this.callback();
|
|
520
|
+
}
|
|
521
|
+
catch (err) {
|
|
522
|
+
this.errorHandler(err);
|
|
523
|
+
}
|
|
524
|
+
finally {
|
|
525
|
+
// reset for the next race
|
|
526
|
+
this.clear();
|
|
527
|
+
this.raceInFlight = false;
|
|
528
|
+
this.limitPromise = new DeferredPromise();
|
|
529
|
+
return winner;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
start() {
|
|
748
533
|
if (!this.raceInFlight)
|
|
749
534
|
this.racePromises();
|
|
750
|
-
}
|
|
751
|
-
|
|
535
|
+
}
|
|
536
|
+
clear() {
|
|
752
537
|
clearTimeout(this.timer);
|
|
753
|
-
}
|
|
754
|
-
|
|
538
|
+
}
|
|
539
|
+
resolve() {
|
|
755
540
|
this.limitPromise.resolve(LimitTimerRaceResolvedValues.LIMIT);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
}());
|
|
759
|
-
export { DeferredCallbackResolver };
|
|
541
|
+
}
|
|
542
|
+
}
|
|
760
543
|
/**
|
|
761
544
|
* merge two sets of patches created by immer produce.
|
|
762
545
|
* newPatches take precedent over oldPatches for patches modifying the same path.
|
|
@@ -775,17 +558,17 @@ export { DeferredCallbackResolver };
|
|
|
775
558
|
* @return merged patches
|
|
776
559
|
*/
|
|
777
560
|
export function mergePatches(originalSource, oldPatches, newPatches) {
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
produce(originalSource,
|
|
561
|
+
const patchesToMerge = oldPatches.concat(newPatches);
|
|
562
|
+
let patches;
|
|
563
|
+
produce(originalSource, draft => {
|
|
781
564
|
applyPatches(draft, patchesToMerge);
|
|
782
|
-
},
|
|
565
|
+
}, p => {
|
|
783
566
|
patches = p;
|
|
784
567
|
});
|
|
785
568
|
return patches;
|
|
786
569
|
}
|
|
787
|
-
export
|
|
788
|
-
|
|
570
|
+
export const getStorename = (namespace, modelName) => {
|
|
571
|
+
const storeName = `${namespace}_${modelName}`;
|
|
789
572
|
return storeName;
|
|
790
573
|
};
|
|
791
574
|
//#region Key Utils
|
|
@@ -821,73 +604,70 @@ export var getStorename = function (namespace, modelName) {
|
|
|
821
604
|
|
|
822
605
|
See 'processCompositeKeys' test in util.test.ts for more examples
|
|
823
606
|
*/
|
|
824
|
-
export
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
return sortKeyFields;
|
|
830
|
-
};
|
|
831
|
-
var compositeKeyFields = attributes
|
|
607
|
+
export const processCompositeKeys = (attributes) => {
|
|
608
|
+
const extractCompositeSortKey = ({ properties: {
|
|
609
|
+
// ignore the HK (fields[0]) we only need to include the composite sort key fields[1...n]
|
|
610
|
+
fields: [, ...sortKeyFields], }, }) => sortKeyFields;
|
|
611
|
+
const compositeKeyFields = attributes
|
|
832
612
|
.filter(isModelAttributeCompositeKey)
|
|
833
613
|
.map(extractCompositeSortKey);
|
|
834
614
|
/*
|
|
835
615
|
if 2 sets of fields have any intersecting fields => combine them into 1 union set
|
|
836
616
|
e.g., ['a', 'b', 'c'] and ['a', 'b', 'd'] => ['a', 'b', 'c', 'd']
|
|
837
617
|
*/
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
combined.push(sortKeyFieldsSet);
|
|
843
|
-
return combined;
|
|
844
|
-
}
|
|
845
|
-
// does the current set share values with another set we've already added to `combined`?
|
|
846
|
-
var intersectingSetIdx = combined.findIndex(function (existingSet) {
|
|
847
|
-
return __spreadArray([], __read(existingSet), false).some(function (f) { return sortKeyFieldsSet.has(f); });
|
|
848
|
-
});
|
|
849
|
-
if (intersectingSetIdx > -1) {
|
|
850
|
-
var union = new Set(__spreadArray(__spreadArray([], __read(combined[intersectingSetIdx]), false), __read(sortKeyFieldsSet), false));
|
|
851
|
-
// combine the current set with the intersecting set we found above
|
|
852
|
-
combined[intersectingSetIdx] = union;
|
|
853
|
-
}
|
|
854
|
-
else {
|
|
855
|
-
// none of the sets in `combined` have intersecting values with the current set
|
|
856
|
-
combined.push(sortKeyFieldsSet);
|
|
857
|
-
}
|
|
618
|
+
const combineIntersecting = (fields) => fields.reduce((combined, sortKeyFields) => {
|
|
619
|
+
const sortKeyFieldsSet = new Set(sortKeyFields);
|
|
620
|
+
if (combined.length === 0) {
|
|
621
|
+
combined.push(sortKeyFieldsSet);
|
|
858
622
|
return combined;
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
|
|
623
|
+
}
|
|
624
|
+
// does the current set share values with another set we've already added to `combined`?
|
|
625
|
+
const intersectingSetIdx = combined.findIndex(existingSet => {
|
|
626
|
+
return [...existingSet].some(f => sortKeyFieldsSet.has(f));
|
|
627
|
+
});
|
|
628
|
+
if (intersectingSetIdx > -1) {
|
|
629
|
+
const union = new Set([
|
|
630
|
+
...combined[intersectingSetIdx],
|
|
631
|
+
...sortKeyFieldsSet,
|
|
632
|
+
]);
|
|
633
|
+
// combine the current set with the intersecting set we found above
|
|
634
|
+
combined[intersectingSetIdx] = union;
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
// none of the sets in `combined` have intersecting values with the current set
|
|
638
|
+
combined.push(sortKeyFieldsSet);
|
|
639
|
+
}
|
|
640
|
+
return combined;
|
|
641
|
+
}, []);
|
|
642
|
+
const initial = combineIntersecting(compositeKeyFields);
|
|
862
643
|
// a single pass pay not be enough to correctly combine all the fields
|
|
863
644
|
// call the function once more to get a final merged list of sets
|
|
864
|
-
|
|
645
|
+
const combined = combineIntersecting(initial);
|
|
865
646
|
return combined;
|
|
866
647
|
};
|
|
867
|
-
export
|
|
868
|
-
|
|
869
|
-
var keyAttribute = (_c = modelDefinition === null || modelDefinition === void 0 ? void 0 : modelDefinition.attributes) === null || _c === void 0 ? void 0 : _c.find(isModelAttributeKey);
|
|
648
|
+
export const extractKeyIfExists = (modelDefinition) => {
|
|
649
|
+
const keyAttribute = modelDefinition?.attributes?.find(isModelAttributeKey);
|
|
870
650
|
return keyAttribute;
|
|
871
651
|
};
|
|
872
|
-
export
|
|
873
|
-
|
|
652
|
+
export const extractPrimaryKeyFieldNames = (modelDefinition) => {
|
|
653
|
+
const keyAttribute = extractKeyIfExists(modelDefinition);
|
|
874
654
|
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
875
655
|
return keyAttribute.properties.fields;
|
|
876
656
|
}
|
|
877
657
|
return [ID];
|
|
878
658
|
};
|
|
879
|
-
export
|
|
880
|
-
return keyFields.map(
|
|
659
|
+
export const extractPrimaryKeyValues = (model, keyFields) => {
|
|
660
|
+
return keyFields.map(key => model[key]);
|
|
881
661
|
};
|
|
882
|
-
export
|
|
883
|
-
|
|
884
|
-
keyFields.forEach(
|
|
662
|
+
export const extractPrimaryKeysAndValues = (model, keyFields) => {
|
|
663
|
+
const primaryKeysAndValues = {};
|
|
664
|
+
keyFields.forEach(key => (primaryKeysAndValues[key] = model[key]));
|
|
885
665
|
return primaryKeysAndValues;
|
|
886
666
|
};
|
|
887
667
|
// IdentifierFields<ManagedIdentifier>
|
|
888
668
|
// Default behavior without explicit @primaryKey defined
|
|
889
|
-
export
|
|
890
|
-
|
|
669
|
+
export const isIdManaged = (modelDefinition) => {
|
|
670
|
+
const keyAttribute = extractKeyIfExists(modelDefinition);
|
|
891
671
|
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
892
672
|
return false;
|
|
893
673
|
}
|
|
@@ -895,26 +675,25 @@ export var isIdManaged = function (modelDefinition) {
|
|
|
895
675
|
};
|
|
896
676
|
// IdentifierFields<OptionallyManagedIdentifier>
|
|
897
677
|
// @primaryKey with explicit `id` in the PK. Single key or composite
|
|
898
|
-
export
|
|
899
|
-
|
|
678
|
+
export const isIdOptionallyManaged = (modelDefinition) => {
|
|
679
|
+
const keyAttribute = extractKeyIfExists(modelDefinition);
|
|
900
680
|
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
901
681
|
return keyAttribute.properties.fields[0] === ID;
|
|
902
682
|
}
|
|
903
683
|
return false;
|
|
904
684
|
};
|
|
905
|
-
export
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
Object.keys(namespace.models).forEach(
|
|
909
|
-
var e_6, _c;
|
|
685
|
+
export const establishRelationAndKeys = (namespace) => {
|
|
686
|
+
const relationship = {};
|
|
687
|
+
const keys = {};
|
|
688
|
+
Object.keys(namespace.models).forEach((mKey) => {
|
|
910
689
|
relationship[mKey] = { indexes: [], relationTypes: [] };
|
|
911
690
|
keys[mKey] = {};
|
|
912
|
-
|
|
913
|
-
Object.keys(model.fields).forEach(
|
|
914
|
-
|
|
691
|
+
const model = namespace.models[mKey];
|
|
692
|
+
Object.keys(model.fields).forEach((attr) => {
|
|
693
|
+
const fieldAttribute = model.fields[attr];
|
|
915
694
|
if (typeof fieldAttribute.type === 'object' &&
|
|
916
695
|
'model' in fieldAttribute.type) {
|
|
917
|
-
|
|
696
|
+
const connectionType = fieldAttribute.association.connectionType;
|
|
918
697
|
relationship[mKey].relationTypes.push({
|
|
919
698
|
fieldName: fieldAttribute.name,
|
|
920
699
|
modelName: fieldAttribute.type.model,
|
|
@@ -924,15 +703,12 @@ export var establishRelationAndKeys = function (namespace) {
|
|
|
924
703
|
associatedWith: fieldAttribute.association['associatedWith'],
|
|
925
704
|
});
|
|
926
705
|
if (connectionType === 'BELONGS_TO') {
|
|
927
|
-
|
|
706
|
+
const targetNames = extractTargetNamesFromSrc(fieldAttribute.association);
|
|
928
707
|
if (targetNames) {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
var _d = __read(_c, 1), index = _d[0];
|
|
932
|
-
return index === idxName_1;
|
|
933
|
-
});
|
|
708
|
+
const idxName = indexNameFromKeys(targetNames);
|
|
709
|
+
const idxExists = relationship[mKey].indexes.find(([index]) => index === idxName);
|
|
934
710
|
if (!idxExists) {
|
|
935
|
-
relationship[mKey].indexes.push([
|
|
711
|
+
relationship[mKey].indexes.push([idxName, targetNames]);
|
|
936
712
|
}
|
|
937
713
|
}
|
|
938
714
|
}
|
|
@@ -940,37 +716,21 @@ export var establishRelationAndKeys = function (namespace) {
|
|
|
940
716
|
});
|
|
941
717
|
if (model.attributes) {
|
|
942
718
|
keys[mKey].compositeKeys = processCompositeKeys(model.attributes);
|
|
943
|
-
|
|
719
|
+
for (const attribute of model.attributes) {
|
|
944
720
|
if (!isModelAttributeKey(attribute)) {
|
|
945
|
-
|
|
721
|
+
continue;
|
|
946
722
|
}
|
|
947
|
-
|
|
723
|
+
const { fields } = attribute.properties;
|
|
948
724
|
if (isModelAttributePrimaryKey(attribute)) {
|
|
949
725
|
keys[mKey].primaryKey = fields;
|
|
950
|
-
|
|
726
|
+
continue;
|
|
951
727
|
}
|
|
952
728
|
// create indexes for all other keys
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
var _d = __read(_c, 1), index = _d[0];
|
|
956
|
-
return index === idxName;
|
|
957
|
-
});
|
|
729
|
+
const idxName = indexNameFromKeys(fields);
|
|
730
|
+
const idxExists = relationship[mKey].indexes.find(([index]) => index === idxName);
|
|
958
731
|
if (!idxExists) {
|
|
959
732
|
relationship[mKey].indexes.push([idxName, fields]);
|
|
960
733
|
}
|
|
961
|
-
};
|
|
962
|
-
try {
|
|
963
|
-
for (var _d = __values(model.attributes), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
964
|
-
var attribute = _e.value;
|
|
965
|
-
_loop_1(attribute);
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
969
|
-
finally {
|
|
970
|
-
try {
|
|
971
|
-
if (_e && !_e.done && (_c = _d.return)) _c.call(_d);
|
|
972
|
-
}
|
|
973
|
-
finally { if (e_6) throw e_6.error; }
|
|
974
734
|
}
|
|
975
735
|
}
|
|
976
736
|
// set 'id' as the PK for models without a custom PK explicitly defined
|
|
@@ -986,29 +746,26 @@ export var establishRelationAndKeys = function (namespace) {
|
|
|
986
746
|
});
|
|
987
747
|
return [relationship, keys];
|
|
988
748
|
};
|
|
989
|
-
export
|
|
990
|
-
|
|
991
|
-
rel.some(
|
|
749
|
+
export const getIndex = (rel, src) => {
|
|
750
|
+
let indexName;
|
|
751
|
+
rel.some((relItem) => {
|
|
992
752
|
if (relItem.modelName === src) {
|
|
993
|
-
|
|
753
|
+
const targetNames = extractTargetNamesFromSrc(relItem);
|
|
994
754
|
indexName = targetNames && indexNameFromKeys(targetNames);
|
|
995
755
|
return true;
|
|
996
756
|
}
|
|
997
757
|
});
|
|
998
758
|
return indexName;
|
|
999
759
|
};
|
|
1000
|
-
export
|
|
1001
|
-
|
|
760
|
+
export const getIndexFromAssociation = (indexes, src) => {
|
|
761
|
+
let indexName;
|
|
1002
762
|
if (Array.isArray(src)) {
|
|
1003
763
|
indexName = indexNameFromKeys(src);
|
|
1004
764
|
}
|
|
1005
765
|
else {
|
|
1006
766
|
indexName = src;
|
|
1007
767
|
}
|
|
1008
|
-
|
|
1009
|
-
var _d = __read(_c, 1), idxName = _d[0];
|
|
1010
|
-
return idxName === indexName;
|
|
1011
|
-
});
|
|
768
|
+
const associationIndex = indexes.find(([idxName]) => idxName === indexName);
|
|
1012
769
|
return associationIndex && associationIndex[0];
|
|
1013
770
|
};
|
|
1014
771
|
/**
|
|
@@ -1018,9 +775,9 @@ the single field `targetName` has been replaced with an array of `targetNames`.
|
|
|
1018
775
|
* @param src {RelationType | ModelAssociation | undefined}
|
|
1019
776
|
* @returns array of targetNames, or `undefined`
|
|
1020
777
|
*/
|
|
1021
|
-
export
|
|
1022
|
-
|
|
1023
|
-
|
|
778
|
+
export const extractTargetNamesFromSrc = (src) => {
|
|
779
|
+
const targetName = src?.targetName;
|
|
780
|
+
const targetNames = src?.targetNames;
|
|
1024
781
|
if (Array.isArray(targetNames)) {
|
|
1025
782
|
return targetNames;
|
|
1026
783
|
}
|
|
@@ -1033,24 +790,23 @@ export var extractTargetNamesFromSrc = function (src) {
|
|
|
1033
790
|
};
|
|
1034
791
|
// Generates spinal-cased index name from an array of key field names
|
|
1035
792
|
// E.g. for keys `[id, title]` => 'id-title'
|
|
1036
|
-
export
|
|
1037
|
-
return keys.reduce(
|
|
793
|
+
export const indexNameFromKeys = (keys) => {
|
|
794
|
+
return keys.reduce((prev, cur, idx) => {
|
|
1038
795
|
if (idx === 0) {
|
|
1039
796
|
return cur;
|
|
1040
797
|
}
|
|
1041
|
-
return
|
|
798
|
+
return `${prev}${IDENTIFIER_KEY_SEPARATOR}${cur}`;
|
|
1042
799
|
}, '');
|
|
1043
800
|
};
|
|
1044
|
-
export
|
|
801
|
+
export const keysEqual = (keysA, keysB) => {
|
|
1045
802
|
if (keysA.length !== keysB.length) {
|
|
1046
803
|
return false;
|
|
1047
804
|
}
|
|
1048
|
-
return keysA.every(
|
|
805
|
+
return keysA.every((key, idx) => key === keysB[idx]);
|
|
1049
806
|
};
|
|
1050
807
|
// Returns primary keys for a model
|
|
1051
|
-
export
|
|
1052
|
-
|
|
1053
|
-
var keyPath = (_d = (_c = namespace === null || namespace === void 0 ? void 0 : namespace.keys) === null || _c === void 0 ? void 0 : _c[modelName]) === null || _d === void 0 ? void 0 : _d.primaryKey;
|
|
808
|
+
export const getIndexKeys = (namespace, modelName) => {
|
|
809
|
+
const keyPath = namespace?.keys?.[modelName]?.primaryKey;
|
|
1054
810
|
if (keyPath) {
|
|
1055
811
|
return keyPath;
|
|
1056
812
|
}
|
|
@@ -1069,14 +825,16 @@ export var getIndexKeys = function (namespace, modelName) {
|
|
|
1069
825
|
* @param definition modelDefinition to inspect.
|
|
1070
826
|
* @returns An object mapping `createdAt` and `updatedAt` to their field names.
|
|
1071
827
|
*/
|
|
1072
|
-
export
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
var defaultFields = {
|
|
828
|
+
export const getTimestampFields = (definition) => {
|
|
829
|
+
const modelAttributes = definition.attributes?.find(attr => attr.type === 'model');
|
|
830
|
+
const timestampFieldsMap = modelAttributes?.properties?.timestamps;
|
|
831
|
+
const defaultFields = {
|
|
1077
832
|
createdAt: 'createdAt',
|
|
1078
833
|
updatedAt: 'updatedAt',
|
|
1079
834
|
};
|
|
1080
|
-
|
|
1081
|
-
return
|
|
835
|
+
const customFields = timestampFieldsMap || {};
|
|
836
|
+
return {
|
|
837
|
+
...defaultFields,
|
|
838
|
+
...customFields,
|
|
839
|
+
};
|
|
1082
840
|
};
|