import { type UnionToIntersection, type ExcludeEmpty, } from '@aws-amplify/data-schema-types'; import { __modelMeta__ } from '../runtime/'; import type { PrimaryIndexIrShape } from '../util'; import type { ModelType } from '../ModelType'; import type { ModelRelationshipFieldParamShape } from '../ModelRelationshipField'; export type ModelIdentifier = { [Property in keyof T]: T[Property] extends ModelType ? R['identifier'] extends PrimaryIndexIrShape ? { identifier: R['identifier'] } : never : never; }; export type ModelSecondaryIndexes = { [Property in keyof T]: T[Property] extends ModelType ? R['secondaryIndexes'] extends any[] ? { secondaryIndexes: R['secondaryIndexes'] } : never : never; }; export type RelationshipMetadata< ResolvedSchema, ResolvedFields extends Record, IdentifierMeta extends Record, > = UnionToIntersection< ExcludeEmpty< { [ModelName in keyof ResolvedSchema]: { [Field in keyof ResolvedSchema[ModelName] as ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field]['relationshipType'] extends | 'hasOne' | 'belongsTo' ? // For hasOne we're adding metadata to the model itself // E.g. if Post hasOne Author, we need to add a postAuthorId field to the Post model ModelName : never : never]: ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field] extends ModelRelationshipFieldParamShape ? ResolvedSchema[ModelName][Field]['relationshipType'] extends 'hasMany' ? { relationshipInputFields: Partial< Record< // For M:N and 1:M we add a parent model field to the child `${Uncapitalize}`, NormalizeInputFields< ResolvedFields[ModelName & string], ExtractModelIdentifier > > >; } : { relationshipInputFields: Partial< Record< // For 1:1 and Belongs To we add a child model field to the parent Field, NormalizeInputFields< ResolvedFields[ResolvedSchema[ModelName][Field]['relatedModel']], ExtractModelIdentifier< `${Capitalize}`, IdentifierMeta > > > >; } : never : never; }; }[keyof ResolvedSchema] > >; type ExtractModelIdentifier< ModelName, IdentifierMeta extends Record, > = ModelName extends keyof IdentifierMeta ? IdentifierMeta[ModelName] : never; type NormalizeInputFields< ModelFields, IdentifierMeta extends { identifier: PrimaryIndexIrShape }, IdFields extends keyof ModelFields = | (keyof IdentifierMeta['identifier']['pk'] & keyof ModelFields) | (IdentifierMeta['identifier']['sk'] extends never ? never : keyof IdentifierMeta['identifier']['sk'] & keyof ModelFields), > = Partial> & Required>;