|
|
|
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<T extends Model<T>, T2 extends Model<T2>> extends HasOneOrMany<T, T2> {
|
|
|
|
/**
|
|
|
|
* 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<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
|
|
|
|
}
|
|
|
|
}
|