@aws-amplify/data-schema 0.13.6 → 0.13.8
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/lib-esm/src/CustomOperation.d.ts +4 -5
- package/lib-esm/src/CustomOperation.js +3 -1
- package/lib-esm/src/CustomType.d.ts +5 -3
- package/lib-esm/src/Handler.d.ts +43 -18
- package/lib-esm/src/Handler.js +22 -9
- package/lib-esm/src/MappedTypes/ExtractNonModelTypes.d.ts +51 -13
- package/lib-esm/src/MappedTypes/ResolveFieldProperties.d.ts +15 -10
- package/lib-esm/src/MappedTypes/ResolveSchema.d.ts +27 -2
- package/lib-esm/src/SchemaProcessor.d.ts +1 -1
- package/lib-esm/src/SchemaProcessor.js +214 -20
- package/lib-esm/src/util/Brand.d.ts +6 -0
- package/lib-esm/src/util/Brand.js +10 -1
- package/lib-esm/src/util/index.d.ts +1 -1
- package/lib-esm/src/util/index.js +2 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/lib-esm/src/types.d.ts +0 -18
- package/lib-esm/src/types.js +0 -8
|
@@ -5,14 +5,14 @@ import { Authorization } from './Authorization';
|
|
|
5
5
|
import { RefType, InternalRef } from './RefType';
|
|
6
6
|
import { EnumType, EnumTypeParamShape } from './EnumType';
|
|
7
7
|
import { CustomType } from './CustomType';
|
|
8
|
-
import { Handler } from './Handler';
|
|
8
|
+
import type { HandlerType as Handler } from './Handler';
|
|
9
9
|
type CustomArguments = Record<string, ModelField<any, any> | EnumType<EnumTypeParamShape>>;
|
|
10
10
|
type CustomReturnType = RefType<any> | CustomType<any>;
|
|
11
11
|
type CustomFunctionRefType = string;
|
|
12
12
|
type InternalCustomArguments = Record<string, InternalField>;
|
|
13
13
|
type InternalCustomReturnType = InternalRef;
|
|
14
|
+
type HandlerInputType = Handler | Handler[number];
|
|
14
15
|
declare const brandName = "customOperation";
|
|
15
|
-
type HandlerInputType = Handler | Handler[];
|
|
16
16
|
export declare const CustomOperationNames: readonly ["Query", "Mutation", "Subscription"];
|
|
17
17
|
type CustomOperationName = (typeof CustomOperationNames)[number];
|
|
18
18
|
type CustomData = {
|
|
@@ -21,7 +21,7 @@ type CustomData = {
|
|
|
21
21
|
functionRef: string | null;
|
|
22
22
|
authorization: Authorization<any, any, any>[];
|
|
23
23
|
typeName: CustomOperationName;
|
|
24
|
-
handlers: Handler
|
|
24
|
+
handlers: Handler | null;
|
|
25
25
|
};
|
|
26
26
|
type InternalCustomData = CustomData & {
|
|
27
27
|
arguments: InternalCustomArguments;
|
|
@@ -35,14 +35,13 @@ export type CustomOperationParamShape = {
|
|
|
35
35
|
functionRef: string | null;
|
|
36
36
|
authorization: Authorization<any, any, any>[];
|
|
37
37
|
typeName: CustomOperationName;
|
|
38
|
-
handlers: Handler[] | null;
|
|
39
38
|
};
|
|
40
39
|
export type CustomOperation<T extends CustomOperationParamShape, K extends keyof CustomOperation<T> = never> = Omit<{
|
|
41
40
|
arguments<Arguments extends CustomArguments>(args: Arguments): CustomOperation<SetTypeSubArg<T, 'arguments', Arguments>, K | 'arguments'>;
|
|
42
41
|
returns<ReturnType extends CustomReturnType>(returnType: ReturnType): CustomOperation<SetTypeSubArg<T, 'returnType', ReturnType>, K | 'returns'>;
|
|
43
42
|
function<FunctionRef extends CustomFunctionRefType>(functionRefOrName: FunctionRef): CustomOperation<SetTypeSubArg<T, 'functionRef', FunctionRef>, K | 'function'>;
|
|
44
43
|
authorization<AuthRuleType extends Authorization<any, any, any>>(rules: AuthRuleType[]): CustomOperation<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization'>;
|
|
45
|
-
handler(handlers: HandlerInputType): CustomOperation<
|
|
44
|
+
handler(handlers: HandlerInputType): CustomOperation<T, K | 'handler'>;
|
|
46
45
|
}, K> & Brand<typeof brandName>;
|
|
47
46
|
/**
|
|
48
47
|
* Internal representation of Custom Type that exposes the `data` property.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { Brand } from './util';
|
|
2
|
-
import { ModelField, InternalField, ModelFieldTypeParamOuter } from './ModelField';
|
|
1
|
+
import type { Brand } from './util';
|
|
2
|
+
import type { ModelField, InternalField, ModelFieldTypeParamOuter } from './ModelField';
|
|
3
|
+
import type { RefType } from './RefType';
|
|
4
|
+
import type { EnumType, EnumTypeParamShape } from './EnumType';
|
|
3
5
|
/**
|
|
4
6
|
* Custom Types
|
|
5
7
|
*
|
|
@@ -7,7 +9,7 @@ import { ModelField, InternalField, ModelFieldTypeParamOuter } from './ModelFiel
|
|
|
7
9
|
*
|
|
8
10
|
*/
|
|
9
11
|
export type CustomTypeAllowedModifiers = 'authorization' | 'array' | 'required';
|
|
10
|
-
type CustomTypeFields = Record<string, ModelField<ModelFieldTypeParamOuter, CustomTypeAllowedModifiers, any>>;
|
|
12
|
+
type CustomTypeFields = Record<string, ModelField<ModelFieldTypeParamOuter, CustomTypeAllowedModifiers, any> | RefType<any, any, any> | EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape>>;
|
|
11
13
|
type InternalModelFields = Record<string, InternalField>;
|
|
12
14
|
type CustomTypeData = {
|
|
13
15
|
fields: CustomTypeFields;
|
package/lib-esm/src/Handler.d.ts
CHANGED
|
@@ -1,22 +1,47 @@
|
|
|
1
1
|
import { Brand } from './util';
|
|
2
|
-
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
} &
|
|
11
|
-
declare function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
import { RefType } from './RefType';
|
|
3
|
+
export type HandlerType = InlineSqlHandler[] | SqlReferenceHandler[] | CustomHandler[] | FunctionHandler[];
|
|
4
|
+
declare const dataSymbol: unique symbol;
|
|
5
|
+
type AllHandlers = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
|
|
6
|
+
export declare function getHandlerData<H extends AllHandlers>(handler: H): H[typeof dataSymbol];
|
|
7
|
+
declare const inlineSqlBrand = "inlineSql";
|
|
8
|
+
export type InlineSqlHandler = {
|
|
9
|
+
[dataSymbol]: string;
|
|
10
|
+
} & Brand<typeof inlineSqlBrand>;
|
|
11
|
+
declare function inlineSql(sql: string): InlineSqlHandler;
|
|
12
|
+
declare const sqlReferenceBrand = "sqlReference";
|
|
13
|
+
export type SqlReferenceHandler = {
|
|
14
|
+
[dataSymbol]: string;
|
|
15
|
+
} & Brand<typeof sqlReferenceBrand>;
|
|
16
|
+
declare function sqlReference(sqlReference: string): SqlReferenceHandler;
|
|
17
|
+
type CustomHandlerInput = {
|
|
18
|
+
/**
|
|
19
|
+
* The data source used by the function.
|
|
20
|
+
* Can reference a model in the schema with a.ref('ModelName') or any string data source name configured in your API
|
|
21
|
+
*
|
|
22
|
+
* Defaults to 'NONE_DS'
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
dataSource?: string | RefType<any, any, any>;
|
|
26
|
+
/**
|
|
27
|
+
* The path to the file that contains the function entry point.
|
|
28
|
+
* If this is a relative path, it is computed relative to the file where this handler is defined
|
|
29
|
+
*/
|
|
30
|
+
entry: string;
|
|
31
|
+
};
|
|
32
|
+
export type CustomHandlerData = CustomHandlerInput & {
|
|
33
|
+
stack: string | undefined;
|
|
34
|
+
};
|
|
35
|
+
declare const customHandlerBrand = "customHandler";
|
|
36
|
+
export type CustomHandler = {
|
|
37
|
+
[dataSymbol]: CustomHandlerData;
|
|
38
|
+
} & Brand<typeof customHandlerBrand>;
|
|
39
|
+
declare function custom(customHandler: CustomHandlerInput): CustomHandler;
|
|
40
|
+
declare const functionHandlerBrand = "functionHandler";
|
|
41
|
+
export type FunctionHandler = {
|
|
42
|
+
[dataSymbol]: (...args: any[]) => any;
|
|
43
|
+
} & Brand<typeof functionHandlerBrand>;
|
|
44
|
+
declare function fcn(fn: (...args: any[]) => any): FunctionHandler;
|
|
20
45
|
export declare const handler: {
|
|
21
46
|
inlineSql: typeof inlineSql;
|
|
22
47
|
sqlReference: typeof sqlReference;
|
package/lib-esm/src/Handler.js
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handler = void 0;
|
|
3
|
+
exports.handler = exports.getHandlerData = void 0;
|
|
4
4
|
const util_1 = require("./util");
|
|
5
|
-
const
|
|
6
|
-
function buildHandler() {
|
|
5
|
+
const dataSymbol = Symbol('Data');
|
|
6
|
+
function buildHandler(brandName) {
|
|
7
7
|
return (0, util_1.brand)(brandName);
|
|
8
8
|
}
|
|
9
|
+
function getHandlerData(handler) {
|
|
10
|
+
return handler[dataSymbol];
|
|
11
|
+
}
|
|
12
|
+
exports.getHandlerData = getHandlerData;
|
|
13
|
+
const inlineSqlBrand = 'inlineSql';
|
|
9
14
|
function inlineSql(sql) {
|
|
10
|
-
return { sql, ...buildHandler() };
|
|
15
|
+
return { [dataSymbol]: sql, ...buildHandler(inlineSqlBrand) };
|
|
11
16
|
}
|
|
17
|
+
const sqlReferenceBrand = 'sqlReference';
|
|
12
18
|
function sqlReference(sqlReference) {
|
|
13
|
-
return { sqlReference, ...buildHandler() };
|
|
19
|
+
return { [dataSymbol]: sqlReference, ...buildHandler(sqlReferenceBrand) };
|
|
14
20
|
}
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
const customHandlerBrand = 'customHandler';
|
|
22
|
+
function custom(customHandler) {
|
|
23
|
+
// used to determine caller directory in order to resolve relative path downstream
|
|
24
|
+
const stack = new Error().stack;
|
|
25
|
+
return {
|
|
26
|
+
[dataSymbol]: { ...customHandler, stack },
|
|
27
|
+
...buildHandler(customHandlerBrand),
|
|
28
|
+
};
|
|
17
29
|
}
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
const functionHandlerBrand = 'functionHandler';
|
|
31
|
+
function fcn(fn) {
|
|
32
|
+
return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };
|
|
20
33
|
}
|
|
21
34
|
exports.handler = {
|
|
22
35
|
inlineSql,
|
|
@@ -1,21 +1,63 @@
|
|
|
1
1
|
import type { UnionToIntersection } from '@aws-amplify/data-schema-types';
|
|
2
2
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
3
3
|
import type { EnumType, EnumTypeParamShape } from '../EnumType';
|
|
4
|
-
import type { SchemaTypes,
|
|
5
|
-
import type {
|
|
4
|
+
import type { SchemaTypes, ModelAndCustomTypes, FieldTypesOfCustomType } from './ResolveSchema';
|
|
5
|
+
import type { ModelType, ModelTypeParamShape } from '../ModelType';
|
|
6
6
|
export type NonModelTypesShape = {
|
|
7
7
|
enums: Record<string, EnumType<any>>;
|
|
8
8
|
customTypes: Record<string, any>;
|
|
9
9
|
};
|
|
10
10
|
export type ExtractNonModelTypes<Schema> = ResolveNonModelFields<ResolveNonModelTypes<Schema, ExtractImplicitNonModelTypes<Schema>>>;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Extract all implicit non-model types a `FieldName: Field` map recursively,
|
|
13
|
+
* and store them in a flat map. The keys are serialized property path.
|
|
14
|
+
*
|
|
15
|
+
* For example, with the following schema structure:
|
|
16
|
+
* ```
|
|
17
|
+
* ExtractAndFlattenImplicitNonModelTypesFromFields<
|
|
18
|
+
* 'Post',
|
|
19
|
+
* {
|
|
20
|
+
* content: string,
|
|
21
|
+
* status: enum(['reviewing'], ['published']),
|
|
22
|
+
* meta: customType({
|
|
23
|
+
* tag: enum(['internal', 'external']),
|
|
24
|
+
* views: number
|
|
25
|
+
* }),
|
|
26
|
+
* tag: ref('SomeExplicitEnum'),
|
|
27
|
+
* }
|
|
28
|
+
* >
|
|
29
|
+
* ```
|
|
30
|
+
* It generates the result as:
|
|
31
|
+
* ```
|
|
32
|
+
* {
|
|
33
|
+
* PostStatus: enum(['reviewing'], ['published']),
|
|
34
|
+
* PostMeta: customType({
|
|
35
|
+
* tag: enum(['internal', 'external']),
|
|
36
|
+
* views: number
|
|
37
|
+
* }),
|
|
38
|
+
* PostMetaTag: enum(['internal', 'external']),
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
13
41
|
*/
|
|
14
|
-
type
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
}
|
|
18
|
-
}[
|
|
42
|
+
export type ExtractAndFlattenImplicitNonModelTypesFromFields<ParentTypeName extends string, Fields> = {
|
|
43
|
+
[FieldProp in keyof Fields as Fields[FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? FieldProp : never]: (x: NonNullable<Fields[FieldProp]> extends infer FieldType ? FieldType extends EnumType<EnumTypeParamShape> ? {
|
|
44
|
+
[Key in `${ParentTypeName}${Capitalize<FieldProp & string>}`]: Fields[FieldProp];
|
|
45
|
+
} : FieldType extends CustomType<infer CustomTypeShape extends CustomTypeParamShape> ? // recursively extract to the Nested CustomType, and return the
|
|
46
|
+
ExtractAndFlattenImplicitNonModelTypesFromFields<`${ParentTypeName}${Capitalize<FieldProp & string>}`, CustomTypeShape['fields']> & {
|
|
47
|
+
[Key in `${ParentTypeName}${Capitalize<FieldProp & string>}`]: Fields[FieldProp];
|
|
48
|
+
} : never : never) => void;
|
|
49
|
+
} extends Record<string, infer Func> ? Func extends (x: infer P) => void ? P : Record<never, never> : Record<never, never>;
|
|
50
|
+
/**
|
|
51
|
+
* Pulls out implicit, i.e. field-level non-model types from `ModelType` and
|
|
52
|
+
* `CustomType` which may contain deeply nested implicit (inline) non-model
|
|
53
|
+
* types.
|
|
54
|
+
*/
|
|
55
|
+
export type ExtractImplicitNonModelTypes<Schema, Targets = ModelAndCustomTypes<SchemaTypes<Schema>>> = UnionToIntersection<{
|
|
56
|
+
[Model in keyof Targets]: Targets[Model] extends CustomType<infer R extends CustomTypeParamShape> ? // extract nested non-model types from it, and include itself
|
|
57
|
+
ExtractAndFlattenImplicitNonModelTypesFromFields<Capitalize<Model & string>, R['fields']> & {
|
|
58
|
+
[key in Model]: Targets[Model];
|
|
59
|
+
} : Targets[Model] extends ModelType<infer R extends ModelTypeParamShape, any> ? ExtractAndFlattenImplicitNonModelTypesFromFields<Capitalize<Model & string>, R['fields']> : Targets[Model];
|
|
60
|
+
}[keyof Targets]>;
|
|
19
61
|
type ResolveNonModelTypes<Schema, Extracted, ResolvedSchema = SchemaTypes<Schema> & Extracted> = {
|
|
20
62
|
enums: {
|
|
21
63
|
[Model in keyof ResolvedSchema as ResolvedSchema[Model] extends EnumType<EnumTypeParamShape> ? Model : never]: ResolvedSchema[Model] extends EnumType<infer R extends EnumTypeParamShape> ? R['values'][number] : never;
|
|
@@ -26,10 +68,6 @@ type ResolveNonModelTypes<Schema, Extracted, ResolvedSchema = SchemaTypes<Schema
|
|
|
26
68
|
};
|
|
27
69
|
type ResolveNonModelFields<T extends NonModelTypesShape> = {
|
|
28
70
|
enums: T['enums'];
|
|
29
|
-
customTypes:
|
|
30
|
-
[CustomType in keyof T['customTypes']]: {
|
|
31
|
-
[FieldProp in keyof T['customTypes'][CustomType]]: T['customTypes'][CustomType][FieldProp] extends ModelField<infer R, any, any> ? R : never;
|
|
32
|
-
};
|
|
33
|
-
};
|
|
71
|
+
customTypes: FieldTypesOfCustomType<T['customTypes']>;
|
|
34
72
|
};
|
|
35
73
|
export {};
|
|
@@ -15,7 +15,7 @@ import type { EnumType, EnumTypeParamShape } from '../EnumType';
|
|
|
15
15
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
16
16
|
export type ResolveFieldProperties<Schema extends ModelSchema<any, any>, NonModelTypes extends NonModelTypesShape, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, {
|
|
17
17
|
identifier: string;
|
|
18
|
-
}> = ModelIdentifier<SchemaTypes<Schema>>, FieldsWithInjectedModels = InjectImplicitModels<ResolvedSchema>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<FieldsWithInjectedModels, IdentifierMeta>, FieldsWithRelationships = ResolveRelationships<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<
|
|
18
|
+
}> = ModelIdentifier<SchemaTypes<Schema>>, FieldsWithInjectedModels = InjectImplicitModels<ResolvedSchema>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<FieldsWithInjectedModels, IdentifierMeta>, FieldsWithRelationships = ResolveRelationships<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<MarkModelsNonNullableFieldsRequired<FieldsWithRelationships>>, FilterFieldTypes<MarkModelsNullableFieldsOptional<FieldsWithRelationships>>, FilterFieldTypes<ModelImpliedAuthFields<Schema>>, AllImpliedFKs<ResolvedSchema, IdentifierMeta>>;
|
|
19
19
|
type ExtractImplicitModelNames<Schema> = UnionToIntersection<ExcludeEmpty<{
|
|
20
20
|
[ModelProp in keyof Schema]: {
|
|
21
21
|
[FieldProp in keyof Schema[ModelProp] as Schema[ModelProp][FieldProp] extends ModelRelationalFieldParamShape ? Schema[ModelProp][FieldProp]['relationName'] extends string ? Schema[ModelProp][FieldProp]['relationName'] extends keyof Schema ? never : Schema[ModelProp][FieldProp]['relationName'] : never : never]: {
|
|
@@ -28,7 +28,10 @@ type GetRelationshipRef<T, RM extends keyof T, TypeArg extends ModelRelationalFi
|
|
|
28
28
|
type ResolveRelationalFieldsForModel<Schema, ModelName extends keyof Schema, Flat extends boolean> = {
|
|
29
29
|
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends ModelRelationalFieldParamShape ? Schema[ModelName][FieldName]['relatedModel'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelName][FieldName]['relatedModel'], Schema[ModelName][FieldName], Flat> : never : Schema[ModelName][FieldName];
|
|
30
30
|
};
|
|
31
|
-
export type ResolveRef<NonModelTypes extends NonModelTypesShape, Ref extends RefTypeParamShape, Link extends string = Ref['link'], Value = Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? NonModelTypes['customTypes'][Link] : never> = Ref['required'] extends true ? Value : Value | null;
|
|
31
|
+
export type ResolveRef<NonModelTypes extends NonModelTypesShape, Ref extends RefTypeParamShape, Link extends string = Ref['link'], Value = Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? ResolveRefsOfCustomType<NonModelTypes, NonModelTypes['customTypes'][Link]> : never> = Ref['required'] extends true ? Value : Value | null;
|
|
32
|
+
type ResolveRefsOfCustomType<NonModelTypes extends NonModelTypesShape, T> = {
|
|
33
|
+
[Prop in keyof T]: T[Prop] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : T[Prop];
|
|
34
|
+
} extends infer Resolved ? ExtractNullableFieldsToOptionalFields<Resolved> & ExtractNonNullableFieldsToRequiredFields<Resolved> : never;
|
|
32
35
|
type ResolveRelationships<Schema, NonModelTypes extends NonModelTypesShape, Flat extends boolean = false> = {
|
|
33
36
|
[ModelProp in keyof Schema]: {
|
|
34
37
|
[FieldProp in keyof Schema[ModelProp]]: Schema[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : Schema[ModelProp][FieldProp] extends ModelRelationalFieldParamShape ? Schema[ModelProp][FieldProp]['relatedModel'] extends keyof Schema ? Schema[ModelProp][FieldProp]['relationshipType'] extends 'manyToMany' ? Schema[ModelProp][FieldProp]['relationName'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelProp][FieldProp]['relationName'], Schema[ModelProp][FieldProp], Flat> : never : GetRelationshipRef<Schema, Schema[ModelProp][FieldProp]['relatedModel'], Schema[ModelProp][FieldProp], Flat> : never : Schema[ModelProp][FieldProp];
|
|
@@ -39,15 +42,17 @@ type FilterFieldTypes<Schema> = {
|
|
|
39
42
|
[FieldProp in keyof Schema[ModelProp] as Schema[ModelProp][FieldProp] extends undefined ? never : FieldProp]: Schema[ModelProp][FieldProp];
|
|
40
43
|
};
|
|
41
44
|
};
|
|
42
|
-
type
|
|
43
|
-
[
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
type ExtractNullableFieldsToOptionalFields<Fields> = Partial<{
|
|
46
|
+
[FieldProp in keyof Fields as null extends Fields[FieldProp] ? FieldProp : never]: null extends Fields[FieldProp] ? Fields[FieldProp] : never;
|
|
47
|
+
}>;
|
|
48
|
+
type MarkModelsNullableFieldsOptional<Schema> = {
|
|
49
|
+
[ModelProp in keyof Schema]: ExtractNullableFieldsToOptionalFields<Schema[ModelProp]>;
|
|
46
50
|
};
|
|
47
|
-
type
|
|
48
|
-
[
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
type ExtractNonNullableFieldsToRequiredFields<Fields> = {
|
|
52
|
+
[FieldProp in keyof Fields as null extends Fields[FieldProp] ? never : FieldProp]: null extends Fields[FieldProp] ? never : Fields[FieldProp];
|
|
53
|
+
};
|
|
54
|
+
type MarkModelsNonNullableFieldsRequired<Schema> = {
|
|
55
|
+
[ModelProp in keyof Schema]: ExtractNonNullableFieldsToRequiredFields<Schema[ModelProp]>;
|
|
51
56
|
};
|
|
52
57
|
type Intersection<A, B, C, D> = A & B & C & D extends infer U ? {
|
|
53
58
|
[P in keyof U]: U[P];
|
|
@@ -18,6 +18,13 @@ export type SchemaTypes<T> = T extends ModelSchema<any, any> ? T['data']['types'
|
|
|
18
18
|
export type ModelTypes<Schema> = {
|
|
19
19
|
[Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<infer R, any> ? R['fields'] : never;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Gets the collection of all ModelTypes and CustomTypes which are explicitly
|
|
23
|
+
* defined in the schema.
|
|
24
|
+
*/
|
|
25
|
+
export type ModelAndCustomTypes<Schema> = {
|
|
26
|
+
[Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<any, any> ? Schema[Model] : Schema[Model] extends CustomType<CustomTypeParamShape> ? Schema[Model] : never;
|
|
27
|
+
};
|
|
21
28
|
/**
|
|
22
29
|
* Resolves field types
|
|
23
30
|
*
|
|
@@ -25,11 +32,29 @@ export type ModelTypes<Schema> = {
|
|
|
25
32
|
*/
|
|
26
33
|
export type FieldTypes<T> = {
|
|
27
34
|
[ModelProp in keyof T]: {
|
|
28
|
-
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['required'] extends true ? T[ModelProp][FieldProp] : T[ModelProp][FieldProp] | null : T[ModelProp][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
|
|
35
|
+
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends ModelField<infer R, any, any> ? R : T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['required'] extends true ? T[ModelProp][FieldProp] : T[ModelProp][FieldProp] | null : T[ModelProp][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
|
|
29
36
|
link: `${Capitalize<ModelProp & string>}${Capitalize<FieldProp & string>}`;
|
|
30
37
|
type: 'ref';
|
|
31
38
|
required: false;
|
|
32
39
|
authorization: [];
|
|
33
|
-
}> | null : T[ModelProp][FieldProp] extends ModelRelationalField<infer R, string, RelationTypeFunctionOmitMapping<ModelRelationshipTypes>, any> ? R :
|
|
40
|
+
}> | null : T[ModelProp][FieldProp] extends ModelRelationalField<infer R, string, RelationTypeFunctionOmitMapping<ModelRelationshipTypes>, any> ? R : never;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Resolves field types for a CustomType.
|
|
45
|
+
*
|
|
46
|
+
* This utility type is needed in addition to the `FieldTypes` utility type as
|
|
47
|
+
* without checking `ModelRelationalField` can improve ~5% on resolving performance.
|
|
48
|
+
*
|
|
49
|
+
* Non-model types are replaced with Refs. Refs remain and are resolved in ResolveFieldProperties.ts
|
|
50
|
+
*/
|
|
51
|
+
export type FieldTypesOfCustomType<T> = {
|
|
52
|
+
[CustomTypeName in keyof T]: {
|
|
53
|
+
[FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends ModelField<infer R, any, any> ? R : T[CustomTypeName][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['required'] extends true ? T[CustomTypeName][FieldProp] : T[CustomTypeName][FieldProp] | null : T[CustomTypeName][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
|
|
54
|
+
link: `${Capitalize<CustomTypeName & string>}${Capitalize<FieldProp & string>}`;
|
|
55
|
+
type: 'ref';
|
|
56
|
+
required: false;
|
|
57
|
+
authorization: [];
|
|
58
|
+
}> | null : never;
|
|
34
59
|
};
|
|
35
60
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { InternalSchema } from './ModelSchema';
|
|
2
|
-
import { DerivedApiDefinition } from '
|
|
2
|
+
import { DerivedApiDefinition } from '@aws-amplify/data-schema-types';
|
|
3
3
|
/**
|
|
4
4
|
* Returns API definition from ModelSchema or string schema
|
|
5
5
|
* @param arg - { schema }
|