@aws-amplify/data-schema 1.1.5 → 1.2.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/ModelSchema.js +19 -0
- package/dist/cjs/ModelSchema.js.map +1 -1
- package/dist/cjs/SchemaProcessor.js +1 -4
- package/dist/cjs/SchemaProcessor.js.map +1 -1
- package/dist/cjs/util/Brand.js +4 -4
- package/dist/cjs/util/Brand.js.map +1 -1
- package/dist/cjs/util/usedMethods.js +4 -0
- package/dist/cjs/util/usedMethods.js.map +1 -0
- package/dist/esm/CustomOperation.d.ts +3 -3
- package/dist/esm/CustomType.d.ts +3 -3
- package/dist/esm/EnumType.d.ts +7 -9
- package/dist/esm/MappedTypes/CustomOperations.d.ts +3 -3
- package/dist/esm/MappedTypes/ExtractNonModelTypes.d.ts +4 -4
- package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +2 -2
- package/dist/esm/MappedTypes/ResolveSchema.d.ts +6 -6
- package/dist/esm/ModelField.d.ts +15 -12
- package/dist/esm/ModelSchema.d.ts +17 -5
- package/dist/esm/ModelSchema.mjs +20 -1
- package/dist/esm/ModelSchema.mjs.map +1 -1
- package/dist/esm/ModelType.d.ts +16 -12
- package/dist/esm/SchemaProcessor.mjs +1 -4
- package/dist/esm/SchemaProcessor.mjs.map +1 -1
- package/dist/esm/util/Brand.d.ts +1 -2
- package/dist/esm/util/Brand.mjs +1 -1
- package/dist/esm/util/Brand.mjs.map +1 -1
- package/dist/esm/util/usedMethods.d.ts +4 -0
- package/dist/esm/util/usedMethods.mjs +2 -0
- package/dist/esm/util/usedMethods.mjs.map +1 -0
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/CustomOperation.ts +3 -7
- package/src/CustomType.ts +4 -8
- package/src/EnumType.ts +16 -22
- package/src/MappedTypes/CustomOperations.ts +4 -6
- package/src/MappedTypes/ExtractNonModelTypes.ts +39 -40
- package/src/MappedTypes/ResolveFieldProperties.ts +2 -2
- package/src/MappedTypes/ResolveSchema.ts +18 -21
- package/src/ModelField.ts +33 -18
- package/src/ModelSchema.ts +51 -13
- package/src/ModelType.ts +30 -34
- package/src/SchemaProcessor.ts +23 -48
- package/src/util/Brand.ts +1 -1
- package/src/util/usedMethods.ts +5 -0
package/src/EnumType.ts
CHANGED
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { brandSymbol } from './util/Brand.js';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export type EnumTypeParamShape = {
|
|
9
|
-
type: 'enum';
|
|
10
|
-
values: readonly string[];
|
|
11
|
-
};
|
|
3
|
+
type EnumTypeParamShape<values extends readonly string[] = readonly string[]> =
|
|
4
|
+
{
|
|
5
|
+
type: 'enum';
|
|
6
|
+
values: values;
|
|
7
|
+
};
|
|
12
8
|
|
|
13
|
-
export
|
|
9
|
+
export interface EnumType<values extends readonly string[] = readonly string[]>
|
|
10
|
+
extends EnumTypeParamShape<values> {
|
|
11
|
+
[brandSymbol]: 'enum';
|
|
12
|
+
}
|
|
14
13
|
|
|
15
|
-
function _enum<
|
|
16
|
-
const data:
|
|
14
|
+
function _enum<values extends readonly string[]>(values: values) {
|
|
15
|
+
const data: EnumTypeParamShape = {
|
|
17
16
|
type: 'enum',
|
|
18
17
|
values,
|
|
19
18
|
};
|
|
20
19
|
|
|
21
|
-
return data as EnumType<
|
|
20
|
+
return data as EnumType<values>;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
type EnumTypeArgFactory<Values extends readonly string[]> = {
|
|
25
|
-
type: 'enum';
|
|
26
|
-
values: Values;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
23
|
/**
|
|
30
24
|
* this type param pattern allows us to infer literal type values from the array without using the `as const` suffix
|
|
31
25
|
*/
|
|
32
|
-
export function enumType<
|
|
33
|
-
values:
|
|
26
|
+
export function enumType<const values extends readonly string[]>(
|
|
27
|
+
values: values,
|
|
34
28
|
) {
|
|
35
|
-
return _enum
|
|
29
|
+
return _enum(values);
|
|
36
30
|
}
|
|
@@ -4,7 +4,7 @@ import type {
|
|
|
4
4
|
CustomOperation,
|
|
5
5
|
CustomOperationParamShape,
|
|
6
6
|
} from '../CustomOperation';
|
|
7
|
-
import type {
|
|
7
|
+
import type { BaseModelField } from '../ModelField';
|
|
8
8
|
import type { RefType, RefTypeParamShape } from '../RefType';
|
|
9
9
|
import type {
|
|
10
10
|
ResolveFieldRequirements,
|
|
@@ -66,10 +66,8 @@ export type CustomOpArguments<Shape extends CustomOperationParamShape> =
|
|
|
66
66
|
Shape['arguments'] extends null
|
|
67
67
|
? never
|
|
68
68
|
: ResolveFieldRequirements<{
|
|
69
|
-
[FieldName in keyof Shape['arguments']]: Shape['arguments'][FieldName] extends
|
|
70
|
-
infer R
|
|
71
|
-
any,
|
|
72
|
-
any
|
|
69
|
+
[FieldName in keyof Shape['arguments']]: Shape['arguments'][FieldName] extends BaseModelField<
|
|
70
|
+
infer R
|
|
73
71
|
>
|
|
74
72
|
? R
|
|
75
73
|
: never;
|
|
@@ -101,7 +99,7 @@ export type CustomOpReturnType<
|
|
|
101
99
|
NonModelTypes,
|
|
102
100
|
CustomOperations
|
|
103
101
|
>
|
|
104
|
-
: Shape['returnType'] extends
|
|
102
|
+
: Shape['returnType'] extends BaseModelField<infer R>
|
|
105
103
|
? R
|
|
106
104
|
: Shape['returnType'] extends CustomType<infer R>
|
|
107
105
|
?
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { UnionToIntersection } from '@aws-amplify/data-schema-types';
|
|
2
2
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
3
|
-
import type { EnumType
|
|
3
|
+
import type { EnumType } from '../EnumType';
|
|
4
4
|
import type {
|
|
5
5
|
SchemaTypes,
|
|
6
6
|
ModelAndCustomTypes,
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
import type { ModelType, ModelTypeParamShape } from '../ModelType';
|
|
10
10
|
|
|
11
11
|
export type NonModelTypesShape = {
|
|
12
|
-
enums: Record<string,
|
|
12
|
+
enums: Record<string, any>;
|
|
13
13
|
customTypes: Record<string, any>;
|
|
14
14
|
};
|
|
15
15
|
|
|
@@ -51,43 +51,44 @@ export type ExtractNonModelTypes<Schema> = ResolveNonModelFields<
|
|
|
51
51
|
export type ExtractAndFlattenImplicitNonModelTypesFromFields<
|
|
52
52
|
ParentTypeName extends string,
|
|
53
53
|
Fields,
|
|
54
|
-
> =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
FieldProp & string
|
|
67
|
-
>}`]: Fields[FieldProp];
|
|
68
|
-
}
|
|
69
|
-
: // if the field is a CustomType
|
|
70
|
-
FieldType extends CustomType<
|
|
71
|
-
infer CustomTypeShape extends CustomTypeParamShape
|
|
72
|
-
>
|
|
73
|
-
? // recursively extract to the Nested CustomType, and return the
|
|
74
|
-
// merge of the current CustomType and the extracted (if any)
|
|
75
|
-
ExtractAndFlattenImplicitNonModelTypesFromFields<
|
|
76
|
-
`${ParentTypeName}${Capitalize<FieldProp & string>}`,
|
|
77
|
-
CustomTypeShape['fields']
|
|
78
|
-
> & {
|
|
54
|
+
> =
|
|
55
|
+
{
|
|
56
|
+
// loop through the fields - omit the field that's not a non-model type
|
|
57
|
+
[FieldProp in keyof Fields as Fields[FieldProp] extends
|
|
58
|
+
| EnumType
|
|
59
|
+
| CustomType<CustomTypeParamShape>
|
|
60
|
+
? FieldProp
|
|
61
|
+
: never]: (
|
|
62
|
+
x: NonNullable<Fields[FieldProp]> extends infer FieldType
|
|
63
|
+
? // if the filed is a enum extract it as is
|
|
64
|
+
FieldType extends EnumType
|
|
65
|
+
? {
|
|
79
66
|
[Key in `${ParentTypeName}${Capitalize<
|
|
80
67
|
FieldProp & string
|
|
81
68
|
>}`]: Fields[FieldProp];
|
|
82
69
|
}
|
|
83
|
-
:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
70
|
+
: // if the field is a CustomType
|
|
71
|
+
FieldType extends CustomType<
|
|
72
|
+
infer CustomTypeShape extends CustomTypeParamShape
|
|
73
|
+
>
|
|
74
|
+
? // recursively extract to the Nested CustomType, and return the
|
|
75
|
+
// merge of the current CustomType and the extracted (if any)
|
|
76
|
+
ExtractAndFlattenImplicitNonModelTypesFromFields<
|
|
77
|
+
`${ParentTypeName}${Capitalize<FieldProp & string>}`,
|
|
78
|
+
CustomTypeShape['fields']
|
|
79
|
+
> & {
|
|
80
|
+
[Key in `${ParentTypeName}${Capitalize<
|
|
81
|
+
FieldProp & string
|
|
82
|
+
>}`]: Fields[FieldProp];
|
|
83
|
+
}
|
|
84
|
+
: never
|
|
85
|
+
: never,
|
|
86
|
+
) => void;
|
|
87
|
+
} extends Record<string, infer Func>
|
|
88
|
+
? Func extends (x: infer P) => void
|
|
89
|
+
? P // extract the union of all types of the `x` param used above
|
|
90
|
+
: Record<never, never> // return an empty mapped object (nothing got extracted)
|
|
91
|
+
: Record<never, never>; // return an empty mapped object (nothing got extracted)
|
|
91
92
|
|
|
92
93
|
/**
|
|
93
94
|
* Pulls out implicit, i.e. field-level non-model types from `ModelType` and
|
|
@@ -129,12 +130,10 @@ type ResolveNonModelTypes<
|
|
|
129
130
|
ResolvedSchema = SchemaTypes<Schema> & Extracted,
|
|
130
131
|
> = {
|
|
131
132
|
enums: {
|
|
132
|
-
[Model in keyof ResolvedSchema as ResolvedSchema[Model] extends EnumType
|
|
133
|
+
[Model in keyof ResolvedSchema as ResolvedSchema[Model] extends EnumType
|
|
133
134
|
? Model
|
|
134
|
-
: never]: ResolvedSchema[Model] extends EnumType<
|
|
135
|
-
|
|
136
|
-
>
|
|
137
|
-
? R['values'][number]
|
|
135
|
+
: never]: ResolvedSchema[Model] extends EnumType<infer values>
|
|
136
|
+
? values[number]
|
|
138
137
|
: never;
|
|
139
138
|
};
|
|
140
139
|
customTypes: {
|
|
@@ -20,7 +20,7 @@ import type { RefType, RefTypeParamShape } from '../RefType';
|
|
|
20
20
|
import type { NonModelTypesShape } from './ExtractNonModelTypes';
|
|
21
21
|
|
|
22
22
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
23
|
-
import type { EnumType
|
|
23
|
+
import type { EnumType } from '../EnumType';
|
|
24
24
|
import type {
|
|
25
25
|
CustomOperation,
|
|
26
26
|
CustomOperationParamShape,
|
|
@@ -214,7 +214,7 @@ type Intersection<
|
|
|
214
214
|
// TODO: this should probably happen in InjectImplicitModelFields instead. Keeping here for now to reduce refactor
|
|
215
215
|
// blast radius
|
|
216
216
|
export type ModelImpliedAuthFields<Schema extends GenericModelSchema<any>> = {
|
|
217
|
-
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType
|
|
217
|
+
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType
|
|
218
218
|
? never
|
|
219
219
|
: Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape>
|
|
220
220
|
? never
|
|
@@ -5,9 +5,9 @@ import type {
|
|
|
5
5
|
ModelRelationshipTypes,
|
|
6
6
|
RelationTypeFunctionOmitMapping,
|
|
7
7
|
} from '../ModelRelationalField';
|
|
8
|
-
import type {
|
|
8
|
+
import type { BaseModelField } from '../ModelField';
|
|
9
9
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
10
|
-
import type { EnumType
|
|
10
|
+
import type { EnumType } from '../EnumType';
|
|
11
11
|
import type { RefType, RefTypeParamShape } from '../RefType';
|
|
12
12
|
import type {
|
|
13
13
|
CustomOperation,
|
|
@@ -17,9 +17,8 @@ import type {
|
|
|
17
17
|
export type ResolveSchema<Schema> = FieldTypes<ModelTypes<SchemaTypes<Schema>>>;
|
|
18
18
|
|
|
19
19
|
// TODO: find better name
|
|
20
|
-
export type SchemaTypes<T> =
|
|
21
|
-
? T['data']['types']
|
|
22
|
-
: never;
|
|
20
|
+
export type SchemaTypes<T> =
|
|
21
|
+
T extends GenericModelSchema<any> ? T['data']['types'] : never;
|
|
23
22
|
|
|
24
23
|
/**
|
|
25
24
|
* Resolves model types
|
|
@@ -30,7 +29,7 @@ export type SchemaTypes<T> = T extends GenericModelSchema<any>
|
|
|
30
29
|
*/
|
|
31
30
|
export type ModelTypes<Schema> = {
|
|
32
31
|
[Model in keyof Schema as Schema[Model] extends
|
|
33
|
-
| EnumType
|
|
32
|
+
| EnumType
|
|
34
33
|
| CustomType<CustomTypeParamShape>
|
|
35
34
|
| CustomOperation<CustomOperationParamShape, any>
|
|
36
35
|
? never
|
|
@@ -45,14 +44,16 @@ export type ModelTypes<Schema> = {
|
|
|
45
44
|
*/
|
|
46
45
|
export type ModelAndCustomTypes<Schema> = {
|
|
47
46
|
[Model in keyof Schema as Schema[Model] extends
|
|
48
|
-
| EnumType
|
|
47
|
+
| EnumType
|
|
49
48
|
| CustomOperation<CustomOperationParamShape, any>
|
|
50
49
|
? never
|
|
51
|
-
:
|
|
50
|
+
: // TODO: This should use BaseModel, but seems to only work because it relies on
|
|
51
|
+
// omitting extra methods
|
|
52
|
+
Model]: Schema[Model] extends
|
|
53
|
+
| ModelType<any, any>
|
|
54
|
+
| CustomType<CustomTypeParamShape>
|
|
52
55
|
? Schema[Model]
|
|
53
|
-
:
|
|
54
|
-
? Schema[Model]
|
|
55
|
-
: never;
|
|
56
|
+
: never;
|
|
56
57
|
};
|
|
57
58
|
|
|
58
59
|
/**
|
|
@@ -62,11 +63,9 @@ export type ModelAndCustomTypes<Schema> = {
|
|
|
62
63
|
*/
|
|
63
64
|
export type FieldTypes<T> = {
|
|
64
65
|
[ModelProp in keyof T]: {
|
|
65
|
-
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends
|
|
66
|
+
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends BaseModelField<
|
|
66
67
|
// Match the most common field type to improve resolving performance
|
|
67
|
-
infer R
|
|
68
|
-
any,
|
|
69
|
-
any
|
|
68
|
+
infer R
|
|
70
69
|
>
|
|
71
70
|
? R
|
|
72
71
|
: T[ModelProp][FieldProp] extends RefType<
|
|
@@ -80,7 +79,7 @@ export type FieldTypes<T> = {
|
|
|
80
79
|
: T[ModelProp][FieldProp] | null
|
|
81
80
|
: // replace non-model types with Ref
|
|
82
81
|
T[ModelProp][FieldProp] extends
|
|
83
|
-
| EnumType
|
|
82
|
+
| EnumType
|
|
84
83
|
| CustomType<CustomTypeParamShape>
|
|
85
84
|
? RefType<{
|
|
86
85
|
link: `${Capitalize<ModelProp & string>}${Capitalize<
|
|
@@ -114,10 +113,8 @@ export type FieldTypes<T> = {
|
|
|
114
113
|
*/
|
|
115
114
|
export type FieldTypesOfCustomType<T> = {
|
|
116
115
|
[CustomTypeName in keyof T]: {
|
|
117
|
-
[FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends
|
|
118
|
-
infer R
|
|
119
|
-
any,
|
|
120
|
-
any
|
|
116
|
+
[FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends BaseModelField<
|
|
117
|
+
infer R
|
|
121
118
|
>
|
|
122
119
|
? R
|
|
123
120
|
: T[CustomTypeName][FieldProp] extends RefType<
|
|
@@ -131,7 +128,7 @@ export type FieldTypesOfCustomType<T> = {
|
|
|
131
128
|
: T[CustomTypeName][FieldProp] | null
|
|
132
129
|
: // replace non-model types with Ref
|
|
133
130
|
T[CustomTypeName][FieldProp] extends
|
|
134
|
-
| EnumType
|
|
131
|
+
| EnumType
|
|
135
132
|
| CustomType<CustomTypeParamShape>
|
|
136
133
|
? RefType<{
|
|
137
134
|
link: `${Capitalize<CustomTypeName & string>}${Capitalize<
|
package/src/ModelField.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { brand } from './util';
|
|
2
2
|
import { AllowModifier, Authorization, allow } from './Authorization';
|
|
3
|
+
import type { methodKeyOf, satisfy } from './util/usedMethods.js';
|
|
4
|
+
import type { brandSymbol } from './util/Brand.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Used to "attach" auth types to ModelField without exposing them on the builder.
|
|
@@ -34,7 +36,7 @@ export enum ModelFieldDataType {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
type FieldMeta = {
|
|
37
|
-
lastInvokedMethod: null |
|
|
39
|
+
lastInvokedMethod: null | methodKeyOf<ModelField>;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
type FieldData = {
|
|
@@ -67,56 +69,69 @@ export type ArrayField<T> = [T] extends [ModelFieldTypeParamInner]
|
|
|
67
69
|
? Array<T> | null // optional by default
|
|
68
70
|
: never;
|
|
69
71
|
|
|
72
|
+
export type BaseModelField<
|
|
73
|
+
T extends ModelFieldTypeParamOuter = ModelFieldTypeParamOuter,
|
|
74
|
+
> = ModelField<T, UsableModelFieldKey, any>;
|
|
75
|
+
|
|
76
|
+
export type UsableModelFieldKey = satisfy<
|
|
77
|
+
methodKeyOf<ModelField>,
|
|
78
|
+
'required' | 'default' | 'authorization'
|
|
79
|
+
>;
|
|
80
|
+
|
|
70
81
|
/**
|
|
71
82
|
* Public API for the chainable builder methods exposed by Model Field.
|
|
72
83
|
* The type is narrowing e.g., after calling .array() it will be omitted from intellisense suggestions
|
|
73
84
|
*
|
|
74
85
|
* @typeParam T - holds the JS data type of the field
|
|
75
|
-
* @typeParam
|
|
86
|
+
* @typeParam UsedMethod - union of strings representing already-invoked method names. Used to improve Intellisense
|
|
76
87
|
*/
|
|
77
88
|
export type ModelField<
|
|
78
|
-
T extends ModelFieldTypeParamOuter,
|
|
79
|
-
|
|
80
|
-
K extends keyof ModelField<T> = never,
|
|
89
|
+
T extends ModelFieldTypeParamOuter = ModelFieldTypeParamOuter,
|
|
90
|
+
UsedMethod extends UsableModelFieldKey = never,
|
|
81
91
|
Auth = undefined,
|
|
82
92
|
> = Omit<
|
|
83
93
|
{
|
|
94
|
+
// This is a lie. This property is never set at runtime. It's just used to smuggle auth types through.
|
|
95
|
+
[__auth]?: Auth;
|
|
96
|
+
[brandSymbol]: typeof brandName;
|
|
97
|
+
|
|
84
98
|
/**
|
|
85
99
|
* Marks a field as required.
|
|
86
100
|
*/
|
|
87
|
-
required(): ModelField<Required<T>,
|
|
101
|
+
required(): ModelField<Required<T>, UsedMethod | 'required'>;
|
|
88
102
|
// Exclude `optional` after calling array, because both the value and the array itself can be optional
|
|
89
103
|
/**
|
|
90
104
|
* Converts a field type definition to an array of the field type.
|
|
91
105
|
*/
|
|
92
|
-
array(): ModelField<ArrayField<T>, Exclude<
|
|
106
|
+
array(): ModelField<ArrayField<T>, Exclude<UsedMethod, 'required'>>;
|
|
93
107
|
// TODO: should be T, but .array breaks this constraint. Fix later
|
|
94
108
|
/**
|
|
95
109
|
* Sets a default value for the scalar type.
|
|
96
110
|
* @param value the default value
|
|
97
111
|
*/
|
|
98
|
-
default(
|
|
112
|
+
default(
|
|
113
|
+
value: ModelFieldTypeParamOuter,
|
|
114
|
+
): ModelField<T, UsedMethod | 'default'>;
|
|
99
115
|
/**
|
|
100
116
|
* Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
|
|
101
117
|
* multiple authorization rules for this field.
|
|
102
118
|
*/
|
|
103
119
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
104
|
-
callback: (
|
|
105
|
-
|
|
120
|
+
callback: (
|
|
121
|
+
allow: Omit<AllowModifier, 'resource'>,
|
|
122
|
+
) => AuthRuleType | AuthRuleType[],
|
|
123
|
+
): ModelField<T, UsedMethod | 'authorization', AuthRuleType>;
|
|
106
124
|
},
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// This is a lie. This property is never set at runtime. It's just used to smuggle auth types through.
|
|
110
|
-
[__auth]?: Auth;
|
|
111
|
-
} & Brand<typeof brandName>;
|
|
125
|
+
UsedMethod
|
|
126
|
+
>;
|
|
112
127
|
|
|
113
128
|
/**
|
|
114
129
|
* Internal representation of Model Field that exposes the `data` property.
|
|
115
130
|
* Used at buildtime.
|
|
116
131
|
*/
|
|
117
|
-
export
|
|
132
|
+
export interface InternalField extends ModelField {
|
|
118
133
|
data: FieldData;
|
|
119
|
-
}
|
|
134
|
+
}
|
|
120
135
|
|
|
121
136
|
/**
|
|
122
137
|
* Model Field Implementation
|
package/src/ModelSchema.ts
CHANGED
|
@@ -7,14 +7,13 @@ import type {
|
|
|
7
7
|
UnionToIntersection,
|
|
8
8
|
} from '@aws-amplify/data-schema-types';
|
|
9
9
|
import {
|
|
10
|
-
type ModelType,
|
|
11
|
-
type ModelTypeParamShape,
|
|
12
10
|
type InternalModel,
|
|
13
11
|
isSchemaModelType,
|
|
14
12
|
SchemaModelType,
|
|
15
13
|
AddRelationshipFieldsToModelTypeFields,
|
|
14
|
+
type BaseModelType,
|
|
16
15
|
} from './ModelType';
|
|
17
|
-
import type { EnumType
|
|
16
|
+
import type { EnumType } from './EnumType';
|
|
18
17
|
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
19
18
|
import type {
|
|
20
19
|
CustomOperation,
|
|
@@ -26,7 +25,7 @@ import type {
|
|
|
26
25
|
} from './CustomOperation';
|
|
27
26
|
import { processSchema } from './SchemaProcessor';
|
|
28
27
|
import { AllowModifier, SchemaAuthorization, allow } from './Authorization';
|
|
29
|
-
import { Brand, brand } from './util';
|
|
28
|
+
import { Brand, brand, getBrand } from './util';
|
|
30
29
|
import {
|
|
31
30
|
ModelRelationalField,
|
|
32
31
|
ModelRelationalFieldParamShape,
|
|
@@ -41,11 +40,15 @@ const ddbSchemaBrand = brand(ddbSchemaBrandName);
|
|
|
41
40
|
export type DDBSchemaBrand = Brand<typeof ddbSchemaBrandName>;
|
|
42
41
|
|
|
43
42
|
type SchemaContent =
|
|
44
|
-
|
|
|
43
|
+
| BaseModelType
|
|
45
44
|
| CustomType<CustomTypeParamShape>
|
|
46
|
-
| EnumType
|
|
45
|
+
| EnumType
|
|
47
46
|
| CustomOperation<CustomOperationParamShape, any>;
|
|
48
47
|
|
|
48
|
+
// The SQL-only `addToSchema` accepts all top-level entities, excepts models
|
|
49
|
+
type AddToSchemaContent = Exclude<SchemaContent, BaseModelType>;
|
|
50
|
+
type AddToSchemaContents = Record<string, AddToSchemaContent>;
|
|
51
|
+
|
|
49
52
|
type NonEmpty<T> = keyof T extends never ? never : T;
|
|
50
53
|
|
|
51
54
|
type ModelSchemaContents = Record<string, SchemaContent>;
|
|
@@ -76,10 +79,7 @@ export type BaseSchema<
|
|
|
76
79
|
> = {
|
|
77
80
|
data: T;
|
|
78
81
|
models: {
|
|
79
|
-
[TypeKey in keyof T['types']]: T['types'][TypeKey] extends
|
|
80
|
-
ModelTypeParamShape,
|
|
81
|
-
never | 'identifier'
|
|
82
|
-
>
|
|
82
|
+
[TypeKey in keyof T['types']]: T['types'][TypeKey] extends BaseModelType
|
|
83
83
|
? SchemaModelType<T['types'][TypeKey], TypeKey & string, IsRDS>
|
|
84
84
|
: never;
|
|
85
85
|
};
|
|
@@ -107,6 +107,7 @@ export type ModelSchema<
|
|
|
107
107
|
DDBSchemaBrand;
|
|
108
108
|
|
|
109
109
|
type RDSModelSchemaFunctions =
|
|
110
|
+
| 'addToSchema'
|
|
110
111
|
| 'addQueries'
|
|
111
112
|
| 'addMutations'
|
|
112
113
|
| 'addSubscriptions'
|
|
@@ -128,18 +129,33 @@ export type RDSModelSchema<
|
|
|
128
129
|
>,
|
|
129
130
|
> = Omit<
|
|
130
131
|
{
|
|
132
|
+
addToSchema: <AddedTypes extends AddToSchemaContents>(
|
|
133
|
+
types: AddedTypes,
|
|
134
|
+
) => RDSModelSchema<
|
|
135
|
+
SetTypeSubArg<T, 'types', T['types'] & AddedTypes>,
|
|
136
|
+
UsedMethods | 'addToSchema'
|
|
137
|
+
>;
|
|
138
|
+
/**
|
|
139
|
+
* @deprecated use `addToSchema()` to add operations to a SQL schema
|
|
140
|
+
*/
|
|
131
141
|
addQueries: <Queries extends Record<string, QueryCustomOperation>>(
|
|
132
142
|
types: Queries,
|
|
133
143
|
) => RDSModelSchema<
|
|
134
144
|
SetTypeSubArg<T, 'types', T['types'] & Queries>,
|
|
135
145
|
UsedMethods | 'addQueries'
|
|
136
146
|
>;
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated use `addToSchema()` to add operations to a SQL schema
|
|
149
|
+
*/
|
|
137
150
|
addMutations: <Mutations extends Record<string, MutationCustomOperation>>(
|
|
138
151
|
types: Mutations,
|
|
139
152
|
) => RDSModelSchema<
|
|
140
153
|
SetTypeSubArg<T, 'types', T['types'] & Mutations>,
|
|
141
154
|
UsedMethods | 'addMutations'
|
|
142
155
|
>;
|
|
156
|
+
/**
|
|
157
|
+
* @deprecated use `addToSchema()` to add operations to a SQL schema
|
|
158
|
+
*/
|
|
143
159
|
addSubscriptions: <
|
|
144
160
|
Subscriptions extends Record<string, SubscriptionCustomOperation>,
|
|
145
161
|
>(
|
|
@@ -258,6 +274,22 @@ export const isModelSchema = (
|
|
|
258
274
|
return typeof schema === 'object' && schema.data !== undefined;
|
|
259
275
|
};
|
|
260
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Ensures that only supported entities are being added to the SQL schema through `addToSchema`
|
|
279
|
+
* Models are not supported for brownfield SQL
|
|
280
|
+
*
|
|
281
|
+
* @param types - purposely widened to ModelSchemaContents, because we need to validate at runtime that a model is not being passed in here
|
|
282
|
+
*/
|
|
283
|
+
function validateAddToSchema(types: ModelSchemaContents): void {
|
|
284
|
+
for (const [name, type] of Object.entries(types)) {
|
|
285
|
+
if (getBrand(type) === 'modelType') {
|
|
286
|
+
throw new Error(
|
|
287
|
+
`Invalid value specified for ${name} in addToSchema(). Models cannot be manually added to a SQL schema.`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
261
293
|
function _rdsSchema<
|
|
262
294
|
T extends RDSModelSchemaParamShape,
|
|
263
295
|
DSC extends SchemaConfiguration<any, any>,
|
|
@@ -282,6 +314,12 @@ function _rdsSchema<
|
|
|
282
314
|
const { authorization: _, ...rest } = this;
|
|
283
315
|
return rest;
|
|
284
316
|
},
|
|
317
|
+
addToSchema(types: AddToSchemaContents): any {
|
|
318
|
+
validateAddToSchema(types);
|
|
319
|
+
this.data.types = { ...this.data.types, ...types };
|
|
320
|
+
const { addToSchema: _, ...rest } = this;
|
|
321
|
+
return rest;
|
|
322
|
+
},
|
|
285
323
|
addQueries(types: Record<string, QueryCustomOperation>): any {
|
|
286
324
|
this.data.types = { ...this.data.types, ...types };
|
|
287
325
|
const { addQueries: _, ...rest } = this;
|
|
@@ -334,7 +372,7 @@ function _rdsSchema<
|
|
|
334
372
|
models[newName] = currentType;
|
|
335
373
|
data.types[newName] = currentType;
|
|
336
374
|
models[newName].data.originalName = curName;
|
|
337
|
-
|
|
375
|
+
|
|
338
376
|
delete models[curName];
|
|
339
377
|
delete data.types[curName];
|
|
340
378
|
});
|
|
@@ -348,7 +386,7 @@ function _rdsSchema<
|
|
|
348
386
|
function _ddbSchema<
|
|
349
387
|
T extends ModelSchemaParamShape,
|
|
350
388
|
DSC extends SchemaConfiguration<any, any>,
|
|
351
|
-
>(types: T['types'], config: DSC) {
|
|
389
|
+
>(types: T['types'], config: DSC): ModelSchema<T> {
|
|
352
390
|
const data: ModelSchemaParamShape = {
|
|
353
391
|
types,
|
|
354
392
|
authorization: [],
|
|
@@ -369,7 +407,7 @@ function _ddbSchema<
|
|
|
369
407
|
},
|
|
370
408
|
models: filterSchemaModelTypes(data.types),
|
|
371
409
|
...ddbSchemaBrand,
|
|
372
|
-
}
|
|
410
|
+
} satisfies ModelSchema<any> as never;
|
|
373
411
|
}
|
|
374
412
|
|
|
375
413
|
type SchemaReturnType<
|