|
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
|
|
|
|