import BaseEntity from '../models/BaseEntity' ; import getSlothData from '../utils/getSlothData' ; import getProtoData from '../utils/getProtoData' ; typeparam param docKeyName export default function SlothField < T > ( docKeyName ? ) { return function ( this , target , key ) { const docKey = docKeyName || key ; const desc = Reflect . getOwnPropertyDescriptor ( target , key ) ; if ( desc ) { if ( desc . get || desc . set ) { throw new Error ( 'Cannot apply SlothField on top of another decorator' ) ; } } const data = getProtoData ( target , true ) ; data . fields . push ( { key , docKey } ) ; Reflect . deleteProperty ( target , key ) ; Reflect . defineProperty ( target , key , { enumerable : true , get : function ( ) : T | undefined { const { updatedProps , props = { } , defaultProps } = getSlothData ( this ) ; if ( docKey in updatedProps ) { return ( updatedProps as any ) [ docKey ] ; } if ( docKey in props ) { return ( props as any ) [ docKey ] ; } return ( defaultProps as any ) [ docKey ] ; } , set : function ( value ) { const { props , defaultProps , updatedProps } = getSlothData < any > ( this ) ; if ( ! props ) { defaultProps [ docKey ] = value ; return ; } if ( docKey in defaultProps && value == null ) { delete props [ docKey ] ; delete updatedProps [ docKey ] ; return ; } Object . assign ( updatedProps , { [ docKey ] : value } ) ; } } ) ; } ; }