@aws-amplify/datastore 4.0.12 → 4.0.13-push-notification-dryrun.43

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.
Files changed (111) hide show
  1. package/lib/datastore/datastore.d.ts +29 -3
  2. package/lib/datastore/datastore.js +308 -147
  3. package/lib/datastore/datastore.js.map +1 -1
  4. package/lib/predicates/index.d.ts +77 -7
  5. package/lib/predicates/index.js +142 -122
  6. package/lib/predicates/index.js.map +1 -1
  7. package/lib/predicates/next.d.ts +51 -10
  8. package/lib/predicates/next.js +111 -91
  9. package/lib/predicates/next.js.map +1 -1
  10. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +28 -30
  11. package/lib/storage/adapter/AsyncStorageAdapter.js +135 -532
  12. package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  13. package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  14. package/lib/storage/adapter/IndexedDBAdapter.d.ts +28 -29
  15. package/lib/storage/adapter/IndexedDBAdapter.js +490 -885
  16. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  17. package/lib/storage/adapter/StorageAdapterBase.d.ts +134 -0
  18. package/lib/storage/adapter/StorageAdapterBase.js +439 -0
  19. package/lib/storage/adapter/StorageAdapterBase.js.map +1 -0
  20. package/lib/storage/relationship.d.ts +9 -0
  21. package/lib/storage/relationship.js +9 -0
  22. package/lib/storage/relationship.js.map +1 -1
  23. package/lib/storage/storage.d.ts +1 -1
  24. package/lib/storage/storage.js +4 -3
  25. package/lib/storage/storage.js.map +1 -1
  26. package/lib/sync/index.d.ts +15 -1
  27. package/lib/sync/index.js +80 -13
  28. package/lib/sync/index.js.map +1 -1
  29. package/lib/sync/outbox.js +14 -7
  30. package/lib/sync/outbox.js.map +1 -1
  31. package/lib/sync/processors/mutation.d.ts +10 -1
  32. package/lib/sync/processors/mutation.js +33 -12
  33. package/lib/sync/processors/mutation.js.map +1 -1
  34. package/lib/sync/processors/subscription.d.ts +7 -1
  35. package/lib/sync/processors/subscription.js +196 -135
  36. package/lib/sync/processors/subscription.js.map +1 -1
  37. package/lib/sync/processors/sync.d.ts +1 -1
  38. package/lib/sync/processors/sync.js.map +1 -1
  39. package/lib/sync/utils.d.ts +66 -2
  40. package/lib/sync/utils.js +264 -16
  41. package/lib/sync/utils.js.map +1 -1
  42. package/lib/types.d.ts +9 -1
  43. package/lib/types.js.map +1 -1
  44. package/lib/util.d.ts +16 -0
  45. package/lib/util.js +31 -2
  46. package/lib/util.js.map +1 -1
  47. package/lib-esm/datastore/datastore.d.ts +29 -3
  48. package/lib-esm/datastore/datastore.js +310 -149
  49. package/lib-esm/datastore/datastore.js.map +1 -1
  50. package/lib-esm/predicates/index.d.ts +77 -7
  51. package/lib-esm/predicates/index.js +143 -123
  52. package/lib-esm/predicates/index.js.map +1 -1
  53. package/lib-esm/predicates/next.d.ts +51 -10
  54. package/lib-esm/predicates/next.js +111 -91
  55. package/lib-esm/predicates/next.js.map +1 -1
  56. package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +28 -30
  57. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +138 -535
  58. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  59. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  60. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +28 -29
  61. package/lib-esm/storage/adapter/IndexedDBAdapter.js +489 -884
  62. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  63. package/lib-esm/storage/adapter/StorageAdapterBase.d.ts +134 -0
  64. package/lib-esm/storage/adapter/StorageAdapterBase.js +437 -0
  65. package/lib-esm/storage/adapter/StorageAdapterBase.js.map +1 -0
  66. package/lib-esm/storage/relationship.d.ts +9 -0
  67. package/lib-esm/storage/relationship.js +9 -0
  68. package/lib-esm/storage/relationship.js.map +1 -1
  69. package/lib-esm/storage/storage.d.ts +1 -1
  70. package/lib-esm/storage/storage.js +4 -3
  71. package/lib-esm/storage/storage.js.map +1 -1
  72. package/lib-esm/sync/index.d.ts +15 -1
  73. package/lib-esm/sync/index.js +82 -15
  74. package/lib-esm/sync/index.js.map +1 -1
  75. package/lib-esm/sync/outbox.js +14 -7
  76. package/lib-esm/sync/outbox.js.map +1 -1
  77. package/lib-esm/sync/processors/mutation.d.ts +10 -1
  78. package/lib-esm/sync/processors/mutation.js +33 -12
  79. package/lib-esm/sync/processors/mutation.js.map +1 -1
  80. package/lib-esm/sync/processors/subscription.d.ts +7 -1
  81. package/lib-esm/sync/processors/subscription.js +197 -136
  82. package/lib-esm/sync/processors/subscription.js.map +1 -1
  83. package/lib-esm/sync/processors/sync.d.ts +1 -1
  84. package/lib-esm/sync/processors/sync.js.map +1 -1
  85. package/lib-esm/sync/utils.d.ts +66 -2
  86. package/lib-esm/sync/utils.js +261 -18
  87. package/lib-esm/sync/utils.js.map +1 -1
  88. package/lib-esm/types.d.ts +9 -1
  89. package/lib-esm/types.js.map +1 -1
  90. package/lib-esm/util.d.ts +16 -0
  91. package/lib-esm/util.js +32 -3
  92. package/lib-esm/util.js.map +1 -1
  93. package/package.json +12 -11
  94. package/src/datastore/datastore.ts +288 -159
  95. package/src/predicates/index.ts +145 -175
  96. package/src/predicates/next.ts +114 -81
  97. package/src/storage/adapter/AsyncStorageAdapter.ts +97 -563
  98. package/src/storage/adapter/AsyncStorageDatabase.ts +2 -2
  99. package/src/storage/adapter/IndexedDBAdapter.ts +318 -770
  100. package/src/storage/adapter/StorageAdapterBase.ts +545 -0
  101. package/src/storage/relationship.ts +9 -0
  102. package/src/storage/storage.ts +12 -9
  103. package/src/sync/index.ts +108 -20
  104. package/src/sync/outbox.ts +17 -11
  105. package/src/sync/processors/mutation.ts +35 -4
  106. package/src/sync/processors/subscription.ts +124 -10
  107. package/src/sync/processors/sync.ts +4 -1
  108. package/src/sync/utils.ts +285 -15
  109. package/src/types.ts +15 -2
  110. package/src/util.ts +40 -1
  111. package/CHANGELOG.md +0 -904
@@ -1,9 +1,6 @@
1
1
  import {
2
- AllOperators,
3
2
  ModelPredicate,
4
3
  PersistentModel,
5
- PredicateExpression,
6
- PredicateGroups,
7
4
  PredicatesGroup,
8
5
  ProducerModelPredicate,
9
6
  SchemaModel,
@@ -20,12 +17,42 @@ export function isPredicatesAll(
20
17
  return predicatesAllSet.has(predicate);
21
18
  }
22
19
 
20
+ /**
21
+ * The valid logical grouping keys for a predicate group.
22
+ */
23
23
  const groupKeys = new Set(['and', 'or', 'not']);
24
+
25
+ /**
26
+ * Determines whether an object is a GraphQL style predicate "group", which must be an
27
+ * object containing a single "group key", which then contains the child condition(s).
28
+ *
29
+ * E.g.,
30
+ *
31
+ * ```
32
+ * { and: [ ... ] }
33
+ * { not: { ... } }
34
+ * ```
35
+ *
36
+ * @param o The object to test.
37
+ */
24
38
  const isGroup = o => {
25
39
  const keys = [...Object.keys(o)];
26
40
  return keys.length === 1 && groupKeys.has(keys[0]);
27
41
  };
28
42
 
43
+ /**
44
+ * Determines whether an object specifies no conditions and should match everything,
45
+ * as would be the case with `Predicates.ALL`.
46
+ *
47
+ * @param o The object to test.
48
+ */
49
+ const isEmpty = o => {
50
+ return !Array.isArray(o) && Object.keys(o).length === 0;
51
+ };
52
+
53
+ /**
54
+ * The valid comparison operators that can be used as keys in a predicate comparison object.
55
+ */
29
56
  export const comparisonKeys = new Set([
30
57
  'eq',
31
58
  'ne',
@@ -38,16 +65,29 @@ export const comparisonKeys = new Set([
38
65
  'beginsWith',
39
66
  'between',
40
67
  ]);
68
+
69
+ /**
70
+ * Determines whether an object is a GraphQL style predicate comparison node, which must
71
+ * be an object containing a single "comparison operator" key, which then contains the
72
+ * operand or operands to compare against.
73
+ *
74
+ * @param o The object to test.
75
+ */
41
76
  const isComparison = o => {
42
77
  const keys = [...Object.keys(o)];
43
78
  return !Array.isArray(o) && keys.length === 1 && comparisonKeys.has(keys[0]);
44
79
  };
45
80
 
81
+ /**
82
+ * A light check to determine whether an object is a valid GraphQL Condition AST.
83
+ *
84
+ * @param o The object to test.
85
+ */
46
86
  const isValid = o => {
47
87
  if (Array.isArray(o)) {
48
88
  return o.every(v => isValid(v));
49
89
  } else {
50
- return Object.keys(o).length === 1;
90
+ return Object.keys(o).length <= 1;
51
91
  }
52
92
  };
53
93
 
@@ -65,106 +105,37 @@ export class Predicates {
65
105
  }
66
106
 
67
107
  export class ModelPredicateCreator {
108
+ /**
109
+ * Map of storage predicates (key objects) to storage predicate AST's.
110
+ */
68
111
  private static predicateGroupsMap = new WeakMap<
69
112
  ModelPredicate<any>,
70
113
  PredicatesGroup<any>
71
114
  >();
72
115
 
73
- static createPredicateBuilder<T extends PersistentModel>(
74
- modelDefinition: SchemaModel
75
- ) {
76
- const { name: modelName } = modelDefinition;
77
- const fieldNames = new Set<keyof T>(Object.keys(modelDefinition.fields));
78
-
79
- let handler: ProxyHandler<ModelPredicate<T>>;
80
-
81
- const predicate = new Proxy(
82
- {} as ModelPredicate<T>,
83
- (handler = {
84
- get(
85
- _,
86
- propertyKey,
87
- self: ModelPredicate<T>
88
- ): PredicateExpression<T, any> {
89
- const groupType = propertyKey as keyof PredicateGroups<T>;
90
-
91
- switch (groupType) {
92
- case 'and':
93
- case 'or':
94
- case 'not':
95
- const result: PredicateExpression<T, any> = (
96
- newPredicate: (criteria: ModelPredicate<T>) => ModelPredicate<T>
97
- ) => {
98
- const group: PredicatesGroup<T> = {
99
- type: groupType,
100
- predicates: [],
101
- };
102
-
103
- // Create a new recorder
104
- const tmpPredicateRecorder = new Proxy(
105
- {} as ModelPredicate<T>,
106
- handler
107
- );
108
-
109
- // Set the recorder group
110
- ModelPredicateCreator.predicateGroupsMap.set(
111
- tmpPredicateRecorder as any,
112
- group
113
- );
114
-
115
- // Apply the predicates to the recorder (this is the step that records the changes)
116
- newPredicate(tmpPredicateRecorder);
117
-
118
- // Push the group to the top-level recorder
119
- ModelPredicateCreator.predicateGroupsMap
120
- .get(self as any)!
121
- .predicates.push(group);
122
-
123
- return self;
124
- };
125
-
126
- return result;
127
- default:
128
- // intentionally blank.
129
- }
130
-
131
- const field = propertyKey as keyof T;
132
-
133
- if (!fieldNames.has(field)) {
134
- throw new Error(
135
- `Invalid field for model. field: ${field}, model: ${modelName}`
136
- );
137
- }
138
-
139
- const result: PredicateExpression<T, any> = (
140
- operator: keyof AllOperators,
141
- operand: any
142
- ) => {
143
- ModelPredicateCreator.predicateGroupsMap
144
- .get(self as any)!
145
- .predicates.push({ field, operator, operand });
146
- return self;
147
- };
148
- return result;
149
- },
150
- })
151
- );
152
-
153
- const group: PredicatesGroup<T> = {
154
- type: 'and',
155
- predicates: [],
156
- };
157
- ModelPredicateCreator.predicateGroupsMap.set(predicate as any, group);
158
-
159
- return predicate;
160
- }
161
-
116
+ /**
117
+ * Determines whether the given storage predicate (lookup key) is a predicate
118
+ * key that DataStore recognizes.
119
+ *
120
+ * @param predicate The storage predicate (lookup key) to test.
121
+ */
162
122
  static isValidPredicate<T extends PersistentModel>(
163
123
  predicate: any
164
124
  ): predicate is ModelPredicate<T> {
165
125
  return ModelPredicateCreator.predicateGroupsMap.has(predicate);
166
126
  }
167
127
 
128
+ /**
129
+ * Looks for the storage predicate AST that corresponds to a given storage
130
+ * predicate key.
131
+ *
132
+ * The key must have been created internally by a DataStore utility
133
+ * method, such as `ModelPredicate.createFromAST()`.
134
+ *
135
+ * @param predicate The predicate reference to look up.
136
+ * @param throwOnInvalid Whether to throw an exception if the predicate
137
+ * isn't a valid DataStore predicate.
138
+ */
168
139
  static getPredicates<T extends PersistentModel>(
169
140
  predicate: ModelPredicate<T>,
170
141
  throwOnInvalid: boolean = true
@@ -173,33 +144,17 @@ export class ModelPredicateCreator {
173
144
  throw new Error('The predicate is not valid');
174
145
  }
175
146
 
176
- return ModelPredicateCreator.predicateGroupsMap.get(predicate as any);
177
- }
178
-
179
- // transforms cb-style predicate into Proxy
180
- static createFromExisting<T extends PersistentModel>(
181
- modelDefinition?: SchemaModel,
182
- existing?: ProducerModelPredicate<T>
183
- ) {
184
- if (!existing || !modelDefinition) {
185
- return undefined;
186
- }
187
-
188
- return existing(
189
- ModelPredicateCreator.createPredicateBuilder(modelDefinition)
190
- );
191
- }
192
-
193
- static createForSingleField<T extends PersistentModel>(
194
- modelDefinition: SchemaModel,
195
- fieldName: string,
196
- value: string
197
- ) {
198
- return ModelPredicateCreator.createPredicateBuilder<T>(modelDefinition)[
199
- fieldName
200
- ](<any>'eq', <any>value);
147
+ return ModelPredicateCreator.predicateGroupsMap.get(predicate);
201
148
  }
202
149
 
150
+ /**
151
+ * using the PK values from the given `model` (which can be a partial of T
152
+ * Creates a predicate that matches an instance described by `modelDefinition`
153
+ * that contains only PK field values.)
154
+ *
155
+ * @param modelDefinition The model definition to create a predicate for.
156
+ * @param model The model instance to extract value equalities from.
157
+ */
203
158
  static createForPk<T extends PersistentModel>(
204
159
  modelDefinition: SchemaModel,
205
160
  model: T
@@ -207,15 +162,14 @@ export class ModelPredicateCreator {
207
162
  const keyFields = extractPrimaryKeyFieldNames(modelDefinition);
208
163
  const keyValues = extractPrimaryKeyValues(model, keyFields);
209
164
 
210
- let modelPredicate =
211
- ModelPredicateCreator.createPredicateBuilder<T>(modelDefinition);
212
-
213
- keyFields.forEach((field, idx) => {
214
- const operand = keyValues[idx];
215
- modelPredicate = modelPredicate[field](<any>'eq', <any>operand);
165
+ const predicate = this.createFromAST<T>(modelDefinition, {
166
+ and: keyFields.map((field, idx) => {
167
+ const operand = keyValues[idx];
168
+ return { [field]: { eq: operand } };
169
+ }),
216
170
  });
217
171
 
218
- return modelPredicate;
172
+ return predicate;
219
173
  }
220
174
 
221
175
  /**
@@ -231,53 +185,48 @@ export class ModelPredicateCreator {
231
185
  modelDefinition: SchemaModel,
232
186
  flatEqualities: Record<string, any>
233
187
  ) {
234
- let predicate =
235
- ModelPredicateCreator.createPredicateBuilder<T>(modelDefinition);
236
-
237
- for (const [field, value] of Object.entries(flatEqualities)) {
238
- predicate = predicate[field]('eq' as any, value);
239
- }
240
-
241
- return predicate;
242
- }
243
-
244
- static createGroupFromExisting<T extends PersistentModel>(
245
- modelDefinition: SchemaModel,
246
- group: 'and' | 'or' | 'not',
247
- existingPredicates: (ProducerModelPredicate<T> | ModelPredicate<T>)[]
248
- ) {
249
- let outer =
250
- ModelPredicateCreator.createPredicateBuilder<T>(modelDefinition);
251
-
252
- outer = outer[group](seed => {
253
- let inner = seed;
254
- for (const existing of existingPredicates) {
255
- if (typeof existing === 'function') {
256
- inner = existing(inner);
257
- } else {
258
- ModelPredicateCreator.predicateGroupsMap
259
- .get(inner)
260
- ?.predicates.push(
261
- ModelPredicateCreator.predicateGroupsMap.get(
262
- existing as ModelPredicate<T>
263
- )!
264
- );
265
- }
266
- }
267
- return inner;
268
- });
269
-
270
- return outer;
188
+ const ast = {
189
+ and: Object.entries(flatEqualities).map(([k, v]) => ({ [k]: { eq: v } })),
190
+ };
191
+ return this.createFromAST<T>(modelDefinition, ast);
271
192
  }
272
193
 
273
- static transformGraphQLtoPredicateAST(gql: any) {
194
+ /**
195
+ * Accepts a GraphQL style filter predicate tree and transforms it into an
196
+ * AST that can be used for a storage adapter predicate. Example input:
197
+ *
198
+ * ```js
199
+ * {
200
+ * and: [
201
+ * { name: { eq: "Bob Jones" } },
202
+ * { age: { between: [32, 64] } },
203
+ * { not: {
204
+ * or: [
205
+ * { favoriteFood: { eq: 'pizza' } },
206
+ * { favoriteFood: { eq: 'tacos' } },
207
+ * ]
208
+ * }}
209
+ * ]
210
+ * }
211
+ * ```
212
+ *
213
+ * @param gql GraphQL style filter node.
214
+ */
215
+ static transformGraphQLFilterNodeToPredicateAST(gql: any) {
274
216
  if (!isValid(gql)) {
275
- throw new Error('Invalid QGL AST: ' + gql);
217
+ throw new Error('Invalid GraphQL Condition or subtree: ' + gql);
276
218
  }
277
219
 
278
- if (isGroup(gql)) {
220
+ if (isEmpty(gql)) {
221
+ return {
222
+ type: 'and',
223
+ predicates: [],
224
+ };
225
+ } else if (isGroup(gql)) {
279
226
  const groupkey = Object.keys(gql)[0];
280
- const children = this.transformGraphQLtoPredicateAST(gql[groupkey]);
227
+ const children = this.transformGraphQLFilterNodeToPredicateAST(
228
+ gql[groupkey]
229
+ );
281
230
  return {
282
231
  type: groupkey,
283
232
  predicates: Array.isArray(children) ? children : [children],
@@ -290,29 +239,50 @@ export class ModelPredicateCreator {
290
239
  };
291
240
  } else {
292
241
  if (Array.isArray(gql)) {
293
- return gql.map(o => this.transformGraphQLtoPredicateAST(o));
242
+ return gql.map(o => this.transformGraphQLFilterNodeToPredicateAST(o));
294
243
  } else {
295
244
  const fieldKey = Object.keys(gql)[0];
296
245
  return {
297
246
  field: fieldKey,
298
- ...this.transformGraphQLtoPredicateAST(gql[fieldKey]),
247
+ ...this.transformGraphQLFilterNodeToPredicateAST(gql[fieldKey]),
299
248
  };
300
249
  }
301
250
  }
302
251
  }
303
252
 
304
- static createFromAST(
253
+ /**
254
+ * Accepts a GraphQL style filter predicate tree and transforms it into a predicate
255
+ * that storage adapters understand. Example input:
256
+ *
257
+ * ```js
258
+ * {
259
+ * and: [
260
+ * { name: { eq: "Bob Jones" } },
261
+ * { age: { between: [32, 64] } },
262
+ * { not: {
263
+ * or: [
264
+ * { favoriteFood: { eq: 'pizza' } },
265
+ * { favoriteFood: { eq: 'tacos' } },
266
+ * ]
267
+ * }}
268
+ * ]
269
+ * }
270
+ * ```
271
+ *
272
+ * @param modelDefinition The model that the AST/predicate must be compatible with.
273
+ * @param ast The graphQL style AST that should specify conditions for `modelDefinition`.
274
+ */
275
+ static createFromAST<T extends PersistentModel>(
305
276
  modelDefinition: SchemaModel,
306
277
  ast: any
307
- ): ModelPredicate<any> {
308
- const predicate =
309
- ModelPredicateCreator.createPredicateBuilder(modelDefinition);
278
+ ): ModelPredicate<T> {
279
+ const key = {} as ModelPredicate<T>;
310
280
 
311
281
  ModelPredicateCreator.predicateGroupsMap.set(
312
- predicate,
313
- this.transformGraphQLtoPredicateAST(ast)
282
+ key,
283
+ this.transformGraphQLFilterNodeToPredicateAST(ast)
314
284
  );
315
285
 
316
- return predicate;
286
+ return key;
317
287
  }
318
288
  }