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"; export class HasOne, T2 extends Model> extends HasOneOrMany { protected _value?: T2 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 } }