import { AutoGenerateAttributeRawMetadataOptions , EntityRawMetadataOptions , IsAutoGenerateAttributeRawMetadataOptions , MetadataManager , Table , } from '@typedorm/common' ; import { isUsedForPrimaryKey } from '../../helpers/is-used-for-primary-key' ; import { AttributeMetadata } from '../metadata/attribute-metadata' ; import { AutoGeneratedAttributeMetadata } from '../metadata/auto-generated-attribute-metadata' ; export class AttributesMetadataBuilder { constructor ( ) { } build ( table , attributeTargetEntityClass , decoratedEntityClass ) { const getRawAttributesForEntity = MetadataManager . metadataStorage . getRawAttributesForEntity ( attributeTargetEntityClass ) ; return getRawAttributesForEntity . map ( attr => { if ( IsAutoGenerateAttributeRawMetadataOptions ( attr ) ) { const entityMetadata = this . tryGetEntityMetadata ( attributeTargetEntityClass , decoratedEntityClass ) ; return this . validateAndBuildAutoGeneratedAttributeMetadata ( attr , entityMetadata ) ; } return new AttributeMetadata ( { table , entityClass : decoratedEntityClass ?? attributeTargetEntityClass , ... attr , } ) ; } ) ; } private validateAndBuildAutoGeneratedAttributeMetadata ( attr , entityMetadata ) { if ( attr . autoUpdate ) { if ( isUsedForPrimaryKey ( entityMetadata . primaryKey , attr . name ) ) { throw new Error ( `Failed to build metadata for "${ attr . name }", attributes referenced in primary key cannot be auto updated.` ) ; } } return new AutoGeneratedAttributeMetadata ( { ... attr , } ) ; } private tryGetEntityMetadata ( attributeTargetEntityClass , decoratedEntityClass ) { let entityMetadata ; try { entityMetadata = MetadataManager . metadataStorage . getRawEntityByTarget ( attributeTargetEntityClass ) ; } catch ( err ) { entityMetadata = MetadataManager . metadataStorage . getRawEntityByTarget ( decoratedEntityClass ) ; } return entityMetadata ; } }