@aws-amplify/datastore 3.12.6-next.13 → 3.12.6-next.32
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/CHANGELOG.md +58 -0
- package/lib/authModeStrategies/multiAuthStrategy.js +17 -64
- package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/lib/datastore/datastore.js +682 -469
- package/lib/datastore/datastore.js.map +1 -1
- package/lib/index.js +2 -4
- package/lib/index.js.map +1 -1
- package/lib/predicates/index.js +12 -2
- package/lib/predicates/index.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +393 -298
- package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageDatabase.js +97 -122
- package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib/storage/adapter/InMemoryStore.js +16 -67
- package/lib/storage/adapter/InMemoryStore.js.map +1 -1
- package/lib/storage/adapter/InMemoryStore.native.js +2 -4
- package/lib/storage/adapter/InMemoryStore.native.js.map +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js +497 -404
- package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib/storage/adapter/getDefaultAdapter/index.js +3 -5
- package/lib/storage/adapter/getDefaultAdapter/index.js.map +1 -1
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -4
- package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
- package/lib/storage/storage.js +129 -151
- package/lib/storage/storage.js.map +1 -1
- package/lib/sync/datastoreConnectivity.js +13 -17
- package/lib/sync/datastoreConnectivity.js.map +1 -1
- package/lib/sync/datastoreReachability/index.native.js +2 -4
- package/lib/sync/datastoreReachability/index.native.js.map +1 -1
- package/lib/sync/index.js +544 -488
- package/lib/sync/index.js.map +1 -1
- package/lib/sync/merger.js +21 -80
- package/lib/sync/merger.js.map +1 -1
- package/lib/sync/outbox.js +95 -162
- package/lib/sync/outbox.js.map +1 -1
- package/lib/sync/processors/errorMaps.js +4 -34
- package/lib/sync/processors/errorMaps.js.map +1 -1
- package/lib/sync/processors/mutation.js +285 -312
- package/lib/sync/processors/mutation.js.map +1 -1
- package/lib/sync/processors/subscription.js +218 -259
- package/lib/sync/processors/subscription.js.map +1 -1
- package/lib/sync/processors/sync.js +141 -212
- package/lib/sync/processors/sync.js.map +1 -1
- package/lib/sync/utils.js +50 -61
- package/lib/sync/utils.js.map +1 -1
- package/lib/types.js +13 -39
- package/lib/types.js.map +1 -1
- package/lib/util.js +429 -242
- package/lib/util.js.map +1 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.d.ts +11 -0
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +13 -57
- package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/lib-esm/datastore/datastore.d.ts +107 -17
- package/lib-esm/datastore/datastore.js +649 -433
- package/lib-esm/datastore/datastore.js.map +1 -1
- package/lib-esm/index.d.ts +3 -19
- package/lib-esm/predicates/index.d.ts +3 -2
- package/lib-esm/predicates/index.js +13 -3
- package/lib-esm/predicates/index.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +4 -3
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +356 -258
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageDatabase.d.ts +14 -4
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +67 -92
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib-esm/storage/adapter/InMemoryStore.js +1 -52
- package/lib-esm/storage/adapter/InMemoryStore.js.map +1 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +26 -4
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +446 -346
- package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib-esm/storage/adapter/index.d.ts +1 -1
- package/lib-esm/storage/storage.d.ts +1 -1
- package/lib-esm/storage/storage.js +94 -113
- package/lib-esm/storage/storage.js.map +1 -1
- package/lib-esm/sync/datastoreConnectivity.d.ts +1 -0
- package/lib-esm/sync/datastoreConnectivity.js +10 -11
- package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
- package/lib-esm/sync/index.d.ts +31 -5
- package/lib-esm/sync/index.js +525 -466
- package/lib-esm/sync/index.js.map +1 -1
- package/lib-esm/sync/merger.d.ts +9 -3
- package/lib-esm/sync/merger.js +14 -73
- package/lib-esm/sync/merger.js.map +1 -1
- package/lib-esm/sync/outbox.d.ts +2 -2
- package/lib-esm/sync/outbox.js +79 -146
- package/lib-esm/sync/outbox.js.map +1 -1
- package/lib-esm/sync/processors/errorMaps.js +1 -31
- package/lib-esm/sync/processors/errorMaps.js.map +1 -1
- package/lib-esm/sync/processors/mutation.d.ts +2 -0
- package/lib-esm/sync/processors/mutation.js +271 -295
- package/lib-esm/sync/processors/mutation.js.map +1 -1
- package/lib-esm/sync/processors/subscription.d.ts +2 -0
- package/lib-esm/sync/processors/subscription.js +214 -245
- package/lib-esm/sync/processors/subscription.js.map +1 -1
- package/lib-esm/sync/processors/sync.d.ts +2 -1
- package/lib-esm/sync/processors/sync.js +127 -195
- package/lib-esm/sync/processors/sync.js.map +1 -1
- package/lib-esm/sync/utils.d.ts +3 -2
- package/lib-esm/sync/utils.js +45 -57
- package/lib-esm/sync/utils.js.map +1 -1
- package/lib-esm/types.d.ts +65 -26
- package/lib-esm/types.js +10 -38
- package/lib-esm/types.js.map +1 -1
- package/lib-esm/util.d.ts +67 -24
- package/lib-esm/util.js +420 -233
- package/lib-esm/util.js.map +1 -1
- package/package.json +14 -7
- package/src/authModeStrategies/multiAuthStrategy.ts +12 -1
- package/src/datastore/datastore.ts +798 -397
- package/src/predicates/index.ts +32 -10
- package/src/storage/adapter/AsyncStorageAdapter.ts +309 -93
- package/src/storage/adapter/AsyncStorageDatabase.ts +74 -26
- package/src/storage/adapter/IndexedDBAdapter.ts +358 -134
- package/src/storage/adapter/index.ts +1 -1
- package/src/storage/storage.ts +69 -22
- package/src/sync/datastoreConnectivity.ts +6 -0
- package/src/sync/index.ts +521 -412
- package/src/sync/merger.ts +20 -4
- package/src/sync/outbox.ts +22 -9
- package/src/sync/processors/mutation.ts +188 -150
- package/src/sync/processors/subscription.ts +289 -253
- package/src/sync/processors/sync.ts +151 -138
- package/src/sync/utils.ts +67 -12
- package/src/types.ts +182 -30
- package/src/util.ts +505 -176
- package/build.js +0 -5
- package/dist/aws-amplify-datastore.js +0 -98255
- package/dist/aws-amplify-datastore.js.map +0 -1
- package/dist/aws-amplify-datastore.min.js +0 -66
- package/dist/aws-amplify-datastore.min.js.map +0 -1
- package/index.js +0 -7
- package/lib/authModeStrategies/defaultAuthStrategy.d.ts +0 -2
- package/lib/authModeStrategies/index.d.ts +0 -2
- package/lib/authModeStrategies/multiAuthStrategy.d.ts +0 -2
- package/lib/datastore/datastore.d.ts +0 -66
- package/lib/index.d.ts +0 -31
- package/lib/predicates/index.d.ts +0 -15
- package/lib/predicates/sort.d.ts +0 -8
- package/lib/ssr/index.d.ts +0 -3
- package/lib/storage/adapter/AsyncStorageAdapter.d.ts +0 -40
- package/lib/storage/adapter/AsyncStorageDatabase.d.ts +0 -29
- package/lib/storage/adapter/InMemoryStore.d.ts +0 -11
- package/lib/storage/adapter/InMemoryStore.native.d.ts +0 -1
- package/lib/storage/adapter/IndexedDBAdapter.d.ts +0 -37
- package/lib/storage/adapter/getDefaultAdapter/index.d.ts +0 -3
- package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +0 -3
- package/lib/storage/adapter/index.d.ts +0 -9
- package/lib/storage/storage.d.ts +0 -49
- package/lib/sync/datastoreConnectivity.d.ts +0 -15
- package/lib/sync/datastoreReachability/index.d.ts +0 -3
- package/lib/sync/datastoreReachability/index.native.d.ts +0 -3
- package/lib/sync/index.d.ts +0 -63
- package/lib/sync/merger.d.ts +0 -11
- package/lib/sync/outbox.d.ts +0 -27
- package/lib/sync/processors/errorMaps.d.ts +0 -17
- package/lib/sync/processors/mutation.d.ts +0 -56
- package/lib/sync/processors/subscription.d.ts +0 -31
- package/lib/sync/processors/sync.d.ts +0 -27
- package/lib/sync/utils.d.ts +0 -41
- package/lib/types.d.ts +0 -462
- package/lib/util.d.ts +0 -113
- package/webpack.config.dev.js +0 -6
package/lib-esm/util.js
CHANGED
|
@@ -1,76 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
38
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
39
|
-
if (!m) return o;
|
|
40
|
-
var i = m.call(o), r, ar = [], e;
|
|
41
|
-
try {
|
|
42
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
43
|
-
}
|
|
44
|
-
catch (error) { e = { error: error }; }
|
|
45
|
-
finally {
|
|
46
|
-
try {
|
|
47
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
48
|
-
}
|
|
49
|
-
finally { if (e) throw e.error; }
|
|
50
|
-
}
|
|
51
|
-
return ar;
|
|
52
|
-
};
|
|
53
|
-
var __spread = (this && this.__spread) || function () {
|
|
54
|
-
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
55
|
-
return ar;
|
|
56
|
-
};
|
|
57
|
-
var __values = (this && this.__values) || function(o) {
|
|
58
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
59
|
-
if (m) return m.call(o);
|
|
60
|
-
if (o && typeof o.length === "number") return {
|
|
61
|
-
next: function () {
|
|
62
|
-
if (o && i >= o.length) o = void 0;
|
|
63
|
-
return { value: o && o[i++], done: !o };
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
67
|
-
};
|
|
1
|
+
import { __awaiter, __generator, __read, __spread, __values } from "tslib";
|
|
68
2
|
import { Buffer } from 'buffer';
|
|
69
3
|
import { monotonicFactory } from 'ulid';
|
|
70
4
|
import { v4 as uuid } from 'uuid';
|
|
71
5
|
import { produce, applyPatches } from 'immer';
|
|
72
6
|
import { isPredicateGroup, isPredicateObj, SortDirection, isModelAttributeKey, isModelAttributePrimaryKey, isModelAttributeCompositeKey, LimitTimerRaceResolvedValues, } from './types';
|
|
73
7
|
import { WordArray } from 'amazon-cognito-identity-js';
|
|
8
|
+
export var ID = 'id';
|
|
9
|
+
/**
|
|
10
|
+
* Used by the Async Storage Adapter to concatenate key values
|
|
11
|
+
* for a record. For instance, if a model has the following keys:
|
|
12
|
+
* `customId: ID! @primaryKey(sortKeyFields: ["createdAt"])`,
|
|
13
|
+
* we concatenate the `customId` and `createdAt` as:
|
|
14
|
+
* `12-234-5#2022-09-28T00:00:00.000Z`
|
|
15
|
+
*/
|
|
16
|
+
export var DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR = '#';
|
|
17
|
+
/**
|
|
18
|
+
* Used for generating spinal-cased index name from an array of
|
|
19
|
+
* key field names.
|
|
20
|
+
* E.g. for keys `[id, title]` => 'id-title'
|
|
21
|
+
*/
|
|
22
|
+
export var IDENTIFIER_KEY_SEPARATOR = '-';
|
|
23
|
+
export var errorMessages = {
|
|
24
|
+
idEmptyString: 'An index field cannot contain an empty string value',
|
|
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',
|
|
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',
|
|
27
|
+
observeWithObjectLiteral: 'Object literal syntax cannot be used with observe. Use a predicate instead: https://docs.amplify.aws/lib/datastore/data-access/q/platform/js/#predicates',
|
|
28
|
+
};
|
|
74
29
|
export var NAMESPACES;
|
|
75
30
|
(function (NAMESPACES) {
|
|
76
31
|
NAMESPACES["DATASTORE"] = "datastore";
|
|
@@ -168,148 +123,6 @@ export function registerNonModelClass(clazz) {
|
|
|
168
123
|
export var isNonModelConstructor = function (obj) {
|
|
169
124
|
return nonModelClasses.has(obj);
|
|
170
125
|
};
|
|
171
|
-
/*
|
|
172
|
-
When we have GSI(s) with composite sort keys defined on a model
|
|
173
|
-
There are some very particular rules regarding which fields must be included in the update mutation input
|
|
174
|
-
The field selection becomes more complex as the number of GSIs with composite sort keys grows
|
|
175
|
-
|
|
176
|
-
To summarize: any time we update a field that is part of the composite sort key of a GSI, we must include:
|
|
177
|
-
1. all of the other fields in that composite sort key
|
|
178
|
-
2. all of the fields from any other composite sort key that intersect with the fields from 1.
|
|
179
|
-
|
|
180
|
-
E.g.,
|
|
181
|
-
Model @model
|
|
182
|
-
@key(name: 'key1' fields: ['hk', 'a', 'b', 'c'])
|
|
183
|
-
@key(name: 'key2' fields: ['hk', 'a', 'b', 'd'])
|
|
184
|
-
@key(name: 'key3' fields: ['hk', 'x', 'y', 'z'])
|
|
185
|
-
|
|
186
|
-
Model.a is updated => include ['a', 'b', 'c', 'd']
|
|
187
|
-
Model.c is updated => include ['a', 'b', 'c', 'd']
|
|
188
|
-
Model.d is updated => include ['a', 'b', 'c', 'd']
|
|
189
|
-
Model.x is updated => include ['x', 'y', 'z']
|
|
190
|
-
|
|
191
|
-
This function accepts a model's attributes and returns grouped sets of composite key fields
|
|
192
|
-
Using our example Model above, the function will return:
|
|
193
|
-
[
|
|
194
|
-
Set('a', 'b', 'c', 'd'),
|
|
195
|
-
Set('x', 'y', 'z'),
|
|
196
|
-
]
|
|
197
|
-
|
|
198
|
-
This gives us the opportunity to correctly include the required fields for composite keys
|
|
199
|
-
When crafting the mutation input in Storage.getUpdateMutationInput
|
|
200
|
-
|
|
201
|
-
See 'processCompositeKeys' test in util.test.ts for more examples
|
|
202
|
-
*/
|
|
203
|
-
export var processCompositeKeys = function (attributes) {
|
|
204
|
-
var extractCompositeSortKey = function (_c) {
|
|
205
|
-
var
|
|
206
|
-
// ignore the HK (fields[0]) we only need to include the composite sort key fields[1...n]
|
|
207
|
-
_d = __read(_c.properties.fields), sortKeyFields = _d.slice(1);
|
|
208
|
-
return sortKeyFields;
|
|
209
|
-
};
|
|
210
|
-
var compositeKeyFields = attributes
|
|
211
|
-
.filter(isModelAttributeCompositeKey)
|
|
212
|
-
.map(extractCompositeSortKey);
|
|
213
|
-
/*
|
|
214
|
-
if 2 sets of fields have any intersecting fields => combine them into 1 union set
|
|
215
|
-
e.g., ['a', 'b', 'c'] and ['a', 'b', 'd'] => ['a', 'b', 'c', 'd']
|
|
216
|
-
*/
|
|
217
|
-
var combineIntersecting = function (fields) {
|
|
218
|
-
return fields.reduce(function (combined, sortKeyFields) {
|
|
219
|
-
var sortKeyFieldsSet = new Set(sortKeyFields);
|
|
220
|
-
if (combined.length === 0) {
|
|
221
|
-
combined.push(sortKeyFieldsSet);
|
|
222
|
-
return combined;
|
|
223
|
-
}
|
|
224
|
-
// does the current set share values with another set we've already added to `combined`?
|
|
225
|
-
var intersectingSetIdx = combined.findIndex(function (existingSet) {
|
|
226
|
-
return __spread(existingSet).some(function (f) { return sortKeyFieldsSet.has(f); });
|
|
227
|
-
});
|
|
228
|
-
if (intersectingSetIdx > -1) {
|
|
229
|
-
var union = new Set(__spread(combined[intersectingSetIdx], sortKeyFieldsSet));
|
|
230
|
-
// combine the current set with the intersecting set we found above
|
|
231
|
-
combined[intersectingSetIdx] = union;
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
// none of the sets in `combined` have intersecting values with the current set
|
|
235
|
-
combined.push(sortKeyFieldsSet);
|
|
236
|
-
}
|
|
237
|
-
return combined;
|
|
238
|
-
}, []);
|
|
239
|
-
};
|
|
240
|
-
var initial = combineIntersecting(compositeKeyFields);
|
|
241
|
-
// a single pass pay not be enough to correctly combine all the fields
|
|
242
|
-
// call the function once more to get a final merged list of sets
|
|
243
|
-
var combined = combineIntersecting(initial);
|
|
244
|
-
return combined;
|
|
245
|
-
};
|
|
246
|
-
export var establishRelationAndKeys = function (namespace) {
|
|
247
|
-
var relationship = {};
|
|
248
|
-
var keys = {};
|
|
249
|
-
Object.keys(namespace.models).forEach(function (mKey) {
|
|
250
|
-
var e_1, _c, e_2, _d;
|
|
251
|
-
relationship[mKey] = { indexes: [], relationTypes: [] };
|
|
252
|
-
keys[mKey] = {};
|
|
253
|
-
var model = namespace.models[mKey];
|
|
254
|
-
Object.keys(model.fields).forEach(function (attr) {
|
|
255
|
-
var fieldAttribute = model.fields[attr];
|
|
256
|
-
if (typeof fieldAttribute.type === 'object' &&
|
|
257
|
-
'model' in fieldAttribute.type) {
|
|
258
|
-
var connectionType = fieldAttribute.association.connectionType;
|
|
259
|
-
relationship[mKey].relationTypes.push({
|
|
260
|
-
fieldName: fieldAttribute.name,
|
|
261
|
-
modelName: fieldAttribute.type.model,
|
|
262
|
-
relationType: connectionType,
|
|
263
|
-
targetName: fieldAttribute.association['targetName'],
|
|
264
|
-
associatedWith: fieldAttribute.association['associatedWith'],
|
|
265
|
-
});
|
|
266
|
-
if (connectionType === 'BELONGS_TO') {
|
|
267
|
-
relationship[mKey].indexes.push(fieldAttribute.association['targetName']);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
if (model.attributes) {
|
|
272
|
-
keys[mKey].compositeKeys = processCompositeKeys(model.attributes);
|
|
273
|
-
try {
|
|
274
|
-
for (var _e = __values(model.attributes), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
275
|
-
var attribute = _f.value;
|
|
276
|
-
if (!isModelAttributeKey(attribute)) {
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
if (isModelAttributePrimaryKey(attribute)) {
|
|
280
|
-
keys[mKey].primaryKey = attribute.properties.fields;
|
|
281
|
-
}
|
|
282
|
-
var fields = attribute.properties.fields;
|
|
283
|
-
try {
|
|
284
|
-
for (var fields_1 = (e_2 = void 0, __values(fields)), fields_1_1 = fields_1.next(); !fields_1_1.done; fields_1_1 = fields_1.next()) {
|
|
285
|
-
var field = fields_1_1.value;
|
|
286
|
-
// only add index if it hasn't already been added
|
|
287
|
-
var exists = relationship[mKey].indexes.includes(field);
|
|
288
|
-
if (!exists) {
|
|
289
|
-
relationship[mKey].indexes.push(field);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
294
|
-
finally {
|
|
295
|
-
try {
|
|
296
|
-
if (fields_1_1 && !fields_1_1.done && (_d = fields_1.return)) _d.call(fields_1);
|
|
297
|
-
}
|
|
298
|
-
finally { if (e_2) throw e_2.error; }
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
303
|
-
finally {
|
|
304
|
-
try {
|
|
305
|
-
if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
|
|
306
|
-
}
|
|
307
|
-
finally { if (e_1) throw e_1.error; }
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
return [relationship, keys];
|
|
312
|
-
};
|
|
313
126
|
var topologicallySortedModels = new WeakMap();
|
|
314
127
|
export var traverseModel = function (srcModelName, instance, namespace, modelInstanceCreator, getModelConstructorByModelName) {
|
|
315
128
|
var relationships = namespace.relationships;
|
|
@@ -335,12 +148,26 @@ export var traverseModel = function (srcModelName, instance, namespace, modelIns
|
|
|
335
148
|
item: instance[rItem.fieldName],
|
|
336
149
|
instance: modelInstance,
|
|
337
150
|
});
|
|
338
|
-
|
|
151
|
+
var targetNames = extractTargetNamesFromSrc(rItem);
|
|
152
|
+
// `targetName` will be defined for Has One if feature flag
|
|
339
153
|
// https://docs.amplify.aws/cli/reference/feature-flags/#useAppsyncModelgenPlugin
|
|
340
154
|
// is true (default as of 5/7/21)
|
|
341
155
|
// Making this conditional for backward-compatibility
|
|
342
|
-
if (
|
|
343
|
-
|
|
156
|
+
if (targetNames) {
|
|
157
|
+
targetNames.forEach(function (targetName, idx) {
|
|
158
|
+
// Get the connected record
|
|
159
|
+
var relatedRecordInProxy = (draftInstance[rItem.fieldName]);
|
|
160
|
+
// Previously, we used the hardcoded 'id' as they key,
|
|
161
|
+
// now we need the value of the key to get the PK (and SK)
|
|
162
|
+
// values from the related record
|
|
163
|
+
var primaryKey = namespace.keys[modelConstructor.name].primaryKey;
|
|
164
|
+
var keyField = primaryKey && primaryKey[idx];
|
|
165
|
+
// Get the value
|
|
166
|
+
var relatedRecordInProxyPkValue = relatedRecordInProxy[keyField];
|
|
167
|
+
// Set the targetName value
|
|
168
|
+
draftInstance[targetName] = relatedRecordInProxyPkValue;
|
|
169
|
+
});
|
|
170
|
+
// Delete the instance from the proxy
|
|
344
171
|
delete draftInstance[rItem.fieldName];
|
|
345
172
|
}
|
|
346
173
|
else {
|
|
@@ -367,8 +194,25 @@ export var traverseModel = function (srcModelName, instance, namespace, modelIns
|
|
|
367
194
|
}
|
|
368
195
|
}
|
|
369
196
|
if (draftInstance[rItem.fieldName]) {
|
|
370
|
-
|
|
371
|
-
|
|
197
|
+
var targetNames = extractTargetNamesFromSrc(rItem);
|
|
198
|
+
if (targetNames) {
|
|
199
|
+
targetNames.forEach(function (targetName, idx) {
|
|
200
|
+
// Get the connected record
|
|
201
|
+
var relatedRecordInProxy = (draftInstance[rItem.fieldName]);
|
|
202
|
+
// Previously, we used the hardcoded `id` for the key.
|
|
203
|
+
// Now, we need the value of the key to get the PK (and SK)
|
|
204
|
+
// values from the related record
|
|
205
|
+
var primaryKey = namespace.keys[modelConstructor.name].primaryKey;
|
|
206
|
+
// fall back to ID if
|
|
207
|
+
var keyField = primaryKey && primaryKey[idx];
|
|
208
|
+
// Get the value
|
|
209
|
+
var relatedRecordInProxyPkValue = relatedRecordInProxy[keyField];
|
|
210
|
+
// Set the targetName value
|
|
211
|
+
draftInstance[targetName] = relatedRecordInProxyPkValue;
|
|
212
|
+
});
|
|
213
|
+
// Delete the instance from the proxy
|
|
214
|
+
delete draftInstance[rItem.fieldName];
|
|
215
|
+
}
|
|
372
216
|
}
|
|
373
217
|
break;
|
|
374
218
|
case 'HAS_MANY':
|
|
@@ -394,19 +238,6 @@ export var traverseModel = function (srcModelName, instance, namespace, modelIns
|
|
|
394
238
|
});
|
|
395
239
|
return result;
|
|
396
240
|
};
|
|
397
|
-
export var getIndex = function (rel, src) {
|
|
398
|
-
var index = '';
|
|
399
|
-
rel.some(function (relItem) {
|
|
400
|
-
if (relItem.modelName === src) {
|
|
401
|
-
index = relItem.targetName;
|
|
402
|
-
}
|
|
403
|
-
});
|
|
404
|
-
return index;
|
|
405
|
-
};
|
|
406
|
-
export var getIndexFromAssociation = function (indexes, src) {
|
|
407
|
-
var index = indexes.find(function (idx) { return idx === src; });
|
|
408
|
-
return index;
|
|
409
|
-
};
|
|
410
241
|
var privateModeCheckResult;
|
|
411
242
|
export var isPrivateMode = function () {
|
|
412
243
|
return new Promise(function (resolve) {
|
|
@@ -446,6 +277,95 @@ export var isPrivateMode = function () {
|
|
|
446
277
|
db.onsuccess = isNotPrivate;
|
|
447
278
|
});
|
|
448
279
|
};
|
|
280
|
+
var safariCompatabilityModeResult;
|
|
281
|
+
/**
|
|
282
|
+
* Whether the browser's implementation of IndexedDB breaks on array lookups
|
|
283
|
+
* against composite indexes whose keypath contains a single column.
|
|
284
|
+
*
|
|
285
|
+
* E.g., Whether `store.createIndex(indexName, ['id'])` followed by
|
|
286
|
+
* `store.index(indexName).get([1])` will *ever* return records.
|
|
287
|
+
*
|
|
288
|
+
* In all known, modern Safari browsers as of Q4 2022, the query against an index like
|
|
289
|
+
* this will *always* return `undefined`. So, the index needs to be created as a scalar.
|
|
290
|
+
*/
|
|
291
|
+
export var isSafariCompatabilityMode = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
292
|
+
var dbName_1, storeName_1, indexName_1, db_1, rwTx, rwStore, result, error_1;
|
|
293
|
+
return __generator(this, function (_c) {
|
|
294
|
+
switch (_c.label) {
|
|
295
|
+
case 0:
|
|
296
|
+
_c.trys.push([0, 6, , 7]);
|
|
297
|
+
dbName_1 = uuid();
|
|
298
|
+
storeName_1 = 'indexedDBFeatureProbeStore';
|
|
299
|
+
indexName_1 = 'idx';
|
|
300
|
+
if (indexedDB === null)
|
|
301
|
+
return [2 /*return*/, false];
|
|
302
|
+
if (safariCompatabilityModeResult !== undefined) {
|
|
303
|
+
return [2 /*return*/, safariCompatabilityModeResult];
|
|
304
|
+
}
|
|
305
|
+
return [4 /*yield*/, new Promise(function (resolve) {
|
|
306
|
+
var dbOpenRequest = indexedDB.open(dbName_1);
|
|
307
|
+
dbOpenRequest.onerror = function () { return resolve(false); };
|
|
308
|
+
dbOpenRequest.onsuccess = function () {
|
|
309
|
+
var db = dbOpenRequest.result;
|
|
310
|
+
resolve(db);
|
|
311
|
+
};
|
|
312
|
+
dbOpenRequest.onupgradeneeded = function (event) {
|
|
313
|
+
var _c;
|
|
314
|
+
var db = (_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.result;
|
|
315
|
+
db.onerror = function () { return resolve(false); };
|
|
316
|
+
var store = db.createObjectStore(storeName_1, {
|
|
317
|
+
autoIncrement: true,
|
|
318
|
+
});
|
|
319
|
+
store.createIndex(indexName_1, ['id']);
|
|
320
|
+
};
|
|
321
|
+
})];
|
|
322
|
+
case 1:
|
|
323
|
+
db_1 = _c.sent();
|
|
324
|
+
if (!db_1) {
|
|
325
|
+
throw new Error('Could not open probe DB');
|
|
326
|
+
}
|
|
327
|
+
rwTx = db_1.transaction(storeName_1, 'readwrite');
|
|
328
|
+
rwStore = rwTx.objectStore(storeName_1);
|
|
329
|
+
rwStore.add({
|
|
330
|
+
id: 1,
|
|
331
|
+
});
|
|
332
|
+
rwTx.commit();
|
|
333
|
+
return [4 /*yield*/, new Promise(function (resolve) {
|
|
334
|
+
var tx = db_1.transaction(storeName_1, 'readonly');
|
|
335
|
+
var store = tx.objectStore(storeName_1);
|
|
336
|
+
var index = store.index(indexName_1);
|
|
337
|
+
var getRequest = index.get([1]);
|
|
338
|
+
getRequest.onerror = function () { return resolve(false); };
|
|
339
|
+
getRequest.onsuccess = function (event) {
|
|
340
|
+
var _c;
|
|
341
|
+
resolve((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.result);
|
|
342
|
+
};
|
|
343
|
+
})];
|
|
344
|
+
case 2:
|
|
345
|
+
result = _c.sent();
|
|
346
|
+
if (!(db_1 && typeof db_1.close === 'function')) return [3 /*break*/, 4];
|
|
347
|
+
return [4 /*yield*/, db_1.close()];
|
|
348
|
+
case 3:
|
|
349
|
+
_c.sent();
|
|
350
|
+
_c.label = 4;
|
|
351
|
+
case 4: return [4 /*yield*/, indexedDB.deleteDatabase(dbName_1)];
|
|
352
|
+
case 5:
|
|
353
|
+
_c.sent();
|
|
354
|
+
if (result === undefined) {
|
|
355
|
+
safariCompatabilityModeResult = true;
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
safariCompatabilityModeResult = false;
|
|
359
|
+
}
|
|
360
|
+
return [3 /*break*/, 7];
|
|
361
|
+
case 6:
|
|
362
|
+
error_1 = _c.sent();
|
|
363
|
+
safariCompatabilityModeResult = false;
|
|
364
|
+
return [3 /*break*/, 7];
|
|
365
|
+
case 7: return [2 /*return*/, safariCompatabilityModeResult];
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}); };
|
|
449
369
|
var randomBytes = function (nBytes) {
|
|
450
370
|
return Buffer.from(new WordArray().random(nBytes).toString(), 'hex');
|
|
451
371
|
};
|
|
@@ -480,7 +400,7 @@ export function getNow() {
|
|
|
480
400
|
}
|
|
481
401
|
export function sortCompareFunction(sortPredicates) {
|
|
482
402
|
return function compareFunction(a, b) {
|
|
483
|
-
var
|
|
403
|
+
var e_1, _c;
|
|
484
404
|
try {
|
|
485
405
|
// enable multi-field sort by iterating over predicates until
|
|
486
406
|
// a comparison returns -1 or 1
|
|
@@ -497,12 +417,12 @@ export function sortCompareFunction(sortPredicates) {
|
|
|
497
417
|
}
|
|
498
418
|
}
|
|
499
419
|
}
|
|
500
|
-
catch (
|
|
420
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
501
421
|
finally {
|
|
502
422
|
try {
|
|
503
423
|
if (sortPredicates_1_1 && !sortPredicates_1_1.done && (_c = sortPredicates_1.return)) _c.call(sortPredicates_1);
|
|
504
424
|
}
|
|
505
|
-
finally { if (
|
|
425
|
+
finally { if (e_1) throw e_1.error; }
|
|
506
426
|
}
|
|
507
427
|
return 0;
|
|
508
428
|
};
|
|
@@ -513,7 +433,7 @@ export function sortCompareFunction(sortPredicates) {
|
|
|
513
433
|
// if nullish is true, treat undefined and null values as equal
|
|
514
434
|
// to normalize for GQL response values for undefined fields
|
|
515
435
|
export function valuesEqual(valA, valB, nullish) {
|
|
516
|
-
var
|
|
436
|
+
var e_2, _c;
|
|
517
437
|
if (nullish === void 0) { nullish = false; }
|
|
518
438
|
var a = valA;
|
|
519
439
|
var b = valB;
|
|
@@ -565,12 +485,12 @@ export function valuesEqual(valA, valB, nullish) {
|
|
|
565
485
|
}
|
|
566
486
|
}
|
|
567
487
|
}
|
|
568
|
-
catch (
|
|
488
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
569
489
|
finally {
|
|
570
490
|
try {
|
|
571
491
|
if (keys_1_1 && !keys_1_1.done && (_c = keys_1.return)) _c.call(keys_1);
|
|
572
492
|
}
|
|
573
|
-
finally { if (
|
|
493
|
+
finally { if (e_2) throw e_2.error; }
|
|
574
494
|
}
|
|
575
495
|
return true;
|
|
576
496
|
}
|
|
@@ -716,4 +636,271 @@ export function mergePatches(originalSource, oldPatches, newPatches) {
|
|
|
716
636
|
});
|
|
717
637
|
return patches;
|
|
718
638
|
}
|
|
639
|
+
export var getStorename = function (namespace, modelName) {
|
|
640
|
+
var storeName = namespace + "_" + modelName;
|
|
641
|
+
return storeName;
|
|
642
|
+
};
|
|
643
|
+
//#region Key Utils
|
|
644
|
+
/*
|
|
645
|
+
When we have GSI(s) with composite sort keys defined on a model
|
|
646
|
+
There are some very particular rules regarding which fields must be included in the update mutation input
|
|
647
|
+
The field selection becomes more complex as the number of GSIs with composite sort keys grows
|
|
648
|
+
|
|
649
|
+
To summarize: any time we update a field that is part of the composite sort key of a GSI, we must include:
|
|
650
|
+
1. all of the other fields in that composite sort key
|
|
651
|
+
2. all of the fields from any other composite sort key that intersect with the fields from 1.
|
|
652
|
+
|
|
653
|
+
E.g.,
|
|
654
|
+
Model @model
|
|
655
|
+
@key(name: 'key1' fields: ['hk', 'a', 'b', 'c'])
|
|
656
|
+
@key(name: 'key2' fields: ['hk', 'a', 'b', 'd'])
|
|
657
|
+
@key(name: 'key3' fields: ['hk', 'x', 'y', 'z'])
|
|
658
|
+
|
|
659
|
+
Model.a is updated => include ['a', 'b', 'c', 'd']
|
|
660
|
+
Model.c is updated => include ['a', 'b', 'c', 'd']
|
|
661
|
+
Model.d is updated => include ['a', 'b', 'c', 'd']
|
|
662
|
+
Model.x is updated => include ['x', 'y', 'z']
|
|
663
|
+
|
|
664
|
+
This function accepts a model's attributes and returns grouped sets of composite key fields
|
|
665
|
+
Using our example Model above, the function will return:
|
|
666
|
+
[
|
|
667
|
+
Set('a', 'b', 'c', 'd'),
|
|
668
|
+
Set('x', 'y', 'z'),
|
|
669
|
+
]
|
|
670
|
+
|
|
671
|
+
This gives us the opportunity to correctly include the required fields for composite keys
|
|
672
|
+
When crafting the mutation input in Storage.getUpdateMutationInput
|
|
673
|
+
|
|
674
|
+
See 'processCompositeKeys' test in util.test.ts for more examples
|
|
675
|
+
*/
|
|
676
|
+
export var processCompositeKeys = function (attributes) {
|
|
677
|
+
var extractCompositeSortKey = function (_c) {
|
|
678
|
+
var
|
|
679
|
+
// ignore the HK (fields[0]) we only need to include the composite sort key fields[1...n]
|
|
680
|
+
_d = __read(_c.properties.fields), sortKeyFields = _d.slice(1);
|
|
681
|
+
return sortKeyFields;
|
|
682
|
+
};
|
|
683
|
+
var compositeKeyFields = attributes
|
|
684
|
+
.filter(isModelAttributeCompositeKey)
|
|
685
|
+
.map(extractCompositeSortKey);
|
|
686
|
+
/*
|
|
687
|
+
if 2 sets of fields have any intersecting fields => combine them into 1 union set
|
|
688
|
+
e.g., ['a', 'b', 'c'] and ['a', 'b', 'd'] => ['a', 'b', 'c', 'd']
|
|
689
|
+
*/
|
|
690
|
+
var combineIntersecting = function (fields) {
|
|
691
|
+
return fields.reduce(function (combined, sortKeyFields) {
|
|
692
|
+
var sortKeyFieldsSet = new Set(sortKeyFields);
|
|
693
|
+
if (combined.length === 0) {
|
|
694
|
+
combined.push(sortKeyFieldsSet);
|
|
695
|
+
return combined;
|
|
696
|
+
}
|
|
697
|
+
// does the current set share values with another set we've already added to `combined`?
|
|
698
|
+
var intersectingSetIdx = combined.findIndex(function (existingSet) {
|
|
699
|
+
return __spread(existingSet).some(function (f) { return sortKeyFieldsSet.has(f); });
|
|
700
|
+
});
|
|
701
|
+
if (intersectingSetIdx > -1) {
|
|
702
|
+
var union = new Set(__spread(combined[intersectingSetIdx], sortKeyFieldsSet));
|
|
703
|
+
// combine the current set with the intersecting set we found above
|
|
704
|
+
combined[intersectingSetIdx] = union;
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
// none of the sets in `combined` have intersecting values with the current set
|
|
708
|
+
combined.push(sortKeyFieldsSet);
|
|
709
|
+
}
|
|
710
|
+
return combined;
|
|
711
|
+
}, []);
|
|
712
|
+
};
|
|
713
|
+
var initial = combineIntersecting(compositeKeyFields);
|
|
714
|
+
// a single pass pay not be enough to correctly combine all the fields
|
|
715
|
+
// call the function once more to get a final merged list of sets
|
|
716
|
+
var combined = combineIntersecting(initial);
|
|
717
|
+
return combined;
|
|
718
|
+
};
|
|
719
|
+
export var extractKeyIfExists = function (modelDefinition) {
|
|
720
|
+
var _c;
|
|
721
|
+
var keyAttribute = (_c = modelDefinition === null || modelDefinition === void 0 ? void 0 : modelDefinition.attributes) === null || _c === void 0 ? void 0 : _c.find(isModelAttributeKey);
|
|
722
|
+
return keyAttribute;
|
|
723
|
+
};
|
|
724
|
+
export var extractPrimaryKeyFieldNames = function (modelDefinition) {
|
|
725
|
+
var keyAttribute = extractKeyIfExists(modelDefinition);
|
|
726
|
+
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
727
|
+
return keyAttribute.properties.fields;
|
|
728
|
+
}
|
|
729
|
+
return [ID];
|
|
730
|
+
};
|
|
731
|
+
export var extractPrimaryKeyValues = function (model, keyFields) {
|
|
732
|
+
return keyFields.map(function (key) { return model[key]; });
|
|
733
|
+
};
|
|
734
|
+
export var extractPrimaryKeysAndValues = function (model, keyFields) {
|
|
735
|
+
var primaryKeysAndValues = {};
|
|
736
|
+
keyFields.forEach(function (key) { return (primaryKeysAndValues[key] = model[key]); });
|
|
737
|
+
return primaryKeysAndValues;
|
|
738
|
+
};
|
|
739
|
+
// IdentifierFields<ManagedIdentifier>
|
|
740
|
+
// Default behavior without explicit @primaryKey defined
|
|
741
|
+
export var isIdManaged = function (modelDefinition) {
|
|
742
|
+
var keyAttribute = extractKeyIfExists(modelDefinition);
|
|
743
|
+
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
return true;
|
|
747
|
+
};
|
|
748
|
+
// IdentifierFields<OptionallyManagedIdentifier>
|
|
749
|
+
// @primaryKey with explicit `id` in the PK. Single key or composite
|
|
750
|
+
export var isIdOptionallyManaged = function (modelDefinition) {
|
|
751
|
+
var keyAttribute = extractKeyIfExists(modelDefinition);
|
|
752
|
+
if (keyAttribute && isModelAttributePrimaryKey(keyAttribute)) {
|
|
753
|
+
return keyAttribute.properties.fields[0] === ID;
|
|
754
|
+
}
|
|
755
|
+
return false;
|
|
756
|
+
};
|
|
757
|
+
export var establishRelationAndKeys = function (namespace) {
|
|
758
|
+
var relationship = {};
|
|
759
|
+
var keys = {};
|
|
760
|
+
Object.keys(namespace.models).forEach(function (mKey) {
|
|
761
|
+
var e_3, _c;
|
|
762
|
+
relationship[mKey] = { indexes: [], relationTypes: [] };
|
|
763
|
+
keys[mKey] = {};
|
|
764
|
+
var model = namespace.models[mKey];
|
|
765
|
+
Object.keys(model.fields).forEach(function (attr) {
|
|
766
|
+
var fieldAttribute = model.fields[attr];
|
|
767
|
+
if (typeof fieldAttribute.type === 'object' &&
|
|
768
|
+
'model' in fieldAttribute.type) {
|
|
769
|
+
var connectionType = fieldAttribute.association.connectionType;
|
|
770
|
+
relationship[mKey].relationTypes.push({
|
|
771
|
+
fieldName: fieldAttribute.name,
|
|
772
|
+
modelName: fieldAttribute.type.model,
|
|
773
|
+
relationType: connectionType,
|
|
774
|
+
targetName: fieldAttribute.association['targetName'],
|
|
775
|
+
targetNames: fieldAttribute.association['targetNames'],
|
|
776
|
+
associatedWith: fieldAttribute.association['associatedWith'],
|
|
777
|
+
});
|
|
778
|
+
if (connectionType === 'BELONGS_TO') {
|
|
779
|
+
var targetNames = extractTargetNamesFromSrc(fieldAttribute.association);
|
|
780
|
+
if (targetNames) {
|
|
781
|
+
var idxName = indexNameFromKeys(targetNames);
|
|
782
|
+
relationship[mKey].indexes.push([idxName, targetNames]);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
});
|
|
787
|
+
if (model.attributes) {
|
|
788
|
+
keys[mKey].compositeKeys = processCompositeKeys(model.attributes);
|
|
789
|
+
var _loop_1 = function (attribute) {
|
|
790
|
+
if (!isModelAttributeKey(attribute)) {
|
|
791
|
+
return "continue";
|
|
792
|
+
}
|
|
793
|
+
var fields = attribute.properties.fields;
|
|
794
|
+
if (isModelAttributePrimaryKey(attribute)) {
|
|
795
|
+
keys[mKey].primaryKey = fields;
|
|
796
|
+
return "continue";
|
|
797
|
+
}
|
|
798
|
+
// create indexes for all other keys
|
|
799
|
+
var idxName = indexNameFromKeys(fields);
|
|
800
|
+
var idxExists = relationship[mKey].indexes.find(function (_c) {
|
|
801
|
+
var _d = __read(_c, 1), index = _d[0];
|
|
802
|
+
return index === idxName;
|
|
803
|
+
});
|
|
804
|
+
if (!idxExists) {
|
|
805
|
+
relationship[mKey].indexes.push([idxName, fields]);
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
try {
|
|
809
|
+
for (var _d = __values(model.attributes), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
810
|
+
var attribute = _e.value;
|
|
811
|
+
_loop_1(attribute);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
815
|
+
finally {
|
|
816
|
+
try {
|
|
817
|
+
if (_e && !_e.done && (_c = _d.return)) _c.call(_d);
|
|
818
|
+
}
|
|
819
|
+
finally { if (e_3) throw e_3.error; }
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
// set 'id' as the PK for models without a custom PK explicitly defined
|
|
823
|
+
if (!keys[mKey].primaryKey) {
|
|
824
|
+
keys[mKey].primaryKey = [ID];
|
|
825
|
+
}
|
|
826
|
+
// create primary index
|
|
827
|
+
relationship[mKey].indexes.push([
|
|
828
|
+
'byPk',
|
|
829
|
+
keys[mKey].primaryKey,
|
|
830
|
+
{ unique: true },
|
|
831
|
+
]);
|
|
832
|
+
});
|
|
833
|
+
return [relationship, keys];
|
|
834
|
+
};
|
|
835
|
+
export var getIndex = function (rel, src) {
|
|
836
|
+
var indexName;
|
|
837
|
+
rel.some(function (relItem) {
|
|
838
|
+
if (relItem.modelName === src) {
|
|
839
|
+
var targetNames = extractTargetNamesFromSrc(relItem);
|
|
840
|
+
indexName = targetNames && indexNameFromKeys(targetNames);
|
|
841
|
+
return true;
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
return indexName;
|
|
845
|
+
};
|
|
846
|
+
export var getIndexFromAssociation = function (indexes, src) {
|
|
847
|
+
var indexName;
|
|
848
|
+
if (Array.isArray(src)) {
|
|
849
|
+
indexName = indexNameFromKeys(src);
|
|
850
|
+
}
|
|
851
|
+
else {
|
|
852
|
+
indexName = src;
|
|
853
|
+
}
|
|
854
|
+
var associationIndex = indexes.find(function (_c) {
|
|
855
|
+
var _d = __read(_c, 1), idxName = _d[0];
|
|
856
|
+
return idxName === indexName;
|
|
857
|
+
});
|
|
858
|
+
return associationIndex && associationIndex[0];
|
|
859
|
+
};
|
|
860
|
+
/**
|
|
861
|
+
* Backwards-compatability for schema generated prior to custom primary key support:
|
|
862
|
+
the single field `targetName` has been replaced with an array of `targetNames`.
|
|
863
|
+
`targetName` and `targetNames` are exclusive (will never exist on the same schema)
|
|
864
|
+
* @param src {RelationType | ModelAssociation | undefined}
|
|
865
|
+
* @returns array of targetNames, or `undefined`
|
|
866
|
+
*/
|
|
867
|
+
export var extractTargetNamesFromSrc = function (src) {
|
|
868
|
+
var targetName = src === null || src === void 0 ? void 0 : src.targetName;
|
|
869
|
+
var targetNames = src === null || src === void 0 ? void 0 : src.targetNames;
|
|
870
|
+
if (Array.isArray(targetNames)) {
|
|
871
|
+
return targetNames;
|
|
872
|
+
}
|
|
873
|
+
else if (typeof targetName === 'string') {
|
|
874
|
+
return [targetName];
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
return undefined;
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
// Generates spinal-cased index name from an array of key field names
|
|
881
|
+
// E.g. for keys `[id, title]` => 'id-title'
|
|
882
|
+
export var indexNameFromKeys = function (keys) {
|
|
883
|
+
return keys.reduce(function (prev, cur, idx) {
|
|
884
|
+
if (idx === 0) {
|
|
885
|
+
return cur;
|
|
886
|
+
}
|
|
887
|
+
return "" + prev + IDENTIFIER_KEY_SEPARATOR + cur;
|
|
888
|
+
}, '');
|
|
889
|
+
};
|
|
890
|
+
export var keysEqual = function (keysA, keysB) {
|
|
891
|
+
if (keysA.length !== keysB.length) {
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
return keysA.every(function (key, idx) { return key === keysB[idx]; });
|
|
895
|
+
};
|
|
896
|
+
// Returns primary keys for a model
|
|
897
|
+
export var getIndexKeys = function (namespace, modelName) {
|
|
898
|
+
var _c;
|
|
899
|
+
var keyPath = (_c = namespace === null || namespace === void 0 ? void 0 : namespace.keys[modelName]) === null || _c === void 0 ? void 0 : _c.primaryKey;
|
|
900
|
+
if (keyPath) {
|
|
901
|
+
return keyPath;
|
|
902
|
+
}
|
|
903
|
+
return [ID];
|
|
904
|
+
};
|
|
905
|
+
//#endregion
|
|
719
906
|
//# sourceMappingURL=util.js.map
|