import { Backend } from '../base-backend' ; import { BaseField , BaseDbField , BaseVirtualField } from '../fields/base' ; export interface BaseModelParams { name : string ; fields ? : BaseField [ ] ; verboseName ? : string ; } export abstract class BaseModel implements BaseModelParams { name : string ; fields ? : BaseField [ ] ; verboseName ? : string ; backend : Backend ; private getDbFieldsCache : BaseDbField [ ] ; constructor ( backend , params ) { this . backend = backend ; this . name = params . name ; this . fields = params . fields ; this . verboseName = params . verboseName ; } getDbFields ( ) { if ( this . getDbFieldsCache ) return this . getDbFieldsCache ; var result = [ ] ; for ( let field of this . fields ) { if ( field instanceof BaseDbField ) result . push ( field ) ; } this . getDbFieldsCache = result ; return result ; } getField ( name , notFoundResult ? ) { for ( let field of this . fields ) { if ( field . name == name ) return field ; } if ( notFoundResult !== undefined ) return notFoundResult ; else throw new Error ( 'Field "' + name + '" not found' ) ; } getDbField ( name , notFoundResult ? ) { for ( let field of this . fields ) { if ( field . name == name && field instanceof BaseDbField ) return field ; } if ( notFoundResult !== undefined ) return notFoundResult ; else throw new Error ( 'Field "' + name + '" not found' ) ; } hasField ( name ) { return ! ! this . getField ( name ) ; } toString ( ) { return this . verboseName ? this . verboseName : ( this . name . charAt ( 0 ) . toUpperCase ( ) + this . name . slice ( 1 ) ) ; } }