import { IClusterSchemaColumn } from '@cassandratypes/cassandra' ; import { get , set } from 'lodash' ; export interface IColumnDef { name : string ; type : 'partition' | 'clustering' | 'regular' ; isPartitionKey : boolean ; isClusteringKey : boolean ; } export default class ClusterSchema { private schemaMap = { } as { [ keyspaceName ] : { [ tableName ] : { [ columnName ] : string ; } ; } ; } ; constructor ( schemaColumns ) { schemaColumns . forEach ( ( { keyspace , table , column , type } ) => { set ( this . schemaMap , `${ keyspace }.${ table }.${ column }` , type ) ; } ) ; } public getKeyspaceNames ( ) { return Object . keys ( this . schemaMap ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ; } public getTableNames ( keyspaceName ) { const keyspace = this . schemaMap [ this . sanitizeName ( keyspaceName ) ] ; if ( ! keyspace ) { return [ ] ; } return Object . keys ( keyspace ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ; } public getColumnNames ( keyspaceName , tableName ) { const table = get ( this . schemaMap , `${ keyspaceName }.${ tableName }` ) ; if ( ! table ) { return [ ] ; } return Object . keys ( table ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ; } public getColumns ( keyspaceName , tableName , options = { } as { filterable ? : boolean ; } ) { const tableItem = this . getTableColumns ( keyspaceName , tableName ) ; if ( ! tableItem ) { return [ ] ; } const columnDefinitions = [ ] as any [ ] ; for ( const [ columnName , type ] of Object . entries ( tableItem ) ) { const isPartitionKey = type === 'partition' ; const isClusteringColumn = type === 'clustering' ; if ( ! options . filterable || ( options . filterable && ( isPartitionKey || isClusteringColumn ) ) ) { columnDefinitions . push ( { name : columnName , type , isPartitionKey , isClusteringColumn , } ) ; } } return columnDefinitions . sort ( ( a , b ) => { const { name : aName , type : aType } = a ; const { name : bName , type : bType } = b ; if ( aType === 'partition' && bType !== 'partition' ) { return - 1 ; } else if ( aType !== 'partition' && bType === 'partition' ) { return 1 ; } else if ( aType === 'partition' && bType === 'partition' ) { return aName - bName ; } if ( aType === 'clustering' && bType !== 'clustering' ) { return - 1 ; } else if ( aType !== 'clustering' && bType === 'clustering' ) { return 1 ; } else if ( aType === 'clustering' && bType === 'clustering' ) { return aName - bName ; } if ( aName < bName ) { return - 1 ; } else if ( aName > bName ) { return 1 ; } return 0 ; } ) ; } public hasKeyspace ( keyspaceName ) { return ! ! get ( this . schemaMap , keyspaceName ) ; } public hasTable ( keyspaceName , tableName ) { return ! ! get ( this . schemaMap , `${ keyspaceName }.${ tableName }` ) ; } private getTableColumns ( keyspaceName , tableName ) { return ( get ( this . schemaMap , `${ this . sanitizeName ( keyspaceName ) }.${ this . sanitizeName ( tableName ) }` ) as unknown ) as { [ columnName ] : string ; } | undefined ; } private sanitizeName ( name ) { return name ? name . replace ( /"/g , '' ) : name ; } }