1
- import { MetadataTable } from ' ./../metadata-table' ;
2
- import { DatabaseHelper } from ' ./../database-helper' ;
1
+ import { MetadataTable } from " ./../metadata-table" ;
2
+ import { DatabaseHelper } from " ./../database-helper" ;
3
3
import { Expression , ExpressionUtils } from "lambda-expression" ;
4
- import { FieldType , ColumnsCompiled , ValueTypeToParse , Column } from './utils' ;
5
- import { MapperTable } from '../mapper-table' ;
4
+ import { ExpressionOrColumn , Utils , ValueTypeToParse } from "./utils" ;
5
+ import { MapperTable } from "../mapper-table" ;
6
+ import { Column } from "./column" ;
7
+ import { FieldType } from "./enums/field-type" ;
8
+ import { ColumnsCompiled } from "./columns-compiled" ;
6
9
7
- export abstract class ColumnsBaseBuilder < TThis extends ColumnsBaseBuilder < TThis , T , TColumn > , T , TColumn extends Column > {
10
+ export abstract class ColumnsBaseBuilder <
11
+ TThis extends ColumnsBaseBuilder < TThis , T , TColumn > ,
12
+ T ,
13
+ TColumn extends Column
14
+ > {
8
15
9
16
protected columns : TColumn [ ] = [ ] ;
10
- protected readonly expressionUtils : ExpressionUtils ;
11
- protected readonly databaseHelper : DatabaseHelper ;
12
17
13
18
constructor (
14
19
protected readonly metadata : MetadataTable < T > ,
15
- protected readonly modelToSave : T = metadata . instance
20
+ protected readonly modelToSave : T = metadata . instance ,
16
21
) {
17
- this . expressionUtils = new ExpressionUtils ( ) ;
18
- this . databaseHelper = new DatabaseHelper ( ) ;
19
22
}
20
23
21
24
public allColumns ( ) {
@@ -25,49 +28,48 @@ export abstract class ColumnsBaseBuilder<TThis extends ColumnsBaseBuilder<TThis,
25
28
}
26
29
27
30
public setColumn ( column : string , type : FieldType ) : TThis {
28
- this . columns . push ( < TColumn > {
31
+ this . columns . push ( {
29
32
name : column ,
30
- type : type
31
- } ) ;
33
+ type,
34
+ } as TColumn ) ;
32
35
return this . getInstance ( ) ;
33
36
}
34
37
35
- public set ( expression : Expression < T > ) : TThis {
38
+ public set ( expression : ExpressionOrColumn < T > ) : TThis {
36
39
return this . setColumn (
37
- this . expressionUtils . getColumnByExpression ( expression ) ,
38
- this . getTypeByExpression ( expression )
40
+ Utils . getColumn ( expression ) ,
41
+ Utils . getType ( this . metadata . instance , expression ) ,
39
42
) ;
40
43
}
41
44
42
45
public compile ( ) : ColumnsCompiled {
43
- let result : ColumnsCompiled = {
46
+ const result : ColumnsCompiled = {
44
47
columns : [ ] ,
45
- params : [ ]
48
+ params : [ ] ,
46
49
} ;
47
- this . columns . forEach ( column => {
48
- result . columns . push ( this . columnFormat ( column ) ) ;
49
- } ) ;
50
- return result ;
51
- }
52
-
53
- private setAllColumns ( mapper : MapperTable , modelWithValue : T ) : void {
54
- for ( let key in mapper . columns ) {
55
- let column = mapper . columns [ key ] ;
56
- this . setColumnValue ( column . column , this . databaseHelper . getValue ( modelWithValue , column . fieldReference ) , column . fieldType ) ;
50
+ for ( const key in this . columns ) {
51
+ if ( this . columns . hasOwnProperty ( key ) ) {
52
+ const column = this . columns [ key ] ;
53
+ result . columns . push ( this . columnFormat ( column ) ) ;
54
+ }
57
55
}
58
- }
59
-
60
- protected getTypeByValue ( value : ValueTypeToParse ) : FieldType {
61
- return this . databaseHelper . getType ( value ) ;
62
- }
63
-
64
- protected getTypeByExpression ( expression : Expression < T > ) : FieldType {
65
- return this . getTypeByValue ( this . expressionUtils . getValueByExpression ( this . metadata . instance , expression ) ) ;
56
+ return result ;
66
57
}
67
58
68
59
protected abstract columnFormat ( column : TColumn ) : string ;
69
60
70
61
protected abstract getInstance ( ) : TThis ;
71
62
72
63
protected abstract setColumnValue ( column : string , value : ValueTypeToParse , fieldType : FieldType ) : TThis ;
73
- }
64
+
65
+ private setAllColumns ( mapper : MapperTable , modelWithValue : T ) : void {
66
+ for ( const key in mapper . columns ) {
67
+ if ( mapper . columns . hasOwnProperty ( key ) ) {
68
+ const column = mapper . columns [ key ] ;
69
+ this . setColumnValue ( column . column ,
70
+ Utils . getValue ( modelWithValue , column . fieldReference ) ,
71
+ column . fieldType ) ;
72
+ }
73
+ }
74
+ }
75
+ }
0 commit comments