import {Model} from '../Model.ts' import {HasOneOrMany} from './HasOneOrMany.ts' import {Collection} from '../../../../lib/src/collection/Collection.ts' import {Logging} from '../../../../lib/src/service/logging/Logging.ts' /** * Relation class for one-to-one relations. * @extends HasOneOrMany */ export class HasOne, T2 extends Model> extends HasOneOrMany { /** * The cached value of this relation. * @type Model */ protected _value?: T2 /** * True if the relation has been loaded. * @type boolean */ protected _loaded = false public async get(): Promise { return this.fetch().first() } public set_value(related: Collection) { this._value = related.first() this._loaded = true if ( related.length > 1 ) { this.make(Logging).warn(`HasOne relation result contained more than one value for ${this.qualified_local_key} -> ${this.qualified_local_key}`) } } public get_value(): T2 | undefined { return this._value } public is_loaded(): boolean { return this._loaded } }