Standard libraries that lift up your code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

37 lines
1.1 KiB

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<any>
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
}
}
}