import { isScalarTypeProvider } from './../../helpers/is-scalar-type' ; import { AttributeOptionsUniqueType , CompositePrimaryKey , DYNAMO_ATTRIBUTE_PREFIX , EntityTarget , PrimaryKey , SimplePrimaryKey , Table , IsPrimaryKey , ScalarType , } from '@typedorm/common' ; import { buildPrimaryKeySchema } from '../../helpers/build-primary-key-schema' ; import { DynamoEntitySchemaPrimaryKey } from './entity-metadata' ; import { BaseAttributeMetadataOptions , BaseAttributeMetadata , } from './base-attribute-metadata' ; export interface AttributeMetadataOptions extends BaseAttributeMetadataOptions { table : Table ; entityClass : EntityTarget < any > ; unique ? : AttributeOptionsUniqueType ; default ? : ScalarType | ( ( ) => ScalarType ) ; } export class AttributeMetadata extends BaseAttributeMetadata { readonly unique ? : DynamoEntitySchemaPrimaryKey ; readonly default ? : ( entity : any ) => ScalarType ; readonly table : Table ; readonly entityClass : EntityTarget < any > ; constructor ( options ) { const { name , entityClass , unique , table } = options ; super ( options ) ; this . entityClass = entityClass ; this . table = table ; this . default = this . getDefaultValueProvider ( name , options . default ) ; if ( unique ) { this . unique = this . buildUniqueAttributesPrimaryKey ( unique ) ; } } private getDefaultValueProvider ( attrName , defaultValue ) { if ( ! defaultValue ) { return ; } if ( isScalarTypeProvider ( defaultValue ) ) { return defaultValue ; } else { return ( ) => defaultValue ; } } private buildUniqueAttributesPrimaryKey ( unique ) { if ( IsPrimaryKey ( unique ) ) { return buildPrimaryKeySchema ( { table : this . table , primaryKey : unique , attributes : { [ this . name ] : this . type , } , } ) ; } else { return this . autoGeneratedPrimaryKeySchema ( ) ; } } private autoGeneratedPrimaryKeySchema ( ) { const primaryKey = { } as PrimaryKey ; const uniqueKeyValue = `${ DYNAMO_ATTRIBUTE_PREFIX }_${ this . entityClass . name . toUpperCase ( ) }.${ this . name . toUpperCase ( ) }#{{${ this . name }}}` ; if ( this . table . usesCompositeKey ( ) ) { ( primaryKey as CompositePrimaryKey ) . partitionKey = uniqueKeyValue ; ( primaryKey as CompositePrimaryKey ) . sortKey = uniqueKeyValue ; } else { ( primaryKey as SimplePrimaryKey ) . partitionKey = uniqueKeyValue ; } return buildPrimaryKeySchema ( { table : this . table , primaryKey , attributes : { [ this . name ] : this . type , } , } ) ; } }