"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
/**
* TypeUtil util class.
*
* @export
* @class TypeUtil
*/
var TypeUtil = (function () {
function TypeUtil() {
/**
* Array that holds the number types
*
* @private
* @type {string[]}
* @memberof TypeUtil
*/
this.numberTypes = ['int', 'tinyint', 'smallint', 'mediumint', 'bigint', 'decimal', 'float', 'double'];
/**
* Array that holds the string types
*
* @private
* @type {string[]}
* @memberof TypeUtil
*/
this.stringTypes = [
'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
*/
TypeUtil.prototype.convertSqlType = function (type) {
if (utils_1.default.indexOfIgnoreCase(this.numberTypes, type.toLowerCase()) > -1 ||
(type.toLowerCase() === 'timestamp') ||
(type.toLowerCase() === 'year')) {
return 'number';
}
else if (utils_1.default.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
*/
TypeUtil.prototype.convertNoSqlType = function (type) {
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
*/
TypeUtil.prototype.convertRamlTypes = function (type) {
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
*/
TypeUtil.prototype.getEnumValuesAsString = function (type) {
if (type.type === 'enum' && type.values) {
return type.values.map(function (value) { return "'" + 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.
*/
TypeUtil.prototype.isDefaultType = function (type) {
return ['enum', 'number', 'string'].some(function (item) { return type === item; });
};
return TypeUtil;
}());
exports.TypeUtil = TypeUtil;
exports.default = new TypeUtil();
|