import {Model} from '../Model' /** * Decorator for relation methods on a Model implementation. * Caches the relation instances between uses for the life of the model. * @constructor */ export function Related(): MethodDecorator { return (target, propertyKey, descriptor) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const original = descriptor.value // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore descriptor.value = function(...args) { const model = this as Model const cache = model.relationCache const existing = cache.firstWhere('accessor', '=', propertyKey) if ( existing ) { return existing.relation } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const value = original.apply(this, args) cache.push({ accessor: propertyKey, relation: value, }) return value } } }