@aws-amplify/data-schema 0.13.16 → 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.
@@ -1,34 +1,29 @@
1
1
  import { type __modelMeta__ } from '@aws-amplify/data-schema-types';
2
- import type { ModelSchema } from './ModelSchema';
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
7
  import { ResolveCustomOperations, CustomOperationHandlerTypes } from './MappedTypes/CustomOperations';
8
- export type ClientSchema<Schema extends ModelSchema<any, any>> = InternalClientSchema<Schema>;
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 - Type Beast schema type
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 ResolvedSchema - Schema/Models/Fields structure with generic type args extracted
19
- * @internal @typeParam ResolvedFields - Resovled client-facing types used for CRUDL response shapes
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 ModelSchema<any, any>, NonModelTypes extends NonModelTypesShape = ExtractNonModelTypes<Schema>, ResolvedSchema = ResolveSchema<Schema>, ImplicitModels = CreateImplicitModelsFromRelations<ResolvedSchema>, ImplicitModelsIdentifierMeta = {
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 & CustomOperationHandlerTypes<ResolveCustomOperations<Schema, ResolvedFields, NonModelTypes>['customOperations']> & 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,4 +1,4 @@
1
- import type { ModelSchema } from '../ModelSchema';
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';
@@ -8,7 +8,7 @@ import type { AppSyncResolverHandler } from 'aws-lambda';
8
8
  /**
9
9
  * Creates meta types for custom operations from a schema.
10
10
  */
11
- export type ResolveCustomOperations<Schema extends ModelSchema<any, any>, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape> = {
11
+ export type ResolveCustomOperations<Schema extends GenericModelSchema<any>, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape> = {
12
12
  customOperations: {
13
13
  [OpName in keyof CustomOpShapes<Schema>]: {
14
14
  arguments: CustomOpArguments<CustomOpShapes<Schema>[OpName]>;
@@ -22,7 +22,7 @@ export type ResolveCustomOperations<Schema extends ModelSchema<any, any>, FullyR
22
22
  /**
23
23
  * Filtered, mapped list of custom operations shapes from a schema.
24
24
  */
25
- export type CustomOpShapes<Schema extends ModelSchema<any, any>> = {
25
+ export type CustomOpShapes<Schema extends GenericModelSchema<any>> = {
26
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;
27
27
  };
28
28
  /**
@@ -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 { ModelSchema } from '../ModelSchema';
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 ModelSchema<any, any>, NonModelTypes extends NonModelTypesShape, ImplicitModelsSchema, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, {
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 ModelSchema<any, any>> = {
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 ModelSchema<any, 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
+ 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 { ModelSchema } from '../ModelSchema';
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 ModelSchema<any, any> ? T['data']['types'] : never;
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 SQLModelSchemaParamShape = ModelSchemaParamShape & {
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
- export type ModelSchema<T extends ModelSchemaParamShape, UsedMethods extends 'authorization' = never> = Omit<{
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 SQLModelSchemaFunctions = 'setSqlStatementFolderPath' | 'addQueries' | 'addMutations' | 'addSubscriptions';
61
- export type SQLModelSchema<T extends SQLModelSchemaParamShape, UsedMethods extends 'authorization' | SQLModelSchemaFunctions = never> = ModelSchema<T, Exclude<UsedMethods, SQLModelSchemaFunctions>> & Omit<{
62
- setSqlStatementFolderPath: (path: string) => SQLModelSchema<T, UsedMethods | 'setSqlStatementFolderPath'>;
63
- addQueries: (types: Record<string, QueryCustomOperation>) => SQLModelSchema<T, UsedMethods | 'addQueries'>;
64
- addMutations: (types: Record<string, MutationCustomOperation>) => SQLModelSchema<T, UsedMethods | 'addMutations'>;
65
- addSubscriptions: (types: Record<string, SubscriptionCustomOperation>) => SQLModelSchema<T, UsedMethods | 'addSubscriptions'>;
66
- }, UsedMethods>;
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
- }> : SQLModelSchema<{
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 _sqlSchemaExtension(schema) {
37
+ function _rdsSchema(types, config) {
38
+ const data = {
39
+ types,
40
+ authorization: [],
41
+ configuration: config,
42
+ };
33
43
  return {
34
- ...schema,
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 _baseSchema(types, config) {
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
- ? _baseSchema(types, config)
81
- : _sqlSchemaExtension(_baseSchema(types, config)));
101
+ ? _ddbSchema(types, config)
102
+ : _rdsSchema(types, config));
82
103
  };
83
104
  }
84
105
  /**