src/Store/Model/Relation/HasOne.ts   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 37
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 6
mnd 3
bc 3
fnc 3
bpm 1
cpm 2
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A HasOne.getRelationalFields 0 3 1
A HasOne.setName 0 4 1
A HasOne.getValueByParentKey 0 5 4
1
import Relation from "../Relation";
2
import {ModelStaticInterface} from "../../../JeloquentInterfaces";
3
import ForeignKey from "../Field/ForeignKey";
4
5
/**
6
 *
7
 */
8
export default class HasOne extends Relation {
9
10
    constructor(model: ModelStaticInterface) {
11
        super(model);
12
    }
13
14
    get originalValue(): unknown {
15
        return this.getValueByParentKey('originalPrimaryKey');
16
    }
17
18
    get value(): unknown {
19
        return this.getValueByParentKey('primaryKey');
20
    }
21
22
    getRelationalFields():Array<ForeignKey> {
23
        return [];
24
    }
25
26
    setName(): HasOne {
27
        this.foreignKey = `${this.$parent.snakeCaseClassName}_id`;
28
        return this;
29
    }
30
31
    private getValueByParentKey(parentProperty) {
32
        const keyIndex = this.model.getIndexByKey(this.foreignKey);
33
        return globalThis.Store.database().find(this.model.className,
34
            [...keyIndex.get(`${this.$parent[parentProperty]}`)?.values() ?? []]
35
        ).first();
36
    }
37
}