@aws-amplify/data-schema 1.9.2 → 1.10.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/{ModelRelationalField.js → ModelRelationshipField.js} +8 -5
- package/dist/cjs/ModelRelationshipField.js.map +1 -0
- package/dist/cjs/SchemaProcessor.js +31 -24
- package/dist/cjs/SchemaProcessor.js.map +1 -1
- package/dist/cjs/a.js +4 -4
- package/dist/cjs/a.js.map +1 -1
- package/dist/cjs/runtime/internals/APIClient.js +8 -8
- package/dist/cjs/runtime/internals/APIClient.js.map +1 -1
- package/dist/esm/Authorization.d.ts +7 -0
- package/dist/esm/ClientSchema/Core/ClientModel.d.ts +3 -3
- package/dist/esm/ClientSchema/utilities/ResolveField.d.ts +5 -5
- package/dist/esm/CustomOperation.d.ts +7 -0
- package/dist/esm/CustomType.d.ts +5 -0
- package/dist/esm/EnumType.d.ts +5 -0
- package/dist/esm/MappedTypes/ModelMetadata.d.ts +5 -5
- package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +9 -9
- package/dist/esm/MappedTypes/ResolveSchema.d.ts +3 -3
- package/dist/esm/ModelField.d.ts +5 -0
- package/dist/esm/{ModelRelationalField.d.ts → ModelRelationshipField.d.ts} +31 -14
- package/dist/esm/{ModelRelationalField.mjs → ModelRelationshipField.mjs} +8 -5
- package/dist/esm/ModelRelationshipField.mjs.map +1 -0
- package/dist/esm/ModelSchema.d.ts +9 -3
- package/dist/esm/ModelType.d.ts +18 -5
- package/dist/esm/RefType.d.ts +6 -0
- package/dist/esm/SchemaProcessor.mjs +23 -16
- package/dist/esm/SchemaProcessor.mjs.map +1 -1
- package/dist/esm/a.d.ts +1 -1
- package/dist/esm/a.mjs +1 -1
- package/dist/esm/a.mjs.map +1 -1
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/runtime/client/index.d.ts +3 -3
- package/dist/esm/runtime/internals/APIClient.mjs +8 -8
- package/dist/esm/runtime/internals/APIClient.mjs.map +1 -1
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Authorization.ts +7 -0
- package/src/ClientSchema/Core/ClientModel.ts +3 -3
- package/src/ClientSchema/utilities/ResolveField.ts +11 -12
- package/src/CustomOperation.ts +7 -0
- package/src/CustomType.ts +8 -3
- package/src/EnumType.ts +5 -0
- package/src/MappedTypes/ModelMetadata.ts +7 -7
- package/src/MappedTypes/ResolveFieldProperties.ts +13 -13
- package/src/MappedTypes/ResolveSchema.ts +5 -5
- package/src/ModelField.ts +5 -3
- package/src/{ModelRelationalField.ts → ModelRelationshipField.ts} +55 -35
- package/src/ModelSchema.ts +16 -6
- package/src/ModelType.ts +21 -8
- package/src/RefType.ts +6 -0
- package/src/SchemaProcessor.ts +45 -25
- package/src/a.ts +1 -1
- package/src/index.ts +14 -0
- package/src/runtime/client/index.ts +7 -3
- package/src/runtime/internals/APIClient.ts +8 -8
- package/dist/cjs/ModelRelationalField.js.map +0 -1
- package/dist/esm/ModelRelationalField.mjs.map +0 -1
package/dist/esm/EnumType.d.ts
CHANGED
|
@@ -3,6 +3,11 @@ type EnumTypeParamShape<values extends readonly string[] = readonly string[]> =
|
|
|
3
3
|
type: 'enum';
|
|
4
4
|
values: values;
|
|
5
5
|
};
|
|
6
|
+
/**
|
|
7
|
+
* Enum type definition content
|
|
8
|
+
*
|
|
9
|
+
* @param values - The values of the enum
|
|
10
|
+
*/
|
|
6
11
|
export interface EnumType<values extends readonly string[] = readonly string[]> extends EnumTypeParamShape<values> {
|
|
7
12
|
[brandSymbol]: 'enum';
|
|
8
13
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type UnionToIntersection, type ExcludeEmpty } from '@aws-amplify/data-schema-types';
|
|
2
2
|
import type { PrimaryIndexIrShape } from '../util';
|
|
3
3
|
import type { ModelType } from '../ModelType';
|
|
4
|
-
import type {
|
|
4
|
+
import type { ModelRelationshipFieldParamShape } from '../ModelRelationshipField';
|
|
5
5
|
export type ModelIdentifier<T> = {
|
|
6
6
|
[Property in keyof T]: T[Property] extends ModelType<infer R, any> ? R['identifier'] extends PrimaryIndexIrShape ? {
|
|
7
7
|
identifier: R['identifier'];
|
|
@@ -12,14 +12,14 @@ export type ModelSecondaryIndexes<T> = {
|
|
|
12
12
|
secondaryIndexes: R['secondaryIndexes'];
|
|
13
13
|
} : never : never;
|
|
14
14
|
};
|
|
15
|
-
export type
|
|
15
|
+
export type RelationshipMetadata<ResolvedSchema, ResolvedFields extends Record<string, unknown>, IdentifierMeta extends Record<string, {
|
|
16
16
|
identifier: PrimaryIndexIrShape;
|
|
17
17
|
}>> = UnionToIntersection<ExcludeEmpty<{
|
|
18
18
|
[ModelName in keyof ResolvedSchema]: {
|
|
19
|
-
[Field in keyof ResolvedSchema[ModelName] as ResolvedSchema[ModelName][Field] extends
|
|
20
|
-
|
|
19
|
+
[Field in keyof ResolvedSchema[ModelName] as ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field]['relationshipType'] extends 'hasOne' | 'belongsTo' ? ModelName : never : never]: ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field]['relationshipType'] extends 'hasMany' ? {
|
|
20
|
+
relationshipInputFields: Partial<Record<`${Uncapitalize<ModelName & string>}`, NormalizeInputFields<ResolvedFields[ModelName & string], ExtractModelIdentifier<ModelName, IdentifierMeta>>>>;
|
|
21
21
|
} : {
|
|
22
|
-
|
|
22
|
+
relationshipInputFields: Partial<Record<Field, NormalizeInputFields<ResolvedFields[ResolvedSchema[ModelName][Field]['relatedModel']], ExtractModelIdentifier<`${Capitalize<Field & string>}`, IdentifierMeta>>>>;
|
|
23
23
|
} : never : never;
|
|
24
24
|
};
|
|
25
25
|
}[keyof ResolvedSchema]>>;
|
|
@@ -3,7 +3,7 @@ import type { Authorization, ImpliedAuthFields } from '../Authorization';
|
|
|
3
3
|
import type { ModelField } from '../ModelField';
|
|
4
4
|
import type { ModelType, ModelTypeParamShape } from '../ModelType';
|
|
5
5
|
import type { GenericModelSchema } from '../ModelSchema';
|
|
6
|
-
import type {
|
|
6
|
+
import type { ModelRelationshipField, ModelRelationshipFieldParamShape } from '../ModelRelationshipField';
|
|
7
7
|
import type { PrimaryIndexIrShape } from '../util/';
|
|
8
8
|
import type { ResolveSchema, SchemaTypes } from './ResolveSchema';
|
|
9
9
|
import type { InjectImplicitModelFields } from './ImplicitFieldInjector';
|
|
@@ -15,11 +15,11 @@ import type { EnumType } from '../EnumType';
|
|
|
15
15
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
16
16
|
export type ResolveFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, {
|
|
17
17
|
identifier: PrimaryIndexIrShape;
|
|
18
|
-
}> = ModelIdentifier<SchemaTypes<Schema>>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema, IdentifierMeta>, FieldsWithRelationships =
|
|
19
|
-
export type ResolveStaticFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ImplicitModelsSchema, ResolvedSchema = ResolveSchema<Schema>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema & ImplicitModelsSchema, never>, FieldsWithRelationships =
|
|
20
|
-
type GetRelationshipRef<T, RM extends keyof T, TypeArg extends
|
|
21
|
-
type
|
|
22
|
-
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends
|
|
18
|
+
}> = ModelIdentifier<SchemaTypes<Schema>>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema, IdentifierMeta>, FieldsWithRelationships = ResolveModelsRelationshipAndRefFields<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<MarkModelsNonNullableFieldsRequired<FieldsWithRelationships>>, FilterFieldTypes<MarkModelsNullableFieldsOptional<FieldsWithRelationships>>, FilterFieldTypes<ModelImpliedAuthFields<Schema>>>;
|
|
19
|
+
export type ResolveStaticFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ImplicitModelsSchema, ResolvedSchema = ResolveSchema<Schema>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema & ImplicitModelsSchema, never>, FieldsWithRelationships = ResolveModelsRelationshipAndRefFields<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<MarkModelsNonNullableFieldsRequired<FieldsWithRelationships>>, FilterFieldTypes<MarkModelsNullableFieldsOptional<FieldsWithRelationships>>>;
|
|
20
|
+
type GetRelationshipRef<T, RM extends keyof T, TypeArg extends ModelRelationshipFieldParamShape, Flat extends boolean, ResolvedModel = ResolveRelationshipFieldsForModel<T, RM, Flat>, Model = TypeArg['valueRequired'] extends true ? ResolvedModel : ResolvedModel | null | undefined> = LazyLoader<Model, TypeArg['array']>;
|
|
21
|
+
type ResolveRelationshipFieldsForModel<Schema, ModelName extends keyof Schema, Flat extends boolean> = {
|
|
22
|
+
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends ModelRelationshipFieldParamShape ? Schema[ModelName][FieldName]['relatedModel'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelName][FieldName]['relatedModel'], Schema[ModelName][FieldName], Flat> : never : Schema[ModelName][FieldName];
|
|
23
23
|
};
|
|
24
24
|
export type ResolveRef<NonModelTypes extends NonModelTypesShape, Ref extends RefTypeParamShape, Link extends string = Ref['link'], RefValue = Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? ResolveRefsOfCustomType<NonModelTypes, NonModelTypes['customTypes'][Link]> : never, Value = Ref['valueRequired'] extends true ? RefValue : RefValue | null> = ResolveRefValueArrayTraits<Ref, Value>;
|
|
25
25
|
/**
|
|
@@ -31,9 +31,9 @@ export type ResolveRefsOfCustomType<NonModelTypes extends NonModelTypesShape, T>
|
|
|
31
31
|
[Prop in keyof T]: T[Prop] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : T[Prop];
|
|
32
32
|
} extends infer Resolved ? ResolveFieldRequirements<Resolved> : never;
|
|
33
33
|
export type ResolveFieldRequirements<Resolved> = Intersection<ExtractNullableFieldsToOptionalFields<Resolved>, ExtractNonNullableFieldsToRequiredFields<Resolved>>;
|
|
34
|
-
type
|
|
34
|
+
type ResolveModelsRelationshipAndRefFields<Schema, NonModelTypes extends NonModelTypesShape, Flat extends boolean = false> = {
|
|
35
35
|
[ModelProp in keyof Schema]: {
|
|
36
|
-
[FieldProp in keyof Schema[ModelProp]]: Schema[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : Schema[ModelProp][FieldProp] extends
|
|
36
|
+
[FieldProp in keyof Schema[ModelProp]]: Schema[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : Schema[ModelProp][FieldProp] extends ModelRelationshipFieldParamShape ? Schema[ModelProp][FieldProp]['relatedModel'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelProp][FieldProp]['relatedModel'], Schema[ModelProp][FieldProp], Flat> : never : Schema[ModelProp][FieldProp];
|
|
37
37
|
};
|
|
38
38
|
};
|
|
39
39
|
type FilterFieldTypes<Schema> = {
|
|
@@ -60,5 +60,5 @@ export type ModelImpliedAuthFields<Schema extends GenericModelSchema<any>> = {
|
|
|
60
60
|
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType ? never : Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomOperation<CustomOperationParamShape, any> ? never : ModelKey]: Schema['data']['types'][ModelKey] extends ModelType<infer Model, any> ? AllAuthFieldsForModel<Schema, Model> : object;
|
|
61
61
|
};
|
|
62
62
|
type AllAuthFieldsForModel<Schema extends GenericModelSchema<any>, Model extends Schema['data']['types'][keyof Schema['data']['types']]> = (Model['authorization'][number] extends never ? Schema['data']['authorization'][number] extends never ? object : ImpliedAuthFields<Schema['data']['authorization'][number]> : ImpliedAuthFields<Model['authorization'][number]>) & ImpliedAuthFieldsFromFields<Model>;
|
|
63
|
-
type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<T extends ModelTypeParamShape ? T['fields'][keyof T['fields']] extends ModelField<any, any, infer Auth> |
|
|
63
|
+
type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<T extends ModelTypeParamShape ? T['fields'][keyof T['fields']] extends ModelField<any, any, infer Auth> | ModelRelationshipField<any, any, any, infer Auth> | RefType<any, any, infer Auth> ? Auth extends Authorization<any, any, any> ? ImpliedAuthFields<Auth> : object : object : object>;
|
|
64
64
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ModelType } from '../ModelType';
|
|
2
2
|
import type { GenericModelSchema } from '../ModelSchema';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ModelRelationshipField, ModelRelationshipTypes, RelationTypeFunctionOmitMapping } from '../ModelRelationshipField';
|
|
4
4
|
import type { BaseModelField } from '../ModelField';
|
|
5
5
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
6
6
|
import type { EnumType } from '../EnumType';
|
|
@@ -39,14 +39,14 @@ export type FieldTypes<T> = {
|
|
|
39
39
|
array: false;
|
|
40
40
|
arrayRequired: false;
|
|
41
41
|
authorization: [];
|
|
42
|
-
}> | null : T[ModelProp][FieldProp] extends
|
|
42
|
+
}> | null : T[ModelProp][FieldProp] extends ModelRelationshipField<infer R, string, RelationTypeFunctionOmitMapping<ModelRelationshipTypes>, any> ? R : never;
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* Resolves field types for a CustomType.
|
|
47
47
|
*
|
|
48
48
|
* This utility type is needed in addition to the `FieldTypes` utility type as
|
|
49
|
-
* without checking `
|
|
49
|
+
* without checking `ModelRelationshipField` can improve ~5% on resolving performance.
|
|
50
50
|
*
|
|
51
51
|
* Non-model types are replaced with Refs. Refs remain and are resolved in ResolveFieldProperties.ts
|
|
52
52
|
*/
|
package/dist/esm/ModelField.d.ts
CHANGED
|
@@ -42,6 +42,11 @@ type FieldData = {
|
|
|
42
42
|
authorization: Authorization<any, any, any>[];
|
|
43
43
|
};
|
|
44
44
|
type ModelFieldTypeParamInner = string | number | boolean | Date | Json | null;
|
|
45
|
+
/**
|
|
46
|
+
* A precise, recursive Json type blows the type calculation stack without installing
|
|
47
|
+
* explicit `Json extends T ? short-circuit : ...` type checks all over the place.
|
|
48
|
+
* We may take that on later. But, this is a good-enough approximation for now.
|
|
49
|
+
*/
|
|
45
50
|
export type Json = null | string | number | boolean | object | any[];
|
|
46
51
|
export type ModelFieldTypeParamOuter = ModelFieldTypeParamInner | Array<ModelFieldTypeParamInner> | null;
|
|
47
52
|
/**
|
|
@@ -5,14 +5,17 @@ import { AllowModifier, Authorization } from './Authorization';
|
|
|
5
5
|
* Used to "attach" auth types to ModelField without exposing them on the builder.
|
|
6
6
|
*/
|
|
7
7
|
export declare const __auth: unique symbol;
|
|
8
|
-
declare const brandName = "
|
|
8
|
+
declare const brandName = "modelRelationshipField";
|
|
9
|
+
/**
|
|
10
|
+
* Model relationship types
|
|
11
|
+
*/
|
|
9
12
|
export declare enum ModelRelationshipTypes {
|
|
10
13
|
hasOne = "hasOne",
|
|
11
14
|
hasMany = "hasMany",
|
|
12
15
|
belongsTo = "belongsTo"
|
|
13
16
|
}
|
|
14
17
|
type RelationshipTypes = `${ModelRelationshipTypes}`;
|
|
15
|
-
type
|
|
18
|
+
type ModelRelationshipFieldData = {
|
|
16
19
|
fieldType: 'model';
|
|
17
20
|
type: ModelRelationshipTypes;
|
|
18
21
|
relatedModel: string;
|
|
@@ -22,7 +25,7 @@ type ModelRelationalFieldData = {
|
|
|
22
25
|
references: string[];
|
|
23
26
|
authorization: Authorization<any, any, any>[];
|
|
24
27
|
};
|
|
25
|
-
export type
|
|
28
|
+
export type ModelRelationshipFieldParamShape = {
|
|
26
29
|
type: 'model';
|
|
27
30
|
relationshipType: string;
|
|
28
31
|
relatedModel: string;
|
|
@@ -31,33 +34,47 @@ export type ModelRelationalFieldParamShape = {
|
|
|
31
34
|
references: string[];
|
|
32
35
|
arrayRequired: boolean;
|
|
33
36
|
};
|
|
34
|
-
type
|
|
37
|
+
type ModelRelationshipFieldFunctions<T extends ModelRelationshipFieldParamShape, RM extends string | symbol, K extends keyof ModelRelationshipField<T, RM> = never> = {
|
|
35
38
|
/**
|
|
36
39
|
* When set, it requires the value of the relationship type to be required.
|
|
37
40
|
*/
|
|
38
|
-
valueRequired():
|
|
41
|
+
valueRequired(): ModelRelationshipField<SetTypeSubArg<T, 'valueRequired', true>, K | 'valueRequired'>;
|
|
39
42
|
/**
|
|
40
43
|
* When set, it requires the relationship to always return a value
|
|
41
44
|
*/
|
|
42
|
-
required():
|
|
45
|
+
required(): ModelRelationshipField<SetTypeSubArg<T, 'arrayRequired', true>, K | 'required'>;
|
|
43
46
|
/**
|
|
44
47
|
* Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
|
|
45
48
|
* multiple authorization rules for this field.
|
|
46
49
|
*/
|
|
47
|
-
authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: AllowModifier) => AuthRuleType | AuthRuleType[]):
|
|
50
|
+
authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: AllowModifier) => AuthRuleType | AuthRuleType[]): ModelRelationshipField<T, K | 'authorization', K, AuthRuleType>;
|
|
48
51
|
};
|
|
49
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Model relationship field definition interface
|
|
54
|
+
*
|
|
55
|
+
* @param T - The shape of the model relationship field
|
|
56
|
+
* @param RM - Adds structural separation with ModelField; easier to identify it when mapping to ClientTypes
|
|
57
|
+
* @param K - The keys already defined
|
|
58
|
+
*/
|
|
59
|
+
export type ModelRelationshipField<T extends ModelRelationshipFieldParamShape, RM extends string | symbol, K extends keyof ModelRelationshipField<T, RM> = never, Auth = undefined> = Omit<ModelRelationshipFieldFunctions<T, RM, K>, K> & {
|
|
50
60
|
[__auth]?: Auth;
|
|
51
61
|
} & Brand<typeof brandName>;
|
|
52
62
|
/**
|
|
53
63
|
* Internal representation of Model Field that exposes the `data` property.
|
|
54
64
|
* Used at buildtime.
|
|
55
65
|
*/
|
|
56
|
-
export type
|
|
57
|
-
data:
|
|
66
|
+
export type InternalRelationshipField = ModelRelationshipField<ModelRelationshipFieldParamShape, string, never> & {
|
|
67
|
+
data: ModelRelationshipFieldData;
|
|
58
68
|
};
|
|
59
69
|
export type RelationTypeFunctionOmitMapping<Type extends ModelRelationshipTypes> = Type extends ModelRelationshipTypes.belongsTo ? 'required' | 'valueRequired' : Type extends ModelRelationshipTypes.hasMany ? 'required' : Type extends ModelRelationshipTypes.hasOne ? 'valueRequired' : never;
|
|
60
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Model relationship type definition content
|
|
72
|
+
*
|
|
73
|
+
* @param RM - The related model name
|
|
74
|
+
* @param RT - The relationship type
|
|
75
|
+
* @param IsArray - Whether the relationship is an array
|
|
76
|
+
*/
|
|
77
|
+
export type ModelRelationshipTypeArgFactory<RM extends string, RT extends RelationshipTypes, IsArray extends boolean> = {
|
|
61
78
|
type: 'model';
|
|
62
79
|
relatedModel: RM;
|
|
63
80
|
relationshipType: RT;
|
|
@@ -91,7 +108,7 @@ export type ModelRelationalTypeArgFactory<RM extends string, RT extends Relation
|
|
|
91
108
|
* @param references the field(s) that should be used to reference the related model
|
|
92
109
|
* @returns a one-to-one relationship definition
|
|
93
110
|
*/
|
|
94
|
-
export declare function hasOne<RM extends string>(relatedModel: RM, references: string | string[]):
|
|
111
|
+
export declare function hasOne<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationshipField<ModelRelationshipTypeArgFactory<RM, ModelRelationshipTypes.hasOne, false>, RM, "valueRequired", undefined>;
|
|
95
112
|
/**
|
|
96
113
|
* Create a one-directional one-to-many relationship between two models using the `hasMany("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
97
114
|
* @example
|
|
@@ -118,7 +135,7 @@ export declare function hasOne<RM extends string>(relatedModel: RM, references:
|
|
|
118
135
|
* @param references the field(s) that should be used to reference the related model
|
|
119
136
|
* @returns a one-to-many relationship definition
|
|
120
137
|
*/
|
|
121
|
-
export declare function hasMany<RM extends string>(relatedModel: RM, references: string | string[]):
|
|
138
|
+
export declare function hasMany<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationshipField<ModelRelationshipTypeArgFactory<RM, ModelRelationshipTypes.hasMany, true>, RM, "required", undefined>;
|
|
122
139
|
/**
|
|
123
140
|
* Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
|
|
124
141
|
* The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
|
|
@@ -166,5 +183,5 @@ export declare function hasMany<RM extends string>(relatedModel: RM, references:
|
|
|
166
183
|
* @param references the field(s) that should be used to reference the related model
|
|
167
184
|
* @returns a belong-to relationship definition
|
|
168
185
|
*/
|
|
169
|
-
export declare function belongsTo<RM extends string>(relatedModel: RM, references: string | string[]):
|
|
186
|
+
export declare function belongsTo<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationshipField<ModelRelationshipTypeArgFactory<RM, ModelRelationshipTypes.belongsTo, false>, RM, "required" | "valueRequired", undefined>;
|
|
170
187
|
export {};
|
|
@@ -4,6 +4,9 @@ import { allow } from './Authorization.mjs';
|
|
|
4
4
|
* Used to "attach" auth types to ModelField without exposing them on the builder.
|
|
5
5
|
*/
|
|
6
6
|
const __auth = Symbol('__auth');
|
|
7
|
+
/**
|
|
8
|
+
* Model relationship types
|
|
9
|
+
*/
|
|
7
10
|
var ModelRelationshipTypes;
|
|
8
11
|
(function (ModelRelationshipTypes) {
|
|
9
12
|
ModelRelationshipTypes["hasOne"] = "hasOne";
|
|
@@ -15,7 +18,7 @@ const relationModifierMap = {
|
|
|
15
18
|
hasMany: ['valueRequired', 'authorization'],
|
|
16
19
|
hasOne: ['required', 'authorization'],
|
|
17
20
|
};
|
|
18
|
-
function
|
|
21
|
+
function _modelRelationshipField(type, relatedModel, references) {
|
|
19
22
|
const data = {
|
|
20
23
|
relatedModel,
|
|
21
24
|
type,
|
|
@@ -77,7 +80,7 @@ function _modelRelationalField(type, relatedModel, references) {
|
|
|
77
80
|
* @returns a one-to-one relationship definition
|
|
78
81
|
*/
|
|
79
82
|
function hasOne(relatedModel, references) {
|
|
80
|
-
return
|
|
83
|
+
return _modelRelationshipField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);
|
|
81
84
|
}
|
|
82
85
|
/**
|
|
83
86
|
* Create a one-directional one-to-many relationship between two models using the `hasMany("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
@@ -106,7 +109,7 @@ function hasOne(relatedModel, references) {
|
|
|
106
109
|
* @returns a one-to-many relationship definition
|
|
107
110
|
*/
|
|
108
111
|
function hasMany(relatedModel, references) {
|
|
109
|
-
return
|
|
112
|
+
return _modelRelationshipField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);
|
|
110
113
|
}
|
|
111
114
|
/**
|
|
112
115
|
* Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
|
|
@@ -156,8 +159,8 @@ function hasMany(relatedModel, references) {
|
|
|
156
159
|
* @returns a belong-to relationship definition
|
|
157
160
|
*/
|
|
158
161
|
function belongsTo(relatedModel, references) {
|
|
159
|
-
return
|
|
162
|
+
return _modelRelationshipField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);
|
|
160
163
|
}
|
|
161
164
|
|
|
162
165
|
export { ModelRelationshipTypes, __auth, belongsTo, hasMany, hasOne };
|
|
163
|
-
//# sourceMappingURL=
|
|
166
|
+
//# sourceMappingURL=ModelRelationshipField.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModelRelationshipField.mjs","sources":["../../src/ModelRelationshipField.ts"],"sourcesContent":["import { allow } from './Authorization';\n/**\n * Used to \"attach\" auth types to ModelField without exposing them on the builder.\n */\nexport const __auth = Symbol('__auth');\nconst brandName = 'modelRelationshipField';\n/**\n * Model relationship types\n */\nexport var ModelRelationshipTypes;\n(function (ModelRelationshipTypes) {\n ModelRelationshipTypes[\"hasOne\"] = \"hasOne\";\n ModelRelationshipTypes[\"hasMany\"] = \"hasMany\";\n ModelRelationshipTypes[\"belongsTo\"] = \"belongsTo\";\n})(ModelRelationshipTypes || (ModelRelationshipTypes = {}));\nconst relationshipModifiers = [\n 'required',\n 'valueRequired',\n 'authorization',\n];\nconst relationModifierMap = {\n belongsTo: ['authorization'],\n hasMany: ['valueRequired', 'authorization'],\n hasOne: ['required', 'authorization'],\n};\nfunction _modelRelationshipField(type, relatedModel, references) {\n const data = {\n relatedModel,\n type,\n fieldType: 'model',\n array: false,\n valueRequired: false,\n arrayRequired: false,\n references,\n authorization: [],\n };\n data.array = type === 'hasMany';\n const relationshipBuilderFunctions = {\n required() {\n data.arrayRequired = true;\n return this;\n },\n valueRequired() {\n data.valueRequired = true;\n return this;\n },\n authorization(callback) {\n const rules = callback(allow);\n data.authorization = Array.isArray(rules) ? rules : [rules];\n return this;\n },\n };\n const builder = Object.fromEntries(relationModifierMap[type].map((key) => [\n key,\n relationshipBuilderFunctions[key],\n ]));\n return {\n ...builder,\n data,\n };\n}\n/**\n * Create one-to-one relationship between two models using the `hasOne(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field\n * unless overwritten with the `identifier()` method.\n * @example\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-one relationship definition\n */\nexport function hasOne(relatedModel, references) {\n return _modelRelationshipField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Create a one-directional one-to-many relationship between two models using the `hasMany(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * @example\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-many relationship definition\n */\nexport function hasMany(relatedModel, references) {\n return _modelRelationshipField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.\n * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from\n * parent to the related model.\n *\n * @example\n * // one-to-many relationship\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @example\n * // one-to-one relationship\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}\n * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model\n * @param references the field(s) that should be used to reference the related model\n * @returns a belong-to relationship definition\n */\nexport function belongsTo(relatedModel, references) {\n return _modelRelationshipField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,EAAE;AAEvC;AACA;AACA;AACU,IAAC,uBAAuB;AAClC,CAAC,UAAU,sBAAsB,EAAE;AACnC,IAAI,sBAAsB,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAChD,IAAI,sBAAsB,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAClD,IAAI,sBAAsB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;AACtD,CAAC,EAAE,sBAAsB,KAAK,sBAAsB,GAAG,EAAE,CAAC,CAAC,CAAC;AAM5D,MAAM,mBAAmB,GAAG;AAC5B,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC;AAChC,IAAI,OAAO,EAAE,CAAC,eAAe,EAAE,eAAe,CAAC;AAC/C,IAAI,MAAM,EAAE,CAAC,UAAU,EAAE,eAAe,CAAC;AACzC,CAAC,CAAC;AACF,SAAS,uBAAuB,CAAC,IAAI,EAAE,YAAY,EAAE,UAAU,EAAE;AACjE,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,YAAY;AACpB,QAAQ,IAAI;AACZ,QAAQ,SAAS,EAAE,OAAO;AAC1B,QAAQ,KAAK,EAAE,KAAK;AACpB,QAAQ,aAAa,EAAE,KAAK;AAC5B,QAAQ,aAAa,EAAE,KAAK;AAC5B,QAAQ,UAAU;AAClB,QAAQ,aAAa,EAAE,EAAE;AACzB,KAAK,CAAC;AACN,IAAI,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,SAAS,CAAC;AACpC,IAAI,MAAM,4BAA4B,GAAG;AACzC,QAAQ,QAAQ,GAAG;AACnB,YAAY,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;AACtC,YAAY,OAAO,IAAI,CAAC;AACxB,SAAS;AACT,QAAQ,aAAa,GAAG;AACxB,YAAY,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;AACtC,YAAY,OAAO,IAAI,CAAC;AACxB,SAAS;AACT,QAAQ,aAAa,CAAC,QAAQ,EAAE;AAChC,YAAY,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC1C,YAAY,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC;AACxE,YAAY,OAAO,IAAI,CAAC;AACxB,SAAS;AACT,KAAK,CAAC;AACN,IAAI,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK;AAC9E,QAAQ,GAAG;AACX,QAAQ,4BAA4B,CAAC,GAAG,CAAC;AACzC,KAAK,CAAC,CAAC,CAAC;AACR,IAAI,OAAO;AACX,QAAQ,GAAG,OAAO;AAClB,QAAQ,IAAI;AACZ,KAAK,CAAC;AACN,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,MAAM,CAAC,YAAY,EAAE,UAAU,EAAE;AACjD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACvI,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,OAAO,CAAC,YAAY,EAAE,UAAU,EAAE;AAClD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACxI,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE;AACpD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AAC1I;;;;"}
|
|
@@ -5,7 +5,7 @@ import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
|
5
5
|
import type { CustomOperation, CustomOperationParamShape, InternalCustom, MutationCustomOperation, QueryCustomOperation, SubscriptionCustomOperation } from './CustomOperation';
|
|
6
6
|
import { AllowModifier, SchemaAuthorization } from './Authorization';
|
|
7
7
|
import { Brand, RenameUsingTuples } from './util';
|
|
8
|
-
import {
|
|
8
|
+
import { ModelRelationshipField, ModelRelationshipFieldParamShape } from './ModelRelationshipField';
|
|
9
9
|
import { ConversationType } from './ai/ConversationType';
|
|
10
10
|
export { ModelType } from './ModelType';
|
|
11
11
|
export { EnumType } from './EnumType';
|
|
@@ -50,6 +50,12 @@ export type BaseSchema<T extends ModelSchemaParamShape, IsRDS extends boolean =
|
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
52
|
export type GenericModelSchema<T extends ModelSchemaParamShape> = BaseSchema<T> & Brand<typeof rdsSchemaBrandName | typeof ddbSchemaBrandName>;
|
|
53
|
+
/**
|
|
54
|
+
* Model schema definition interface
|
|
55
|
+
*
|
|
56
|
+
* @param T - The shape of the model schema
|
|
57
|
+
* @param UsedMethods - The method keys already defined
|
|
58
|
+
*/
|
|
53
59
|
export type ModelSchema<T extends ModelSchemaParamShape, UsedMethods extends 'authorization' | 'relationships' = never> = Omit<{
|
|
54
60
|
authorization: <AuthRules extends SchemaAuthorization<any, any, any>>(callback: (allow: AllowModifier) => AuthRules | AuthRules[]) => ModelSchema<SetTypeSubArg<T, 'authorization', AuthRules[]>, UsedMethods | 'authorization'>;
|
|
55
61
|
}, UsedMethods> & BaseSchema<T> & DDBSchemaBrand;
|
|
@@ -57,7 +63,7 @@ type RDSModelSchemaFunctions = 'addToSchema' | 'addQueries' | 'addMutations' | '
|
|
|
57
63
|
type OmitFromEach<Models, Modifier extends string> = {
|
|
58
64
|
[ModelName in keyof Models]: Omit<Models[ModelName], Modifier>;
|
|
59
65
|
};
|
|
60
|
-
type RelationshipTemplate = Record<string,
|
|
66
|
+
type RelationshipTemplate = Record<string, ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>>;
|
|
61
67
|
export type RDSModelSchema<T extends RDSModelSchemaParamShape, UsedMethods extends RDSModelSchemaFunctions = never> = Omit<{
|
|
62
68
|
addToSchema: <AddedTypes extends AddToSchemaContents>(types: AddedTypes) => RDSModelSchema<SetTypeSubArg<T, 'types', T['types'] & AddedTypes>, UsedMethods | 'addToSchema'>;
|
|
63
69
|
/**
|
|
@@ -83,7 +89,7 @@ export type RDSModelSchema<T extends RDSModelSchemaParamShape, UsedMethods exten
|
|
|
83
89
|
* Amplify API Next Model Schema shape
|
|
84
90
|
*/
|
|
85
91
|
export type ModelSchemaType = ModelSchema<ModelSchemaParamShape>;
|
|
86
|
-
type ModelWithRelationships<Types extends Record<string, any>, Relationships extends ReadonlyArray<Record<string, RelationshipTemplate | undefined>>, ModelName extends keyof Types, RelationshipMap extends UnionToIntersection<Relationships[number]> = UnionToIntersection<Relationships[number]>> = ModelName extends keyof RelationshipMap ? RelationshipMap[ModelName] extends Record<string,
|
|
92
|
+
type ModelWithRelationships<Types extends Record<string, any>, Relationships extends ReadonlyArray<Record<string, RelationshipTemplate | undefined>>, ModelName extends keyof Types, RelationshipMap extends UnionToIntersection<Relationships[number]> = UnionToIntersection<Relationships[number]>> = ModelName extends keyof RelationshipMap ? RelationshipMap[ModelName] extends Record<string, ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>> ? AddRelationshipFieldsToModelTypeFields<Types[ModelName], RelationshipMap[ModelName]> : Types[ModelName] : Types[ModelName];
|
|
87
93
|
/**
|
|
88
94
|
* Model Schema type guard
|
|
89
95
|
* @param schema - api-next ModelSchema or string
|
package/dist/esm/ModelType.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SetTypeSubArg } from '@aws-amplify/data-schema-types';
|
|
2
2
|
import { type PrimaryIndexIrShape, type SecondaryIndexIrShape } from './util';
|
|
3
3
|
import type { InternalField, BaseModelField } from './ModelField';
|
|
4
|
-
import type {
|
|
4
|
+
import type { ModelRelationshipField, InternalRelationshipField, ModelRelationshipFieldParamShape } from './ModelRelationshipField';
|
|
5
5
|
import { type AllowModifier, type Authorization } from './Authorization';
|
|
6
6
|
import type { RefType, RefTypeParamShape } from './RefType';
|
|
7
7
|
import type { EnumType } from './EnumType';
|
|
@@ -12,8 +12,8 @@ import type { brandSymbol } from './util/Brand.js';
|
|
|
12
12
|
import type { methodKeyOf } from './util/usedMethods.js';
|
|
13
13
|
declare const brandName = "modelType";
|
|
14
14
|
export type deferredRefResolvingPrefix = 'deferredRefResolving:';
|
|
15
|
-
type ModelFields = Record<string, BaseModelField |
|
|
16
|
-
type InternalModelFields = Record<string, InternalField |
|
|
15
|
+
type ModelFields = Record<string, BaseModelField | ModelRelationshipField<any, string, any, any> | RefType<any, any, any> | EnumType | CustomType<CustomTypeParamShape>>;
|
|
16
|
+
type InternalModelFields = Record<string, InternalField | InternalRelationshipField>;
|
|
17
17
|
export type DisableOperationsOptions = 'queries' | 'mutations' | 'subscriptions' | 'list' | 'get' | 'create' | 'update' | 'delete' | 'onCreate' | 'onUpdate' | 'onDelete';
|
|
18
18
|
type ModelData = {
|
|
19
19
|
fields: ModelFields;
|
|
@@ -54,9 +54,15 @@ export type ModelTypeParamShape = {
|
|
|
54
54
|
export type ExtractSecondaryIndexIRFields<T extends ModelTypeParamShape, RequiredOnly extends boolean = false> = {
|
|
55
55
|
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends BaseModelField<infer R> ? NonNullable<R> extends string | number ? RequiredOnly extends false ? FieldProp : null extends R ? never : FieldProp : never : T['fields'][FieldProp] extends EnumType | RefType<RefTypeParamShape, any, any> ? FieldProp : never]: T['fields'][FieldProp] extends BaseModelField<infer R> ? R : T['fields'][FieldProp] extends EnumType<infer values> ? values[number] : T['fields'][FieldProp] extends RefType<infer R, any, any> ? `${deferredRefResolvingPrefix}${R['link']}` : never;
|
|
56
56
|
};
|
|
57
|
-
export type AddRelationshipFieldsToModelTypeFields<Model, RelationshipFields extends Record<string,
|
|
57
|
+
export type AddRelationshipFieldsToModelTypeFields<Model, RelationshipFields extends Record<string, ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>>> = Model extends ModelType<infer ModelParam extends ModelTypeParamShape, infer HiddenKeys> ? ModelType<SetTypeSubArg<ModelParam, 'fields', ModelParam['fields'] & RelationshipFields>, HiddenKeys> : never;
|
|
58
58
|
export type BaseModelType<T extends ModelTypeParamShape = ModelTypeParamShape> = ModelType<T, UsableModelTypeKey>;
|
|
59
59
|
export type UsableModelTypeKey = methodKeyOf<ModelType>;
|
|
60
|
+
/**
|
|
61
|
+
* Model type definition interface
|
|
62
|
+
*
|
|
63
|
+
* @param T - The shape of the model type
|
|
64
|
+
* @param UsedMethod - The method keys already defined
|
|
65
|
+
*/
|
|
60
66
|
export type ModelType<T extends ModelTypeParamShape = ModelTypeParamShape, UsedMethod extends UsableModelTypeKey = never> = Omit<{
|
|
61
67
|
[brandSymbol]: typeof brandName;
|
|
62
68
|
identifier<PrimaryIndexFields = ExtractSecondaryIndexIRFields<T, true>, PrimaryIndexPool extends string = keyof PrimaryIndexFields & string, const ID extends ReadonlyArray<PrimaryIndexPool> = readonly [], const PrimaryIndexIR extends PrimaryIndexIrShape = PrimaryIndexFieldsToIR<ID, PrimaryIndexFields>>(identifier: ID): ModelType<SetTypeSubArg<T, 'identifier', PrimaryIndexIR>, UsedMethod | 'identifier'>;
|
|
@@ -69,7 +75,7 @@ export type ModelType<T extends ModelTypeParamShape = ModelTypeParamShape, UsedM
|
|
|
69
75
|
* Used on the complete schema object.
|
|
70
76
|
*/
|
|
71
77
|
export type SchemaModelType<T extends BaseModelType = ModelType<ModelTypeParamShape, 'identifier'>, ModelName extends string = string, IsRDS extends boolean = false> = IsRDS extends true ? T & {
|
|
72
|
-
relationships<Param extends Record<string,
|
|
78
|
+
relationships<Param extends Record<string, ModelRelationshipField<any, string, any, any>> = Record<never, never>>(relationships: Param): Record<ModelName, Param>;
|
|
73
79
|
fields: T extends ModelType<infer R, any> ? R['fields'] : never;
|
|
74
80
|
} : T;
|
|
75
81
|
/**
|
|
@@ -85,6 +91,13 @@ export type InternalModel = SchemaModelType<ModelType<ModelTypeParamShape>, stri
|
|
|
85
91
|
* @returns true if the given value is a ModelSchema
|
|
86
92
|
*/
|
|
87
93
|
export declare const isSchemaModelType: (modelType: any | SchemaModelType) => modelType is ModelType<ModelTypeParamShape, "identifier">;
|
|
94
|
+
/**
|
|
95
|
+
* Model default identifier
|
|
96
|
+
*
|
|
97
|
+
* @param pk - primary key
|
|
98
|
+
* @param sk - secondary key
|
|
99
|
+
* @param compositeSk - composite secondary key
|
|
100
|
+
*/
|
|
88
101
|
export type ModelDefaultIdentifier = {
|
|
89
102
|
pk: {
|
|
90
103
|
readonly id: string;
|
package/dist/esm/RefType.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ export type RefTypeParamShape = {
|
|
|
21
21
|
authorization: Authorization<any, any, any>[];
|
|
22
22
|
};
|
|
23
23
|
type MutationOperations = 'create' | 'update' | 'delete';
|
|
24
|
+
/**
|
|
25
|
+
* Reference type definition interface
|
|
26
|
+
*
|
|
27
|
+
* @param T - The shape of the reference type
|
|
28
|
+
* @param K - The keys already defined
|
|
29
|
+
*/
|
|
24
30
|
export type RefType<T extends RefTypeParamShape, K extends keyof RefType<T> = never, Auth = undefined> = Omit<{
|
|
25
31
|
/**
|
|
26
32
|
* Marks a field as required.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ModelFieldType, string, __generated } from './ModelField.mjs';
|
|
2
|
-
import { ModelRelationshipTypes } from './
|
|
2
|
+
import { ModelRelationshipTypes } from './ModelRelationshipField.mjs';
|
|
3
3
|
import { accessData, accessSchemaData } from './Authorization.mjs';
|
|
4
4
|
import { CustomOperationNames } from './CustomOperation.mjs';
|
|
5
5
|
import { getBrand, brandSymbol } from './util/Brand.mjs';
|
|
@@ -158,7 +158,9 @@ function transformFunctionHandler(handlers, functionFieldName) {
|
|
|
158
158
|
else if (typeof handlerData.handler.getInstance === 'function') {
|
|
159
159
|
const fnName = `Fn${capitalize(functionFieldName)}${idx === 0 ? '' : `${idx + 1}`}`;
|
|
160
160
|
lambdaFunctionDefinition[fnName] = handlerData.handler;
|
|
161
|
-
const invocationTypeArg = handlerData.invocationType === 'Event'
|
|
161
|
+
const invocationTypeArg = handlerData.invocationType === 'Event'
|
|
162
|
+
? ', invocationType: Event)'
|
|
163
|
+
: ')';
|
|
162
164
|
gqlHandlerContent += `@function(name: "${fnName}"${invocationTypeArg} `;
|
|
163
165
|
}
|
|
164
166
|
else {
|
|
@@ -837,11 +839,12 @@ const schemaPreprocessor = (schema) => {
|
|
|
837
839
|
// - it only happens once per schema
|
|
838
840
|
// - downstream validation based on `getRefTypeForSchema` finds the EventInvocationResponse type
|
|
839
841
|
const containsAsyncLambdaCustomOperation = Object.entries(schema.data.types).find(([_, typeDef]) => {
|
|
840
|
-
return isCustomOperation(typeDef)
|
|
841
|
-
|
|
842
|
+
return (isCustomOperation(typeDef) &&
|
|
843
|
+
finalHandlerIsAsyncFunctionHandler(typeDef.data.handlers));
|
|
842
844
|
});
|
|
843
845
|
if (containsAsyncLambdaCustomOperation) {
|
|
844
|
-
schema.data.types['EventInvocationResponse'] =
|
|
846
|
+
schema.data.types['EventInvocationResponse'] =
|
|
847
|
+
eventInvocationResponseCustomType;
|
|
845
848
|
}
|
|
846
849
|
const topLevelTypes = sortTopLevelTypes(Object.entries(schema.data.types));
|
|
847
850
|
const { schemaAuth, functionSchemaAccess } = extractFunctionSchemaAccess(schema.data.authorization);
|
|
@@ -1019,7 +1022,9 @@ function validateCustomOperations(typeDef, typeName, authRules, getRefType) {
|
|
|
1019
1022
|
(opType === 'Query' || opType === 'Mutation' || opType === 'Generation')) {
|
|
1020
1023
|
// TODO: There should be a more elegant and readable way to handle this check.
|
|
1021
1024
|
// Maybe it's not even necessary anymore since we're the setting returnType in the handler() method.
|
|
1022
|
-
if (!handlers ||
|
|
1025
|
+
if (!handlers ||
|
|
1026
|
+
handlers.length === 0 ||
|
|
1027
|
+
handlers[handlers.length - 1][brandSymbol] !== 'asyncFunctionHandler') {
|
|
1023
1028
|
const typeDescription = opType === 'Generation' ? 'Generation Route' : `Custom ${opType}`;
|
|
1024
1029
|
throw new Error(`Invalid ${typeDescription} definition. A ${typeDescription} must include a return type. ${typeName} has no return type specified.`);
|
|
1025
1030
|
}
|
|
@@ -1077,10 +1082,12 @@ const isCustomHandler = (handler) => {
|
|
|
1077
1082
|
return Array.isArray(handler) && getBrand(handler[0]) === 'customHandler';
|
|
1078
1083
|
};
|
|
1079
1084
|
const isFunctionHandler = (handler) => {
|
|
1080
|
-
return Array.isArray(handler) &&
|
|
1085
|
+
return (Array.isArray(handler) &&
|
|
1086
|
+
['functionHandler', 'asyncFunctionHandler'].includes(getBrand(handler[0])));
|
|
1081
1087
|
};
|
|
1082
1088
|
const finalHandlerIsAsyncFunctionHandler = (handler) => {
|
|
1083
|
-
return Array.isArray(handler) &&
|
|
1089
|
+
return (Array.isArray(handler) &&
|
|
1090
|
+
getBrand(handler[handler.length - 1]) === 'asyncFunctionHandler');
|
|
1084
1091
|
};
|
|
1085
1092
|
const normalizeDataSourceName = (dataSource) => {
|
|
1086
1093
|
// default data source
|
|
@@ -1143,11 +1150,11 @@ const eventInvocationResponseCustomType = {
|
|
|
1143
1150
|
required: true,
|
|
1144
1151
|
array: false,
|
|
1145
1152
|
arrayRequired: false,
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1153
|
+
},
|
|
1154
|
+
},
|
|
1148
1155
|
},
|
|
1149
|
-
type: 'customType'
|
|
1150
|
-
}
|
|
1156
|
+
type: 'customType',
|
|
1157
|
+
},
|
|
1151
1158
|
};
|
|
1152
1159
|
function transformCustomOperations(typeDef, typeName, authRules, databaseType, getRefType) {
|
|
1153
1160
|
const { typeName: opType, handlers } = typeDef.data;
|
|
@@ -1239,8 +1246,8 @@ function validateRelationships(typeName, record, getInternalModel) {
|
|
|
1239
1246
|
// Create a structure representing the relationship for validation.
|
|
1240
1247
|
const relationship = getModelRelationship(typeName, { name: name, def: field.data }, getInternalModel);
|
|
1241
1248
|
// Validate that the references defined in the relationship follow the
|
|
1242
|
-
//
|
|
1243
|
-
|
|
1249
|
+
// relationship definition rules.
|
|
1250
|
+
validateRelationshipReferences(relationship);
|
|
1244
1251
|
}
|
|
1245
1252
|
}
|
|
1246
1253
|
/**
|
|
@@ -1266,7 +1273,7 @@ function describeConnectFieldRelationship(sourceField, sourceModelName) {
|
|
|
1266
1273
|
* Validates that the types of child model's reference fields match the types of the parent model's identifier fields.
|
|
1267
1274
|
* @param relationship The {@link ModelRelationship} to validate.
|
|
1268
1275
|
*/
|
|
1269
|
-
function
|
|
1276
|
+
function validateRelationshipReferences(relationship) {
|
|
1270
1277
|
const { parent, parentConnectionField, child, childConnectionField, references, } = relationship;
|
|
1271
1278
|
const parentIdentifiers = getIndentifierTypes(parent);
|
|
1272
1279
|
const childReferenceTypes = [];
|
|
@@ -1373,7 +1380,7 @@ function getAssociatedConnectionField(sourceModelName, sourceConnectionField, as
|
|
|
1373
1380
|
}
|
|
1374
1381
|
// In order to find that associated connection field, we need to do some validation that we'll depend on further downstream.
|
|
1375
1382
|
// 1. Field type matches the source model's type.
|
|
1376
|
-
// 2. A valid counterpart
|
|
1383
|
+
// 2. A valid counterpart relationship modifier is defined on the field. See `associatedRelationshipTypes` for more information.
|
|
1377
1384
|
// 3. The reference arguments provided to the field match (element count + string comparison) references passed to the source connection field.
|
|
1378
1385
|
return (connectionField.data.relatedModel === sourceModelName &&
|
|
1379
1386
|
associatedRelationshipOptions.includes(connectionField.data.type) &&
|