import { BaseModel , IFieldSchema } from '../BaseModel' ; import { initModelClassSchema } from '../initModelClassSchema' ; export function Field ( type ? , options ? ) ; export function Field ( options ? ) ; export function Field ( typeOrOptions ? , options ? ) { let type ; if ( typeof typeOrOptions == 'object' ) { options = typeOrOptions ; } else if ( typeOrOptions ) { type = typeOrOptions ; } if ( ! type && options && options . type ) { type = options . type ; } return ( target , propertyName , _propertyDesc ? ) : any => { let schema = ( initModelClassSchema ( target . constructor as any ) . fields [ propertyName ] = { } ) ; if ( options && options . dbFieldName ) { schema . dbFieldName = options . dbFieldName ; } if ( type ) { schema . type = type ; } if ( options && options . default != null ) { schema . default = options . default ; } if ( options && options . validate ) { schema . validate = options . validate ; } const KEY_VALUE = Symbol ( 'value:' + propertyName ) ; return { configurable : true , enumerable : true , get ( this ) { return type && type ( ) . $schema . collectionName ? this . fetchField ( propertyName as any ) : this [ KEY_VALUE ] ; } , set ( this , value ) { this . setField ( propertyName as any , value , KEY_VALUE ) ; } } ; } ; }