import {Model} from '../Model.ts' /** * Decorator for model relations. This caches the relation value, so lookups are only done once. * @constructor */ export function Relation(): MethodDecorator { return (target: any, propertyKey, descriptor) => { // @ts-ignore const original = descriptor.value // @ts-ignore descriptor.value = function(...args) { const model = this as Model const cache = model.relation_cache const existing_relation = cache.firstWhere('accessor_name', '=', propertyKey) if ( existing_relation ) return existing_relation.relation // @ts-ignore const value = original.apply(this, args) cache.push({ accessor_name: propertyKey, relation: value, }) return value } } }