import { Converter , Key } from 'aws-sdk/clients/dynamodb' ; import { InternalProcessingException } from '../base/exceptions' ; import { CollectionLayout } from '../base/layout' ; import { SEPARATOR } from '../base/util' ; internal export type ItemIdDescriptor = { /** The collection containing the item */ collection : string ; id : string ; parentId ? : string ; } ; internal export const parseKey = ( layout , key ) : ItemIdDescriptor => { const partitionKey = Converter . output ( key [ layout . primaryKey . partitionKey ] ) ; const sortKey = Converter . output ( key [ layout . primaryKey . sortKey ] ) ; if ( ! partitionKey || ! sortKey ) throw new InternalProcessingException ( `Selected layout could not find key names ${ JSON . stringify ( key ) } for table ${ layout . tableName }` ) ; if ( typeof partitionKey !== 'string' ) throw new InternalProcessingException ( `Partition key ${ layout . primaryKey . partitionKey } for table ${ layout . tableName } is not a string` ) ; if ( typeof sortKey !== 'string' ) throw new InternalProcessingException ( `Sort key ${ layout . primaryKey . sortKey } for table ${ layout . tableName } is not a string` ) ; const [ , parentId ] = partitionKey . split ( layout . indexKeySeparator ?? SEPARATOR , 2 ) ; const [ childCollection , id ] = sortKey . split ( layout . indexKeySeparator ?? SEPARATOR , 2 ) ; return { collection : childCollection , parentId : parentId !== id ? parentId : undefined , id : id , } ; } ;