all files / commons/utils/ schemaUtil.js

100% Statements 49/49
100% Branches 34/34
100% Functions 18/18
100% Lines 44/44
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                                              22× 22×       20×   22×                               109× 33× 16× 72×     17×                   40× 15× 14× 69×                         37× 15× 14× 67×                     15×               11×      
"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();