@aws-amplify/data-schema 0.13.15 → 0.13.17
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/ClientSchema.d.ts +9 -14
- package/lib-esm/src/MappedTypes/CustomOperations.d.ts +79 -3
- package/lib-esm/src/MappedTypes/ResolveFieldProperties.d.ts +5 -4
- package/lib-esm/src/MappedTypes/ResolveSchema.d.ts +2 -2
- package/lib-esm/src/ModelSchema.d.ts +21 -12
- package/lib-esm/src/ModelSchema.js +27 -6
- package/lib-esm/src/SchemaProcessor.js +39 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -3
|
@@ -1,34 +1,29 @@
|
|
|
1
1
|
import { type __modelMeta__ } from '@aws-amplify/data-schema-types';
|
|
2
|
-
import type
|
|
2
|
+
import { type GenericModelSchema, type RDSModelSchema } from './ModelSchema';
|
|
3
3
|
import type { ResolveSchema, SchemaTypes } from './MappedTypes/ResolveSchema';
|
|
4
|
-
import type { CreateImplicitModelsFromRelations, ResolveFieldProperties } from './MappedTypes/ResolveFieldProperties';
|
|
4
|
+
import type { CreateImplicitModelsFromRelations, ResolveFieldProperties, ResolveStaticFieldProperties } from './MappedTypes/ResolveFieldProperties';
|
|
5
5
|
import type { ModelIdentifier, ModelSecondaryIndexes, RelationalMetadata } from './MappedTypes/ModelMetadata';
|
|
6
6
|
import type { ExtractNonModelTypes, NonModelTypesShape } from './MappedTypes/ExtractNonModelTypes';
|
|
7
|
-
import
|
|
8
|
-
export type ClientSchema<Schema extends
|
|
7
|
+
import { ResolveCustomOperations, CustomOperationHandlerTypes } from './MappedTypes/CustomOperations';
|
|
8
|
+
export type ClientSchema<Schema extends GenericModelSchema<any>> = InternalClientSchema<Schema>;
|
|
9
9
|
/**
|
|
10
10
|
* Types for unwrapping generic type args into client-consumable types
|
|
11
11
|
*
|
|
12
|
-
* @typeParam Schema -
|
|
12
|
+
* @typeParam Schema - Data schema builder model type
|
|
13
13
|
*
|
|
14
14
|
* The following params are used solely as variables in order to simplify mapped type usage.
|
|
15
15
|
* They should not receive external type args.
|
|
16
16
|
*
|
|
17
17
|
* @internal @typeParam NonModelTypes - Custom Types, Enums, and Custom Operations
|
|
18
|
-
* @internal @typeParam
|
|
19
|
-
* @internal @typeParam ResolvedFields -
|
|
20
|
-
* @internal @typeParam IdentifierMeta - Map of model primary index metadata
|
|
18
|
+
* @internal @typeParam ImplicitModels - The implicit models created to represent relationships
|
|
19
|
+
* @internal @typeParam ResolvedFields - Resolved client-facing types used for CRUDL response shapes
|
|
21
20
|
* @internal @typeParam SecondaryIndexes - Map of model secondary index metadata
|
|
22
|
-
*
|
|
23
|
-
* @internal @typeParam Meta - Stores schema metadata: identifier, relationship metadata;
|
|
24
|
-
* used by `API.generateClient` to craft strongly typed mutation inputs; hidden from customer-facing types behind __modelMeta__ symbol
|
|
25
|
-
*
|
|
26
21
|
*/
|
|
27
|
-
type InternalClientSchema<Schema extends
|
|
22
|
+
type InternalClientSchema<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape = ExtractNonModelTypes<Schema>, ResolvedSchema = ResolveSchema<Schema>, ImplicitModels = Schema extends RDSModelSchema<any, any> ? object : CreateImplicitModelsFromRelations<ResolvedSchema>, ImplicitModelsIdentifierMeta = {
|
|
28
23
|
[ImplicitModel in keyof ImplicitModels]: {
|
|
29
24
|
identifier: 'id';
|
|
30
25
|
};
|
|
31
|
-
}, ResolvedFields extends Record<string, unknown> = ResolveFieldProperties<Schema, NonModelTypes, ImplicitModels>, IdentifierMeta extends Record<string, any> = ModelIdentifier<SchemaTypes<Schema>>, SecondaryIndexes extends Record<string, any> = ModelSecondaryIndexes<SchemaTypes<Schema>>> = ResolvedFields & {
|
|
26
|
+
}, ResolvedFields extends Record<string, unknown> = Schema extends RDSModelSchema<any, any> ? ResolveStaticFieldProperties<Schema, NonModelTypes, object> : ResolveFieldProperties<Schema, NonModelTypes, ImplicitModels>, IdentifierMeta extends Record<string, any> = ModelIdentifier<SchemaTypes<Schema>>, SecondaryIndexes extends Record<string, any> = Schema extends RDSModelSchema<any, any> ? object : ModelSecondaryIndexes<SchemaTypes<Schema>>> = CustomOperationHandlerTypes<ResolveCustomOperations<Schema, ResolvedFields, NonModelTypes>['customOperations']> & ResolvedFields & {
|
|
32
27
|
[__modelMeta__]: IdentifierMeta & ImplicitModelsIdentifierMeta & SecondaryIndexes & RelationalMetadata<ResolvedSchema, ResolvedFields, IdentifierMeta> & NonModelTypes & ResolveCustomOperations<Schema, ResolvedFields, NonModelTypes>;
|
|
33
28
|
};
|
|
34
29
|
export {};
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GenericModelSchema } from '../ModelSchema';
|
|
2
2
|
import type { NonModelTypesShape } from './ExtractNonModelTypes';
|
|
3
3
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
4
4
|
import type { ModelField } from '../ModelField';
|
|
5
5
|
import type { RefType, RefTypeParamShape } from '../RefType';
|
|
6
6
|
import type { ResolveRefsOfCustomType, ResolveRefValueArrayTraits } from './ResolveFieldProperties';
|
|
7
|
+
import type { AppSyncResolverHandler } from 'aws-lambda';
|
|
7
8
|
/**
|
|
8
9
|
* Creates meta types for custom operations from a schema.
|
|
9
10
|
*/
|
|
10
|
-
export type ResolveCustomOperations<Schema extends
|
|
11
|
+
export type ResolveCustomOperations<Schema extends GenericModelSchema<any>, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape> = {
|
|
11
12
|
customOperations: {
|
|
12
13
|
[OpName in keyof CustomOpShapes<Schema>]: {
|
|
13
14
|
arguments: CustomOpArguments<CustomOpShapes<Schema>[OpName]>;
|
|
@@ -21,7 +22,7 @@ export type ResolveCustomOperations<Schema extends ModelSchema<any, any>, FullyR
|
|
|
21
22
|
/**
|
|
22
23
|
* Filtered, mapped list of custom operations shapes from a schema.
|
|
23
24
|
*/
|
|
24
|
-
export type CustomOpShapes<Schema extends
|
|
25
|
+
export type CustomOpShapes<Schema extends GenericModelSchema<any>> = {
|
|
25
26
|
[K in keyof Schema['data']['types'] as Schema['data']['types'][K] extends CustomOperation<any, any> ? K : never]: Schema['data']['types'][K] extends CustomOperation<infer Shape, any> ? Shape : never;
|
|
26
27
|
};
|
|
27
28
|
/**
|
|
@@ -44,3 +45,78 @@ export type CustomOpReturnType<Shape extends CustomOperationParamShape, FullyRes
|
|
|
44
45
|
* with the addition that allows .ref() a model with custom operations.
|
|
45
46
|
*/
|
|
46
47
|
export type ResolveRef<Shape extends RefTypeParamShape, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape, Link = Shape['link'], RefValue = Link extends keyof FullyResolvedSchema ? FullyResolvedSchema[Link] : Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? ResolveRefsOfCustomType<NonModelTypes, NonModelTypes['customTypes'][Link]> : never, Value = Shape['valueRequired'] extends true ? RefValue : RefValue | null> = ResolveRefValueArrayTraits<Shape, Value>;
|
|
48
|
+
/**
|
|
49
|
+
* The kind of shape we need to map to custom handler (e.g., lambda) function
|
|
50
|
+
* signatures.
|
|
51
|
+
*/
|
|
52
|
+
type CustomOperationsMap = Record<string, CustomOperationMinimalDef>;
|
|
53
|
+
/**
|
|
54
|
+
* The minimal amount of structure needed to extract types for a custom handler.
|
|
55
|
+
*/
|
|
56
|
+
type CustomOperationMinimalDef = {
|
|
57
|
+
arguments: any;
|
|
58
|
+
returnType: any;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Derives the signature and types for a lambda handler for a particular
|
|
62
|
+
* custom Query or Mutation from a Schema.
|
|
63
|
+
*/
|
|
64
|
+
type IndvidualCustomHandlerTypes<Op extends CustomOperationMinimalDef> = {
|
|
65
|
+
/**
|
|
66
|
+
* Handler type for lambda function implementations. E.g.,
|
|
67
|
+
*
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import type { Schema } from './resource';
|
|
70
|
+
*
|
|
71
|
+
* export const handler: Schema['echo']['functionHandler'] = async (event, context) => {
|
|
72
|
+
* // event and context will be fully typed inside here.
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
functionHandler: AppSyncResolverHandler<Op['arguments'], LambdaReturnType<Op['returnType']>>;
|
|
77
|
+
/**
|
|
78
|
+
* The `context.arguments` type for lambda function implementations.
|
|
79
|
+
*
|
|
80
|
+
* ```typescript
|
|
81
|
+
* import type { Schema } from './resource';
|
|
82
|
+
*
|
|
83
|
+
* export const handler: Schema['echo']['functionHandler'] = async (event, context) => {
|
|
84
|
+
* // Provides this type, if needed:
|
|
85
|
+
* const args: Schema['echo']['functionHandlerArguments'] = event.arguments;
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
functionHandlerArguments: Op['arguments'];
|
|
90
|
+
/**
|
|
91
|
+
* The return type expected by a lambda function handler.
|
|
92
|
+
*
|
|
93
|
+
* ```typescript
|
|
94
|
+
* import type { Schema } from './resource';
|
|
95
|
+
*
|
|
96
|
+
* export const handler: Schema['echo']['functionHandler'] = async (event, context) => {
|
|
97
|
+
* // Result type enforced here:
|
|
98
|
+
* const result: Schema['echo']['functionHandlerResult'] = buildResult(...);
|
|
99
|
+
*
|
|
100
|
+
* // `Result` type matches expected function return type here:
|
|
101
|
+
* return result;
|
|
102
|
+
* }
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
functionHandlerResult: LambdaReturnType<Op['returnType']>;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Derives the function signatures for a lambda handlers for all the provided
|
|
109
|
+
* custom queries and mutations.
|
|
110
|
+
*/
|
|
111
|
+
export type CustomOperationHandlerTypes<CustomOperations extends CustomOperationsMap> = {
|
|
112
|
+
[K in keyof CustomOperations]: IndvidualCustomHandlerTypes<CustomOperations[K]>;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Returns a return type with lazy loaders removed.
|
|
116
|
+
*
|
|
117
|
+
* (Custom handlers should not return lazy loaded fields -- they're *lazy loaded*.)
|
|
118
|
+
*/
|
|
119
|
+
type LambdaReturnType<T> = T extends Record<string, any> ? {
|
|
120
|
+
[K in keyof Exclude<T, null | undefined> as Exclude<T, null | undefined>[K] extends (...args: any) => any ? never : K]: Exclude<T, null | undefined>[K];
|
|
121
|
+
} : T | (null extends T ? null : never) | (undefined extends T ? undefined : never);
|
|
122
|
+
export {};
|
|
@@ -2,7 +2,7 @@ import type { UnionToIntersection, LazyLoader, ExcludeEmpty } from '@aws-amplify
|
|
|
2
2
|
import type { Authorization, ImpliedAuthFields } from '../Authorization';
|
|
3
3
|
import type { ModelField } from '../ModelField';
|
|
4
4
|
import type { ModelType, ModelTypeParamShape } from '../ModelType';
|
|
5
|
-
import type {
|
|
5
|
+
import type { GenericModelSchema } from '../ModelSchema';
|
|
6
6
|
import type { ModelRelationalField, ModelRelationalFieldParamShape, ModelRelationalTypeArgFactory, ModelRelationshipTypes } from '../ModelRelationalField';
|
|
7
7
|
import type { AllImpliedFKs } from './ForeignKeys';
|
|
8
8
|
import type { ResolveSchema, SchemaTypes } from './ResolveSchema';
|
|
@@ -13,9 +13,10 @@ import type { NonModelTypesShape } from './ExtractNonModelTypes';
|
|
|
13
13
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
14
14
|
import type { EnumType, EnumTypeParamShape } from '../EnumType';
|
|
15
15
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
16
|
-
export type ResolveFieldProperties<Schema extends
|
|
16
|
+
export type ResolveFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ImplicitModelsSchema, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, {
|
|
17
17
|
identifier: string;
|
|
18
18
|
}> = ModelIdentifier<SchemaTypes<Schema>>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema & ImplicitModelsSchema, IdentifierMeta>, FieldsWithRelationships = ResolveRelationships<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<MarkModelsNonNullableFieldsRequired<FieldsWithRelationships>>, FilterFieldTypes<MarkModelsNullableFieldsOptional<FieldsWithRelationships>>, FilterFieldTypes<ModelImpliedAuthFields<Schema>>, AllImpliedFKs<ResolvedSchema, IdentifierMeta>>;
|
|
19
|
+
export type ResolveStaticFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ImplicitModelsSchema, ResolvedSchema = ResolveSchema<Schema>, FieldsWithInjectedImplicitFields = InjectImplicitModelFields<ResolvedSchema & ImplicitModelsSchema, object>, FieldsWithRelationships = ResolveRelationships<FieldsWithInjectedImplicitFields, NonModelTypes>> = Intersection<FilterFieldTypes<MarkModelsNonNullableFieldsRequired<FieldsWithRelationships>>, FilterFieldTypes<MarkModelsNullableFieldsOptional<FieldsWithRelationships>>>;
|
|
19
20
|
export type CreateImplicitModelsFromRelations<Schema> = UnionToIntersection<ExcludeEmpty<{
|
|
20
21
|
[ModelProp in keyof Schema]: {
|
|
21
22
|
[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]: Record<`${Lowercase<ModelProp & string>}`, ModelRelationalTypeArgFactory<ModelProp & string, ModelRelationshipTypes.hasMany, false>>;
|
|
@@ -59,9 +60,9 @@ type MarkModelsNonNullableFieldsRequired<Schema> = {
|
|
|
59
60
|
type Intersection<A = Record<never, never>, B = Record<never, never>, C = Record<never, never>, D = Record<never, never>> = A & B & C & D extends infer U ? {
|
|
60
61
|
[P in keyof U]: U[P];
|
|
61
62
|
} : never;
|
|
62
|
-
export type ModelImpliedAuthFields<Schema extends
|
|
63
|
+
export type ModelImpliedAuthFields<Schema extends GenericModelSchema<any>> = {
|
|
63
64
|
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType<EnumTypeParamShape> ? 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;
|
|
64
65
|
};
|
|
65
|
-
type AllAuthFieldsForModel<Schema extends
|
|
66
|
+
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>;
|
|
66
67
|
type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<T extends ModelTypeParamShape ? T['fields'][keyof T['fields']] extends ModelField<any, any, infer Auth> | ModelRelationalField<any, any, any, infer Auth> | RefType<any, any, infer Auth> ? Auth extends Authorization<any, any, any> ? ImpliedAuthFields<Auth> : object : object : object>;
|
|
67
68
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ModelType } from '../ModelType';
|
|
2
|
-
import type {
|
|
2
|
+
import type { GenericModelSchema } from '../ModelSchema';
|
|
3
3
|
import type { ModelRelationalField, ModelRelationshipTypes, RelationTypeFunctionOmitMapping } from '../ModelRelationalField';
|
|
4
4
|
import type { ModelField } from '../ModelField';
|
|
5
5
|
import type { CustomType, CustomTypeParamShape } from '../CustomType';
|
|
@@ -7,7 +7,7 @@ import type { EnumType, EnumTypeParamShape } from '../EnumType';
|
|
|
7
7
|
import type { RefType, RefTypeParamShape } from '../RefType';
|
|
8
8
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
9
9
|
export type ResolveSchema<Schema> = FieldTypes<ModelTypes<SchemaTypes<Schema>>>;
|
|
10
|
-
export type SchemaTypes<T> = T extends
|
|
10
|
+
export type SchemaTypes<T> = T extends GenericModelSchema<any> ? T['data']['types'] : never;
|
|
11
11
|
/**
|
|
12
12
|
* Resolves model types
|
|
13
13
|
*
|
|
@@ -4,6 +4,12 @@ import type { EnumType, EnumTypeParamShape } from './EnumType';
|
|
|
4
4
|
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
5
5
|
import type { CustomOperation, CustomOperationParamShape, InternalCustom, MutationCustomOperation, QueryCustomOperation, SubscriptionCustomOperation } from './CustomOperation';
|
|
6
6
|
import { SchemaAuthorization } from './Authorization';
|
|
7
|
+
import { Brand } from './util';
|
|
8
|
+
export declare const rdsSchemaBrandName = "RDSSchema";
|
|
9
|
+
export declare const rdsSchemaBrand: Brand<"RDSSchema">;
|
|
10
|
+
export type RDSSchemaBrand = Brand<typeof rdsSchemaBrandName>;
|
|
11
|
+
export declare const ddbSchemaBrandName = "DDBSchema";
|
|
12
|
+
export type DDBSchemaBrand = Brand<typeof ddbSchemaBrandName>;
|
|
7
13
|
type SchemaContent = ModelType<ModelTypeParamShape, any> | CustomType<CustomTypeParamShape> | EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any>;
|
|
8
14
|
type ModelSchemaContents = Record<string, SchemaContent>;
|
|
9
15
|
type InternalSchemaModels = Record<string, InternalModel | EnumType<any> | CustomType<any> | InternalCustom>;
|
|
@@ -38,7 +44,7 @@ export type ModelSchemaParamShape = {
|
|
|
38
44
|
authorization: SchemaAuthorization<any, any, any>[];
|
|
39
45
|
configuration: SchemaConfig<any, any>;
|
|
40
46
|
};
|
|
41
|
-
export type
|
|
47
|
+
export type RDSModelSchemaParamShape = ModelSchemaParamShape & {
|
|
42
48
|
setSqlStatementFolderPath?: string;
|
|
43
49
|
};
|
|
44
50
|
export type InternalSchema = {
|
|
@@ -48,22 +54,25 @@ export type InternalSchema = {
|
|
|
48
54
|
configuration: SchemaConfig<any, any>;
|
|
49
55
|
};
|
|
50
56
|
};
|
|
51
|
-
|
|
52
|
-
authorization: <AuthRules extends SchemaAuthorization<any, any, any>>(auth: AuthRules[]) => ModelSchema<SetTypeSubArg<T, 'authorization', AuthRules[]>, UsedMethods | 'authorization'>;
|
|
53
|
-
}, UsedMethods> & {
|
|
57
|
+
type BaseSchema<T extends ModelSchemaParamShape> = {
|
|
54
58
|
data: T;
|
|
55
59
|
models: {
|
|
56
60
|
[TypeKey in keyof T['types']]: T['types'][TypeKey] extends ModelType<ModelTypeParamShape> ? SchemaModelType<T['types'][TypeKey]> : never;
|
|
57
61
|
};
|
|
58
62
|
transform: () => DerivedApiDefinition;
|
|
59
63
|
};
|
|
60
|
-
type
|
|
61
|
-
export type
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
export type GenericModelSchema<T extends ModelSchemaParamShape> = BaseSchema<T> & Brand<string>;
|
|
65
|
+
export type ModelSchema<T extends ModelSchemaParamShape, UsedMethods extends 'authorization' = never> = Omit<{
|
|
66
|
+
authorization: <AuthRules extends SchemaAuthorization<any, any, any>>(auth: AuthRules[]) => ModelSchema<SetTypeSubArg<T, 'authorization', AuthRules[]>, UsedMethods | 'authorization'>;
|
|
67
|
+
}, UsedMethods> & BaseSchema<T> & DDBSchemaBrand;
|
|
68
|
+
type RDSModelSchemaFunctions = 'setSqlStatementFolderPath' | 'addQueries' | 'addMutations' | 'addSubscriptions' | 'authorization';
|
|
69
|
+
export type RDSModelSchema<T extends RDSModelSchemaParamShape, UsedMethods extends RDSModelSchemaFunctions = never> = Omit<{
|
|
70
|
+
setSqlStatementFolderPath: (path: string) => RDSModelSchema<T, UsedMethods | 'setSqlStatementFolderPath'>;
|
|
71
|
+
addQueries: (types: Record<string, QueryCustomOperation>) => RDSModelSchema<T, UsedMethods | 'addQueries'>;
|
|
72
|
+
addMutations: (types: Record<string, MutationCustomOperation>) => RDSModelSchema<T, UsedMethods | 'addMutations'>;
|
|
73
|
+
addSubscriptions: (types: Record<string, SubscriptionCustomOperation>) => RDSModelSchema<T, UsedMethods | 'addSubscriptions'>;
|
|
74
|
+
authorization: <AuthRules extends SchemaAuthorization<any, any, any>>(auth: AuthRules[]) => RDSModelSchema<SetTypeSubArg<T, 'authorization', AuthRules[]>, UsedMethods | 'authorization'> & RDSSchemaBrand;
|
|
75
|
+
}, UsedMethods> & BaseSchema<T> & RDSSchemaBrand;
|
|
67
76
|
/**
|
|
68
77
|
* Amplify API Next Model Schema shape
|
|
69
78
|
*/
|
|
@@ -78,7 +87,7 @@ type SchemaReturnType<DE extends DatasourceEngine, Types extends ModelSchemaCont
|
|
|
78
87
|
types: Types;
|
|
79
88
|
authorization: [];
|
|
80
89
|
configuration: any;
|
|
81
|
-
}> :
|
|
90
|
+
}> : RDSModelSchema<{
|
|
82
91
|
types: Types;
|
|
83
92
|
authorization: [];
|
|
84
93
|
configuration: any;
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configure = exports.schema = exports.isModelSchema = void 0;
|
|
3
|
+
exports.configure = exports.schema = exports.isModelSchema = exports.ddbSchemaBrandName = exports.rdsSchemaBrand = exports.rdsSchemaBrandName = void 0;
|
|
4
4
|
const ModelType_1 = require("./ModelType");
|
|
5
5
|
const SchemaProcessor_1 = require("./SchemaProcessor");
|
|
6
|
+
const util_1 = require("./util");
|
|
7
|
+
exports.rdsSchemaBrandName = 'RDSSchema';
|
|
8
|
+
exports.rdsSchemaBrand = (0, util_1.brand)(exports.rdsSchemaBrandName);
|
|
9
|
+
exports.ddbSchemaBrandName = 'DDBSchema';
|
|
10
|
+
const ddbSchemaBrand = (0, util_1.brand)(exports.ddbSchemaBrandName);
|
|
6
11
|
/**
|
|
7
12
|
* Filter the schema types down to only include the ModelTypes as SchemaModelType
|
|
8
13
|
*
|
|
@@ -29,9 +34,23 @@ const isModelSchema = (schema) => {
|
|
|
29
34
|
return typeof schema === 'object' && schema.data !== undefined;
|
|
30
35
|
};
|
|
31
36
|
exports.isModelSchema = isModelSchema;
|
|
32
|
-
function
|
|
37
|
+
function _rdsSchema(types, config) {
|
|
38
|
+
const data = {
|
|
39
|
+
types,
|
|
40
|
+
authorization: [],
|
|
41
|
+
configuration: config,
|
|
42
|
+
};
|
|
33
43
|
return {
|
|
34
|
-
|
|
44
|
+
data,
|
|
45
|
+
transform() {
|
|
46
|
+
const internalSchema = { data };
|
|
47
|
+
return (0, SchemaProcessor_1.processSchema)({ schema: internalSchema });
|
|
48
|
+
},
|
|
49
|
+
authorization(rules) {
|
|
50
|
+
this.data.authorization = rules;
|
|
51
|
+
const { authorization: _, ...rest } = this;
|
|
52
|
+
return rest;
|
|
53
|
+
},
|
|
35
54
|
setSqlStatementFolderPath(path) {
|
|
36
55
|
this.data.setSqlStatementFolderPath = path;
|
|
37
56
|
const { setSqlStatementFolderPath: _, ...rest } = this;
|
|
@@ -52,9 +71,10 @@ function _sqlSchemaExtension(schema) {
|
|
|
52
71
|
const { addSubscriptions: _, ...rest } = this;
|
|
53
72
|
return rest;
|
|
54
73
|
},
|
|
74
|
+
...exports.rdsSchemaBrand,
|
|
55
75
|
};
|
|
56
76
|
}
|
|
57
|
-
function
|
|
77
|
+
function _ddbSchema(types, config) {
|
|
58
78
|
const data = {
|
|
59
79
|
types,
|
|
60
80
|
authorization: [],
|
|
@@ -72,13 +92,14 @@ function _baseSchema(types, config) {
|
|
|
72
92
|
return rest;
|
|
73
93
|
},
|
|
74
94
|
models: filterSchemaModelTypes(data.types),
|
|
95
|
+
...ddbSchemaBrand,
|
|
75
96
|
};
|
|
76
97
|
}
|
|
77
98
|
function bindConfigToSchema(config) {
|
|
78
99
|
return (types) => {
|
|
79
100
|
return (config.database.engine === 'dynamodb'
|
|
80
|
-
?
|
|
81
|
-
:
|
|
101
|
+
? _ddbSchema(types, config)
|
|
102
|
+
: _rdsSchema(types, config));
|
|
82
103
|
};
|
|
83
104
|
}
|
|
84
105
|
/**
|
|
@@ -271,6 +271,27 @@ function addFields(existing, additions) {
|
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Validate that no implicit fields are used by the model definition
|
|
276
|
+
*
|
|
277
|
+
* @param existing An existing field map
|
|
278
|
+
* @param implicitFields A field map inferred from other schema usage
|
|
279
|
+
*
|
|
280
|
+
* @throws An error when an undefined field is used or when a field is used in a way that conflicts with its generated definition
|
|
281
|
+
*/
|
|
282
|
+
function validateStaticFields(existing, implicitFields) {
|
|
283
|
+
if (implicitFields === undefined) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
for (const [k, field] of Object.entries(implicitFields)) {
|
|
287
|
+
if (!existing[k]) {
|
|
288
|
+
throw new Error(`Field ${k} isn't defined.`);
|
|
289
|
+
}
|
|
290
|
+
else if (areConflicting(existing[k], field)) {
|
|
291
|
+
throw new Error(`Field ${k} defined twice with conflicting definitions.`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
274
295
|
/**
|
|
275
296
|
* Produces a new field definition object from every field definition object
|
|
276
297
|
* given as an argument. Performs validation (conflict detection) as objects
|
|
@@ -721,6 +742,7 @@ const schemaPreprocessor = (schema) => {
|
|
|
721
742
|
const databaseType = schema.data.configuration.database.engine === 'dynamodb'
|
|
722
743
|
? 'dynamodb'
|
|
723
744
|
: 'sql';
|
|
745
|
+
const staticSchema = schema.data.configuration.database.engine === 'dynamodb' ? false : true;
|
|
724
746
|
const fkFields = allImpliedFKs(schema);
|
|
725
747
|
const topLevelTypes = Object.entries(schema.data.types);
|
|
726
748
|
const { schemaAuth, functionSchemaAccess } = extractFunctionSchemaAccess(schema.data.authorization);
|
|
@@ -772,6 +794,23 @@ const schemaPreprocessor = (schema) => {
|
|
|
772
794
|
}
|
|
773
795
|
}
|
|
774
796
|
}
|
|
797
|
+
else if (staticSchema) {
|
|
798
|
+
const fields = { ...typeDef.data.fields };
|
|
799
|
+
const identifier = typeDef.data.identifier;
|
|
800
|
+
const [partitionKey] = identifier;
|
|
801
|
+
validateStaticFields(fields, fkFields[typeName]);
|
|
802
|
+
const { authString, authFields } = calculateAuth(mostRelevantAuthRules);
|
|
803
|
+
if (authString == '') {
|
|
804
|
+
throw new Error(`Model \`${typeName}\` is missing authorization rules. Add global rules to the schema or ensure every model has its own rules.`);
|
|
805
|
+
}
|
|
806
|
+
const fieldLevelAuthRules = processFieldLevelAuthRules(fields, authFields);
|
|
807
|
+
validateStaticFields(fields, authFields);
|
|
808
|
+
const { gqlFields, models } = processFields(typeName, fields, fieldLevelAuthRules, identifier, partitionKey);
|
|
809
|
+
topLevelTypes.push(...models);
|
|
810
|
+
const joined = gqlFields.join('\n ');
|
|
811
|
+
const model = `type ${typeName} @model ${authString}\n{\n ${joined}\n}`;
|
|
812
|
+
gqlModels.push(model);
|
|
813
|
+
}
|
|
775
814
|
else {
|
|
776
815
|
const fields = mergeFieldObjects(typeDef.data.fields, fkFields[typeName]);
|
|
777
816
|
const identifier = typeDef.data.identifier;
|