@aws-amplify/data-schema 1.25.6 → 1.26.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/data-schema",
3
- "version": "1.25.6",
3
+ "version": "1.26.0",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -65,7 +65,7 @@
65
65
  "rimraf": "^5.0.5",
66
66
  "rollup": "^4.59.0",
67
67
  "ts-jest": "^29.1.1",
68
- "turbo": "^1.10.14",
68
+ "turbo": "^2.9.14",
69
69
  "typescript": "^5.1.6"
70
70
  },
71
71
  "files": [
@@ -33,7 +33,7 @@ import type {
33
33
 
34
34
  export interface ClientModel<
35
35
  Bag extends Record<string, unknown>,
36
- Metadata extends SchemaMetadata<any>,
36
+ Metadata extends SchemaMetadata<any, any>,
37
37
  IsRDS extends boolean,
38
38
  T extends ModelTypeParamShape,
39
39
  K extends keyof Bag & string,
@@ -92,7 +92,7 @@ type DisabledOpsToMap<Ops extends ReadonlyArray<DisableOperationsOptions>> = {
92
92
 
93
93
  type ClientFields<
94
94
  Bag extends Record<string, unknown>,
95
- Metadata extends SchemaMetadata<any>,
95
+ Metadata extends SchemaMetadata<any, any>,
96
96
  IsRDS extends boolean,
97
97
  T extends ModelTypeParamShape,
98
98
  > = ResolveFields<Bag, T['fields']> &
@@ -102,11 +102,16 @@ type ClientFields<
102
102
 
103
103
  type FlatClientFields<
104
104
  Bag extends Record<string, unknown>,
105
- Metadata extends SchemaMetadata<any>,
105
+ Metadata extends SchemaMetadata<any, any>,
106
106
  IsRDS extends boolean,
107
107
  T extends ModelTypeParamShape,
108
108
  ModelName extends keyof Bag & string,
109
- > = FlatResolveFields<Bag, T['fields'], ModelName> &
109
+ > = FlatResolveFields<
110
+ Bag,
111
+ T['fields'],
112
+ ModelName,
113
+ Metadata['selectionSetDepth']
114
+ > &
110
115
  If<Not<IsRDS>, ImplicitIdentifier<T>> &
111
116
  AuthFields<Metadata, T> &
112
117
  Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>;
@@ -169,7 +174,7 @@ export type ListOptionsPkParams<
169
174
  : Prettify<Partial<IndexQueryInput<Bag, T['identifier']>>>;
170
175
 
171
176
  type AuthFields<
172
- SchemaMeta extends SchemaMetadata<any>,
177
+ SchemaMeta extends SchemaMetadata<any, any>,
173
178
  Model extends ModelTypeParamShape,
174
179
  > = (Model['authorization'][number] extends never
175
180
  ? SchemaMeta['authFields'] extends never
@@ -1,6 +1,7 @@
1
1
  import type { __modelMeta__ } from '../runtime/client';
2
2
  import type {
3
3
  BaseSchema,
4
+ ClientSchemaOptions,
4
5
  ConversationType,
5
6
  CustomOperation,
6
7
  CustomType,
@@ -24,18 +25,22 @@ import type { SchemaMetadata } from './utilities/SchemaMetadata';
24
25
  import type { Brand, Select, SpreadTuple } from '../util';
25
26
  import { ClientConversation } from './ai/ClientConversation';
26
27
 
28
+ export type { ClientSchemaOptions } from '../ModelSchema';
29
+
27
30
  export type ClientSchema<
28
31
  Schema extends GenericModelSchema<any> | CombinedModelSchema<any>,
32
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
29
33
  > =
30
34
  Schema extends GenericModelSchema<any>
31
- ? InternalClientSchema<Schema>
35
+ ? InternalClientSchema<Schema, Options>
32
36
  : Schema extends CombinedModelSchema<any>
33
- ? InternalCombinedSchema<Schema>
37
+ ? InternalCombinedSchema<Schema, Options>
34
38
  : never;
35
39
 
36
40
  type InternalClientSchema<
37
41
  CustomerSchema extends ModelSchemaContents | BaseSchema<any, any>,
38
- Metadata extends SchemaMetadata<any> = never,
42
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
43
+ Metadata extends SchemaMetadata<any, any> = never,
39
44
  IsRDS extends boolean = never,
40
45
  > = CustomerSchema extends ModelSchemaContents
41
46
  ? {
@@ -48,14 +53,15 @@ type InternalClientSchema<
48
53
  : CustomerSchema extends BaseSchema<any, any>
49
54
  ? InternalClientSchema<
50
55
  CustomerSchema['data']['types'],
51
- SchemaMetadata<CustomerSchema>,
56
+ Options,
57
+ SchemaMetadata<CustomerSchema, Options>,
52
58
  CustomerSchema extends Brand<'RDSSchema'> ? true : false
53
59
  >
54
60
  : never;
55
61
 
56
62
  type ClientSchemaProperty<
57
63
  T extends ModelSchemaContents,
58
- Metadata extends SchemaMetadata<any>,
64
+ Metadata extends SchemaMetadata<any, any>,
59
65
  IsRDS extends boolean,
60
66
  K extends keyof T & string,
61
67
  > =
@@ -81,34 +87,34 @@ type RemapEnum<_T extends ModelSchemaContents, E> =
81
87
 
82
88
  type RemapCustomType<
83
89
  T extends ModelSchemaContents,
84
- Metadata extends SchemaMetadata<any>,
90
+ Metadata extends SchemaMetadata<any, any>,
85
91
  IsRDS extends boolean,
86
92
  E,
87
93
  > =
88
94
  E extends CustomType<infer CT>
89
- ? ClientCustomType<InternalClientSchema<T, Metadata, IsRDS>, CT>
95
+ ? ClientCustomType<InternalClientSchema<T, ClientSchemaOptions, Metadata, IsRDS>, CT>
90
96
  : never;
91
97
 
92
98
  type RemapCustomOperation<
93
99
  T extends ModelSchemaContents,
94
- Metadata extends SchemaMetadata<any>,
100
+ Metadata extends SchemaMetadata<any, any>,
95
101
  IsRDS extends boolean,
96
102
  E,
97
103
  > =
98
104
  E extends CustomOperation<infer CO, any>
99
- ? ClientCustomOperation<InternalClientSchema<T, Metadata, IsRDS>, CO>
105
+ ? ClientCustomOperation<InternalClientSchema<T, ClientSchemaOptions, Metadata, IsRDS>, CO>
100
106
  : never;
101
107
 
102
108
  type RemapModel<
103
109
  T extends ModelSchemaContents,
104
- Metadata extends SchemaMetadata<any>,
110
+ Metadata extends SchemaMetadata<any, any>,
105
111
  IsRDS extends boolean,
106
112
  E,
107
113
  K extends keyof T & string,
108
114
  > =
109
115
  E extends ModelType<infer MT, any>
110
116
  ? ClientModel<
111
- InternalClientSchema<T, Metadata, IsRDS>,
117
+ InternalClientSchema<T, ClientSchemaOptions, Metadata, IsRDS>,
112
118
  Metadata,
113
119
  IsRDS,
114
120
  MT,
@@ -121,14 +127,19 @@ type RemapAIRoute<
121
127
  E,
122
128
  > = E extends ConversationType ? ClientConversation : never;
123
129
 
124
- type GetInternalClientSchema<Schema> =
125
- Schema extends GenericModelSchema<any> ? InternalClientSchema<Schema> : never;
130
+ type GetInternalClientSchema<
131
+ Schema,
132
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
133
+ > = Schema extends GenericModelSchema<any>
134
+ ? InternalClientSchema<Schema, Options>
135
+ : never;
126
136
 
127
137
  type CombinedClientSchemas<
128
138
  Schemas extends CombinedModelSchema<any>['schemas'],
139
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
129
140
  > = {
130
141
  [Index in keyof Schemas]: Index extends CombinedSchemaIndexesUnion
131
- ? GetInternalClientSchema<Schemas[Index]>
142
+ ? GetInternalClientSchema<Schemas[Index], Options>
132
143
  : never;
133
144
  };
134
145
 
@@ -142,7 +153,8 @@ type CombinedClientSchemas<
142
153
  */
143
154
  type InternalCombinedSchema<
144
155
  Combined extends CombinedModelSchema<any>,
145
- ClientSchemas extends [...any] = CombinedClientSchemas<Combined['schemas']>,
156
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
157
+ ClientSchemas extends [...any] = CombinedClientSchemas<Combined['schemas'], Options>,
146
158
  > = SpreadTuple<{
147
159
  [I in keyof ClientSchemas]: I extends CombinedSchemaIndexesUnion
148
160
  ? Omit<ClientSchemas[I], typeof __modelMeta__>
@@ -8,6 +8,7 @@ import { CustomType } from '../../CustomType';
8
8
  import { RefType, RefTypeParamShape } from '../../RefType';
9
9
  import { ResolveRef } from './ResolveRef';
10
10
  import { LazyLoader } from '../../runtime';
11
+ import type { DefaultSelectionSetDepth } from '../../ModelSchema';
11
12
  import type { ModelTypeParamShape } from '../../ModelType';
12
13
 
13
14
  type ExtendsNever<T> = [T] extends [never] ? true : false;
@@ -37,8 +38,9 @@ export type FlatResolveFields<
37
38
  Bag extends Record<string, any>,
38
39
  T,
39
40
  FlatModelName extends keyof Bag & string = never,
41
+ Depth extends number = DefaultSelectionSetDepth,
40
42
  > = ShallowPretty<{
41
- [K in keyof T]: ResolveIndividualField<Bag, T[K], FlatModelName>;
43
+ [K in keyof T]: ResolveIndividualField<Bag, T[K], FlatModelName, Depth>;
42
44
  }>;
43
45
 
44
46
  // TODO: Remove ShallowPretty from this layer of resolution. Re-incorporate prettification
@@ -53,13 +55,14 @@ export type ResolveIndividualField<
53
55
  Bag extends Record<string, any>,
54
56
  T,
55
57
  FlatModelName extends keyof Bag & string = never,
58
+ Depth extends number = DefaultSelectionSetDepth,
56
59
  > =
57
60
  T extends BaseModelField<infer FieldShape>
58
61
  ? FieldShape
59
62
  : T extends RefType<infer RefShape, any, any>
60
63
  ? ResolveRef<RefShape, Bag>
61
64
  : T extends ModelRelationshipField<infer RelationshipShape, any, any, any>
62
- ? ResolveRelationship<Bag, RelationshipShape, FlatModelName>
65
+ ? ResolveRelationship<Bag, RelationshipShape, FlatModelName, Depth>
63
66
  : T extends CustomType<infer CT>
64
67
  ? ResolveFields<Bag, CT['fields']> | null
65
68
  : T extends EnumType<infer values>
@@ -112,34 +115,109 @@ type ShortCircuitBiDirectionalRelationship<
112
115
  : Field]: Model[Field];
113
116
  };
114
117
 
118
+ /** Subtract 1 from a depth counter, bounded at 0. Outside 0–5 falls through to 0. */
119
+ type Decrement<N extends number> =
120
+ N extends 5 ? 4
121
+ : N extends 4 ? 3
122
+ : N extends 3 ? 2
123
+ : N extends 2 ? 1
124
+ : N extends 1 ? 0
125
+ : 0;
126
+
127
+ /**
128
+ * Replaces each relationship field on the inlined related-model `type` with the
129
+ * related model's `type` recursively flattened, decrementing `Depth` per hop.
130
+ * At `Depth=0`, strips relationships entirely so ModelPathInner can't recurse
131
+ * into a LazyLoader leaf and the cascade terminates.
132
+ */
133
+ type FlattenRelationships<
134
+ Bag extends Record<string, any>,
135
+ Model extends Record<string, any>,
136
+ RawFields extends Record<string, any>,
137
+ Depth extends number = DefaultSelectionSetDepth,
138
+ > = Depth extends 0
139
+ ? OmitRelationships<Model, RawFields>
140
+ : {
141
+ [K in keyof Model]: K extends keyof RawFields
142
+ ? RawFields[K] extends ModelRelationshipField<infer RS, any, any, any>
143
+ ? RS['array'] extends true
144
+ ? Array<
145
+ FlattenRelationships<
146
+ Bag,
147
+ Bag[RS['relatedModel']]['type'],
148
+ Bag[RS['relatedModel']]['__meta']['rawType']['fields'],
149
+ Decrement<Depth>
150
+ >
151
+ >
152
+ : FlattenRelationships<
153
+ Bag,
154
+ Bag[RS['relatedModel']]['type'],
155
+ Bag[RS['relatedModel']]['__meta']['rawType']['fields'],
156
+ Decrement<Depth>
157
+ >
158
+ : Model[K]
159
+ : Model[K];
160
+ };
161
+
162
+ /** Terminal case at the depth boundary: drop relationships entirely. */
163
+ type OmitRelationships<
164
+ Model extends Record<string, any>,
165
+ RawFields extends Record<string, any>,
166
+ > = {
167
+ [K in keyof Model as K extends keyof RawFields
168
+ ? RawFields[K] extends ModelRelationshipField<any, any, any, any>
169
+ ? never
170
+ : K
171
+ : K]: Model[K];
172
+ };
173
+
174
+ /** Shared `LazyLoader` shape used by the non-flat branch and the depth=0 short-circuit. */
175
+ type LazyLoaderForRelationship<
176
+ Bag extends Record<string, any>,
177
+ RelationshipShape extends ModelRelationshipFieldParamShape,
178
+ > = DependentLazyLoaderOpIsAvailable<Bag, RelationshipShape> extends true
179
+ ? LazyLoader<
180
+ RelationshipShape['valueRequired'] extends true
181
+ ? Bag[RelationshipShape['relatedModel']]['type']
182
+ : Bag[RelationshipShape['relatedModel']]['type'] | null,
183
+ RelationshipShape['array']
184
+ >
185
+ : never;
186
+
115
187
  type ResolveRelationship<
116
188
  Bag extends Record<string, any>,
117
189
  RelationshipShape extends ModelRelationshipFieldParamShape,
118
190
  ParentModelName extends keyof Bag & string = never,
191
+ Depth extends number = DefaultSelectionSetDepth,
119
192
  > =
120
193
  ExtendsNever<ParentModelName> extends true
121
- ? DependentLazyLoaderOpIsAvailable<Bag, RelationshipShape> extends true
122
- ? LazyLoader<
123
- RelationshipShape['valueRequired'] extends true
124
- ? Bag[RelationshipShape['relatedModel']]['type']
125
- : Bag[RelationshipShape['relatedModel']]['type'] | null,
126
- RelationshipShape['array']
127
- >
128
- : never
129
- : // Array-ing inline here vs. (inside of ShortCircuitBiDirectionalRelationship or in a separate conditional type) is significantly more performant
130
- RelationshipShape['array'] extends true
131
- ? Array<
132
- ShortCircuitBiDirectionalRelationship<
133
- Bag[RelationshipShape['relatedModel']]['__meta']['flatModel'],
194
+ ? LazyLoaderForRelationship<Bag, RelationshipShape>
195
+ : Depth extends 0
196
+ ? LazyLoaderForRelationship<Bag, RelationshipShape>
197
+ : // Array-ing inline here vs. (inside of ShortCircuitBiDirectionalRelationship or in a separate conditional type) is significantly more performant
198
+ RelationshipShape['array'] extends true
199
+ ? Array<
200
+ ShortCircuitBiDirectionalRelationship<
201
+ FlattenRelationships<
202
+ Bag,
203
+ Bag[RelationshipShape['relatedModel']]['type'],
204
+ Bag[RelationshipShape['relatedModel']]['__meta']['rawType']['fields'],
205
+ Decrement<Depth>
206
+ >,
207
+ ParentModelName,
208
+ Bag[RelationshipShape['relatedModel']]['__meta']['rawType']['fields']
209
+ >
210
+ >
211
+ : ShortCircuitBiDirectionalRelationship<
212
+ FlattenRelationships<
213
+ Bag,
214
+ Bag[RelationshipShape['relatedModel']]['type'],
215
+ Bag[RelationshipShape['relatedModel']]['__meta']['rawType']['fields'],
216
+ Decrement<Depth>
217
+ >,
134
218
  ParentModelName,
135
219
  Bag[RelationshipShape['relatedModel']]['__meta']['rawType']['fields']
136
- >
137
- >
138
- : ShortCircuitBiDirectionalRelationship<
139
- Bag[RelationshipShape['relatedModel']]['__meta']['flatModel'],
140
- ParentModelName,
141
- Bag[RelationshipShape['relatedModel']]['__meta']['rawType']['fields']
142
- >;
220
+ >;
143
221
 
144
222
  type DependentLazyLoaderOpIsAvailable<
145
223
  Bag extends Record<string, any>,
@@ -1,8 +1,22 @@
1
- import { ImpliedAuthFields } from '../../Authorization';
2
- import { BaseSchema } from '../../ModelSchema';
1
+ import type { ImpliedAuthFields } from '../../Authorization';
2
+ import type {
3
+ BaseSchema,
4
+ ClientSchemaOptions,
5
+ DefaultSelectionSetDepth,
6
+ SelectionSetDepthValue,
7
+ } from '../../ModelSchema';
3
8
 
4
- export interface SchemaMetadata<Schema extends BaseSchema<any, any>> {
9
+ export interface SchemaMetadata<
10
+ Schema extends BaseSchema<any, any>,
11
+ Options extends ClientSchemaOptions = ClientSchemaOptions,
12
+ > {
5
13
  authFields: AuthFields<Schema>;
14
+ // Non-distributive `[T] extends [U]` so a default `Options = ClientSchemaOptions`
15
+ // (where selectionSetDepth is optional, type `... | undefined`) falls through
16
+ // to the default rather than producing a union with undefined.
17
+ selectionSetDepth: [Options['selectionSetDepth']] extends [SelectionSetDepthValue]
18
+ ? Options['selectionSetDepth']
19
+ : DefaultSelectionSetDepth;
6
20
  }
7
21
 
8
22
  type AuthFields<Schema extends Record<string, any>> =
@@ -59,6 +59,15 @@ type AddToSchemaContents = Record<string, AddToSchemaContent>;
59
59
 
60
60
  type NonEmpty<T> = keyof T extends never ? never : T;
61
61
 
62
+ export type SelectionSetDepthValue = 0 | 1 | 2 | 3 | 4 | 5;
63
+
64
+ /** Default depth applied when ClientSchema is used without an explicit Options arg. */
65
+ export type DefaultSelectionSetDepth = 5;
66
+
67
+ export type ClientSchemaOptions = {
68
+ selectionSetDepth?: SelectionSetDepthValue;
69
+ };
70
+
62
71
  export type ModelSchemaContents = Record<string, SchemaContent>;
63
72
 
64
73
  type InternalSchemaModels = Record<
@@ -258,9 +267,8 @@ type ModelWithRelationships<
258
267
  Record<string, RelationshipTemplate | undefined>
259
268
  >,
260
269
  ModelName extends keyof Types,
261
- RelationshipMap extends UnionToIntersection<
262
- Relationships[number]
263
- > = UnionToIntersection<Relationships[number]>,
270
+ RelationshipMap extends UnionToIntersection<Relationships[number]> =
271
+ UnionToIntersection<Relationships[number]>,
264
272
  > = ModelName extends keyof RelationshipMap
265
273
  ? RelationshipMap[ModelName] extends Record<
266
274
  string,