import { P_CREATE_INDEX } from '../../../../../typings' ; import { UniqueKey } from '../unique-key' ; import { FulltextIndex } from '../fulltext-index' ; import { SpatialIndex } from '../spatial-index' ; import { Index } from '../index' ; import { TableModelInterface , DatabaseModelInterface , RuleHandler } from '../typings' ; /**
 * Formatter for P_CREATE_INDEX rule's parsed JSON.
 */ export class CreateIndex implements RuleHandler { database ! : DatabaseModelInterface ; param name getTable ( name ) { return this . database . getTable ( name ) ; } param database setDatabase ( database ) { this . database = database ; } param json handleDef ( json ) { if ( json . id === 'P_CREATE_INDEX' ) { const table = this . getTable ( json . def . table ) ; if ( ! table ) { return ; } const type = json . def . type . toLowerCase ( ) ; if ( type . includes ( 'unique' ) ) { table . pushUniqueKey ( UniqueKey . fromDef ( json ) ) ; } else if ( type . includes ( 'fulltext' ) ) { table . pushFulltextIndex ( FulltextIndex . fromDef ( json ) ) ; } else if ( type . includes ( 'spatial' ) ) { table . pushSpatialIndex ( SpatialIndex . fromDef ( json ) ) ; } else { table . pushIndex ( Index . fromDef ( json ) ) ; } return ; } throw new TypeError ( `Expected P_CREATE_INDEX rule to be handled but received ${ json . id }` ) ; } }