@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/package.json
CHANGED
package/src/Authorization.ts
CHANGED
|
@@ -103,6 +103,13 @@ export type ResourceAuthorizationData = {
|
|
|
103
103
|
operations?: ResourceOperation[];
|
|
104
104
|
};
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Container for authorization schema definition content.
|
|
108
|
+
*
|
|
109
|
+
* @param AuthStrategy The auth strategy to use.
|
|
110
|
+
* @param AuthField The field to use for owner authorization.
|
|
111
|
+
* @param AuthFieldPlurality Whether the field is plural or singular.
|
|
112
|
+
*/
|
|
106
113
|
export type Authorization<
|
|
107
114
|
AuthStrategy extends Strategy,
|
|
108
115
|
AuthField extends string | undefined,
|
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
Prettify,
|
|
15
15
|
} from '@aws-amplify/data-schema-types';
|
|
16
16
|
import type { ModelField } from '../../ModelField';
|
|
17
|
-
import type {
|
|
17
|
+
import type { ModelRelationshipField } from '../../ModelRelationshipField';
|
|
18
18
|
import type { EnumType } from '../../EnumType';
|
|
19
19
|
import type { CustomType, CustomTypeParamShape } from '../../CustomType';
|
|
20
20
|
import type { RefType } from '../../RefType';
|
|
@@ -164,7 +164,7 @@ type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<
|
|
|
164
164
|
T extends ModelTypeParamShape
|
|
165
165
|
? T['fields'][keyof T['fields']] extends
|
|
166
166
|
| ModelField<any, any, infer Auth>
|
|
167
|
-
|
|
|
167
|
+
| ModelRelationshipField<any, any, any, infer Auth>
|
|
168
168
|
| RefType<any, any, infer Auth>
|
|
169
169
|
? Auth extends Authorization<any, any, any>
|
|
170
170
|
? ImpliedAuthFields<Auth>
|
|
@@ -267,7 +267,7 @@ export type IndexQueryInput<
|
|
|
267
267
|
*/
|
|
268
268
|
|
|
269
269
|
/**
|
|
270
|
-
* All required fields and
|
|
270
|
+
* All required fields and relationship fields, exclude readonly fields
|
|
271
271
|
*/
|
|
272
272
|
type MutationInput<
|
|
273
273
|
Model extends ClientModel<any, any, any, any, any>,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BaseModelField } from '../../ModelField';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from '../../
|
|
3
|
+
ModelRelationshipField,
|
|
4
|
+
ModelRelationshipFieldParamShape,
|
|
5
|
+
} from '../../ModelRelationshipField';
|
|
6
6
|
import { EnumType } from '../../EnumType';
|
|
7
7
|
import { CustomType } from '../../CustomType';
|
|
8
8
|
import { RefType, RefTypeParamShape } from '../../RefType';
|
|
@@ -43,7 +43,7 @@ export type ResolveIndividualField<Bag extends Record<string, any>, T> =
|
|
|
43
43
|
? FieldShape
|
|
44
44
|
: T extends RefType<infer RefShape, any, any>
|
|
45
45
|
? ResolveRef<RefShape, Bag>
|
|
46
|
-
: T extends
|
|
46
|
+
: T extends ModelRelationshipField<infer RelationshipShape, any, any, any>
|
|
47
47
|
? ResolveRelationship<Bag, RelationshipShape>
|
|
48
48
|
: T extends CustomType<infer CT>
|
|
49
49
|
? ResolveFields<Bag, CT['fields']> | null
|
|
@@ -56,20 +56,20 @@ export type ResolveIndividualField<Bag extends Record<string, any>, T> =
|
|
|
56
56
|
*/
|
|
57
57
|
type ResolveRelationship<
|
|
58
58
|
Bag extends Record<string, any>,
|
|
59
|
-
RelationshipShape extends
|
|
60
|
-
> =
|
|
59
|
+
RelationshipShape extends ModelRelationshipFieldParamShape,
|
|
60
|
+
> =
|
|
61
|
+
DependentLazyLoaderOpIsAvailable<Bag, RelationshipShape> extends true
|
|
61
62
|
? LazyLoader<
|
|
62
63
|
RelationshipShape['valueRequired'] extends true
|
|
63
64
|
? Bag[RelationshipShape['relatedModel']]['type']
|
|
64
65
|
: Bag[RelationshipShape['relatedModel']]['type'] | null,
|
|
65
66
|
RelationshipShape['array']
|
|
66
67
|
>
|
|
67
|
-
: never
|
|
68
|
-
;
|
|
68
|
+
: never;
|
|
69
69
|
|
|
70
70
|
type DependentLazyLoaderOpIsAvailable<
|
|
71
71
|
Bag extends Record<string, any>,
|
|
72
|
-
RelationshipShape extends
|
|
72
|
+
RelationshipShape extends ModelRelationshipFieldParamShape,
|
|
73
73
|
> = RelationshipShape['relationshipType'] extends 'hasOne' | 'hasMany'
|
|
74
74
|
? // hasOne and hasMany depend on `list`
|
|
75
75
|
'list' extends keyof Bag[RelationshipShape['relatedModel']]['__meta']['disabledOperations']
|
|
@@ -78,8 +78,7 @@ type DependentLazyLoaderOpIsAvailable<
|
|
|
78
78
|
: // the relationship is a belongsTo, which depends on `get`
|
|
79
79
|
'get' extends keyof Bag[RelationshipShape['relatedModel']]['__meta']['disabledOperations']
|
|
80
80
|
? false
|
|
81
|
-
: true
|
|
82
|
-
;
|
|
81
|
+
: true;
|
|
83
82
|
|
|
84
83
|
type IsRequired<T> =
|
|
85
84
|
T extends BaseModelField<infer FieldShape>
|
|
@@ -88,7 +87,7 @@ type IsRequired<T> =
|
|
|
88
87
|
: true
|
|
89
88
|
: T extends RefType<infer RefShape, any, any>
|
|
90
89
|
? IsRefRequired<RefShape>
|
|
91
|
-
: T extends
|
|
90
|
+
: T extends ModelRelationshipField<any, any, any, any>
|
|
92
91
|
? true
|
|
93
92
|
: T extends CustomType<any> | EnumType<any>
|
|
94
93
|
? false
|
package/src/CustomOperation.ts
CHANGED
|
@@ -92,6 +92,13 @@ export type CustomOperationParamShape = {
|
|
|
92
92
|
input?: CustomOperationInput;
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Custom operation definition interface
|
|
97
|
+
*
|
|
98
|
+
* @param T - The shape of the custom operation
|
|
99
|
+
* @param K - The keys already defined
|
|
100
|
+
* @param B - The brand of the custom operation
|
|
101
|
+
*/
|
|
95
102
|
export type CustomOperation<
|
|
96
103
|
T extends CustomOperationParamShape,
|
|
97
104
|
K extends keyof CustomOperation<T> = never,
|
package/src/CustomType.ts
CHANGED
|
@@ -36,6 +36,11 @@ export type CustomTypeParamShape = {
|
|
|
36
36
|
fields: CustomTypeFields;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Custom type container
|
|
41
|
+
*
|
|
42
|
+
* @param T - The shape of the custom type container
|
|
43
|
+
*/
|
|
39
44
|
export type CustomType<T extends CustomTypeParamShape> = T &
|
|
40
45
|
Brand<'customType'>;
|
|
41
46
|
|
|
@@ -57,7 +62,7 @@ function _customType<T extends CustomTypeParamShape>(fields: T['fields']) {
|
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
/**
|
|
60
|
-
* Define a custom type. This type represents an inline, typed JSON object.
|
|
65
|
+
* Define a custom type. This type represents an inline, typed JSON object.
|
|
61
66
|
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/add-fields/#specify-a-custom-field-type}
|
|
62
67
|
* @param fields the fields to be added to the custom type
|
|
63
68
|
* @returns a custom type
|
|
@@ -77,12 +82,12 @@ function _customType<T extends CustomTypeParamShape>(fields: T['fields']) {
|
|
|
77
82
|
* lat: a.float(),
|
|
78
83
|
* long: a.float(),
|
|
79
84
|
* }),
|
|
80
|
-
*
|
|
85
|
+
*
|
|
81
86
|
* Post: a.model({
|
|
82
87
|
* location: a.ref('Location'),
|
|
83
88
|
* content: a.string(),
|
|
84
89
|
* }),
|
|
85
|
-
*
|
|
90
|
+
*
|
|
86
91
|
* User: a.model({
|
|
87
92
|
* lastKnownLocation: a.ref('Location'),
|
|
88
93
|
* }),
|
package/src/EnumType.ts
CHANGED
|
@@ -6,6 +6,11 @@ type EnumTypeParamShape<values extends readonly string[] = readonly string[]> =
|
|
|
6
6
|
values: values;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Enum type definition content
|
|
11
|
+
*
|
|
12
|
+
* @param values - The values of the enum
|
|
13
|
+
*/
|
|
9
14
|
export interface EnumType<values extends readonly string[] = readonly string[]>
|
|
10
15
|
extends EnumTypeParamShape<values> {
|
|
11
16
|
[brandSymbol]: 'enum';
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import { __modelMeta__ } from '../runtime/';
|
|
6
6
|
import type { PrimaryIndexIrShape } from '../util';
|
|
7
7
|
import type { ModelType } from '../ModelType';
|
|
8
|
-
import type {
|
|
8
|
+
import type { ModelRelationshipFieldParamShape } from '../ModelRelationshipField';
|
|
9
9
|
|
|
10
10
|
export type ModelIdentifier<T> = {
|
|
11
11
|
[Property in keyof T]: T[Property] extends ModelType<infer R, any>
|
|
@@ -23,7 +23,7 @@ export type ModelSecondaryIndexes<T> = {
|
|
|
23
23
|
: never;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
export type
|
|
26
|
+
export type RelationshipMetadata<
|
|
27
27
|
ResolvedSchema,
|
|
28
28
|
ResolvedFields extends Record<string, unknown>,
|
|
29
29
|
IdentifierMeta extends Record<string, { identifier: PrimaryIndexIrShape }>,
|
|
@@ -31,7 +31,7 @@ export type RelationalMetadata<
|
|
|
31
31
|
ExcludeEmpty<
|
|
32
32
|
{
|
|
33
33
|
[ModelName in keyof ResolvedSchema]: {
|
|
34
|
-
[Field in keyof ResolvedSchema[ModelName] as ResolvedSchema[ModelName][Field] extends
|
|
34
|
+
[Field in keyof ResolvedSchema[ModelName] as ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape
|
|
35
35
|
? ResolvedSchema[ModelName][Field]['relationshipType'] extends
|
|
36
36
|
| 'hasOne'
|
|
37
37
|
| 'belongsTo'
|
|
@@ -39,11 +39,11 @@ export type RelationalMetadata<
|
|
|
39
39
|
// E.g. if Post hasOne Author, we need to add a postAuthorId field to the Post model
|
|
40
40
|
ModelName
|
|
41
41
|
: never
|
|
42
|
-
: never]: ResolvedSchema[ModelName][Field] extends
|
|
43
|
-
? ResolvedSchema[ModelName][Field] extends
|
|
42
|
+
: never]: ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape
|
|
43
|
+
? ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape
|
|
44
44
|
? ResolvedSchema[ModelName][Field]['relationshipType'] extends 'hasMany'
|
|
45
45
|
? {
|
|
46
|
-
|
|
46
|
+
relationshipInputFields: Partial<
|
|
47
47
|
Record<
|
|
48
48
|
// For M:N and 1:M we add a parent model field to the child
|
|
49
49
|
`${Uncapitalize<ModelName & string>}`,
|
|
@@ -55,7 +55,7 @@ export type RelationalMetadata<
|
|
|
55
55
|
>;
|
|
56
56
|
}
|
|
57
57
|
: {
|
|
58
|
-
|
|
58
|
+
relationshipInputFields: Partial<
|
|
59
59
|
Record<
|
|
60
60
|
// For 1:1 and Belongs To we add a child model field to the parent
|
|
61
61
|
Field,
|
|
@@ -7,9 +7,9 @@ import type { ModelField } from '../ModelField';
|
|
|
7
7
|
import type { ModelType, ModelTypeParamShape } from '../ModelType';
|
|
8
8
|
import type { GenericModelSchema } from '../ModelSchema';
|
|
9
9
|
import type {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from '../
|
|
10
|
+
ModelRelationshipField,
|
|
11
|
+
ModelRelationshipFieldParamShape,
|
|
12
|
+
} from '../ModelRelationshipField';
|
|
13
13
|
import type { PrimaryIndexIrShape } from '../util/';
|
|
14
14
|
|
|
15
15
|
import type { ResolveSchema, SchemaTypes } from './ResolveSchema';
|
|
@@ -38,7 +38,7 @@ export type ResolveFieldProperties<
|
|
|
38
38
|
ResolvedSchema,
|
|
39
39
|
IdentifierMeta
|
|
40
40
|
>,
|
|
41
|
-
FieldsWithRelationships =
|
|
41
|
+
FieldsWithRelationships = ResolveModelsRelationshipAndRefFields<
|
|
42
42
|
FieldsWithInjectedImplicitFields,
|
|
43
43
|
NonModelTypes
|
|
44
44
|
>,
|
|
@@ -59,7 +59,7 @@ export type ResolveStaticFieldProperties<
|
|
|
59
59
|
ResolvedSchema & ImplicitModelsSchema,
|
|
60
60
|
never
|
|
61
61
|
>,
|
|
62
|
-
FieldsWithRelationships =
|
|
62
|
+
FieldsWithRelationships = ResolveModelsRelationshipAndRefFields<
|
|
63
63
|
FieldsWithInjectedImplicitFields,
|
|
64
64
|
NonModelTypes
|
|
65
65
|
>,
|
|
@@ -73,20 +73,20 @@ export type ResolveStaticFieldProperties<
|
|
|
73
73
|
type GetRelationshipRef<
|
|
74
74
|
T,
|
|
75
75
|
RM extends keyof T,
|
|
76
|
-
TypeArg extends
|
|
76
|
+
TypeArg extends ModelRelationshipFieldParamShape,
|
|
77
77
|
Flat extends boolean,
|
|
78
|
-
ResolvedModel =
|
|
78
|
+
ResolvedModel = ResolveRelationshipFieldsForModel<T, RM, Flat>,
|
|
79
79
|
Model = TypeArg['valueRequired'] extends true
|
|
80
80
|
? ResolvedModel
|
|
81
81
|
: ResolvedModel | null | undefined,
|
|
82
82
|
> = LazyLoader<Model, TypeArg['array']>;
|
|
83
83
|
|
|
84
|
-
type
|
|
84
|
+
type ResolveRelationshipFieldsForModel<
|
|
85
85
|
Schema,
|
|
86
86
|
ModelName extends keyof Schema,
|
|
87
87
|
Flat extends boolean,
|
|
88
88
|
> = {
|
|
89
|
-
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends
|
|
89
|
+
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends ModelRelationshipFieldParamShape
|
|
90
90
|
? Schema[ModelName][FieldName]['relatedModel'] extends keyof Schema
|
|
91
91
|
? GetRelationshipRef<
|
|
92
92
|
Schema,
|
|
@@ -147,7 +147,7 @@ export type ResolveFieldRequirements<Resolved> = Intersection<
|
|
|
147
147
|
ExtractNonNullableFieldsToRequiredFields<Resolved>
|
|
148
148
|
>;
|
|
149
149
|
|
|
150
|
-
type
|
|
150
|
+
type ResolveModelsRelationshipAndRefFields<
|
|
151
151
|
Schema,
|
|
152
152
|
NonModelTypes extends NonModelTypesShape,
|
|
153
153
|
Flat extends boolean = false,
|
|
@@ -159,7 +159,7 @@ type ResolveModelsRelationalAndRefFields<
|
|
|
159
159
|
any
|
|
160
160
|
> | null
|
|
161
161
|
? ResolveRef<NonModelTypes, R>
|
|
162
|
-
: Schema[ModelProp][FieldProp] extends
|
|
162
|
+
: Schema[ModelProp][FieldProp] extends ModelRelationshipFieldParamShape
|
|
163
163
|
? Schema[ModelProp][FieldProp]['relatedModel'] extends keyof Schema
|
|
164
164
|
? GetRelationshipRef<
|
|
165
165
|
Schema,
|
|
@@ -167,7 +167,7 @@ type ResolveModelsRelationalAndRefFields<
|
|
|
167
167
|
Schema[ModelProp][FieldProp],
|
|
168
168
|
Flat
|
|
169
169
|
>
|
|
170
|
-
: never // if the field value extends
|
|
170
|
+
: never // if the field value extends ModelRelationshipFieldShape "relatedModel" should always point to a Model (keyof Schema)
|
|
171
171
|
: Schema[ModelProp][FieldProp];
|
|
172
172
|
};
|
|
173
173
|
};
|
|
@@ -245,7 +245,7 @@ type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<
|
|
|
245
245
|
T extends ModelTypeParamShape
|
|
246
246
|
? T['fields'][keyof T['fields']] extends
|
|
247
247
|
| ModelField<any, any, infer Auth>
|
|
248
|
-
|
|
|
248
|
+
| ModelRelationshipField<any, any, any, infer Auth>
|
|
249
249
|
| RefType<any, any, infer Auth>
|
|
250
250
|
? Auth extends Authorization<any, any, any>
|
|
251
251
|
? ImpliedAuthFields<Auth>
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { ModelType } from '../ModelType';
|
|
2
2
|
import type { GenericModelSchema } from '../ModelSchema';
|
|
3
3
|
import type {
|
|
4
|
-
|
|
4
|
+
ModelRelationshipField,
|
|
5
5
|
ModelRelationshipTypes,
|
|
6
6
|
RelationTypeFunctionOmitMapping,
|
|
7
|
-
} from '../
|
|
7
|
+
} from '../ModelRelationshipField';
|
|
8
8
|
import type { BaseModelField } from '../ModelField';
|
|
9
9
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
10
10
|
import type { EnumType } from '../EnumType';
|
|
@@ -91,8 +91,8 @@ export type FieldTypes<T> = {
|
|
|
91
91
|
arrayRequired: false;
|
|
92
92
|
authorization: [];
|
|
93
93
|
}> | null
|
|
94
|
-
: // resolve
|
|
95
|
-
T[ModelProp][FieldProp] extends
|
|
94
|
+
: // resolve relationship and model fields to the their first type arg
|
|
95
|
+
T[ModelProp][FieldProp] extends ModelRelationshipField<
|
|
96
96
|
infer R,
|
|
97
97
|
string,
|
|
98
98
|
RelationTypeFunctionOmitMapping<ModelRelationshipTypes>,
|
|
@@ -107,7 +107,7 @@ export type FieldTypes<T> = {
|
|
|
107
107
|
* Resolves field types for a CustomType.
|
|
108
108
|
*
|
|
109
109
|
* This utility type is needed in addition to the `FieldTypes` utility type as
|
|
110
|
-
* without checking `
|
|
110
|
+
* without checking `ModelRelationshipField` can improve ~5% on resolving performance.
|
|
111
111
|
*
|
|
112
112
|
* Non-model types are replaced with Refs. Refs remain and are resolved in ResolveFieldProperties.ts
|
|
113
113
|
*/
|
package/src/ModelField.ts
CHANGED
|
@@ -55,9 +55,11 @@ type FieldData = {
|
|
|
55
55
|
|
|
56
56
|
type ModelFieldTypeParamInner = string | number | boolean | Date | Json | null;
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
/**
|
|
59
|
+
* A precise, recursive Json type blows the type calculation stack without installing
|
|
60
|
+
* explicit `Json extends T ? short-circuit : ...` type checks all over the place.
|
|
61
|
+
* We may take that on later. But, this is a good-enough approximation for now.
|
|
62
|
+
*/
|
|
61
63
|
export type Json = null | string | number | boolean | object | any[];
|
|
62
64
|
|
|
63
65
|
export type ModelFieldTypeParamOuter =
|
|
@@ -7,8 +7,11 @@ import { AllowModifier, Authorization, allow } from './Authorization';
|
|
|
7
7
|
*/
|
|
8
8
|
export const __auth = Symbol('__auth');
|
|
9
9
|
|
|
10
|
-
const brandName = '
|
|
10
|
+
const brandName = 'modelRelationshipField';
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Model relationship types
|
|
14
|
+
*/
|
|
12
15
|
export enum ModelRelationshipTypes {
|
|
13
16
|
hasOne = 'hasOne',
|
|
14
17
|
hasMany = 'hasMany',
|
|
@@ -17,7 +20,7 @@ export enum ModelRelationshipTypes {
|
|
|
17
20
|
|
|
18
21
|
type RelationshipTypes = `${ModelRelationshipTypes}`;
|
|
19
22
|
|
|
20
|
-
type
|
|
23
|
+
type ModelRelationshipFieldData = {
|
|
21
24
|
fieldType: 'model';
|
|
22
25
|
type: ModelRelationshipTypes;
|
|
23
26
|
relatedModel: string;
|
|
@@ -28,7 +31,7 @@ type ModelRelationalFieldData = {
|
|
|
28
31
|
authorization: Authorization<any, any, any>[];
|
|
29
32
|
};
|
|
30
33
|
|
|
31
|
-
export type
|
|
34
|
+
export type ModelRelationshipFieldParamShape = {
|
|
32
35
|
type: 'model';
|
|
33
36
|
relationshipType: string;
|
|
34
37
|
relatedModel: string;
|
|
@@ -38,23 +41,23 @@ export type ModelRelationalFieldParamShape = {
|
|
|
38
41
|
arrayRequired: boolean;
|
|
39
42
|
};
|
|
40
43
|
|
|
41
|
-
type
|
|
42
|
-
T extends
|
|
44
|
+
type ModelRelationshipFieldFunctions<
|
|
45
|
+
T extends ModelRelationshipFieldParamShape,
|
|
43
46
|
// RM adds structural separation with ModelField; easier to identify it when mapping to ClientTypes
|
|
44
47
|
RM extends string | symbol,
|
|
45
|
-
K extends keyof
|
|
48
|
+
K extends keyof ModelRelationshipField<T, RM> = never,
|
|
46
49
|
> = {
|
|
47
50
|
/**
|
|
48
51
|
* When set, it requires the value of the relationship type to be required.
|
|
49
52
|
*/
|
|
50
|
-
valueRequired():
|
|
53
|
+
valueRequired(): ModelRelationshipField<
|
|
51
54
|
SetTypeSubArg<T, 'valueRequired', true>,
|
|
52
55
|
K | 'valueRequired'
|
|
53
56
|
>;
|
|
54
57
|
/**
|
|
55
58
|
* When set, it requires the relationship to always return a value
|
|
56
59
|
*/
|
|
57
|
-
required():
|
|
60
|
+
required(): ModelRelationshipField<
|
|
58
61
|
// The RM generic cannot be "required" since no such field exists
|
|
59
62
|
SetTypeSubArg<T, 'arrayRequired', true>,
|
|
60
63
|
K | 'required'
|
|
@@ -65,16 +68,22 @@ type ModelRelationalFieldFunctions<
|
|
|
65
68
|
*/
|
|
66
69
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
67
70
|
callback: (allow: AllowModifier) => AuthRuleType | AuthRuleType[],
|
|
68
|
-
):
|
|
71
|
+
): ModelRelationshipField<T, K | 'authorization', K, AuthRuleType>;
|
|
69
72
|
};
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Model relationship field definition interface
|
|
76
|
+
*
|
|
77
|
+
* @param T - The shape of the model relationship field
|
|
78
|
+
* @param RM - Adds structural separation with ModelField; easier to identify it when mapping to ClientTypes
|
|
79
|
+
* @param K - The keys already defined
|
|
80
|
+
*/
|
|
81
|
+
export type ModelRelationshipField<
|
|
82
|
+
T extends ModelRelationshipFieldParamShape,
|
|
74
83
|
RM extends string | symbol,
|
|
75
|
-
K extends keyof
|
|
84
|
+
K extends keyof ModelRelationshipField<T, RM> = never,
|
|
76
85
|
Auth = undefined,
|
|
77
|
-
> = Omit<
|
|
86
|
+
> = Omit<ModelRelationshipFieldFunctions<T, RM, K>, K> & {
|
|
78
87
|
// This is a lie. This property is never set at runtime. It's just used to smuggle auth types through.
|
|
79
88
|
[__auth]?: Auth;
|
|
80
89
|
} & Brand<typeof brandName>;
|
|
@@ -83,15 +92,15 @@ export type ModelRelationalField<
|
|
|
83
92
|
* Internal representation of Model Field that exposes the `data` property.
|
|
84
93
|
* Used at buildtime.
|
|
85
94
|
*/
|
|
86
|
-
export type
|
|
87
|
-
|
|
95
|
+
export type InternalRelationshipField = ModelRelationshipField<
|
|
96
|
+
ModelRelationshipFieldParamShape,
|
|
88
97
|
string,
|
|
89
98
|
never
|
|
90
99
|
> & {
|
|
91
|
-
data:
|
|
100
|
+
data: ModelRelationshipFieldData;
|
|
92
101
|
};
|
|
93
102
|
|
|
94
|
-
const
|
|
103
|
+
const relationshipModifiers = [
|
|
95
104
|
'required',
|
|
96
105
|
'valueRequired',
|
|
97
106
|
'authorization',
|
|
@@ -99,7 +108,7 @@ const relationalModifiers = [
|
|
|
99
108
|
|
|
100
109
|
const relationModifierMap: Record<
|
|
101
110
|
`${ModelRelationshipTypes}`,
|
|
102
|
-
(typeof
|
|
111
|
+
(typeof relationshipModifiers)[number][]
|
|
103
112
|
> = {
|
|
104
113
|
belongsTo: ['authorization'],
|
|
105
114
|
hasMany: ['valueRequired', 'authorization'],
|
|
@@ -116,12 +125,12 @@ export type RelationTypeFunctionOmitMapping<
|
|
|
116
125
|
? 'valueRequired'
|
|
117
126
|
: never;
|
|
118
127
|
|
|
119
|
-
function
|
|
120
|
-
T extends
|
|
128
|
+
function _modelRelationshipField<
|
|
129
|
+
T extends ModelRelationshipFieldParamShape,
|
|
121
130
|
RelatedModel extends string,
|
|
122
131
|
RT extends ModelRelationshipTypes,
|
|
123
132
|
>(type: RT, relatedModel: RelatedModel, references: string[]) {
|
|
124
|
-
const data:
|
|
133
|
+
const data: ModelRelationshipFieldData = {
|
|
125
134
|
relatedModel,
|
|
126
135
|
type,
|
|
127
136
|
fieldType: 'model',
|
|
@@ -150,7 +159,7 @@ function _modelRelationalField<
|
|
|
150
159
|
|
|
151
160
|
return this;
|
|
152
161
|
},
|
|
153
|
-
} as
|
|
162
|
+
} as ModelRelationshipField<T, RelatedModel>;
|
|
154
163
|
|
|
155
164
|
const builder = Object.fromEntries(
|
|
156
165
|
relationModifierMap[type].map((key) => [
|
|
@@ -162,14 +171,21 @@ function _modelRelationalField<
|
|
|
162
171
|
return {
|
|
163
172
|
...builder,
|
|
164
173
|
data,
|
|
165
|
-
} as
|
|
174
|
+
} as InternalRelationshipField as ModelRelationshipField<
|
|
166
175
|
T,
|
|
167
176
|
RelatedModel,
|
|
168
177
|
RelationTypeFunctionOmitMapping<typeof type>
|
|
169
178
|
>;
|
|
170
179
|
}
|
|
171
180
|
|
|
172
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Model relationship type definition content
|
|
183
|
+
*
|
|
184
|
+
* @param RM - The related model name
|
|
185
|
+
* @param RT - The relationship type
|
|
186
|
+
* @param IsArray - Whether the relationship is an array
|
|
187
|
+
*/
|
|
188
|
+
export type ModelRelationshipTypeArgFactory<
|
|
173
189
|
RM extends string,
|
|
174
190
|
RT extends RelationshipTypes,
|
|
175
191
|
IsArray extends boolean,
|
|
@@ -212,8 +228,8 @@ export function hasOne<RM extends string>(
|
|
|
212
228
|
relatedModel: RM,
|
|
213
229
|
references: string | string[],
|
|
214
230
|
) {
|
|
215
|
-
return
|
|
216
|
-
|
|
231
|
+
return _modelRelationshipField<
|
|
232
|
+
ModelRelationshipTypeArgFactory<RM, ModelRelationshipTypes.hasOne, false>,
|
|
217
233
|
RM,
|
|
218
234
|
ModelRelationshipTypes.hasOne
|
|
219
235
|
>(
|
|
@@ -235,7 +251,7 @@ export function hasOne<RM extends string>(
|
|
|
235
251
|
* team: a.belongsTo('Team', 'teamId'),
|
|
236
252
|
* })
|
|
237
253
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
238
|
-
*
|
|
254
|
+
*
|
|
239
255
|
* Team: a.model({
|
|
240
256
|
* mantra: a.string().required(),
|
|
241
257
|
* // 3. Create a hasMany relationship with the reference field
|
|
@@ -253,8 +269,8 @@ export function hasMany<RM extends string>(
|
|
|
253
269
|
relatedModel: RM,
|
|
254
270
|
references: string | string[],
|
|
255
271
|
) {
|
|
256
|
-
return
|
|
257
|
-
|
|
272
|
+
return _modelRelationshipField<
|
|
273
|
+
ModelRelationshipTypeArgFactory<RM, ModelRelationshipTypes.hasMany, true>,
|
|
258
274
|
RM,
|
|
259
275
|
ModelRelationshipTypes.hasMany
|
|
260
276
|
>(
|
|
@@ -268,8 +284,8 @@ export function hasMany<RM extends string>(
|
|
|
268
284
|
* Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
|
|
269
285
|
* The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
|
|
270
286
|
* parent to the related model.
|
|
271
|
-
*
|
|
272
|
-
* @example
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
273
289
|
* // one-to-many relationship
|
|
274
290
|
* const schema = a.schema({
|
|
275
291
|
* Member: a.model({
|
|
@@ -280,7 +296,7 @@ export function hasMany<RM extends string>(
|
|
|
280
296
|
* team: a.belongsTo('Team', 'teamId'),
|
|
281
297
|
* })
|
|
282
298
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
283
|
-
*
|
|
299
|
+
*
|
|
284
300
|
* Team: a.model({
|
|
285
301
|
* mantra: a.string().required(),
|
|
286
302
|
* // 3. Create a hasMany relationship with the reference field
|
|
@@ -315,8 +331,12 @@ export function belongsTo<RM extends string>(
|
|
|
315
331
|
relatedModel: RM,
|
|
316
332
|
references: string | string[],
|
|
317
333
|
) {
|
|
318
|
-
return
|
|
319
|
-
|
|
334
|
+
return _modelRelationshipField<
|
|
335
|
+
ModelRelationshipTypeArgFactory<
|
|
336
|
+
RM,
|
|
337
|
+
ModelRelationshipTypes.belongsTo,
|
|
338
|
+
false
|
|
339
|
+
>,
|
|
320
340
|
RM,
|
|
321
341
|
ModelRelationshipTypes.belongsTo
|
|
322
342
|
>(
|
package/src/ModelSchema.ts
CHANGED
|
@@ -27,9 +27,9 @@ import { processSchema } from './SchemaProcessor';
|
|
|
27
27
|
import { AllowModifier, SchemaAuthorization, allow } from './Authorization';
|
|
28
28
|
import { Brand, brand, getBrand, RenameUsingTuples } from './util';
|
|
29
29
|
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
} from './
|
|
30
|
+
ModelRelationshipField,
|
|
31
|
+
ModelRelationshipFieldParamShape,
|
|
32
|
+
} from './ModelRelationshipField';
|
|
33
33
|
import { ConversationType } from './ai/ConversationType';
|
|
34
34
|
|
|
35
35
|
export { ModelType } from './ModelType';
|
|
@@ -104,6 +104,12 @@ export type BaseSchema<
|
|
|
104
104
|
export type GenericModelSchema<T extends ModelSchemaParamShape> =
|
|
105
105
|
BaseSchema<T> & Brand<typeof rdsSchemaBrandName | typeof ddbSchemaBrandName>;
|
|
106
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Model schema definition interface
|
|
109
|
+
*
|
|
110
|
+
* @param T - The shape of the model schema
|
|
111
|
+
* @param UsedMethods - The method keys already defined
|
|
112
|
+
*/
|
|
107
113
|
export type ModelSchema<
|
|
108
114
|
T extends ModelSchemaParamShape,
|
|
109
115
|
UsedMethods extends 'authorization' | 'relationships' = never,
|
|
@@ -138,7 +144,7 @@ type OmitFromEach<Models, Modifier extends string> = {
|
|
|
138
144
|
|
|
139
145
|
type RelationshipTemplate = Record<
|
|
140
146
|
string,
|
|
141
|
-
|
|
147
|
+
ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>
|
|
142
148
|
>;
|
|
143
149
|
|
|
144
150
|
export type RDSModelSchema<
|
|
@@ -252,7 +258,7 @@ type ModelWithRelationships<
|
|
|
252
258
|
> = ModelName extends keyof RelationshipMap
|
|
253
259
|
? RelationshipMap[ModelName] extends Record<
|
|
254
260
|
string,
|
|
255
|
-
|
|
261
|
+
ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>
|
|
256
262
|
>
|
|
257
263
|
? AddRelationshipFieldsToModelTypeFields<
|
|
258
264
|
Types[ModelName],
|
|
@@ -440,7 +446,11 @@ type SchemaReturnType<
|
|
|
440
446
|
DE extends DatasourceEngine,
|
|
441
447
|
Types extends ModelSchemaContents,
|
|
442
448
|
> = DE extends 'dynamodb'
|
|
443
|
-
? ModelSchema<{
|
|
449
|
+
? ModelSchema<{
|
|
450
|
+
types: Types;
|
|
451
|
+
authorization: [];
|
|
452
|
+
configuration: any;
|
|
453
|
+
}>
|
|
444
454
|
: RDSModelSchema<{
|
|
445
455
|
types: Types;
|
|
446
456
|
authorization: [];
|