import { JSONSchema7 as OriginalJSONSchema7 } from 'json-schema' ; import { TableInterface , JSONSchemaFormatOptions } from '../../../../typings' ; import { isDefined , setProperty } from '../../../../shared/utils' ; import { Column } from './column' ; type JSONSchema7 = OriginalJSONSchema7 & { type : string ; required : string [ ] ; definitions : NonNullable < OriginalJSONSchema7 [ 'definitions' ] > ; properties : NonNullable < OriginalJSONSchema7 [ 'properties' ] > ; } ; /**
 * Class to represent a table as parsed from compact format.
 */ export class Table { /**
     * Table columns.
     */ columns : Column [ ] = [ ] ; /**
     * Table name.
     */ name ! : string ; /**
     * Table comment.
     */ comment ? : string ; param json static fromCompactJson ( json ) { const table = new Table ( ) ; table . name = json . name ; if ( isDefined ( json . columns ) ) { table . columns = json . columns . map ( ( c ) => Column . fromCompactJson ( c ) ) ; } if ( isDefined ( json . primaryKey ) ) { /**
             * Set property in column(s) that is/are primary key(s).
             */ ( json . primaryKey . columns ?? [ ] ) . map ( ( c ) => c . column ) . map ( ( name ) => table . columns . find ( ( c ) => c . name === name ) ) . filter ( isDefined ) . forEach ( ( column ) => { column . isPrimaryKey = true ; } ) ; } const options = json . options ; if ( options ) { if ( isDefined ( options . comment ) ) { table . comment = options . comment ; } } return table ; } param options toJSON ( options ) { const json = { $schema : 'http://json-schema.org/draft-07/schema' , $comment : `JSON Schema for ${ this . name } table` , $id : this . name , title : this . name , description : '' , type : 'object' , required : [ ] , definitions : { } , properties : { } , } ; if ( isDefined ( this . comment ) ) { json . description = this . comment ; } else { delete json . description ; } this . columns . forEach ( ( c ) => { const column = c . toJSON ( ) ; const name = c . name ; const definitions = { } ; json . properties [ name ] = { $ref : `#/definitions/${ name }` , } ; Object . getOwnPropertyNames ( column ) . forEach ( ( key ) => { setProperty ( definitions , key , column [ key as keyof typeof column ] ) ; } ) ; json . definitions [ name ] = definitions ; if ( c . isNullable === false ) { ( json . required as string [ ] ) . push ( name ) ; } } ) ; /**
         * Option to not use $ref, and have properties flattened out to 'properties' node.
         * https://github.com/duartealexf/sql-ddl-to-json-schema/issues/36
         */ if ( options . useRef === false ) { json . properties = json . definitions ; delete json . definitions ; } return json ; } }