@aws-amplify/data-schema 1.2.9 → 1.3.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.
Files changed (122) hide show
  1. package/dist/cjs/ClientSchema/Core/ClientCustomOperations.js +4 -0
  2. package/dist/cjs/ClientSchema/Core/ClientCustomOperations.js.map +1 -0
  3. package/dist/cjs/{MappedTypes/CustomOperations.js → ClientSchema/Core/ClientCustomType.js} +1 -1
  4. package/dist/cjs/ClientSchema/Core/ClientCustomType.js.map +1 -0
  5. package/dist/cjs/{ClientSchema.js → ClientSchema/Core/ClientEnum.js} +1 -1
  6. package/dist/cjs/ClientSchema/Core/ClientEnum.js.map +1 -0
  7. package/dist/cjs/ClientSchema/Core/ClientModel.js +4 -0
  8. package/dist/cjs/ClientSchema/Core/ClientModel.js.map +1 -0
  9. package/dist/cjs/ClientSchema/Core/ClientSchemaProperty.js +4 -0
  10. package/dist/cjs/ClientSchema/Core/ClientSchemaProperty.js.map +1 -0
  11. package/dist/cjs/ClientSchema/Core/index.js +4 -0
  12. package/dist/cjs/ClientSchema/Core/index.js.map +1 -0
  13. package/dist/cjs/ClientSchema/index.js +4 -0
  14. package/dist/cjs/ClientSchema/index.js.map +1 -0
  15. package/dist/cjs/ClientSchema/utilities/ExtractNestedTypes.js +4 -0
  16. package/dist/cjs/ClientSchema/utilities/ExtractNestedTypes.js.map +1 -0
  17. package/dist/cjs/ClientSchema/utilities/ResolveField.js +4 -0
  18. package/dist/cjs/ClientSchema/utilities/ResolveField.js.map +1 -0
  19. package/dist/cjs/ClientSchema/utilities/ResolveRef.js +4 -0
  20. package/dist/cjs/ClientSchema/utilities/ResolveRef.js.map +1 -0
  21. package/dist/cjs/ClientSchema/utilities/SchemaMetadata.js +4 -0
  22. package/dist/cjs/ClientSchema/utilities/SchemaMetadata.js.map +1 -0
  23. package/dist/cjs/ClientSchema/utilities/index.js +9 -0
  24. package/dist/cjs/ClientSchema/utilities/index.js.map +1 -0
  25. package/dist/cjs/util/Filters.js +10 -0
  26. package/dist/cjs/util/Filters.js.map +1 -0
  27. package/dist/cjs/util/IndexShapes.js +4 -0
  28. package/dist/cjs/util/IndexShapes.js.map +1 -0
  29. package/dist/cjs/util/Select.js +4 -0
  30. package/dist/cjs/util/Select.js.map +1 -0
  31. package/dist/esm/Authorization.d.ts +3 -3
  32. package/dist/esm/ClientSchema/Core/ClientCustomOperations.d.ts +88 -0
  33. package/dist/esm/ClientSchema/Core/ClientCustomOperations.mjs +2 -0
  34. package/dist/esm/ClientSchema/Core/ClientCustomOperations.mjs.map +1 -0
  35. package/dist/esm/ClientSchema/Core/ClientCustomType.d.ts +7 -0
  36. package/dist/esm/ClientSchema/Core/ClientCustomType.mjs +2 -0
  37. package/dist/esm/ClientSchema/Core/ClientCustomType.mjs.map +1 -0
  38. package/dist/esm/ClientSchema/Core/ClientEnum.d.ts +5 -0
  39. package/dist/esm/ClientSchema/Core/ClientEnum.mjs +2 -0
  40. package/dist/esm/ClientSchema/Core/ClientEnum.mjs.map +1 -0
  41. package/dist/esm/ClientSchema/Core/ClientModel.d.ts +123 -0
  42. package/dist/esm/ClientSchema/Core/ClientModel.mjs +2 -0
  43. package/dist/esm/ClientSchema/Core/ClientModel.mjs.map +1 -0
  44. package/dist/esm/ClientSchema/Core/ClientSchemaProperty.d.ts +4 -0
  45. package/dist/esm/ClientSchema/Core/ClientSchemaProperty.mjs +2 -0
  46. package/dist/esm/ClientSchema/Core/ClientSchemaProperty.mjs.map +1 -0
  47. package/dist/esm/ClientSchema/Core/index.d.ts +5 -0
  48. package/dist/esm/ClientSchema/Core/index.mjs +2 -0
  49. package/dist/esm/ClientSchema/Core/index.mjs.map +1 -0
  50. package/dist/esm/ClientSchema/index.d.ts +59 -0
  51. package/dist/esm/ClientSchema/index.mjs +2 -0
  52. package/dist/esm/ClientSchema/index.mjs.map +1 -0
  53. package/dist/esm/ClientSchema/utilities/ExtractNestedTypes.d.ts +7 -0
  54. package/dist/esm/ClientSchema/utilities/ExtractNestedTypes.mjs +2 -0
  55. package/dist/esm/ClientSchema/utilities/ExtractNestedTypes.mjs.map +1 -0
  56. package/dist/esm/ClientSchema/utilities/ResolveField.d.ts +29 -0
  57. package/dist/esm/ClientSchema/utilities/ResolveField.mjs +2 -0
  58. package/dist/esm/ClientSchema/utilities/ResolveField.mjs.map +1 -0
  59. package/dist/esm/ClientSchema/utilities/ResolveRef.d.ts +18 -0
  60. package/dist/esm/ClientSchema/utilities/ResolveRef.mjs +2 -0
  61. package/dist/esm/ClientSchema/utilities/ResolveRef.mjs.map +1 -0
  62. package/dist/esm/ClientSchema/utilities/SchemaMetadata.d.ts +7 -0
  63. package/dist/esm/ClientSchema/utilities/SchemaMetadata.mjs +2 -0
  64. package/dist/esm/ClientSchema/utilities/SchemaMetadata.mjs.map +1 -0
  65. package/dist/esm/ClientSchema/utilities/index.d.ts +4 -0
  66. package/dist/esm/ClientSchema/utilities/index.mjs +2 -0
  67. package/dist/esm/ClientSchema/utilities/index.mjs.map +1 -0
  68. package/dist/esm/MappedTypes/ImplicitFieldInjector.d.ts +1 -1
  69. package/dist/esm/MappedTypes/ModelMetadata.d.ts +1 -1
  70. package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +1 -1
  71. package/dist/esm/ModelSchema.d.ts +6 -3
  72. package/dist/esm/ModelType.d.ts +9 -8
  73. package/dist/esm/ModelType.mjs.map +1 -1
  74. package/dist/esm/runtime/client/index.d.ts +77 -223
  75. package/dist/esm/runtime/internals/generateCustomOperationsProperty.d.ts +12 -5
  76. package/dist/esm/util/Filters.d.ts +94 -0
  77. package/dist/esm/util/Filters.mjs +2 -0
  78. package/dist/esm/util/Filters.mjs.map +1 -0
  79. package/dist/esm/util/IndexShapes.d.ts +13 -0
  80. package/dist/esm/util/IndexShapes.mjs +2 -0
  81. package/dist/esm/util/IndexShapes.mjs.map +1 -0
  82. package/dist/esm/util/Select.d.ts +3 -0
  83. package/dist/esm/util/Select.mjs +2 -0
  84. package/dist/esm/util/Select.mjs.map +1 -0
  85. package/dist/esm/util/index.d.ts +3 -0
  86. package/dist/meta/cjs.tsbuildinfo +1 -1
  87. package/package.json +1 -1
  88. package/src/Authorization.ts +2 -2
  89. package/src/ClientSchema/Core/ClientCustomOperations.ts +152 -0
  90. package/src/ClientSchema/Core/ClientCustomType.ts +11 -0
  91. package/src/ClientSchema/Core/ClientEnum.ts +7 -0
  92. package/src/ClientSchema/Core/ClientModel.ts +299 -0
  93. package/src/ClientSchema/Core/ClientSchemaProperty.ts +10 -0
  94. package/src/ClientSchema/Core/index.ts +5 -0
  95. package/src/ClientSchema/index.ts +158 -0
  96. package/src/ClientSchema/utilities/ExtractNestedTypes.ts +15 -0
  97. package/src/ClientSchema/utilities/ResolveField.ts +79 -0
  98. package/src/ClientSchema/utilities/ResolveRef.ts +38 -0
  99. package/src/ClientSchema/utilities/SchemaMetadata.ts +11 -0
  100. package/src/ClientSchema/utilities/index.ts +4 -0
  101. package/src/MappedTypes/ImplicitFieldInjector.ts +1 -1
  102. package/src/MappedTypes/ModelMetadata.ts +2 -1
  103. package/src/MappedTypes/ResolveFieldProperties.ts +1 -1
  104. package/src/ModelSchema.ts +8 -2
  105. package/src/ModelType.ts +12 -3
  106. package/src/runtime/client/index.ts +175 -433
  107. package/src/runtime/internals/generateCustomOperationsProperty.ts +2 -2
  108. package/src/runtime/internals/utils/clientProperties/generateEnumsProperty.ts +1 -1
  109. package/src/util/Filters.ts +102 -0
  110. package/src/util/IndexShapes.ts +10 -0
  111. package/src/util/Select.ts +3 -0
  112. package/src/util/index.ts +3 -0
  113. package/dist/cjs/ClientSchema.js.map +0 -1
  114. package/dist/cjs/MappedTypes/CustomOperations.js.map +0 -1
  115. package/dist/esm/ClientSchema.d.ts +0 -51
  116. package/dist/esm/ClientSchema.mjs +0 -2
  117. package/dist/esm/ClientSchema.mjs.map +0 -1
  118. package/dist/esm/MappedTypes/CustomOperations.d.ts +0 -125
  119. package/dist/esm/MappedTypes/CustomOperations.mjs +0 -2
  120. package/dist/esm/MappedTypes/CustomOperations.mjs.map +0 -1
  121. package/src/ClientSchema.ts +0 -121
  122. package/src/MappedTypes/CustomOperations.ts +0 -254
@@ -0,0 +1,79 @@
1
+ import { BaseModelField } from '../../ModelField';
2
+ import {
3
+ ModelRelationalField,
4
+ ModelRelationalFieldParamShape,
5
+ } from '../../ModelRelationalField';
6
+ import { EnumType } from '../../EnumType';
7
+ import { CustomType } from '../../CustomType';
8
+ import { RefType, RefTypeParamShape } from '../../RefType';
9
+ import { ResolveRef } from './ResolveRef';
10
+ import { LazyLoader } from '../../runtime';
11
+
12
+ /**
13
+ * Takes a `ReturnType<typeof a.model()>` and turns it into a client-consumable type. Fields
14
+ * definitions (e.g., `a.string()`) are turned into the client facing types (e.g., `string`),
15
+ * `a.ref()` fields will be resolved, and relationships (e.g., `a.belongsTo()`) will be
16
+ * turned into `LazyLoader` fields (e.g., `post.comments({...})`).
17
+ *
18
+ * The first type parameter (`Bag`) should always just be the top-level `ClientSchema` that
19
+ * references and related model definitions can be resolved against.
20
+ */
21
+ export type ResolveFields<Bag extends Record<string, any>, T> = ShallowPretty<
22
+ {
23
+ [K in keyof T as IsRequired<T[K]> extends true
24
+ ? K
25
+ : never]: ResolveIndividualField<Bag, T[K]>;
26
+ } & {
27
+ [K in keyof T as IsRequired<T[K]> extends true
28
+ ? never
29
+ : K]+?: ResolveIndividualField<Bag, T[K]>;
30
+ }
31
+ >;
32
+
33
+ // TODO: Remove ShallowPretty from this layer of resolution. Re-incorporate prettification
34
+ // down the line *as-needed*. Performing this *here* is somehow essential to getting 2 unit
35
+ // tests to pass, but hurts performance significantly. E.g., p50/operations/p50-prod-CRUDL.bench.ts
36
+ // goes from `783705` to `1046408`.
37
+ type ShallowPretty<T> = {
38
+ [K in keyof T]: T[K];
39
+ };
40
+
41
+ export type ResolveIndividualField<Bag extends Record<string, any>, T> =
42
+ T extends BaseModelField<infer FieldShape>
43
+ ? FieldShape
44
+ : T extends RefType<infer RefShape, any, any>
45
+ ? ResolveRef<RefShape, Bag>
46
+ : T extends ModelRelationalField<infer RelationshipShape, any, any, any>
47
+ ? ResolveRelationship<Bag, RelationshipShape>
48
+ : T extends CustomType<infer CT>
49
+ ? ResolveFields<Bag, CT['fields']> | null
50
+ : T extends EnumType<infer values>
51
+ ? values[number] | null
52
+ : never;
53
+
54
+ type ResolveRelationship<
55
+ Bag extends Record<string, any>,
56
+ RelationshipShape extends ModelRelationalFieldParamShape,
57
+ > = LazyLoader<
58
+ RelationshipShape['valueRequired'] extends true
59
+ ? Bag[RelationshipShape['relatedModel']]['type']
60
+ : Bag[RelationshipShape['relatedModel']]['type'] | null,
61
+ RelationshipShape['array']
62
+ >;
63
+
64
+ type IsRequired<T> =
65
+ T extends BaseModelField<infer FieldShape>
66
+ ? null extends FieldShape
67
+ ? false
68
+ : true
69
+ : T extends RefType<infer RefShape, any, any>
70
+ ? IsRefRequired<RefShape>
71
+ : T extends ModelRelationalField<any, any, any, any>
72
+ ? true
73
+ : T extends CustomType<any> | EnumType<any>
74
+ ? false
75
+ : never;
76
+
77
+ type IsRefRequired<T extends RefTypeParamShape> = T['array'] extends true
78
+ ? T['arrayRequired']
79
+ : T['valueRequired'];
@@ -0,0 +1,38 @@
1
+ import type { RefTypeParamShape } from '../../RefType';
2
+
3
+ /**
4
+ * Dereferences an `a.ref()` against a given "bag" of independently resolved types.
5
+ *
6
+ * This util will examine the ref for requiredness and array-ness and wrap/augment
7
+ * to referred-to type accordingly.
8
+ */
9
+ export type ResolveRef<
10
+ RefShape extends RefTypeParamShape,
11
+ RefBag extends Record<string, { __entityType: string; type: unknown }>,
12
+ > = ResolveRefValueArrayTraits<
13
+ RefShape,
14
+ ApplyRequiredness<
15
+ RefShape['link'] extends keyof RefBag
16
+ ? RefBag[RefShape['link']]['type']
17
+ : never,
18
+ RefShape['valueRequired']
19
+ >
20
+ >;
21
+
22
+ type ApplyRequiredness<
23
+ Value,
24
+ MakeRequired extends boolean,
25
+ > = MakeRequired extends true ? Exclude<Value, null> : Value | null | undefined;
26
+
27
+ /**
28
+ * Converts the resolved RefType Value type into Array<> according to the
29
+ * `array` and `arrayRequired` properties of the RefType
30
+ */
31
+ type ResolveRefValueArrayTraits<
32
+ Ref extends RefTypeParamShape,
33
+ Value,
34
+ > = Ref['array'] extends false
35
+ ? Value
36
+ : Ref['arrayRequired'] extends true
37
+ ? Array<Value>
38
+ : Array<Value> | null | undefined;
@@ -0,0 +1,11 @@
1
+ import { ImpliedAuthFields } from '../../Authorization';
2
+ import { BaseSchema } from '../../ModelSchema';
3
+
4
+ export interface SchemaMetadata<Schema extends BaseSchema<any, any>> {
5
+ authFields: AuthFields<Schema>;
6
+ }
7
+
8
+ type AuthFields<Schema extends Record<string, any>> =
9
+ Schema['data']['authorization'][number] extends never
10
+ ? object
11
+ : ImpliedAuthFields<Schema['data']['authorization'][number]>;
@@ -0,0 +1,4 @@
1
+ export * from './ExtractNestedTypes';
2
+ export * from './ResolveField';
3
+ export * from './ResolveRef';
4
+ export * from './SchemaMetadata';
@@ -1,4 +1,4 @@
1
- import type { PrimaryIndexIrShape } from '../runtime/';
1
+ import type { PrimaryIndexIrShape } from '../util';
2
2
 
3
3
  type DefaultIdentifierFields = {
4
4
  // implicit `id` is readonly because it's managed by the resolver; explicit `id` is writable
@@ -2,7 +2,8 @@ import {
2
2
  type UnionToIntersection,
3
3
  type ExcludeEmpty,
4
4
  } from '@aws-amplify/data-schema-types';
5
- import { __modelMeta__, type PrimaryIndexIrShape } from '../runtime/';
5
+ import { __modelMeta__ } from '../runtime/';
6
+ import type { PrimaryIndexIrShape } from '../util';
6
7
  import type { ModelType } from '../ModelType';
7
8
  import type { ModelRelationalFieldParamShape } from '../ModelRelationalField';
8
9
 
@@ -10,7 +10,7 @@ import type {
10
10
  ModelRelationalField,
11
11
  ModelRelationalFieldParamShape,
12
12
  } from '../ModelRelationalField';
13
- import type { PrimaryIndexIrShape } from '../runtime/';
13
+ import type { PrimaryIndexIrShape } from '../util/';
14
14
 
15
15
  import type { ResolveSchema, SchemaTypes } from './ResolveSchema';
16
16
  import type { InjectImplicitModelFields } from './ImplicitFieldInjector';
@@ -31,6 +31,11 @@ import {
31
31
  ModelRelationalFieldParamShape,
32
32
  } from './ModelRelationalField';
33
33
 
34
+ export { ModelType } from './ModelType';
35
+ export { EnumType } from './EnumType';
36
+ export { CustomType } from './CustomType';
37
+ export { CustomOperation } from './CustomOperation';
38
+
34
39
  export const rdsSchemaBrandName = 'RDSSchema';
35
40
  export const rdsSchemaBrand = brand(rdsSchemaBrandName);
36
41
  export type RDSSchemaBrand = Brand<typeof rdsSchemaBrandName>;
@@ -51,10 +56,11 @@ type AddToSchemaContents = Record<string, AddToSchemaContent>;
51
56
 
52
57
  type NonEmpty<T> = keyof T extends never ? never : T;
53
58
 
54
- type ModelSchemaContents = Record<string, SchemaContent>;
59
+ export type ModelSchemaContents = Record<string, SchemaContent>;
60
+
55
61
  type InternalSchemaModels = Record<
56
62
  string,
57
- InternalModel | EnumType<any> | CustomType<any> | InternalCustom
63
+ InternalModel | EnumType | CustomType<any> | InternalCustom
58
64
  >;
59
65
 
60
66
  export type ModelSchemaParamShape = {
package/src/ModelType.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { SetTypeSubArg } from '@aws-amplify/data-schema-types';
2
- import type { PrimaryIndexIrShape, SecondaryIndexIrShape } from './runtime';
3
- import { brand } from './util';
2
+ import {
3
+ type PrimaryIndexIrShape,
4
+ type SecondaryIndexIrShape,
5
+ brand,
6
+ } from './util';
4
7
  import type { InternalField, BaseModelField } from './ModelField';
5
8
  import type {
6
9
  ModelRelationalField,
@@ -352,6 +355,12 @@ export const isSchemaModelType = (
352
355
  );
353
356
  };
354
357
 
358
+ export type ModelDefaultIdentifier = {
359
+ pk: { readonly id: string };
360
+ sk: never;
361
+ compositeSk: never;
362
+ };
363
+
355
364
  /**
356
365
  * A data model that creates a matching Amazon DynamoDB table and provides create, read (list and get), update,
357
366
  * delete, and subscription APIs.
@@ -363,7 +372,7 @@ export function model<T extends ModelFields>(
363
372
  fields: T,
364
373
  ): ModelType<{
365
374
  fields: T;
366
- identifier: { pk: { id: string }; sk: never; compositeSk: never };
375
+ identifier: ModelDefaultIdentifier;
367
376
  secondaryIndexes: [];
368
377
  authorization: [];
369
378
  }> {