author Jeremy Daly <jeremy@jeremydaly.com>
 *  license import parseAttributes from './parseTableAttributes' ; import { error , hasValue } from './utils' ; import { TableConstructor , TableIndexes } from '../classes/Table' ; export type ParsedTable = ReturnType < typeof parseTable > ; export const parseTable = ( table ) => { let { name , alias , partitionKey , sortKey , entityField , attributes , indexes , autoExecute , autoParse , removeNullAttributes , entities , DocumentClient , ... args } = table ; if ( Object . keys ( args ) . length > 0 ) error ( `Invalid Table configuration options: ${ Object . keys ( args ) . join ( ', ' ) }` ) ; name = typeof name === 'string' && name . trim ( ) . length > 0 ? name . trim ( ) : error ( `'name' must be defined` ) ; alias = typeof alias === 'string' && alias . trim ( ) . length > 0 ? alias . trim ( ) : alias ? error ( `'alias' must be a string value` ) : null ; partitionKey = typeof partitionKey === 'string' && partitionKey . trim ( ) . length > 0 ? partitionKey . trim ( ) : error ( `'partitionKey' must be defined` ) ; sortKey = typeof sortKey === 'string' && sortKey . trim ( ) . length > 0 ? sortKey . trim ( ) : sortKey ? error ( `'sortKey' must be a string value` ) : null ; entityField = entityField === false ? false : typeof entityField === 'string' && entityField . trim ( ) . length > 0 ? entityField . trim ( ) : '_et' ; attributes = hasValue ( attributes ) && typeof attributes === 'object' && ! Array . isArray ( attributes ) ? attributes : attributes ? error ( `Please provide a valid 'attributes' object` ) : { } ; if ( entityField ) attributes [ entityField ] = 'string' ; indexes = hasValue ( indexes ) && typeof indexes === 'object' && ! Array . isArray ( indexes ) ? parseIndexes ( indexes , partitionKey ) : indexes ? error ( `Please provide a valid 'indexes' object` ) : { } ; return Object . assign ( { name , alias , Table : { partitionKey , sortKey , entityField , attributes : parseAttributes ( attributes , partitionKey , sortKey ) as any , indexes } , autoExecute , autoParse , removeNullAttributes , _entities : [ ] } , DocumentClient ? { DocumentClient } : { } , entities ? { entities } : { } ) ; } ; const parseIndexes = ( indexes , pk ) : TableIndexes => Object . keys ( indexes ) . reduce ( ( acc , index ) => { const { partitionKey , sortKey , ... args } = indexes [ index ] ; if ( Object . keys ( args ) . length > 0 ) error ( `Invalid index options: ${ Object . keys ( args ) . join ( ', ' ) }` ) ; if ( partitionKey && typeof partitionKey !== 'string' ) error ( `'partitionKey' for ${ index } must be a string` ) ; if ( sortKey && typeof sortKey !== 'string' ) error ( `'sortKey' for ${ index } must be a string` ) ; if ( ! sortKey && ! partitionKey ) error ( `A 'partitionKey', 'sortKey' or both, must be provided for ${ index }` ) ; const type = ! partitionKey || partitionKey === pk ? 'LSI' : 'GSI' ; return Object . assign ( acc , { [ index ] : Object . assign ( { } , partitionKey && type === 'GSI' ? { partitionKey } : { } , sortKey ? { sortKey } : { } , { type } ) } ) ; } , { } ) ; export default parseTable ;