import { O_ALTER_TABLE_SPEC_ADD_PRIMARY_KEY , O_CREATE_TABLE_CREATE_DEFINITION , O_CREATE_TABLE_CREATE_DEFINITION_PRIMARY_KEY , PrimaryKeyInterface , } from '../../../../typings' ; import { isDefined } from '../../../../shared/utils' ; import { IndexColumn } from './index-column' ; import { IndexOptions } from './index-options' ; import { PrimaryKeyModelInterface , IndexColumnModelInterface , IndexOptionsModelInterface , TableModelInterface , ColumnModelInterface , } from './typings' ; /**
 * Primary key of a table.
 */ export class PrimaryKey implements PrimaryKeyModelInterface { name ? : string ; indexType ? : string ; columns ? : IndexColumnModelInterface [ ] ; options ? : IndexOptionsModelInterface ; param json static fromDef ( json ) { if ( json . id === 'O_CREATE_TABLE_CREATE_DEFINITION' ) { return PrimaryKey . fromObject ( json . def . primaryKey as O_ALTER_TABLE_SPEC_ADD_PRIMARY_KEY ) ; } throw new TypeError ( `Unknown json id to build primary key from: ${ json . id }` ) ; } param json static fromObject ( json ) { const primaryKey = new PrimaryKey ( ) ; primaryKey . columns = json . columns . map ( IndexColumn . fromDef ) ; if ( json . name ) { primaryKey . name = json . name ; } if ( json . index ) { primaryKey . indexType = json . index . def . toLowerCase ( ) ; } if ( json . options && json . options . length ) { primaryKey . options = IndexOptions . fromArray ( json . options ) ; } return primaryKey ; } /**
     * JSON casting of this object calls this method.
     */ toJSON ( ) { const json = { columns : ( this . columns ?? [ ] ) . map ( ( c ) => c . toJSON ( ) ) , } ; if ( isDefined ( this . name ) ) { json . name = this . name ; } if ( isDefined ( this . options ) ) { json . options = this . options . toJSON ( ) ; } if ( isDefined ( this . indexType ) ) { json . indexType = this . indexType ; } return json ; } /**
     * Create a deep clone of this model.
     */ clone ( ) { const primaryKey = new PrimaryKey ( ) ; primaryKey . columns = ( this . columns ?? [ ] ) . map ( ( c ) => c . clone ( ) ) ; if ( isDefined ( this . indexType ) ) { primaryKey . indexType = this . indexType ; } if ( this . options ) { primaryKey . options = this . options . clone ( ) ; } return primaryKey ; } param { IndexColumn } indexColumn pushColumn ( indexColumn ) { if ( ! this . columns ) { this . columns = [ ] ; } this . columns . push ( indexColumn ) ; } param name dropColumn ( name ) { if ( ! this . columns ) { return false ; } let pos = - 1 ; const found = this . columns . some ( ( c , i ) => { pos = i ; return c . column === name ; } ) ; if ( ! found || pos < 0 ) { return false ; } const end = this . columns . splice ( pos ) ; end . shift ( ) ; this . columns = this . columns . concat ( end ) ; return true ; } param table getColumnsFromTable ( table ) { return ( table . columns ?? [ ] ) . filter ( ( tableColumn ) => ( this . columns ?? [ ] ) . some ( ( indexColumn ) => indexColumn . column === tableColumn . name ) ) ; } param table hasAllColumnsFromTable ( table ) { return ( ( table . columns ?? [ ] ) . filter ( ( tableColumn ) => ( this . columns ?? [ ] ) . some ( ( indexColumn ) => indexColumn . column === tableColumn . name ) ) . length === ( this . columns ?? [ ] ) . length ) ; } param column param newName renameColumn ( column , newName ) { return this . columns ?. filter ( ( c ) => c . column === column . name ) . forEach ( ( c ) => { c . column = newName ; } ) ; } }