"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
/**
* Class which holds helper tools when working with schema.
*
* @export
* @class SchemaUtil
*/
var SchemaUtil = (function () {
function SchemaUtil() {
}
/**
* Get normalized schema by type
* @param {Schema} schema - collection of tables
* @param {string} type - data type normalized (E.g: User, Account)
* @return {Table} table from schema that matches type.
*/
SchemaUtil.prototype.getNormalizedTableByType = function (schema, type) {
for (var _i = 0, schema_1 = schema; _i < schema_1.length; _i++) {
var table = schema_1[_i];
/* istanbul ignore else */
if (utils_1.default.toTitleCase(table.name) === type) {
return table;
}
}
return undefined;
};
/**
* Convert values from COLUMN_TYPE column
* @param {Column} column - schema.
* @return {Column} converted column
*/
SchemaUtil.prototype.convertValues = function (column) {
var columnType = column.dataType.rawValues || '';
switch (column.dataType.type) {
case 'enum':
column.dataType.values = columnType ?
(columnType.substring(4).replace(/["'()]/g, '').replace(' ', '').split(',')) : [];
break;
default:
delete column.dataType.rawValues;
}
return column;
};
/**
* Convert array of values to raml values
* @param {string[]} values - ['Yes', 'No']
* @return {string} - 'Yes | No'
*/
SchemaUtil.prototype.valuesToRamlDataType = function (values) {
return (values && values.length > 0) ? values.join(' | ') : '';
};
/**
* Checks if a circular reference exists between the source and target table.
*
* @param {Table} source - source table
* @param {Column} column - column which references another table
* @param {Schema} schema - whole schema
* @return {boolean} true if a circular reference exists in the target table
*/
SchemaUtil.prototype.isCircularRelation = function (source, column, schema) {
var target = schema.find(function (table) { return table.name === utils_1.default.toTableName(column.name) || table.name === utils_1.default.toTableName(column.dataType.type); });
if (target) {
return target.columns.some(function (col) {
return utils_1.default.toTableName(col.name) === source.name || utils_1.default.toTableName(col.dataType.type) === source.name;
});
}
return false;
};
/**
* Checks if a circular reference is required.
*
* @param {Table} source - source table
* @param {Column} column - column which references another table
* @param {Schema} schema - whole schema
* @return {boolean} true if a circular reference is required
*/
SchemaUtil.prototype.circularRelationIsRequired = function (source, column, schema) {
var target = schema.find(function (table) { return table.name === utils_1.default.toTableName(column.name) || table.name === utils_1.default.toTableName(column.dataType.type); });
if (target) {
return target.columns.some(function (col) {
return (utils_1.default.toTableName(col.name) === source.name || utils_1.default.toTableName(col.dataType.type) === source.name) &&
!col.allowNull;
});
}
return false;
};
/**
* Checks if a circular reference is array.
*
* @param {Table} source - source table
* @param {Column} column - column which references another table
* @param {Schema} schema - whole schema
* @return {boolean} true if a circular reference is array
*/
SchemaUtil.prototype.circularRelationIsArray = function (source, column, schema) {
var target = schema.find(function (table) { return table.name === utils_1.default.toTableName(column.name) || table.name === utils_1.default.toTableName(column.dataType.type); });
if (target) {
return target.columns.some(function (col) {
return (utils_1.default.toTableName(col.name) === source.name || utils_1.default.toTableName(col.dataType.type) === source.name) &&
Boolean(col.dataType.isArray);
});
}
return false;
};
/**
* Get related table names for a given table.
*
* @param {Table} table - given table
* @returns {Column[]} - related tables
*/
SchemaUtil.prototype.getRelatedTablesForTable = function (table) {
return table.columns.filter(function (column) { return column.dataType.relationType; }).map(function (column) { return column; });
};
/**
* Checks if a given column is an alias.
*
* @param {Column} column
* @returns {boolean} returns true if column name and referenced type are aliases.
*/
SchemaUtil.prototype.relationIsAlias = function (column) {
return Boolean(column.dataType.references && utils_1.default.similarity(column.dataType.references.name, column.dataType.references.table) < 0.5);
};
return SchemaUtil;
}());
exports.SchemaUtil = SchemaUtil;
exports.default = new SchemaUtil();
|