module import * as DynamoDB from 'aws-sdk/clients/dynamodb' ; import { PropertyMetadata } from '../../metadata/property-metadata.model' ; import { initOrUpdateProperty } from '../property/init-or-update-property.function' ; import { KEY_PROPERTY } from '../property/key-property.const' ; import { IndexType } from './index-type.enum' ; hidden export interface IndexData { name : string ; keyType : DynamoDB . KeyType ; } hidden export function initOrUpdateIndex ( indexType , indexData , target , propertyKey ) { const properties = Reflect . getMetadata ( KEY_PROPERTY , target . constructor ) || [ ] ; const existingProperty = properties . find ( ( property ) => property . name === propertyKey ) ; let propertyMetadata ; switch ( indexType ) { case IndexType . GSI : propertyMetadata = initOrUpdateGSI ( existingProperty && existingProperty . keyForGSI ? existingProperty . keyForGSI : { } , indexData ) ; break ; case IndexType . LSI : propertyMetadata = initOrUpdateLSI ( existingProperty && existingProperty . sortKeyForLSI ? existingProperty . sortKeyForLSI : [ ] , indexData ) ; break ; default : throw new Error ( `unsupported index type ${ indexType }` ) ; } initOrUpdateProperty ( propertyMetadata , target , propertyKey ) ; } hidden function initOrUpdateGSI ( indexes , indexData ) { if ( indexes [ indexData . name ] ) { } else { indexes [ indexData . name ] = indexData . keyType ; } return { keyForGSI : indexes } ; } hidden function initOrUpdateLSI ( indexes , indexData ) { indexes . push ( indexData . name ) ; return { sortKeyForLSI : indexes } ; }