Passed
Pull Request — master (#103)
by Mark
01:28
created

src/Store/Model/Util/Setup.ts   A

Complexity

Total Complexity 17
Complexity/F 2.43

Size

Lines of Code 81
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 17
mnd 10
bc 10
fnc 7
bpm 1.4285
cpm 2.4285
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Setup.ts ➔ addRelationFieldsToList 0 9 2
A Setup.ts ➔ setFields 0 7 1
C Setup.ts ➔ modelProxy 0 43 11
A Setup.ts ➔ setupTable 0 9 3
1
import Relation from "../Relation";
2
import ForeignKey from "../Field/ForeignKey";
3
import HasManyThrough from "../Relation/HasManyThrough";
4
import Field from "../Field";
5
6
export function addRelationFieldsToList(fields) {
7
    const fieldList = [...fields];
8
    fields.forEach((field, i) => {
9
        if (field instanceof Relation) {
10
            fieldList.splice(i, 0, ...field.getRelationalFields());
11
        }
12
    });
13
    return fieldList;
14
}
15
16
export function setFields(model, fields: Field[]) {
17
    fields.forEach((field) => {
18
        model._originalFields.set(field.name, field);
19
        field.setup(modelProxy(model));
20
    });
21
    model.numberOfFields = model.originalFields.length;
22
}
23
24
export function setupTable(model, table) {
25
    model.originalFields.forEach((field) => {
26
        if (field instanceof ForeignKey) {
27
            field.tableSetup(table);
28
        }
29
30
        if (field instanceof HasManyThrough) {
31
            field.tableSetup();
32
        }
33
    });
34
}
35
36
export function modelProxy(model) {
37
    return new Proxy(model, {
38
        construct(target, argArray, newTarget): object {
39
            return Reflect.construct(target, argArray, newTarget);
40
        },
41
42
        get(target: any, p: string | symbol, receiver: any): any {
43
            if (Reflect.has(target, p)) {
44
                return Reflect.get(target, p);
45
            }
46
47
            if (target._originalFields.has(p)) {
48
                return target._originalFields.get(p).value;
49
            }
50
51
            if (typeof p !== 'string') {
52
                return null;
53
            }
54
55
            if (p.startsWith('original_') && !Reflect.has(target, p.replace('original_', '')) && target._originalFields.has(p.replace('original_', ''))) {
56
                return target._originalFields.get(p.replace('original_', '')).originalValue;
57
            }
58
            return null;
59
        },
60
61
        set(target: any, p: string | symbol, value: any, receiver: any): boolean {
62
            if (Reflect.has(target, p)) {
63
                return Reflect.set(target, p, value);
64
            }
65
66
            if (target._originalFields.has(p)) {
67
                target._originalFields.get(p).value = value;
68
                return true;
69
            }
70
71
            if (p.startsWith('_') && target._originalFields.has(p.replace('_', ''))) {
72
                const myField = target._originalFields.get(p.replace('_', ''));
73
                myField._value = value;
74
                return true;
75
            }
76
77
            return true;
78
        }
79
    });
80
}
81