import indent from 'indent-string' ; export type ConnectorType = 'mysql' | 'mongodb' | 'sqlite' | 'postgresql' | 'sqlserver' | 'jdbc:sqlserver' ; export interface GeneratorConfig { name : string ; output : string | null ; provider : string ; config : Record < string , string > ; } export type Datasource = string | { url : string ; [ key ] : any | undefined ; } ; export interface InternalDatasource { name : string ; provider : ConnectorType ; url : string ; config : any ; } export function printDatasources ( internalDatasources ) { return internalDatasources . map ( ( d ) => String ( new InternalDataSourceClass ( d ) ) ) . join ( '\n\n' ) ; } const tab = 2 ; class InternalDataSourceClass { constructor ( private readonly dataSource ) { } public toString ( ) { const { dataSource } = this ; const obj = { provider : dataSource . provider , url : dataSource . url , } ; if ( dataSource . config && typeof dataSource . config === 'object' ) { Object . assign ( obj , dataSource . config ) ; } return `datasource ${ dataSource . name } {
${ indent ( printDatamodelObject ( obj ) , tab ) }
}` ; } } export function printDatamodelObject ( obj ) { const maxLength = Object . keys ( obj ) . reduce ( ( max , curr ) => Math . max ( max , curr . length ) , 0 ) ; return Object . entries ( obj ) . map ( ( [ key , value ] ) => `${ key . padEnd ( maxLength ) } = ${ typeof value === 'object' && value && value . value ? JSON . stringify ( value . value ) : JSON . stringify ( value ) }` ) . join ( '\n' ) ; }