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.
32 lines
887 B
32 lines
887 B
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<any>
|
|
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
|
|
}
|
|
}
|
|
}
|