1
1
import { DatetimeUtils } from "./datetime-utils" ;
2
- import { ValueType , ValueTypeToParse } from "./core/utils" ;
2
+ import { Utils , ValueType , ValueTypeToParse } from "./core/utils" ;
3
3
import * as moment from "moment" ;
4
4
import { FieldType } from "./core/enums/field-type" ;
5
5
import { ColumnType } from "./core/enums/column-type" ;
6
6
import { DatabaseBuilderError } from "./core/errors" ;
7
7
8
8
export class DatabaseHelper {
9
9
10
+ public isTypeSimpleByType ( type : FieldType ) : boolean {
11
+ return type !== FieldType . OBJECT && type !== FieldType . FUNCTION && type !== FieldType . ARRAY ;
12
+ }
13
+
10
14
public isTypeSimple ( value : ValueTypeToParse ) : boolean {
11
15
const type = this . getType ( value ) ;
12
- return type !== FieldType . OBJECT && type !== FieldType . FUNCTION && type !== FieldType . ARRAY ;
16
+ return this . isTypeSimpleByType ( type ) ;
17
+ }
18
+
19
+ public isTypeIgnoredInMapperByType ( type : FieldType ) : boolean {
20
+ return type === FieldType . ARRAY ;
13
21
}
14
22
15
23
public isTypeIgnoredInMapper ( value : ValueTypeToParse ) : boolean {
16
24
const type = this . getType ( value ) ;
17
- return type === FieldType . ARRAY ;
25
+ return this . isTypeIgnoredInMapperByType ( type ) ;
18
26
}
19
27
20
- public getType ( value : ValueTypeToParse ) : FieldType {
21
- const valueFormatted = this . preFormatValue ( value ) ;
22
- const tipo = typeof valueFormatted ;
23
- switch ( tipo ) {
28
+ public getFieldType < T > ( type : string | ( new ( ) => T ) , constructorName ?: string ) {
29
+ const typeCase : string = ( Utils . isString ( type ) ? type as string : ( type as new ( ) => void ) . name ) . toLowerCase ( ) ;
30
+ switch ( typeCase ) {
24
31
case "string" :
25
32
return FieldType . STRING ;
26
33
case "number" :
27
34
return FieldType . NUMBER ;
28
35
case "boolean" :
29
36
return FieldType . BOOLEAN ;
30
37
case "object" :
31
- // tratar date como inteiro
32
- if (
33
- valueFormatted . constructor . name === "Date"
34
- || valueFormatted . constructor . name === "Moment"
35
- ) {
36
- return FieldType . DATE ;
37
- }
38
- if ( Array . isArray ( valueFormatted ) ) {
39
- return FieldType . ARRAY ;
38
+ if ( constructorName ) {
39
+ // tratar date como inteiro
40
+ if (
41
+ constructorName === "Date"
42
+ ||
43
+ constructorName === "Moment"
44
+ ) {
45
+ return FieldType . DATE ;
46
+ }
47
+ if ( constructorName === "Array" ) {
48
+ return FieldType . ARRAY ;
49
+ }
40
50
}
41
51
// serializar todos os objetos
42
52
return FieldType . OBJECT ;
@@ -45,10 +55,22 @@ export class DatabaseHelper {
45
55
case "undefined" :
46
56
return FieldType . NULL ;
47
57
default :
48
- throw new DatabaseBuilderError ( `type: '${ tipo } ', value: '${ valueFormatted } ' não configurado!` ) ;
58
+ if (
59
+ ! Utils . isString ( type ) &&
60
+ type . constructor . length === 0 ? new ( type as ( new ( ) => T ) ) ( ) : { } instanceof Object
61
+ ) {
62
+ return FieldType . OBJECT ;
63
+ }
64
+ throw new DatabaseBuilderError ( `type: '${ type } ', constructor name: '${ constructorName } ' não configurado!` ) ;
49
65
}
50
66
}
51
67
68
+ public getType ( value : ValueTypeToParse ) : FieldType {
69
+ const valueFormatted = this . preFormatValue ( value ) ;
70
+ const tipo = typeof valueFormatted ;
71
+ return this . getFieldType ( tipo , valueFormatted ? valueFormatted . constructor . name : void 0 ) ;
72
+ }
73
+
52
74
public parseToColumnType ( type : FieldType ) : ColumnType {
53
75
switch ( type ) {
54
76
case FieldType . STRING :
0 commit comments