import { IKeyQuery , IKeyQueryColumnDetails , IKeyQueryOptions , ITableSchema , IRowDetails , ITableColumn , } from '@cassandratypes/cassandra' ; import { getPrimaryKeyColumns } from './cassandra-utils' ; function buildColumnDetails ( columns , row , options ) { const metadataFields = [ 'writetime' , 'ttl' ] ; return columns . reduce ( ( prev , { name , type } ) => { prev [ name ] = { value : row [ name ] , options : { encoding : type === 'blob' ? options . encoding : undefined , } , } ; metadataFields . forEach ( ( field ) => { const metadataKey = `${ field }(${ name })` ; if ( row [ metadataKey ] ) { prev [ metadataKey ] = { value : row [ metadataKey ] , options : { encoding : undefined , } , } ; } } ) ; return prev ; } , { } as { [ columnName ] : IKeyQueryColumnDetails ; } ) ; } export function getKeyQuery ( schema , row , options ) { const { primaryKey : pkColumns } = schema ; const primaryKey = buildColumnDetails ( pkColumns , row , options ) ; return { primaryKey , options , } ; } param schema param row export function getRowWithoutKey ( schema , row ) { const primaryKeyNames = getPrimaryKeyColumns ( schema ) ; return Object . entries ( row ) . filter ( ( [ columnName ] ) => ! primaryKeyNames . has ( columnName ) ) . reduce ( ( prev , [ columnName , columnValue ] ) => ( { ... prev , [ columnName ] : columnValue , } ) , { } ) ; } param schema param row param queryOptions param include export function getRowDetails ( schema , row , queryOptions , include ) { const { columns , primaryKey } = schema ; let includedColumns ; if ( include === 'all' ) { includedColumns = schema . columns ; } else { const pkColumnSet = new Set ( primaryKey . map ( ( col ) => col . name ) ) ; switch ( include ) { case 'primary-key' : includedColumns = primaryKey ; break ; case 'values' : includedColumns = columns . filter ( ( col ) => ! pkColumnSet . has ( col . name ) ) ; break ; default : includedColumns = [ ] ; break ; } } return buildColumnDetails ( includedColumns , row , queryOptions ) ; } export function getKeyQueryFromRowDetails ( schema , row , options ) { const primaryKeyNames = getPrimaryKeyColumns ( schema ) ; const primaryKey = Object . entries ( row ) . reduce ( ( pk , [ columnName , details ] ) => { if ( primaryKeyNames . has ( columnName ) ) { pk [ columnName ] = details ; } return pk ; } , { } as { [ columnName ] : IKeyQueryColumnDetails ; } ) ; return { primaryKey , options , } ; }