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.

31 lines
937 B

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<T extends Model<T>, T2 extends Model<T2>> extends HasOneOrMany<T, T2> {
protected _value?: T2
protected _loaded = false
public async get(): Promise<T2 | undefined> {
return this.fetch().first()
}
public set_value(related: Collection<T2>) {
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
}
}