101 lines
4.8 KiB
JavaScript
101 lines
4.8 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 = default_1;
|
|
const varDatabaseDbHandler_1 = __importDefault(require("./varDatabaseDbHandler"));
|
|
const numberfy_1 = __importDefault(require("../../utils/numberfy"));
|
|
const updateDbEntry_1 = __importDefault(require("../../functions/backend/db/updateDbEntry"));
|
|
const addDbEntry_1 = __importDefault(require("../../functions/backend/db/addDbEntry"));
|
|
const slug_to_normal_text_1 = __importDefault(require("../../utils/slug-to-normal-text"));
|
|
const lodash_1 = __importDefault(require("lodash"));
|
|
/**
|
|
* # Handle Table Record Update and Insert
|
|
*/
|
|
function default_1(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ tableSchema, recordedDbEntry, update, isMain, }) {
|
|
var _b;
|
|
if (isMain)
|
|
return undefined;
|
|
let tableId;
|
|
const targetDatabase = "datasquirel";
|
|
const targetTableName = "user_database_tables";
|
|
if (!(tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.tableName)) {
|
|
return undefined;
|
|
}
|
|
const newTableSchema = lodash_1.default.cloneDeep(tableSchema);
|
|
try {
|
|
if (!recordedDbEntry) {
|
|
throw new Error("Recorded Db entry not found!");
|
|
}
|
|
// const existingTableName = newTableSchema.tableNameOld
|
|
// ? newTableSchema.tableNameOld
|
|
// : newTableSchema.tableName;
|
|
const newTableEntry = {
|
|
user_id: recordedDbEntry.user_id,
|
|
db_id: recordedDbEntry.id,
|
|
db_slug: recordedDbEntry.db_slug,
|
|
table_name: (0, slug_to_normal_text_1.default)(newTableSchema.tableName),
|
|
table_slug: newTableSchema.tableName,
|
|
child_table: newTableSchema.childTable ? 1 : 0,
|
|
child_table_parent_database_schema_id: newTableSchema.childTableDbId
|
|
? (0, numberfy_1.default)(newTableSchema.childTableDbId)
|
|
: 0,
|
|
child_table_parent_table_schema_id: newTableSchema.childTableId
|
|
? (0, numberfy_1.default)(newTableSchema.childTableId)
|
|
: 0,
|
|
table_schema_id: newTableSchema.id
|
|
? (0, numberfy_1.default)(newTableSchema.id)
|
|
: 0,
|
|
active_data: newTableSchema.updateData ? 1 : 0,
|
|
};
|
|
const existingTable = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SELECT * FROM ${targetDatabase}.${targetTableName} WHERE db_id = ? AND table_slug = ?`,
|
|
queryValuesArray: [
|
|
String(recordedDbEntry.id),
|
|
String(newTableSchema.tableName),
|
|
],
|
|
});
|
|
const table = existingTable === null || existingTable === void 0 ? void 0 : existingTable[0];
|
|
if (table === null || table === void 0 ? void 0 : table.id) {
|
|
tableId = table.id;
|
|
if (update) {
|
|
yield (0, updateDbEntry_1.default)({
|
|
data: newTableEntry,
|
|
identifierColumnName: "id",
|
|
identifierValue: table.id,
|
|
tableName: targetTableName,
|
|
dbFullName: targetDatabase,
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
const newTableEntryRes = yield (0, addDbEntry_1.default)({
|
|
data: newTableEntry,
|
|
tableName: targetTableName,
|
|
dbFullName: targetDatabase,
|
|
});
|
|
if ((_b = newTableEntryRes === null || newTableEntryRes === void 0 ? void 0 : newTableEntryRes.payload) === null || _b === void 0 ? void 0 : _b.insertId) {
|
|
tableId = newTableEntryRes.payload.insertId;
|
|
}
|
|
}
|
|
if (newTableSchema.tableNameOld) {
|
|
}
|
|
return tableId;
|
|
}
|
|
catch (error) {
|
|
return undefined;
|
|
}
|
|
});
|
|
}
|