@aws-amplify/datastore 5.0.32 → 5.0.33-s-auth.30d0cd2.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/dist/cjs/authModeStrategies/multiAuthStrategy.js +1 -1
- package/dist/cjs/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/dist/cjs/datastore/datastore.js +46 -41
- package/dist/cjs/datastore/datastore.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/predicates/index.js +1 -0
- package/dist/cjs/predicates/index.js.map +1 -1
- package/dist/cjs/predicates/next.js +13 -12
- package/dist/cjs/predicates/next.js.map +1 -1
- package/dist/cjs/predicates/sort.js +2 -2
- package/dist/cjs/predicates/sort.js.map +1 -1
- package/dist/cjs/storage/adapter/AsyncStorageAdapter.js +21 -18
- package/dist/cjs/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/dist/cjs/storage/adapter/AsyncStorageDatabase.js +10 -9
- package/dist/cjs/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/dist/cjs/storage/adapter/InMemoryStore.js +4 -1
- package/dist/cjs/storage/adapter/InMemoryStore.js.map +1 -1
- package/dist/cjs/storage/adapter/IndexedDBAdapter.js +21 -23
- package/dist/cjs/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/dist/cjs/storage/adapter/StorageAdapterBase.js +7 -7
- package/dist/cjs/storage/adapter/StorageAdapterBase.js.map +1 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.js +4 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.js.map +1 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js +1 -0
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
- package/dist/cjs/storage/relationship.js +1 -0
- package/dist/cjs/storage/relationship.js.map +1 -1
- package/dist/cjs/storage/storage.js +14 -14
- package/dist/cjs/storage/storage.js.map +1 -1
- package/dist/cjs/sync/datastoreConnectivity.js +0 -3
- package/dist/cjs/sync/datastoreConnectivity.js.map +1 -1
- package/dist/cjs/sync/index.js +47 -43
- package/dist/cjs/sync/index.js.map +1 -1
- package/dist/cjs/sync/merger.js +1 -1
- package/dist/cjs/sync/merger.js.map +1 -1
- package/dist/cjs/sync/outbox.js +19 -17
- package/dist/cjs/sync/outbox.js.map +1 -1
- package/dist/cjs/sync/processors/errorMaps.js.map +1 -1
- package/dist/cjs/sync/processors/mutation.js +31 -28
- package/dist/cjs/sync/processors/mutation.js.map +1 -1
- package/dist/cjs/sync/processors/subscription.js +21 -12
- package/dist/cjs/sync/processors/subscription.js.map +1 -1
- package/dist/cjs/sync/processors/sync.js +25 -21
- package/dist/cjs/sync/processors/sync.js.map +1 -1
- package/dist/cjs/sync/utils.js +20 -21
- package/dist/cjs/sync/utils.js.map +1 -1
- package/dist/cjs/types.js +1 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/util.js +46 -25
- package/dist/cjs/util.js.map +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.d.ts +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.mjs +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.mjs.map +1 -1
- package/dist/esm/datastore/datastore.d.ts +4 -6
- package/dist/esm/datastore/datastore.mjs +42 -34
- package/dist/esm/datastore/datastore.mjs.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/predicates/index.mjs +1 -0
- package/dist/esm/predicates/index.mjs.map +1 -1
- package/dist/esm/predicates/next.d.ts +7 -7
- package/dist/esm/predicates/next.mjs +13 -12
- package/dist/esm/predicates/next.mjs.map +1 -1
- package/dist/esm/predicates/sort.d.ts +1 -1
- package/dist/esm/predicates/sort.mjs +2 -2
- package/dist/esm/predicates/sort.mjs.map +1 -1
- package/dist/esm/storage/adapter/AsyncStorageAdapter.d.ts +1 -1
- package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs +21 -18
- package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs.map +1 -1
- package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs +10 -9
- package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs.map +1 -1
- package/dist/esm/storage/adapter/InMemoryStore.mjs +4 -1
- package/dist/esm/storage/adapter/InMemoryStore.mjs.map +1 -1
- package/dist/esm/storage/adapter/IndexedDBAdapter.mjs +21 -23
- package/dist/esm/storage/adapter/IndexedDBAdapter.mjs.map +1 -1
- package/dist/esm/storage/adapter/StorageAdapterBase.d.ts +2 -2
- package/dist/esm/storage/adapter/StorageAdapterBase.mjs +7 -7
- package/dist/esm/storage/adapter/StorageAdapterBase.mjs.map +1 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs +3 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs.map +1 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs +1 -0
- package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs.map +1 -1
- package/dist/esm/storage/adapter/index.d.ts +1 -1
- package/dist/esm/storage/relationship.mjs +1 -0
- package/dist/esm/storage/relationship.mjs.map +1 -1
- package/dist/esm/storage/storage.d.ts +11 -11
- package/dist/esm/storage/storage.mjs +10 -10
- package/dist/esm/storage/storage.mjs.map +1 -1
- package/dist/esm/sync/datastoreConnectivity.d.ts +2 -2
- package/dist/esm/sync/datastoreConnectivity.mjs +0 -3
- package/dist/esm/sync/datastoreConnectivity.mjs.map +1 -1
- package/dist/esm/sync/index.d.ts +3 -3
- package/dist/esm/sync/index.mjs +47 -43
- package/dist/esm/sync/index.mjs.map +1 -1
- package/dist/esm/sync/merger.d.ts +1 -1
- package/dist/esm/sync/merger.mjs +1 -1
- package/dist/esm/sync/merger.mjs.map +1 -1
- package/dist/esm/sync/outbox.d.ts +4 -4
- package/dist/esm/sync/outbox.mjs +19 -17
- package/dist/esm/sync/outbox.mjs.map +1 -1
- package/dist/esm/sync/processors/errorMaps.mjs.map +1 -1
- package/dist/esm/sync/processors/mutation.d.ts +5 -5
- package/dist/esm/sync/processors/mutation.mjs +31 -28
- package/dist/esm/sync/processors/mutation.mjs.map +1 -1
- package/dist/esm/sync/processors/subscription.d.ts +1 -1
- package/dist/esm/sync/processors/subscription.mjs +21 -12
- package/dist/esm/sync/processors/subscription.mjs.map +1 -1
- package/dist/esm/sync/processors/sync.d.ts +3 -3
- package/dist/esm/sync/processors/sync.mjs +13 -9
- package/dist/esm/sync/processors/sync.mjs.map +1 -1
- package/dist/esm/sync/utils.d.ts +11 -13
- package/dist/esm/sync/utils.mjs +20 -21
- package/dist/esm/sync/utils.mjs.map +1 -1
- package/dist/esm/types.d.ts +121 -131
- package/dist/esm/types.mjs +3 -2
- package/dist/esm/types.mjs.map +1 -1
- package/dist/esm/util.d.ts +5 -5
- package/dist/esm/util.mjs +46 -25
- package/dist/esm/util.mjs.map +1 -1
- package/package.json +74 -73
- package/src/authModeStrategies/multiAuthStrategy.ts +8 -4
- package/src/datastore/datastore.ts +145 -123
- package/src/index.ts +10 -8
- package/src/predicates/index.ts +10 -2
- package/src/predicates/next.ts +39 -28
- package/src/predicates/sort.ts +23 -26
- package/src/storage/adapter/AsyncStorageAdapter.ts +43 -25
- package/src/storage/adapter/AsyncStorageDatabase.ts +18 -10
- package/src/storage/adapter/InMemoryStore.ts +5 -1
- package/src/storage/adapter/IndexedDBAdapter.ts +50 -37
- package/src/storage/adapter/StorageAdapterBase.ts +24 -18
- package/src/storage/adapter/getDefaultAdapter/index.native.ts +1 -0
- package/src/storage/adapter/getDefaultAdapter/index.ts +4 -1
- package/src/storage/adapter/index.ts +2 -2
- package/src/storage/relationship.ts +5 -1
- package/src/storage/storage.ts +38 -30
- package/src/sync/datastoreConnectivity.ts +4 -6
- package/src/sync/index.ts +228 -230
- package/src/sync/merger.ts +4 -3
- package/src/sync/outbox.ts +26 -24
- package/src/sync/processors/errorMaps.ts +5 -0
- package/src/sync/processors/mutation.ts +80 -72
- package/src/sync/processors/subscription.ts +69 -53
- package/src/sync/processors/sync.ts +49 -37
- package/src/sync/utils.ts +60 -41
- package/src/types.ts +165 -165
- package/src/util.ts +89 -53
package/src/index.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import {
|
|
4
|
+
USER,
|
|
5
|
+
isModelConstructor,
|
|
6
|
+
isNonModelConstructor,
|
|
7
|
+
traverseModel,
|
|
8
|
+
validatePredicate,
|
|
9
|
+
} from './util';
|
|
10
|
+
|
|
3
11
|
export {
|
|
4
12
|
DataStore,
|
|
5
13
|
DataStoreClass,
|
|
6
14
|
initSchema,
|
|
7
15
|
ModelInstanceCreator,
|
|
16
|
+
// eslint-disable-next-line import/export
|
|
8
17
|
AsyncCollection,
|
|
9
18
|
AsyncItem,
|
|
10
19
|
} from './datastore/datastore';
|
|
@@ -16,14 +25,6 @@ export {
|
|
|
16
25
|
} from './predicates';
|
|
17
26
|
export { Adapter as StorageAdapter } from './storage/adapter';
|
|
18
27
|
|
|
19
|
-
import {
|
|
20
|
-
traverseModel,
|
|
21
|
-
validatePredicate,
|
|
22
|
-
USER,
|
|
23
|
-
isNonModelConstructor,
|
|
24
|
-
isModelConstructor,
|
|
25
|
-
} from './util';
|
|
26
|
-
|
|
27
28
|
export { NAMESPACES } from './util';
|
|
28
29
|
|
|
29
30
|
export const utils = {
|
|
@@ -34,4 +35,5 @@ export const utils = {
|
|
|
34
35
|
isModelConstructor,
|
|
35
36
|
};
|
|
36
37
|
|
|
38
|
+
// eslint-disable-next-line import/export
|
|
37
39
|
export * from './types';
|
package/src/predicates/index.ts
CHANGED
|
@@ -39,6 +39,7 @@ const groupKeys = new Set(['and', 'or', 'not']);
|
|
|
39
39
|
*/
|
|
40
40
|
const isGroup = o => {
|
|
41
41
|
const keys = [...Object.keys(o)];
|
|
42
|
+
|
|
42
43
|
return keys.length === 1 && groupKeys.has(keys[0]);
|
|
43
44
|
};
|
|
44
45
|
|
|
@@ -77,6 +78,7 @@ export const comparisonKeys = new Set([
|
|
|
77
78
|
*/
|
|
78
79
|
const isComparison = o => {
|
|
79
80
|
const keys = [...Object.keys(o)];
|
|
81
|
+
|
|
80
82
|
return !Array.isArray(o) && keys.length === 1 && comparisonKeys.has(keys[0]);
|
|
81
83
|
};
|
|
82
84
|
|
|
@@ -98,11 +100,12 @@ export const PredicateAll = Symbol('A predicate that matches all records');
|
|
|
98
100
|
|
|
99
101
|
export class Predicates {
|
|
100
102
|
public static get ALL(): typeof PredicateAll {
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
101
104
|
const predicate = <ProducerModelPredicate<any>>(c => c);
|
|
102
105
|
|
|
103
106
|
predicatesAllSet.add(predicate);
|
|
104
107
|
|
|
105
|
-
return
|
|
108
|
+
return predicate as unknown as typeof PredicateAll;
|
|
106
109
|
}
|
|
107
110
|
}
|
|
108
111
|
|
|
@@ -140,7 +143,7 @@ export class ModelPredicateCreator {
|
|
|
140
143
|
*/
|
|
141
144
|
static getPredicates<T extends PersistentModel>(
|
|
142
145
|
predicate: ModelPredicate<T>,
|
|
143
|
-
throwOnInvalid
|
|
146
|
+
throwOnInvalid = true,
|
|
144
147
|
) {
|
|
145
148
|
if (throwOnInvalid && !ModelPredicateCreator.isValidPredicate(predicate)) {
|
|
146
149
|
throw new Error('The predicate is not valid');
|
|
@@ -167,6 +170,7 @@ export class ModelPredicateCreator {
|
|
|
167
170
|
const predicate = this.createFromAST<T>(modelDefinition, {
|
|
168
171
|
and: keyFields.map((field, idx) => {
|
|
169
172
|
const operand = keyValues[idx];
|
|
173
|
+
|
|
170
174
|
return { [field]: { eq: operand } };
|
|
171
175
|
}),
|
|
172
176
|
});
|
|
@@ -190,6 +194,7 @@ export class ModelPredicateCreator {
|
|
|
190
194
|
const ast = {
|
|
191
195
|
and: Object.entries(flatEqualities).map(([k, v]) => ({ [k]: { eq: v } })),
|
|
192
196
|
};
|
|
197
|
+
|
|
193
198
|
return this.createFromAST<T>(modelDefinition, ast);
|
|
194
199
|
}
|
|
195
200
|
|
|
@@ -231,12 +236,14 @@ export class ModelPredicateCreator {
|
|
|
231
236
|
const children = this.transformGraphQLFilterNodeToPredicateAST(
|
|
232
237
|
gql[groupkey],
|
|
233
238
|
);
|
|
239
|
+
|
|
234
240
|
return {
|
|
235
241
|
type: groupkey,
|
|
236
242
|
predicates: Array.isArray(children) ? children : [children],
|
|
237
243
|
};
|
|
238
244
|
} else if (isComparison(gql)) {
|
|
239
245
|
const operatorKey = Object.keys(gql)[0];
|
|
246
|
+
|
|
240
247
|
return {
|
|
241
248
|
operator: operatorKey,
|
|
242
249
|
operand: gql[operatorKey],
|
|
@@ -246,6 +253,7 @@ export class ModelPredicateCreator {
|
|
|
246
253
|
return gql.map(o => this.transformGraphQLFilterNodeToPredicateAST(o));
|
|
247
254
|
} else {
|
|
248
255
|
const fieldKey = Object.keys(gql)[0];
|
|
256
|
+
|
|
249
257
|
return {
|
|
250
258
|
field: fieldKey,
|
|
251
259
|
...this.transformGraphQLFilterNodeToPredicateAST(gql[fieldKey]),
|
package/src/predicates/next.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
AllFieldOperators,
|
|
5
5
|
ModelFieldType,
|
|
6
6
|
ModelMeta,
|
|
7
|
-
ModelPredicate as StoragePredicate,
|
|
8
|
-
AllFieldOperators,
|
|
9
|
-
PredicateInternalsKey,
|
|
10
7
|
V5ModelPredicate as ModelPredicate,
|
|
8
|
+
PersistentModel,
|
|
9
|
+
PredicateInternalsKey,
|
|
11
10
|
RecursiveModelPredicate,
|
|
12
|
-
RecursiveModelPredicateExtender,
|
|
13
11
|
RecursiveModelPredicateAggregateExtender,
|
|
12
|
+
RecursiveModelPredicateExtender,
|
|
13
|
+
ModelPredicate as StoragePredicate,
|
|
14
14
|
} from '../types';
|
|
15
|
+
import { ExclusiveStorage as StorageAdapter } from '../storage/storage';
|
|
16
|
+
import { ModelRelationship } from '../storage/relationship';
|
|
17
|
+
import { asyncEvery, asyncSome } from '../util';
|
|
15
18
|
|
|
16
19
|
import {
|
|
17
20
|
ModelPredicateCreator as FlatModelPredicateCreator,
|
|
18
21
|
comparisonKeys,
|
|
19
22
|
} from './index';
|
|
20
|
-
import { ExclusiveStorage as StorageAdapter } from '../storage/storage';
|
|
21
|
-
import { ModelRelationship } from '../storage/relationship';
|
|
22
|
-
import { asyncSome, asyncEvery } from '../util';
|
|
23
23
|
|
|
24
24
|
const ops = [...comparisonKeys] as AllFieldOperators[];
|
|
25
25
|
|
|
26
26
|
type GroupOperator = 'and' | 'or' | 'not';
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
fetch
|
|
30
|
-
matches
|
|
28
|
+
interface UntypedCondition {
|
|
29
|
+
fetch(storage: StorageAdapter): Promise<Record<string, any>[]>;
|
|
30
|
+
matches(item: Record<string, any>): Promise<boolean>;
|
|
31
31
|
copy(
|
|
32
32
|
extract?: GroupCondition,
|
|
33
33
|
): [UntypedCondition, GroupCondition | undefined];
|
|
34
34
|
toAST(): any;
|
|
35
|
-
}
|
|
35
|
+
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* A map from keys (exposed to customers) to the internal predicate data
|
|
@@ -52,6 +52,7 @@ const predicateInternalsMap = new Map<PredicateInternalsKey, GroupCondition>();
|
|
|
52
52
|
const registerPredicateInternals = (condition: GroupCondition, key?: any) => {
|
|
53
53
|
const finalKey = key || new PredicateInternalsKey();
|
|
54
54
|
predicateInternalsMap.set(finalKey, condition);
|
|
55
|
+
|
|
55
56
|
return finalKey;
|
|
56
57
|
};
|
|
57
58
|
|
|
@@ -72,6 +73,7 @@ export const internals = (key: any) => {
|
|
|
72
73
|
"Invalid predicate. Terminate your predicate with a valid condition (e.g., `p => p.field.eq('value')`) or pass `Predicates.ALL`.",
|
|
73
74
|
);
|
|
74
75
|
}
|
|
76
|
+
|
|
75
77
|
return predicateInternalsMap.get(key)!;
|
|
76
78
|
};
|
|
77
79
|
|
|
@@ -113,7 +115,7 @@ export class FieldCondition {
|
|
|
113
115
|
* @param extract Not used. Present only to fulfill the `UntypedCondition` interface.
|
|
114
116
|
* @returns A new, identitical `FieldCondition`.
|
|
115
117
|
*/
|
|
116
|
-
copy(
|
|
118
|
+
copy(): [FieldCondition, GroupCondition | undefined] {
|
|
117
119
|
return [
|
|
118
120
|
new FieldCondition(this.field, this.operator, [...this.operands]),
|
|
119
121
|
undefined,
|
|
@@ -191,7 +193,8 @@ export class FieldCondition {
|
|
|
191
193
|
* @param storage N/A. If ever implemented, the storage adapter to query.
|
|
192
194
|
* @returns N/A. If ever implemented, return items from `storage` that match.
|
|
193
195
|
*/
|
|
194
|
-
async fetch(
|
|
196
|
+
async fetch(): Promise<Record<string, any>[]> {
|
|
197
|
+
// eslint-disable-next-line prefer-promise-reject-errors
|
|
195
198
|
return Promise.reject('No implementation needed [yet].');
|
|
196
199
|
}
|
|
197
200
|
|
|
@@ -217,6 +220,7 @@ export class FieldCondition {
|
|
|
217
220
|
const operation = operations[this.operator as keyof typeof operations];
|
|
218
221
|
if (operation) {
|
|
219
222
|
const result = operation();
|
|
223
|
+
|
|
220
224
|
return result;
|
|
221
225
|
} else {
|
|
222
226
|
throw new Error(`Invalid operator given: ${this.operator}`);
|
|
@@ -234,6 +238,7 @@ export class FieldCondition {
|
|
|
234
238
|
*/
|
|
235
239
|
const argumentCount = count => {
|
|
236
240
|
const argsClause = count === 1 ? 'argument is' : 'arguments are';
|
|
241
|
+
|
|
237
242
|
return () => {
|
|
238
243
|
if (this.operands.length !== count) {
|
|
239
244
|
return `Exactly ${count} ${argsClause} required.`;
|
|
@@ -278,6 +283,7 @@ export class FieldCondition {
|
|
|
278
283
|
*/
|
|
279
284
|
const getGroupId = (() => {
|
|
280
285
|
let seed = 1;
|
|
286
|
+
|
|
281
287
|
return () => `group_${seed++}`;
|
|
282
288
|
})();
|
|
283
289
|
|
|
@@ -345,7 +351,7 @@ export class GroupCondition {
|
|
|
345
351
|
* This is used to guard against infinitely fetch -> optimize -> fetch
|
|
346
352
|
* recursion.
|
|
347
353
|
*/
|
|
348
|
-
public isOptimized
|
|
354
|
+
public isOptimized = false,
|
|
349
355
|
) {}
|
|
350
356
|
|
|
351
357
|
/**
|
|
@@ -386,6 +392,7 @@ export class GroupCondition {
|
|
|
386
392
|
*/
|
|
387
393
|
withFieldConditionsOnly(negate: boolean) {
|
|
388
394
|
const negateChildren = negate !== (this.operator === 'not');
|
|
395
|
+
|
|
389
396
|
return new GroupCondition(
|
|
390
397
|
this.model,
|
|
391
398
|
undefined,
|
|
@@ -495,7 +502,7 @@ export class GroupCondition {
|
|
|
495
502
|
return this.optimized().fetch(storage);
|
|
496
503
|
}
|
|
497
504
|
|
|
498
|
-
const resultGroups:
|
|
505
|
+
const resultGroups: Record<string, any>[][] = [];
|
|
499
506
|
|
|
500
507
|
const operator = (negate ? negations[this.operator] : this.operator) as
|
|
501
508
|
| 'or'
|
|
@@ -564,7 +571,7 @@ export class GroupCondition {
|
|
|
564
571
|
|
|
565
572
|
const relationship = ModelRelationship.from(this.model, g.field);
|
|
566
573
|
|
|
567
|
-
type JoinCondition =
|
|
574
|
+
type JoinCondition = Record<string, { eq: any }>;
|
|
568
575
|
if (relationship) {
|
|
569
576
|
const allJoinConditions: { and: JoinCondition[] }[] = [];
|
|
570
577
|
for (const relative of relatives) {
|
|
@@ -665,7 +672,7 @@ export class GroupCondition {
|
|
|
665
672
|
*/
|
|
666
673
|
async matches(
|
|
667
674
|
item: Record<string, any>,
|
|
668
|
-
ignoreFieldName
|
|
675
|
+
ignoreFieldName = false,
|
|
669
676
|
): Promise<boolean> {
|
|
670
677
|
const itemToCheck =
|
|
671
678
|
this.field && !ignoreFieldName ? await item[this.field] : item;
|
|
@@ -686,6 +693,7 @@ export class GroupCondition {
|
|
|
686
693
|
return true;
|
|
687
694
|
}
|
|
688
695
|
}
|
|
696
|
+
|
|
689
697
|
return false;
|
|
690
698
|
}
|
|
691
699
|
|
|
@@ -699,6 +707,7 @@ export class GroupCondition {
|
|
|
699
707
|
'Invalid arguments! `not()` accepts exactly one predicate expression.',
|
|
700
708
|
);
|
|
701
709
|
}
|
|
710
|
+
|
|
702
711
|
return !(await this.operands[0].matches(itemToCheck));
|
|
703
712
|
} else {
|
|
704
713
|
throw new Error('Invalid group operator!');
|
|
@@ -769,7 +778,7 @@ export class GroupCondition {
|
|
|
769
778
|
*/
|
|
770
779
|
export function recursivePredicateFor<T extends PersistentModel>(
|
|
771
780
|
ModelType: ModelMeta<T>,
|
|
772
|
-
allowRecursion
|
|
781
|
+
allowRecursion = true,
|
|
773
782
|
field?: string,
|
|
774
783
|
query?: GroupCondition,
|
|
775
784
|
tail?: GroupCondition,
|
|
@@ -788,15 +797,16 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
788
797
|
registerPredicateInternals(baseCondition, link);
|
|
789
798
|
|
|
790
799
|
const copyLink = () => {
|
|
791
|
-
const [
|
|
800
|
+
const [copiedQuery, newTail] = baseCondition.copy(tailCondition);
|
|
792
801
|
const newLink = recursivePredicateFor(
|
|
793
802
|
ModelType,
|
|
794
803
|
allowRecursion,
|
|
795
804
|
undefined,
|
|
796
|
-
|
|
805
|
+
copiedQuery,
|
|
797
806
|
newTail,
|
|
798
807
|
);
|
|
799
|
-
|
|
808
|
+
|
|
809
|
+
return { query: copiedQuery, newTail, newLink };
|
|
800
810
|
};
|
|
801
811
|
|
|
802
812
|
// Adds .or() and .and() methods to the link.
|
|
@@ -805,7 +815,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
805
815
|
link[op] = (builder: RecursiveModelPredicateAggregateExtender<T>) => {
|
|
806
816
|
// or() and and() will return a copy of the original link
|
|
807
817
|
// to head off mutability concerns.
|
|
808
|
-
const { query, newTail } = copyLink();
|
|
818
|
+
const { query: copiedLinkQuery, newTail } = copyLink();
|
|
809
819
|
|
|
810
820
|
const childConditions = builder(
|
|
811
821
|
recursivePredicateFor(ModelType, allowRecursion),
|
|
@@ -829,7 +839,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
829
839
|
);
|
|
830
840
|
|
|
831
841
|
// FinalPredicate
|
|
832
|
-
return registerPredicateInternals(
|
|
842
|
+
return registerPredicateInternals(copiedLinkQuery);
|
|
833
843
|
};
|
|
834
844
|
});
|
|
835
845
|
|
|
@@ -839,7 +849,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
839
849
|
): PredicateInternalsKey => {
|
|
840
850
|
// not() will return a copy of the original link
|
|
841
851
|
// to head off mutability concerns.
|
|
842
|
-
const { query, newTail } = copyLink();
|
|
852
|
+
const { query: copiedLinkQuery, newTail } = copyLink();
|
|
843
853
|
|
|
844
854
|
// unlike and() and or(), the customer will supply a "singular" child predicate.
|
|
845
855
|
// the difference being: not() does not accept an array of predicate-like objects.
|
|
@@ -853,7 +863,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
853
863
|
// A `FinalModelPredicate`.
|
|
854
864
|
// Return a thing that can no longer be extended, but instead used to `async filter(items)`
|
|
855
865
|
// or query storage: `.__query.fetch(storage)`.
|
|
856
|
-
return registerPredicateInternals(
|
|
866
|
+
return registerPredicateInternals(copiedLinkQuery);
|
|
857
867
|
};
|
|
858
868
|
|
|
859
869
|
// For each field on the model schema, we want to add a getter
|
|
@@ -881,7 +891,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
881
891
|
[operator]: (...operands: any[]) => {
|
|
882
892
|
// build off a fresh copy of the existing `link`, just in case
|
|
883
893
|
// the same link is being used elsewhere by the customer.
|
|
884
|
-
const { query, newTail } = copyLink();
|
|
894
|
+
const { query: copiedLinkQuery, newTail } = copyLink();
|
|
885
895
|
|
|
886
896
|
// normalize operands. if any of the values are `undefiend`, use
|
|
887
897
|
// `null` instead, because that's what will be stored cross-platform.
|
|
@@ -898,7 +908,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
898
908
|
// A `FinalModelPredicate`.
|
|
899
909
|
// Return a thing that can no longer be extended, but instead used to `async filter(items)`
|
|
900
910
|
// or query storage: `.__query.fetch(storage)`.
|
|
901
|
-
return registerPredicateInternals(
|
|
911
|
+
return registerPredicateInternals(copiedLinkQuery);
|
|
902
912
|
},
|
|
903
913
|
};
|
|
904
914
|
}, {});
|
|
@@ -945,6 +955,7 @@ export function recursivePredicateFor<T extends PersistentModel>(
|
|
|
945
955
|
newquery,
|
|
946
956
|
newtail,
|
|
947
957
|
);
|
|
958
|
+
|
|
948
959
|
return newlink;
|
|
949
960
|
} else {
|
|
950
961
|
throw new Error(
|
package/src/predicates/sort.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import {
|
|
4
4
|
PersistentModel,
|
|
5
|
-
SchemaModel,
|
|
6
|
-
SortPredicate,
|
|
7
5
|
ProducerSortPredicate,
|
|
6
|
+
SchemaModel,
|
|
8
7
|
SortDirection,
|
|
8
|
+
SortPredicate,
|
|
9
9
|
SortPredicatesGroup,
|
|
10
10
|
} from '../types';
|
|
11
11
|
|
|
@@ -21,32 +21,29 @@ export class ModelSortPredicateCreator {
|
|
|
21
21
|
const { name: modelName } = modelDefinition;
|
|
22
22
|
const fieldNames = new Set<keyof T>(Object.keys(modelDefinition.fields));
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
(handler = {
|
|
28
|
-
get(_target, propertyKey, receiver: SortPredicate<T>) {
|
|
29
|
-
const field = propertyKey as keyof T;
|
|
24
|
+
const predicate = new Proxy({} as SortPredicate<T>, {
|
|
25
|
+
get(_target, propertyKey, receiver: SortPredicate<T>) {
|
|
26
|
+
const field = propertyKey as keyof T;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
if (!fieldNames.has(field)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Invalid field for model. field: ${String(
|
|
31
|
+
field,
|
|
32
|
+
)}, model: ${modelName}`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
38
35
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
const result = (sortDirection: SortDirection) => {
|
|
37
|
+
ModelSortPredicateCreator.sortPredicateGroupsMap
|
|
38
|
+
.get(receiver)
|
|
39
|
+
?.push({ field, sortDirection });
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
);
|
|
41
|
+
return receiver;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return result;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
50
47
|
|
|
51
48
|
ModelSortPredicateCreator.sortPredicateGroupsMap.set(predicate, []);
|
|
52
49
|
|
|
@@ -61,7 +58,7 @@ export class ModelSortPredicateCreator {
|
|
|
61
58
|
|
|
62
59
|
static getPredicates<T extends PersistentModel>(
|
|
63
60
|
predicate: SortPredicate<T>,
|
|
64
|
-
throwOnInvalid
|
|
61
|
+
throwOnInvalid = true,
|
|
65
62
|
): SortPredicatesGroup<T> {
|
|
66
63
|
if (
|
|
67
64
|
throwOnInvalid &&
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import AsyncStorageDatabase from './AsyncStorageDatabase';
|
|
4
3
|
import {
|
|
5
4
|
ModelInstanceMetadata,
|
|
6
5
|
ModelPredicate,
|
|
@@ -13,21 +12,27 @@ import {
|
|
|
13
12
|
} from '../../types';
|
|
14
13
|
import {
|
|
15
14
|
DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR,
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
getIndexKeys,
|
|
16
|
+
getStorename,
|
|
18
17
|
inMemoryPagination,
|
|
19
18
|
keysEqual,
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
traverseModel,
|
|
20
|
+
validatePredicate,
|
|
22
21
|
} from '../../util';
|
|
22
|
+
|
|
23
|
+
import AsyncStorageDatabase from './AsyncStorageDatabase';
|
|
23
24
|
import { StorageAdapterBase } from './StorageAdapterBase';
|
|
24
25
|
|
|
25
26
|
export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
26
27
|
protected db!: AsyncStorageDatabase;
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
protected async preSetUpChecks() {
|
|
30
|
+
// no-ops for AsyncStorageAdapter
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected async preOpCheck() {
|
|
34
|
+
// no-ops for AsyncStorageAdapter
|
|
35
|
+
}
|
|
31
36
|
|
|
32
37
|
/**
|
|
33
38
|
* Open AsyncStorage database
|
|
@@ -40,6 +45,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
40
45
|
protected async initDb(): Promise<AsyncStorageDatabase> {
|
|
41
46
|
const db = new AsyncStorageDatabase();
|
|
42
47
|
await db.init();
|
|
48
|
+
|
|
43
49
|
return db;
|
|
44
50
|
}
|
|
45
51
|
|
|
@@ -77,15 +83,20 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
77
83
|
|
|
78
84
|
const keyValuesPath = this.getIndexKeyValuesPath(model);
|
|
79
85
|
|
|
80
|
-
const { instance } = connectedModels.find(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
const { instance } = connectedModels.find(
|
|
87
|
+
({ instance: connectedModelInstance }) => {
|
|
88
|
+
const instanceKeyValuesPath = this.getIndexKeyValuesPath(
|
|
89
|
+
connectedModelInstance,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
|
|
93
|
+
},
|
|
94
|
+
)!;
|
|
84
95
|
|
|
85
96
|
batch.push(instance);
|
|
86
97
|
}
|
|
87
98
|
|
|
88
|
-
return
|
|
99
|
+
return this.db.batchSave(storeName, batch, keys);
|
|
89
100
|
}
|
|
90
101
|
|
|
91
102
|
protected async _get<T>(storeName: string, keyArr: string[]): Promise<T> {
|
|
@@ -93,7 +104,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
93
104
|
DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR,
|
|
94
105
|
);
|
|
95
106
|
|
|
96
|
-
return
|
|
107
|
+
return (await this.db.get(itemKeyValuesPath, storeName)) as T;
|
|
97
108
|
}
|
|
98
109
|
|
|
99
110
|
async save<T extends PersistentModel>(
|
|
@@ -109,12 +120,15 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
109
120
|
|
|
110
121
|
const result: [T, OpType.INSERT | OpType.UPDATE][] = [];
|
|
111
122
|
for await (const resItem of connectionStoreNames) {
|
|
112
|
-
const { storeName, item, instance, keys } = resItem;
|
|
123
|
+
const { storeName: storeNameForRestItem, item, instance, keys } = resItem;
|
|
113
124
|
|
|
114
125
|
const itemKeyValues: string[] = keys.map(key => item[key]);
|
|
115
126
|
|
|
116
|
-
const
|
|
117
|
-
|
|
127
|
+
const fromDBForRestItem = (await this._get(
|
|
128
|
+
storeNameForRestItem,
|
|
129
|
+
itemKeyValues,
|
|
130
|
+
)) as T;
|
|
131
|
+
const opType: OpType = fromDBForRestItem ? OpType.UPDATE : OpType.INSERT;
|
|
118
132
|
|
|
119
133
|
if (
|
|
120
134
|
keysEqual(itemKeyValues, modelKeyValues) ||
|
|
@@ -122,7 +136,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
122
136
|
) {
|
|
123
137
|
await this.db.save(
|
|
124
138
|
item,
|
|
125
|
-
|
|
139
|
+
storeNameForRestItem,
|
|
126
140
|
keys,
|
|
127
141
|
itemKeyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR),
|
|
128
142
|
);
|
|
@@ -130,6 +144,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
130
144
|
result.push([instance, opType]);
|
|
131
145
|
}
|
|
132
146
|
}
|
|
147
|
+
|
|
133
148
|
return result;
|
|
134
149
|
}
|
|
135
150
|
|
|
@@ -151,36 +166,39 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
151
166
|
if (queryByKey) {
|
|
152
167
|
const keyValues = queryByKey.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
|
|
153
168
|
const record = await this.getByKey(storeName, keyValues);
|
|
169
|
+
|
|
154
170
|
return record ? [record] : [];
|
|
155
171
|
}
|
|
156
172
|
|
|
157
173
|
if (predicates) {
|
|
158
174
|
const filtered = await this.filterOnPredicate(storeName, predicates);
|
|
175
|
+
|
|
159
176
|
return this.inMemoryPagination(filtered, pagination);
|
|
160
177
|
}
|
|
161
178
|
|
|
162
179
|
if (hasSort || hasPagination) {
|
|
163
180
|
const all = await this.getAll(storeName);
|
|
181
|
+
|
|
164
182
|
return this.inMemoryPagination(all, pagination);
|
|
165
183
|
}
|
|
166
184
|
|
|
167
185
|
return this.getAll(storeName);
|
|
168
186
|
})()) as T[];
|
|
169
187
|
|
|
170
|
-
return
|
|
188
|
+
return this.load(namespaceName, modelConstructor.name, records);
|
|
171
189
|
}
|
|
172
190
|
|
|
173
191
|
private async getByKey<T extends PersistentModel>(
|
|
174
192
|
storeName: string,
|
|
175
193
|
keyValuePath: string,
|
|
176
194
|
): Promise<T> {
|
|
177
|
-
return
|
|
195
|
+
return (await this.db.get(keyValuePath, storeName)) as T;
|
|
178
196
|
}
|
|
179
197
|
|
|
180
198
|
private async getAll<T extends PersistentModel>(
|
|
181
199
|
storeName: string,
|
|
182
200
|
): Promise<T[]> {
|
|
183
|
-
return
|
|
201
|
+
return this.db.getAll(storeName);
|
|
184
202
|
}
|
|
185
203
|
|
|
186
204
|
private async filterOnPredicate<T extends PersistentModel>(
|
|
@@ -189,7 +207,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
189
207
|
) {
|
|
190
208
|
const { predicates: predicateObjs, type } = predicates;
|
|
191
209
|
|
|
192
|
-
const all =
|
|
210
|
+
const all = (await this.getAll(storeName)) as T[];
|
|
193
211
|
|
|
194
212
|
const filtered = predicateObjs
|
|
195
213
|
? all.filter(m => validatePredicate(m, type, predicateObjs))
|
|
@@ -210,7 +228,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
210
228
|
firstOrLast: QueryOne = QueryOne.FIRST,
|
|
211
229
|
): Promise<T | undefined> {
|
|
212
230
|
const storeName = this.getStorenameForModel(modelConstructor);
|
|
213
|
-
const result =
|
|
231
|
+
const result = (await this.db.getOne(firstOrLast, storeName)) as T;
|
|
214
232
|
|
|
215
233
|
return result && this.modelInstanceCreator(modelConstructor, result);
|
|
216
234
|
}
|
|
@@ -232,7 +250,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
232
250
|
}
|
|
233
251
|
}
|
|
234
252
|
|
|
235
|
-
|
|
253
|
+
// #region platform-specific helper methods
|
|
236
254
|
|
|
237
255
|
/**
|
|
238
256
|
* Retrieves concatenated primary key values from a model
|
|
@@ -246,7 +264,7 @@ export class AsyncStorageAdapter extends StorageAdapterBase {
|
|
|
246
264
|
);
|
|
247
265
|
}
|
|
248
266
|
|
|
249
|
-
|
|
267
|
+
// #endregion
|
|
250
268
|
}
|
|
251
269
|
|
|
252
270
|
export default new AsyncStorageAdapter();
|