"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = handleDSQLSchemaFields; const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler")); const default_fields_regexp_1 = __importDefault(require("../../functions/dsql/default-fields-regexp")); const generateColumnDescription_1 = __importDefault(require("./generateColumnDescription")); /** * Handle DATASQUIREL schema fields for current table * =================================================== * @description Iterate through each field object and * perform operations */ function handleDSQLSchemaFields(_a) { return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, fields, allExistingColumns, }) { let sql = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``; for (let i = 0; i < fields.length; i++) { const column = fields[i]; // const prevColumn = fields[i - 1]; // const nextColumn = fields[i + 1]; const { fieldName, dataType, foreignKey } = column; if (!fieldName) continue; if (default_fields_regexp_1.default.test(fieldName)) continue; let updateText = ""; const existingColumnIndex = allExistingColumns === null || allExistingColumns === void 0 ? void 0 : allExistingColumns.findIndex((_column, _index) => _column.Field === fieldName); const existingColumn = existingColumnIndex >= 0 ? allExistingColumns[existingColumnIndex] : undefined; let { fieldEntryText } = (0, generateColumnDescription_1.default)({ columnData: column, }); /** * @description Modify Column(Field) if it already exists * in MYSQL database */ if (existingColumn === null || existingColumn === void 0 ? void 0 : existingColumn.Field) { const { Field, Type } = existingColumn; updateText += ` MODIFY COLUMN ${fieldEntryText}`; } else { /** * @description Append new column to the end of existing columns */ updateText += ` ADD COLUMN ${fieldEntryText}`; } /** * @description Pust SQL code snippet to updateTableQueryArray Array * Add a comma(,) to separate from the next snippet */ if (updateText.match(/./)) { sql += " " + updateText + ","; } } const finalSQL = sql.replace(/\,$/, ""); const updateTable = yield (0, dbHandler_1.default)({ query: finalSQL, }); }); }