133 lines
7.5 KiB
JavaScript
133 lines
7.5 KiB
JavaScript
"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 = createDbSchemaFromDb;
|
|
const varDatabaseDbHandler_1 = __importDefault(require("../../functions/backend/varDatabaseDbHandler"));
|
|
const grabUserSchemaData_1 = __importDefault(require("../../functions/backend/grabUserSchemaData"));
|
|
const setUserSchemaData_1 = __importDefault(require("../../functions/backend/setUserSchemaData"));
|
|
const addDbEntry_1 = __importDefault(require("../../functions/backend/db/addDbEntry"));
|
|
const slugToCamelTitle_1 = __importDefault(require("../../shell/utils/slugToCamelTitle"));
|
|
function createDbSchemaFromDb(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ userId, database, }) {
|
|
var _b, _c, _d, _e, _f, _g;
|
|
try {
|
|
if (!userId) {
|
|
console.log("No user Id provided");
|
|
return;
|
|
}
|
|
const userSchemaData = (0, grabUserSchemaData_1.default)({ userId });
|
|
if (!userSchemaData)
|
|
throw new Error("User schema data not found!");
|
|
const targetDb = userSchemaData.filter((dbObject) => dbObject.dbFullName === database.db_full_name)[0];
|
|
const existingTables = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: database.db_full_name,
|
|
queryString: `SHOW TABLES FROM ${database.db_full_name}`,
|
|
});
|
|
if (!existingTables)
|
|
throw new Error("No Existing Tables");
|
|
for (let i = 0; i < existingTables.length; i++) {
|
|
const table = existingTables[i];
|
|
const tableName = Object.values(table)[0];
|
|
const tableInsert = yield (0, addDbEntry_1.default)({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_database_tables",
|
|
data: {
|
|
user_id: userId,
|
|
db_id: database.id,
|
|
db_slug: database.db_slug,
|
|
table_name: (0, slugToCamelTitle_1.default)(tableName),
|
|
table_slug: tableName,
|
|
},
|
|
});
|
|
const tableObject = {
|
|
tableName: tableName,
|
|
tableFullName: (0, slugToCamelTitle_1.default)(tableName) || "",
|
|
fields: [],
|
|
indexes: [],
|
|
};
|
|
const tableColumns = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: database.db_full_name,
|
|
queryString: `SHOW COLUMNS FROM ${database.db_full_name}.${tableName}`,
|
|
});
|
|
if (tableColumns) {
|
|
for (let k = 0; k < tableColumns.length; k++) {
|
|
const tableColumn = tableColumns[k];
|
|
const { Field, Type, Null, Key, Default, Extra } = tableColumn;
|
|
const fieldObject = {
|
|
fieldName: Field,
|
|
dataType: Type.toUpperCase(),
|
|
};
|
|
if (Null === null || Null === void 0 ? void 0 : Null.match(/^no$/i))
|
|
fieldObject.notNullValue = true;
|
|
if (Key === null || Key === void 0 ? void 0 : Key.match(/^pri$/i))
|
|
fieldObject.primaryKey = true;
|
|
if ((_b = Default === null || Default === void 0 ? void 0 : Default.toString()) === null || _b === void 0 ? void 0 : _b.match(/./))
|
|
fieldObject.defaultValue = Default;
|
|
if ((_c = Default === null || Default === void 0 ? void 0 : Default.toString()) === null || _c === void 0 ? void 0 : _c.match(/timestamp/i)) {
|
|
delete fieldObject.defaultValue;
|
|
fieldObject.defaultValueLiteral = Default;
|
|
}
|
|
if ((_d = Extra === null || Extra === void 0 ? void 0 : Extra.toString()) === null || _d === void 0 ? void 0 : _d.match(/auto_increment/i))
|
|
fieldObject.autoIncrement = true;
|
|
tableObject.fields.push(fieldObject);
|
|
}
|
|
}
|
|
const tableIndexes = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: database.db_full_name,
|
|
queryString: `SHOW INDEXES FROM ${database.db_full_name}.${tableName}`,
|
|
});
|
|
if (tableIndexes) {
|
|
for (let m = 0; m < tableIndexes.length; m++) {
|
|
const indexObject = tableIndexes[m];
|
|
const { Table, Key_name, Column_name, Null, Index_type, Index_comment, } = indexObject;
|
|
if (!(Index_comment === null || Index_comment === void 0 ? void 0 : Index_comment.match(/^schema_index$/)))
|
|
continue;
|
|
const indexNewObject = {
|
|
indexType: (Index_type === null || Index_type === void 0 ? void 0 : Index_type.match(/fulltext/i))
|
|
? "fullText"
|
|
: "regular",
|
|
indexName: Key_name,
|
|
indexTableFields: [],
|
|
};
|
|
const targetTableFieldObject = tableColumns === null || tableColumns === void 0 ? void 0 : tableColumns.filter((col) => col.Field === Column_name)[0];
|
|
const existingIndexField = (_e = tableObject.indexes) === null || _e === void 0 ? void 0 : _e.filter((indx) => indx.indexName == Key_name);
|
|
if (existingIndexField && existingIndexField[0]) {
|
|
(_f = existingIndexField[0].indexTableFields) === null || _f === void 0 ? void 0 : _f.push({
|
|
value: Column_name,
|
|
dataType: targetTableFieldObject.Type.toUpperCase(),
|
|
});
|
|
}
|
|
else {
|
|
indexNewObject.indexTableFields = [
|
|
{
|
|
value: Column_name,
|
|
dataType: targetTableFieldObject.Type.toUpperCase(),
|
|
},
|
|
];
|
|
(_g = tableObject.indexes) === null || _g === void 0 ? void 0 : _g.push(indexNewObject);
|
|
}
|
|
}
|
|
}
|
|
targetDb.tables.push(tableObject);
|
|
}
|
|
(0, setUserSchemaData_1.default)({ schemaData: userSchemaData, userId });
|
|
return true;
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
return false;
|
|
}
|
|
});
|
|
}
|