-
Notifications
You must be signed in to change notification settings - Fork 7
/
typeUtil.ts
112 lines (103 loc) · 2.94 KB
/
typeUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import utils from './utils';
import { DataType } from '../types';
/**
* TypeUtil util class.
*
* @export
* @class TypeUtil
*/
export class TypeUtil {
/**
* Array that holds the number types
*
* @private
* @type {string[]}
* @memberof TypeUtil
*/
private numberTypes: string[] = ['int', 'tinyint', 'smallint', 'mediumint', 'bigint', 'decimal', 'float', 'double'];
/**
* Array that holds the string types
*
* @private
* @type {string[]}
* @memberof TypeUtil
*/
private stringTypes: string[] = [
'char', 'varchar', 'blob', 'text', 'tinyblob', 'tinytext', 'mediumblob', 'mediumtext', 'longblob', 'longtext',
];
/**
* Converts a given database type to the standard one.
* @param {string} type db type.
* @returns {string} standard type
*/
public convertSqlType(type: string): string {
if (utils.indexOfIgnoreCase(this.numberTypes, type.toLowerCase()) > -1 ||
(type.toLowerCase() === 'timestamp') ||
(type.toLowerCase() === 'year')) {
return 'number';
} else if (utils.indexOfIgnoreCase(this.stringTypes, type.toLowerCase()) > -1) {
return 'string';
} else if (type.toLowerCase() === 'date') {
return 'date-only';
} else if (type.toLowerCase() === 'datetime') {
return 'datetime';
} else if (type.toLowerCase() === 'time') {
return 'time-only';
} else if (type.toLowerCase() === 'enum') {
return 'enum';
}
throw new Error('Type not found');
}
/**
* Converts no sql types to standard schema
* @param {string} type - type to be converted
*/
public convertNoSqlType(type: string): void {
throw new Error(`${type} not yet implemented.`);
}
/**
* Converts raml types to standard schema
* @param {{type: string; items: string}} type - type to be converted
* @returns {{type: string; isArray: boolean; values?: string[]}} - converted type
*/
public convertRamlTypes(type: {type: string; items: string}): DataType {
if (type.type.indexOf('|') > -1) {
return {
type: 'enum',
isArray: false,
values: type.type.split(' | '),
};
} else if (type.type === 'array') {
return {
type: type.items,
isArray: true,
};
}
return {
type: type.type,
isArray: false,
};
}
/**
* Converts Enum values to string.
*
* @param {DataType} type source datatype
* @return {string} enum values as string
*/
public getEnumValuesAsString(type: DataType): string {
if (type.type === 'enum' && type.values) {
return type.values.map((value: string) => `'${value}'`).join(', ');
}
return '';
}
/**
* Checks if the provided type is a default one.
*
* @param {string} type - data type
* @return {boolean} returns true if type is a default one.
*/
public isDefaultType(type: string): boolean {
return ['enum', 'number', 'string'].some((item: string) => type === item);
}
}
export default new TypeUtil();