@aws-amplify/datastore 3.14.4 → 3.14.5-unstable.2

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 (178) hide show
  1. package/CHANGELOG.md +0 -8
  2. package/lib/authModeStrategies/defaultAuthStrategy.d.ts +2 -0
  3. package/lib/authModeStrategies/index.d.ts +2 -0
  4. package/lib/authModeStrategies/multiAuthStrategy.d.ts +13 -0
  5. package/lib/authModeStrategies/multiAuthStrategy.js +6 -64
  6. package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
  7. package/lib/datastore/datastore.d.ts +207 -0
  8. package/lib/datastore/datastore.js +703 -297
  9. package/lib/datastore/datastore.js.map +1 -1
  10. package/lib/index.d.ts +16 -0
  11. package/lib/index.js +6 -4
  12. package/lib/index.js.map +1 -1
  13. package/lib/predicates/index.d.ts +30 -0
  14. package/lib/predicates/index.js +127 -6
  15. package/lib/predicates/index.js.map +1 -1
  16. package/lib/predicates/next.d.ts +301 -0
  17. package/lib/predicates/next.js +816 -0
  18. package/lib/predicates/next.js.map +1 -0
  19. package/lib/predicates/sort.d.ts +8 -0
  20. package/lib/predicates/sort.js +10 -4
  21. package/lib/predicates/sort.js.map +1 -1
  22. package/lib/ssr/index.d.ts +3 -0
  23. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +42 -0
  24. package/lib/storage/adapter/AsyncStorageAdapter.js +141 -382
  25. package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  26. package/lib/storage/adapter/AsyncStorageDatabase.d.ts +39 -0
  27. package/lib/storage/adapter/AsyncStorageDatabase.js +37 -98
  28. package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  29. package/lib/storage/adapter/InMemoryStore.d.ts +11 -0
  30. package/lib/storage/adapter/InMemoryStore.js +16 -67
  31. package/lib/storage/adapter/InMemoryStore.js.map +1 -1
  32. package/lib/storage/adapter/InMemoryStore.native.d.ts +1 -0
  33. package/lib/storage/adapter/InMemoryStore.native.js +2 -4
  34. package/lib/storage/adapter/InMemoryStore.native.js.map +1 -1
  35. package/lib/storage/adapter/IndexedDBAdapter.d.ts +61 -0
  36. package/lib/storage/adapter/IndexedDBAdapter.js +275 -419
  37. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  38. package/lib/storage/adapter/getDefaultAdapter/index.d.ts +3 -0
  39. package/lib/storage/adapter/getDefaultAdapter/index.js +3 -5
  40. package/lib/storage/adapter/getDefaultAdapter/index.js.map +1 -1
  41. package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +3 -0
  42. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -4
  43. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
  44. package/lib/storage/adapter/index.d.ts +9 -0
  45. package/lib/storage/relationship.d.ts +140 -0
  46. package/lib/storage/relationship.js +335 -0
  47. package/lib/storage/relationship.js.map +1 -0
  48. package/lib/storage/storage.d.ts +50 -0
  49. package/lib/storage/storage.js +72 -143
  50. package/lib/storage/storage.js.map +1 -1
  51. package/lib/sync/datastoreConnectivity.d.ts +16 -0
  52. package/lib/sync/datastoreConnectivity.js +6 -55
  53. package/lib/sync/datastoreConnectivity.js.map +1 -1
  54. package/lib/sync/datastoreReachability/index.d.ts +3 -0
  55. package/lib/sync/datastoreReachability/index.native.d.ts +3 -0
  56. package/lib/sync/datastoreReachability/index.native.js +2 -4
  57. package/lib/sync/datastoreReachability/index.native.js.map +1 -1
  58. package/lib/sync/index.d.ts +89 -0
  59. package/lib/sync/index.js +49 -124
  60. package/lib/sync/index.js.map +1 -1
  61. package/lib/sync/merger.d.ts +17 -0
  62. package/lib/sync/merger.js +8 -74
  63. package/lib/sync/merger.js.map +1 -1
  64. package/lib/sync/outbox.d.ts +27 -0
  65. package/lib/sync/outbox.js +24 -97
  66. package/lib/sync/outbox.js.map +1 -1
  67. package/lib/sync/processors/errorMaps.d.ts +17 -0
  68. package/lib/sync/processors/errorMaps.js +5 -35
  69. package/lib/sync/processors/errorMaps.js.map +1 -1
  70. package/lib/sync/processors/mutation.d.ts +58 -0
  71. package/lib/sync/processors/mutation.js +47 -131
  72. package/lib/sync/processors/mutation.js.map +1 -1
  73. package/lib/sync/processors/subscription.d.ts +33 -0
  74. package/lib/sync/processors/subscription.js +29 -102
  75. package/lib/sync/processors/subscription.js.map +1 -1
  76. package/lib/sync/processors/sync.d.ts +28 -0
  77. package/lib/sync/processors/sync.js +26 -102
  78. package/lib/sync/processors/sync.js.map +1 -1
  79. package/lib/sync/utils.d.ts +42 -0
  80. package/lib/sync/utils.js +40 -103
  81. package/lib/sync/utils.js.map +1 -1
  82. package/lib/types.d.ts +554 -0
  83. package/lib/types.js +9 -39
  84. package/lib/types.js.map +1 -1
  85. package/lib/util.d.ts +189 -0
  86. package/lib/util.js +192 -188
  87. package/lib/util.js.map +1 -1
  88. package/lib-esm/authModeStrategies/multiAuthStrategy.js +2 -57
  89. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
  90. package/lib-esm/datastore/datastore.d.ts +59 -8
  91. package/lib-esm/datastore/datastore.js +642 -234
  92. package/lib-esm/datastore/datastore.js.map +1 -1
  93. package/lib-esm/index.d.ts +3 -2
  94. package/lib-esm/index.js +2 -1
  95. package/lib-esm/index.js.map +1 -1
  96. package/lib-esm/predicates/index.d.ts +16 -2
  97. package/lib-esm/predicates/index.js +128 -7
  98. package/lib-esm/predicates/index.js.map +1 -1
  99. package/lib-esm/predicates/next.d.ts +301 -0
  100. package/lib-esm/predicates/next.js +812 -0
  101. package/lib-esm/predicates/next.js.map +1 -0
  102. package/lib-esm/predicates/sort.js +10 -4
  103. package/lib-esm/predicates/sort.js.map +1 -1
  104. package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +2 -1
  105. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +112 -350
  106. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  107. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +7 -68
  108. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  109. package/lib-esm/storage/adapter/InMemoryStore.d.ts +1 -1
  110. package/lib-esm/storage/adapter/InMemoryStore.js +1 -52
  111. package/lib-esm/storage/adapter/InMemoryStore.js.map +1 -1
  112. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +4 -2
  113. package/lib-esm/storage/adapter/IndexedDBAdapter.js +230 -367
  114. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  115. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +1 -1
  116. package/lib-esm/storage/relationship.d.ts +140 -0
  117. package/lib-esm/storage/relationship.js +333 -0
  118. package/lib-esm/storage/relationship.js.map +1 -0
  119. package/lib-esm/storage/storage.d.ts +7 -6
  120. package/lib-esm/storage/storage.js +33 -101
  121. package/lib-esm/storage/storage.js.map +1 -1
  122. package/lib-esm/sync/datastoreConnectivity.js +1 -47
  123. package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
  124. package/lib-esm/sync/index.js +4 -76
  125. package/lib-esm/sync/index.js.map +1 -1
  126. package/lib-esm/sync/merger.js +1 -67
  127. package/lib-esm/sync/merger.js.map +1 -1
  128. package/lib-esm/sync/outbox.js +1 -74
  129. package/lib-esm/sync/outbox.js.map +1 -1
  130. package/lib-esm/sync/processors/errorMaps.js +2 -32
  131. package/lib-esm/sync/processors/errorMaps.js.map +1 -1
  132. package/lib-esm/sync/processors/mutation.js +12 -93
  133. package/lib-esm/sync/processors/mutation.js.map +1 -1
  134. package/lib-esm/sync/processors/subscription.js +6 -69
  135. package/lib-esm/sync/processors/subscription.js.map +1 -1
  136. package/lib-esm/sync/processors/sync.js +2 -75
  137. package/lib-esm/sync/processors/sync.js.map +1 -1
  138. package/lib-esm/sync/utils.d.ts +1 -1
  139. package/lib-esm/sync/utils.js +32 -95
  140. package/lib-esm/sync/utils.js.map +1 -1
  141. package/lib-esm/types.d.ts +63 -10
  142. package/lib-esm/types.js +7 -38
  143. package/lib-esm/types.js.map +1 -1
  144. package/lib-esm/util.d.ts +39 -6
  145. package/lib-esm/util.js +171 -171
  146. package/lib-esm/util.js.map +1 -1
  147. package/package.json +21 -14
  148. package/src/authModeStrategies/multiAuthStrategy.ts +2 -2
  149. package/src/datastore/datastore.ts +699 -206
  150. package/src/index.ts +4 -0
  151. package/src/predicates/index.ts +143 -17
  152. package/src/predicates/next.ts +967 -0
  153. package/src/predicates/sort.ts +8 -2
  154. package/src/storage/adapter/AsyncStorageAdapter.ts +59 -181
  155. package/src/storage/adapter/AsyncStorageDatabase.ts +16 -15
  156. package/src/storage/adapter/InMemoryStore.ts +5 -2
  157. package/src/storage/adapter/IndexedDBAdapter.ts +169 -192
  158. package/src/storage/adapter/getDefaultAdapter/index.ts +2 -2
  159. package/src/storage/relationship.ts +272 -0
  160. package/src/storage/storage.ts +56 -37
  161. package/src/sync/datastoreConnectivity.ts +4 -4
  162. package/src/sync/index.ts +22 -28
  163. package/src/sync/merger.ts +1 -1
  164. package/src/sync/outbox.ts +6 -6
  165. package/src/sync/processors/errorMaps.ts +1 -1
  166. package/src/sync/processors/mutation.ts +23 -19
  167. package/src/sync/processors/subscription.ts +20 -16
  168. package/src/sync/processors/sync.ts +17 -17
  169. package/src/sync/utils.ts +42 -48
  170. package/src/types.ts +128 -16
  171. package/src/util.ts +108 -150
  172. package/build.js +0 -5
  173. package/dist/aws-amplify-datastore.js +0 -92787
  174. package/dist/aws-amplify-datastore.js.map +0 -1
  175. package/dist/aws-amplify-datastore.min.js +0 -66
  176. package/dist/aws-amplify-datastore.min.js.map +0 -1
  177. package/index.js +0 -7
  178. package/webpack.config.dev.js +0 -6
package/src/types.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ModelInstanceCreator } from './datastore/datastore';
2
2
  import {
3
- exhaustiveCheck,
4
3
  isAWSDate,
5
4
  isAWSTime,
6
5
  isAWSDateTime,
@@ -10,18 +9,27 @@ import {
10
9
  isAWSURL,
11
10
  isAWSPhone,
12
11
  isAWSIPAddress,
12
+ NAMESPACES,
13
13
  extractPrimaryKeyFieldNames,
14
14
  } from './util';
15
15
  import { PredicateAll } from './predicates';
16
16
  import { GRAPHQL_AUTH_MODE } from '@aws-amplify/api-graphql';
17
17
  import { Auth } from '@aws-amplify/auth';
18
18
  import { API } from '@aws-amplify/api';
19
- import Cache from '@aws-amplify/cache';
19
+ import { Cache } from '@aws-amplify/cache';
20
20
  import { Adapter } from './storage/adapter';
21
+ import {
22
+ ModelPredicateExtender,
23
+ PredicateInternalsKey,
24
+ ModelPredicate as V5ModelPredicate,
25
+ } from './predicates/next';
26
+
27
+ export type Scalar<T> = T extends Array<infer InnerType> ? InnerType : T;
21
28
 
22
29
  //#region Schema types
23
30
  export type Schema = UserSchema & {
24
31
  version: string;
32
+ codegenVersion: string;
25
33
  };
26
34
  export type UserSchema = {
27
35
  models: SchemaModels;
@@ -34,6 +42,7 @@ export type UserSchema = {
34
42
  export type InternalSchema = {
35
43
  namespaces: SchemaNamespaces;
36
44
  version: string;
45
+ codegenVersion: string;
37
46
  };
38
47
  export type SchemaNamespaces = Record<string, SchemaNamespace>;
39
48
  export type SchemaNamespace = UserSchema & {
@@ -68,7 +77,11 @@ type SchemaEnum = {
68
77
  name: string;
69
78
  values: string[];
70
79
  };
71
-
80
+ export type ModelMeta<T extends PersistentModel> = {
81
+ builder: PersistentModelConstructor<T>;
82
+ schema: SchemaModel;
83
+ pkField: string[];
84
+ };
72
85
  export type ModelAssociation = AssociatedWith | TargetNameAssociation;
73
86
  type AssociatedWith = {
74
87
  connectionType: 'HAS_MANY' | 'HAS_ONE';
@@ -93,6 +106,16 @@ export function isTargetNameAssociation(
93
106
  return obj?.targetName || obj?.targetNames;
94
107
  }
95
108
 
109
+ type FieldAssociation = {
110
+ connectionType: 'HAS_ONE' | 'BELONGS_TO' | 'HAS_MANY';
111
+ };
112
+ export function isFieldAssociation(
113
+ obj: any,
114
+ fieldName: string
115
+ ): obj is FieldAssociation {
116
+ return obj?.fields[fieldName]?.association?.connectionType;
117
+ }
118
+
96
119
  export type ModelAttributes = ModelAttribute[];
97
120
  export type ModelAttribute = { type: string; properties?: Record<string, any> };
98
121
 
@@ -226,7 +249,7 @@ export namespace GraphQLScalarType {
226
249
  typeof GraphQLScalarType,
227
250
  'getJSType' | 'getValidationFunction'
228
251
  >
229
- ): 'string' | 'number' | 'boolean' | 'object' {
252
+ ) {
230
253
  switch (scalar) {
231
254
  case 'Boolean':
232
255
  return 'boolean';
@@ -247,7 +270,7 @@ export namespace GraphQLScalarType {
247
270
  case 'AWSJSON':
248
271
  return 'object';
249
272
  default:
250
- exhaustiveCheck(scalar as never);
273
+ throw new Error('Invalid scalar type');
251
274
  }
252
275
  }
253
276
 
@@ -256,7 +279,7 @@ export namespace GraphQLScalarType {
256
279
  typeof GraphQLScalarType,
257
280
  'getJSType' | 'getValidationFunction'
258
281
  >
259
- ): ((val: string | number) => boolean) | undefined {
282
+ ): ((val: string) => boolean) | ((val: number) => boolean) | undefined {
260
283
  switch (scalar) {
261
284
  case 'AWSDate':
262
285
  return isAWSDate;
@@ -301,8 +324,13 @@ export function isGraphQLScalarType(
301
324
  return obj && GraphQLScalarType[obj] !== undefined;
302
325
  }
303
326
 
304
- export type ModelFieldType = { model: string };
305
- export function isModelFieldType(obj: any): obj is ModelFieldType {
327
+ export type ModelFieldType = {
328
+ model: string;
329
+ modelConstructor?: ModelMeta<PersistentModel>;
330
+ };
331
+ export function isModelFieldType<T extends PersistentModel>(
332
+ obj: any
333
+ ): obj is ModelFieldType {
306
334
  const modelField: keyof ModelFieldType = 'model';
307
335
  if (obj && obj[modelField]) return true;
308
336
 
@@ -358,6 +386,24 @@ export type PersistentModelConstructor<T extends PersistentModel> = {
358
386
  ): T;
359
387
  };
360
388
 
389
+ /**
390
+ * @private
391
+ * Internal use of Amplify only.
392
+ *
393
+ * Indicates to use lazy models or eager models.
394
+ */
395
+ export declare class LazyLoadingDisabled {
396
+ disabled: true;
397
+ }
398
+
399
+ /**
400
+ * @private
401
+ * Internal use of Amplify only.
402
+ *
403
+ * Indicates to use lazy models or eager models.
404
+ */
405
+ export declare class LazyLoading {}
406
+
361
407
  export type TypeConstructorMap = Record<
362
408
  string,
363
409
  PersistentModelConstructor<any> | NonModelTypeConstructor<unknown>
@@ -439,6 +485,53 @@ export type PersistentModelMetaData<T> = {
439
485
  readOnlyFields?: string;
440
486
  };
441
487
 
488
+ export interface AsyncCollection<T> extends AsyncIterable<T> {
489
+ toArray(options?: { max?: number }): Promise<T[]>;
490
+ }
491
+
492
+ export type SettableFieldType<T> = T extends Promise<infer InnerPromiseType>
493
+ ? undefined extends InnerPromiseType
494
+ ? InnerPromiseType | null
495
+ : InnerPromiseType
496
+ : T extends AsyncCollection<infer InnerCollectionType>
497
+ ? InnerCollectionType[] | undefined
498
+ : undefined extends T
499
+ ? T | null
500
+ : T;
501
+
502
+ export type PredicateFieldType<T> = NonNullable<
503
+ Scalar<
504
+ T extends Promise<infer InnerPromiseType>
505
+ ? InnerPromiseType
506
+ : T extends AsyncCollection<infer InnerCollectionType>
507
+ ? InnerCollectionType
508
+ : T
509
+ >
510
+ >;
511
+
512
+ type KeysOfType<T, FilterType> = {
513
+ [P in keyof T]: T[P] extends FilterType ? P : never;
514
+ }[keyof T];
515
+
516
+ type KeysOfSuperType<T, FilterType> = {
517
+ [P in keyof T]: FilterType extends T[P] ? P : never;
518
+ }[keyof T];
519
+
520
+ type OptionalRelativesOf<T> =
521
+ | KeysOfType<T, AsyncCollection<any>>
522
+ | KeysOfSuperType<T, Promise<undefined>>;
523
+
524
+ type OmitOptionalRelatives<T> = Omit<T, OptionalRelativesOf<T>>;
525
+ type PickOptionalRelatives<T> = Pick<T, OptionalRelativesOf<T>>;
526
+ type OmitOptionalFields<T> = Omit<
527
+ T,
528
+ KeysOfSuperType<T, undefined> | OptionalRelativesOf<T>
529
+ >;
530
+ type PickOptionalFields<T> = Pick<
531
+ T,
532
+ KeysOfSuperType<T, undefined> | OptionalRelativesOf<T>
533
+ >;
534
+
442
535
  export type DefaultPersistentModelMetaData = {
443
536
  identifier: ManagedIdentifier<{ id: string }, 'id'>;
444
537
  readOnlyFields: never;
@@ -468,7 +561,7 @@ export type MetadataReadOnlyFields<
468
561
  // This type omits readOnlyFields in the constructor init object
469
562
  // This type requires some identifiers in the constructor init object (e.g. CustomIdentifier)
470
563
  // This type makes optional some identifiers in the constructor init object (e.g. OptionallyManagedIdentifier)
471
- export type ModelInit<
564
+ export type ModelInitBase<
472
565
  T extends PersistentModel,
473
566
  M extends PersistentModelMetaData<T> = {}
474
567
  > = Omit<
@@ -482,9 +575,26 @@ export type ModelInit<
482
575
  ? Partial<Pick<T, IdentifierFieldsForInit<T, M>>>
483
576
  : Required<Pick<T, IdentifierFieldsForInit<T, M>>>);
484
577
 
578
+ export type ModelInit<
579
+ T extends PersistentModel,
580
+ M extends PersistentModelMetaData<T> = {}
581
+ > = {
582
+ [P in keyof OmitOptionalRelatives<ModelInitBase<T, M>>]: SettableFieldType<
583
+ ModelInitBase<T, M>[P]
584
+ >;
585
+ } & {
586
+ [P in keyof PickOptionalRelatives<ModelInitBase<T, M>>]+?: SettableFieldType<
587
+ ModelInitBase<T, M>[P]
588
+ >;
589
+ };
590
+
485
591
  type DeepWritable<T> = {
486
592
  -readonly [P in keyof T]: T[P] extends TypeName<T[P]>
487
593
  ? T[P]
594
+ : T[P] extends Promise<infer InnerPromiseType>
595
+ ? InnerPromiseType
596
+ : T[P] extends AsyncCollection<infer InnerCollectionType>
597
+ ? InnerCollectionType[] | undefined
488
598
  : DeepWritable<T[P]>;
489
599
  };
490
600
 
@@ -753,10 +863,10 @@ export type SystemComponent = {
753
863
  namespaceResolver: NamespaceResolver,
754
864
  modelInstanceCreator: ModelInstanceCreator,
755
865
  getModelConstructorByModelName: (
756
- namsespaceName: string,
866
+ namsespaceName: NAMESPACES,
757
867
  modelName: string
758
868
  ) => PersistentModelConstructor<any>,
759
- appId: string
869
+ appId?: string
760
870
  ): Promise<void>;
761
871
  };
762
872
 
@@ -890,16 +1000,18 @@ syncExpressions: [
890
1000
  }),
891
1001
  ]
892
1002
  */
1003
+
893
1004
  type Option0 = [];
894
- type Option1<T extends PersistentModel> = [ModelPredicate<T> | undefined];
1005
+ type Option1<T extends PersistentModel> = [V5ModelPredicate<T> | undefined];
895
1006
  type Option<T extends PersistentModel> = Option0 | Option1<T>;
896
1007
 
897
1008
  type Lookup<T extends PersistentModel> = {
898
1009
  0:
899
- | ProducerModelPredicate<T>
900
- | Promise<ProducerModelPredicate<T>>
901
- | typeof PredicateAll;
902
- 1: ModelPredicate<T> | undefined;
1010
+ | ModelPredicateExtender<T>
1011
+ | Promise<ModelPredicateExtender<T>>
1012
+ | typeof PredicateAll
1013
+ | Promise<typeof PredicateAll | symbol>;
1014
+ 1: PredicateInternalsKey | undefined;
903
1015
  };
904
1016
 
905
1017
  type ConditionProducer<T extends PersistentModel, A extends Option<T>> = (
package/src/util.ts CHANGED
@@ -7,7 +7,6 @@ import {
7
7
  AllOperators,
8
8
  isPredicateGroup,
9
9
  isPredicateObj,
10
- ModelInstanceMetadata,
11
10
  PersistentModel,
12
11
  PersistentModelConstructor,
13
12
  PredicateGroups,
@@ -24,6 +23,7 @@ import {
24
23
  isModelAttributePrimaryKey,
25
24
  isModelAttributeCompositeKey,
26
25
  NonModelTypeConstructor,
26
+ PaginationInput,
27
27
  DeferredCallbackResolverOptions,
28
28
  LimitTimerRaceResolvedValues,
29
29
  SchemaModel,
@@ -32,6 +32,7 @@ import {
32
32
  ModelAssociation,
33
33
  } from './types';
34
34
  import { WordArray } from 'amazon-cognito-identity-js';
35
+ import { ModelSortPredicateCreator } from './predicates';
35
36
 
36
37
  export const ID = 'id';
37
38
 
@@ -110,7 +111,7 @@ export const validatePredicate = <T extends PersistentModel>(
110
111
  filterType = 'some';
111
112
  break;
112
113
  default:
113
- exhaustiveCheck(groupType);
114
+ throw new Error(`Invalid ${groupType}`);
114
115
  }
115
116
 
116
117
  const result: boolean = predicatesOrGroups[filterType](predicateOrGroup => {
@@ -169,7 +170,6 @@ export const validatePredicateField = <T>(
169
170
  (<string>(<unknown>value)).indexOf(<string>(<unknown>operand)) === -1
170
171
  );
171
172
  default:
172
- exhaustiveCheck(operator, false);
173
173
  return false;
174
174
  }
175
175
  };
@@ -202,155 +202,22 @@ export const traverseModel = <T extends PersistentModel>(
202
202
  namespace: SchemaNamespace,
203
203
  modelInstanceCreator: ModelInstanceCreator,
204
204
  getModelConstructorByModelName: (
205
- namsespaceName: string,
205
+ namsespaceName: NAMESPACES,
206
206
  modelName: string
207
207
  ) => PersistentModelConstructor<any>
208
208
  ) => {
209
- const relationships = namespace.relationships;
210
-
211
209
  const modelConstructor = getModelConstructorByModelName(
212
- namespace.name,
210
+ namespace.name as NAMESPACES,
213
211
  srcModelName
214
212
  );
215
213
 
216
- const relation = relationships[srcModelName];
217
-
218
214
  const result: {
219
215
  modelName: string;
220
216
  item: T;
221
217
  instance: T;
222
218
  }[] = [];
223
219
 
224
- const newInstance = modelConstructor.copyOf(instance, draftInstance => {
225
- relation.relationTypes.forEach((rItem: RelationType) => {
226
- const modelConstructor = getModelConstructorByModelName(
227
- namespace.name,
228
- rItem.modelName
229
- );
230
-
231
- switch (rItem.relationType) {
232
- case 'HAS_ONE':
233
- if (instance[rItem.fieldName]) {
234
- let modelInstance: T;
235
- try {
236
- modelInstance = modelInstanceCreator(
237
- modelConstructor,
238
- instance[rItem.fieldName]
239
- );
240
- } catch (error) {
241
- // Do nothing
242
- console.log(error);
243
- }
244
-
245
- result.push({
246
- modelName: rItem.modelName,
247
- item: instance[rItem.fieldName],
248
- instance: modelInstance,
249
- });
250
-
251
- const targetNames: string[] | undefined =
252
- extractTargetNamesFromSrc(rItem);
253
-
254
- // `targetName` will be defined for Has One if feature flag
255
- // https://docs.amplify.aws/cli/reference/feature-flags/#useAppsyncModelgenPlugin
256
- // is true (default as of 5/7/21)
257
- // Making this conditional for backward-compatibility
258
- if (targetNames) {
259
- targetNames.forEach((targetName, idx) => {
260
- // Get the connected record
261
- const relatedRecordInProxy = <PersistentModel>(
262
- draftInstance[rItem.fieldName]
263
- );
264
-
265
- // Previously, we used the hardcoded 'id' as they key,
266
- // now we need the value of the key to get the PK (and SK)
267
- // values from the related record
268
-
269
- const { primaryKey } = namespace.keys[modelConstructor.name];
270
- const keyField = primaryKey && primaryKey[idx];
271
-
272
- // Get the value
273
- const relatedRecordInProxyPkValue =
274
- relatedRecordInProxy[keyField];
275
-
276
- // Set the targetName value
277
- (<any>draftInstance)[targetName] = relatedRecordInProxyPkValue;
278
- });
279
- // Delete the instance from the proxy
280
- delete (<any>draftInstance)[rItem.fieldName];
281
- } else {
282
- (<any>draftInstance)[rItem.fieldName] = (<PersistentModel>(
283
- draftInstance[rItem.fieldName]
284
- )).id;
285
- }
286
- }
287
-
288
- break;
289
- case 'BELONGS_TO':
290
- if (instance[rItem.fieldName]) {
291
- let modelInstance: T;
292
- try {
293
- modelInstance = modelInstanceCreator(
294
- modelConstructor,
295
- instance[rItem.fieldName]
296
- );
297
- } catch (error) {
298
- // Do nothing
299
- }
300
-
301
- const isDeleted = (<ModelInstanceMetadata>(
302
- draftInstance[rItem.fieldName]
303
- ))._deleted;
304
-
305
- if (!isDeleted) {
306
- result.push({
307
- modelName: rItem.modelName,
308
- item: instance[rItem.fieldName],
309
- instance: modelInstance,
310
- });
311
- }
312
- }
313
-
314
- if (draftInstance[rItem.fieldName]) {
315
- const targetNames: string[] | undefined =
316
- extractTargetNamesFromSrc(rItem);
317
-
318
- if (targetNames) {
319
- targetNames.forEach((targetName, idx) => {
320
- // Get the connected record
321
- const relatedRecordInProxy = <PersistentModel>(
322
- draftInstance[rItem.fieldName]
323
- );
324
- // Previously, we used the hardcoded `id` for the key.
325
- // Now, we need the value of the key to get the PK (and SK)
326
- // values from the related record
327
- const { primaryKey } = namespace.keys[modelConstructor.name];
328
-
329
- // fall back to ID if
330
- const keyField = primaryKey && primaryKey[idx];
331
-
332
- // Get the value
333
- const relatedRecordInProxyPkValue =
334
- relatedRecordInProxy[keyField];
335
-
336
- // Set the targetName value
337
- (<any>draftInstance)[targetName] = relatedRecordInProxyPkValue;
338
- });
339
- // Delete the instance from the proxy
340
- delete (<any>draftInstance)[rItem.fieldName];
341
- }
342
- }
343
-
344
- break;
345
- case 'HAS_MANY':
346
- // Intentionally blank
347
- break;
348
- default:
349
- exhaustiveCheck(rItem.relationType);
350
- break;
351
- }
352
- });
353
- });
220
+ const newInstance = modelConstructor.copyOf(instance, () => {});
354
221
 
355
222
  result.unshift({
356
223
  modelName: srcModelName,
@@ -361,7 +228,7 @@ export const traverseModel = <T extends PersistentModel>(
361
228
  if (!topologicallySortedModels.has(namespace)) {
362
229
  topologicallySortedModels.set(
363
230
  namespace,
364
- Array.from(namespace.modelTopologicalOrdering.keys())
231
+ Array.from(namespace.modelTopologicalOrdering!.keys())
365
232
  );
366
233
  }
367
234
 
@@ -369,7 +236,7 @@ export const traverseModel = <T extends PersistentModel>(
369
236
 
370
237
  result.sort((a, b) => {
371
238
  return (
372
- sortedModels.indexOf(a.modelName) - sortedModels.indexOf(b.modelName)
239
+ sortedModels!.indexOf(a.modelName) - sortedModels!.indexOf(b.modelName)
373
240
  );
374
241
  });
375
242
 
@@ -618,8 +485,8 @@ export function valuesEqual(
618
485
  }
619
486
 
620
487
  if (a instanceof Map && b instanceof Map) {
621
- a = Object.fromEntries(a);
622
- b = Object.fromEntries(b);
488
+ a = (Object as any).fromEntries(a);
489
+ b = (Object as any).fromEntries(b);
623
490
  }
624
491
 
625
492
  const aKeys = Object.keys(a);
@@ -647,6 +514,97 @@ export function valuesEqual(
647
514
  return true;
648
515
  }
649
516
 
517
+ /**
518
+ * Statelessly extracts the specified page from an array.
519
+ *
520
+ * @param records - The source array to extract a page from.
521
+ * @param pagination - A definition of the page to extract.
522
+ * @returns This items from `records` matching the `pagination` definition.
523
+ */
524
+ export function inMemoryPagination<T extends PersistentModel>(
525
+ records: T[],
526
+ pagination?: PaginationInput<T>
527
+ ): T[] {
528
+ if (pagination && records.length > 1) {
529
+ if (pagination.sort) {
530
+ const sortPredicates = ModelSortPredicateCreator.getPredicates(
531
+ pagination.sort
532
+ );
533
+
534
+ if (sortPredicates.length) {
535
+ const compareFn = sortCompareFunction(sortPredicates);
536
+ records.sort(compareFn);
537
+ }
538
+ }
539
+ const { page = 0, limit = 0 } = pagination;
540
+ const start = Math.max(0, page * limit) || 0;
541
+
542
+ const end = limit > 0 ? start + limit : records.length;
543
+
544
+ return records.slice(start, end);
545
+ }
546
+ return records;
547
+ }
548
+
549
+ /**
550
+ * An `aysnc` implementation of `Array.some()`. Returns as soon as a match is found.
551
+ * @param items The items to check.
552
+ * @param matches The async matcher function, expected to
553
+ * return Promise<boolean>: `true` for a matching item, `false` otherwise.
554
+ * @returns A `Promise<boolean>`, `true` if "some" items match; `false` otherwise.
555
+ */
556
+ export async function asyncSome(
557
+ items: Record<string, any>[],
558
+ matches: (item: Record<string, any>) => Promise<boolean>
559
+ ): Promise<boolean> {
560
+ for (const item of items) {
561
+ if (await matches(item)) {
562
+ return true;
563
+ }
564
+ }
565
+ return false;
566
+ }
567
+
568
+ /**
569
+ * An `aysnc` implementation of `Array.every()`. Returns as soon as a non-match is found.
570
+ * @param items The items to check.
571
+ * @param matches The async matcher function, expected to
572
+ * return Promise<boolean>: `true` for a matching item, `false` otherwise.
573
+ * @returns A `Promise<boolean>`, `true` if every item matches; `false` otherwise.
574
+ */
575
+ export async function asyncEvery(
576
+ items: Record<string, any>[],
577
+ matches: (item: Record<string, any>) => Promise<boolean>
578
+ ): Promise<boolean> {
579
+ for (const item of items) {
580
+ if (!(await matches(item))) {
581
+ return false;
582
+ }
583
+ }
584
+ return true;
585
+ }
586
+
587
+ /**
588
+ * An `async` implementation of `Array.filter()`. Returns after all items have been filtered.
589
+ * TODO: Return AsyncIterable.
590
+ * @param items The items to filter.
591
+ * @param matches The `async` matcher function, expected to
592
+ * return Promise<boolean>: `true` for a matching item, `false` otherwise.
593
+ * @returns A `Promise<T>` of matching items.
594
+ */
595
+ export async function asyncFilter<T>(
596
+ items: T[],
597
+ matches: (item: T) => Promise<boolean>
598
+ ): Promise<T[]> {
599
+ const results: T[] = [];
600
+ for (const item of items) {
601
+ if (await matches(item)) {
602
+ results.push(item);
603
+ }
604
+ }
605
+ return results;
606
+ }
607
+
650
608
  export const isAWSDate = (val: string): boolean => {
651
609
  return !!/^\d{4}-\d{2}-\d{2}(Z|[+-]\d{2}:\d{2}($|:\d{2}))?$/.exec(val);
652
610
  };
@@ -761,7 +719,7 @@ export class DeferredCallbackResolver {
761
719
  this.raceInFlight = false;
762
720
  this.limitPromise = new DeferredPromise();
763
721
 
764
- return winner;
722
+ return winner!;
765
723
  }
766
724
  }
767
725
 
@@ -811,7 +769,7 @@ export function mergePatches<T>(
811
769
  patches = p;
812
770
  }
813
771
  );
814
- return patches;
772
+ return patches!;
815
773
  }
816
774
 
817
775
  export const getStorename = (namespace: string, modelName: string) => {
@@ -987,14 +945,14 @@ export const establishRelationAndKeys = (
987
945
  typeof fieldAttribute.type === 'object' &&
988
946
  'model' in fieldAttribute.type
989
947
  ) {
990
- const connectionType = fieldAttribute.association.connectionType;
948
+ const connectionType = fieldAttribute.association!.connectionType;
991
949
  relationship[mKey].relationTypes.push({
992
950
  fieldName: fieldAttribute.name,
993
951
  modelName: fieldAttribute.type.model,
994
952
  relationType: connectionType,
995
- targetName: fieldAttribute.association['targetName'],
996
- targetNames: fieldAttribute.association['targetNames'],
997
- associatedWith: fieldAttribute.association['associatedWith'],
953
+ targetName: fieldAttribute.association!['targetName'],
954
+ targetNames: fieldAttribute.association!['targetNames'],
955
+ associatedWith: fieldAttribute.association!['associatedWith'],
998
956
  });
999
957
 
1000
958
  if (connectionType === 'BELONGS_TO') {
@@ -1130,7 +1088,7 @@ export const getIndexKeys = (
1130
1088
  namespace: SchemaNamespace,
1131
1089
  modelName: string
1132
1090
  ): string[] => {
1133
- const keyPath = namespace?.keys[modelName]?.primaryKey;
1091
+ const keyPath = namespace?.keys?.[modelName]?.primaryKey;
1134
1092
 
1135
1093
  if (keyPath) {
1136
1094
  return keyPath;
package/build.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- const build = require('../../scripts/build');
4
-
5
- build(process.argv[2], process.argv[3]);