import BaseEntity from '../models/BaseEntity' ; import getSlothData from '../utils/getSlothData' ; import getProtoData from '../utils/getProtoData' ; import { RelationDescriptor } from '../models/relationDescriptors' ; import SlothDatabase from '../models/SlothDatabase' ; see param rel export default function SlothRel ( rel ) { return function ( this , target , key ) { const desc = Reflect . getOwnPropertyDescriptor ( target , key ) ; if ( desc ) { if ( desc . get || desc . set ) { throw new Error ( 'Cannot apply SlothRel on top of another decorator' ) ; } } const { fields , rels } = getProtoData ( target , true ) ; if ( 'belongsTo' in rel ) { fields . push ( { key , docKey : key } ) ; } rels . push ( { ... rel , key } ) ; Reflect . deleteProperty ( target , key ) ; Reflect . defineProperty ( target , key , { get : function ( this , target = this ) { if ( 'hasMany' in rel ) { return ( ) => rel . hasMany ( ) . withRoot ( this . _id ) ; } const { updatedProps , props } = getSlothData ( target ) ; if ( key in updatedProps ) { return ( updatedProps as any ) [ key ] ; } return ( props as any ) [ key ] ; } , set : function ( value ) { if ( value === '' ) { return ; } Object . assign ( getSlothData ( this ) . updatedProps , { [ key ] : value } ) ; } } ) ; } ; }