11import { DatetimeUtils } from "./datetime-utils" ;
2- import { ValueType , ValueTypeToParse } from "./core/utils" ;
2+ import { Utils , ValueType , ValueTypeToParse } from "./core/utils" ;
33import * as moment from "moment" ;
44import { FieldType } from "./core/enums/field-type" ;
55import { ColumnType } from "./core/enums/column-type" ;
66import { DatabaseBuilderError } from "./core/errors" ;
77
88export class DatabaseHelper {
99
10+ public isTypeSimpleByType ( type : FieldType ) : boolean {
11+ return type !== FieldType . OBJECT && type !== FieldType . FUNCTION && type !== FieldType . ARRAY ;
12+ }
13+
1014 public isTypeSimple ( value : ValueTypeToParse ) : boolean {
1115 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 ;
1321 }
1422
1523 public isTypeIgnoredInMapper ( value : ValueTypeToParse ) : boolean {
1624 const type = this . getType ( value ) ;
17- return type === FieldType . ARRAY ;
25+ return this . isTypeIgnoredInMapperByType ( type ) ;
1826 }
1927
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 ) {
2431 case "string" :
2532 return FieldType . STRING ;
2633 case "number" :
2734 return FieldType . NUMBER ;
2835 case "boolean" :
2936 return FieldType . BOOLEAN ;
3037 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+ }
4050 }
4151 // serializar todos os objetos
4252 return FieldType . OBJECT ;
@@ -45,10 +55,22 @@ export class DatabaseHelper {
4555 case "undefined" :
4656 return FieldType . NULL ;
4757 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!` ) ;
4965 }
5066 }
5167
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+
5274 public parseToColumnType ( type : FieldType ) : ColumnType {
5375 switch ( type ) {
5476 case FieldType . STRING :
0 commit comments