all files / preprocesors/ RamlSchemaPreprocessor.js

100% Statements 49/49
100% Branches 42/42
100% Functions 13/13
100% Lines 49/49
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 130 131 132 133 134 135 136 137 138 139                            22×                                               18×             17×             18×                   18×             16×             18×                   18×             18×          
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var typeUtil_1 = require("../commons/utils/typeUtil");
var schemaUtil_1 = require("../commons/utils/schemaUtil");
/**
 * Raml schema pre processor.
 *
 * @export
 * @class RamlSchemaPreprocessor
 */
var RamlSchemaPreprocessor = (function () {
    function RamlSchemaPreprocessor() {
    }
    /**
     * Normalize the table schema.
     *
     * @param {RamlColumnSchema} columnSchema column schema to be normalized
     * @returns {Column} normalized column schema
     */
    RamlSchemaPreprocessor.prototype.convertToStandardSchema = function (columnSchema) {
        return {
            name: columnSchema.name,
            primary: columnSchema.name === 'id',
            unique: columnSchema.name === 'id',
            allowNull: !columnSchema.required,
            dataType: typeUtil_1.default.convertRamlTypes({
                type: columnSchema.type.pop() || '',
                items: columnSchema.items,
            }),
        };
    };
    /**
     * Normalize schema relations.
     *
     * @param {Schema} schema - schema which needs to be normalized
     * @returns {Schema} normalized schema
     */
    RamlSchemaPreprocessor.prototype.normalizeSchemaRelations = function (schema) {
        var normalizedSchema = schema;
        normalizedSchema = this.normalizeOneToOneRelation(normalizedSchema);
        normalizedSchema = this.normalizeOneToManyRelation(normalizedSchema);
        normalizedSchema = this.normalizeManyToManyRelation(normalizedSchema);
        return normalizedSchema;
    };
    /**
     * Adds the relation type to schema.
     *
     * @param {Schema} schema - schema which needs normalization
     * @return {Schema} normalized schema
     */
    RamlSchemaPreprocessor.prototype.normalizeOneToOneRelation = function (schema) {
        var updatedSchema = schema;
        updatedSchema.map(function (table) {
            table.columns.map(function (column) {
                if (!column.allowNull &&
                    !column.dataType.isArray &&
                    schemaUtil_1.default.isCircularRelation(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsRequired(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsArray(table, column, schema) &&
                    !typeUtil_1.default.isDefaultType(column.dataType.type)) {
                    column.dataType.relationType = '1-1';
                }
                else if (column.allowNull &&
                    !column.dataType.isArray &&
                    schemaUtil_1.default.isCircularRelation(table, column, schema) &&
                    schemaUtil_1.default.circularRelationIsRequired(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsArray(table, column, schema) &&
                    !typeUtil_1.default.isDefaultType(column.dataType.type)) {
                    column.dataType.relationType = '1-1';
                    column.dataType.isRelationHolder = true;
                }
                return column;
            });
            return table;
        });
        return updatedSchema;
    };
    /**
     * Adds the relation type to schema.
     *
     * @param {Schema} schema - schema which needs normalization
     * @return {Schema} normalized schema
     */
    RamlSchemaPreprocessor.prototype.normalizeOneToManyRelation = function (schema) {
        var updatedSchema = schema;
        updatedSchema.map(function (table) {
            table.columns.map(function (column) {
                if (column.allowNull &&
                    column.dataType.isArray &&
                    schemaUtil_1.default.isCircularRelation(table, column, schema) &&
                    schemaUtil_1.default.circularRelationIsRequired(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsArray(table, column, schema) &&
                    !typeUtil_1.default.isDefaultType(column.dataType.type)) {
                    column.dataType.relationType = '1-n';
                    column.dataType.isRelationHolder = true;
                }
                else if (!column.allowNull &&
                    !column.dataType.isArray &&
                    schemaUtil_1.default.isCircularRelation(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsRequired(table, column, schema) &&
                    schemaUtil_1.default.circularRelationIsArray(table, column, schema) &&
                    !typeUtil_1.default.isDefaultType(column.dataType.type)) {
                    column.dataType.relationType = '1-n';
                }
                return column;
            });
            return table;
        });
        return updatedSchema;
    };
    /**
     * Adds the relation type to schema.
     *
     * @param {Schema} schema - schema which needs normalization
     * @return {Schema} normalized schema
     */
    RamlSchemaPreprocessor.prototype.normalizeManyToManyRelation = function (schema) {
        var updatedSchema = schema;
        updatedSchema.map(function (table) {
            table.columns.map(function (column) {
                if (column.allowNull &&
                    column.dataType.isArray &&
                    schemaUtil_1.default.isCircularRelation(table, column, schema) &&
                    schemaUtil_1.default.circularRelationIsArray(table, column, schema) &&
                    !schemaUtil_1.default.circularRelationIsRequired(table, column, schema) &&
                    !typeUtil_1.default.isDefaultType(column.dataType.type)) {
                    column.dataType.relationType = 'n-n';
                    column.dataType.isRelationHolder = true;
                }
                return column;
            });
            return table;
        });
        return updatedSchema;
    };
    return RamlSchemaPreprocessor;
}());
exports.default = RamlSchemaPreprocessor;