113 lines
3.7 KiB
TypeScript
113 lines
3.7 KiB
TypeScript
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
|
import { DSQL_TableSchemaType } from "../../types";
|
|
import {
|
|
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
|
DSQL_DATASQUIREL_USER_DATABASES,
|
|
} from "../../types/dsql";
|
|
import numberfy from "../../utils/numberfy";
|
|
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
|
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
|
import slugToNormalText from "../../utils/slug-to-normal-text";
|
|
import debugLog from "../../utils/logging/debug-log";
|
|
import _ from "lodash";
|
|
|
|
type Param = {
|
|
tableSchema?: DSQL_TableSchemaType;
|
|
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
|
update?: boolean;
|
|
isMain?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # Handle Table Record Update and Insert
|
|
*/
|
|
export default async function ({
|
|
tableSchema,
|
|
recordedDbEntry,
|
|
update,
|
|
isMain,
|
|
}: Param): Promise<number | undefined> {
|
|
if (isMain) return undefined;
|
|
|
|
let tableId: number | undefined;
|
|
|
|
const targetDatabase = "datasquirel";
|
|
const targetTableName = "user_database_tables";
|
|
|
|
if (!tableSchema?.tableName) {
|
|
return undefined;
|
|
}
|
|
|
|
const newTableSchema = _.cloneDeep(tableSchema);
|
|
|
|
try {
|
|
if (!recordedDbEntry) {
|
|
throw new Error("Recorded Db entry not found!");
|
|
}
|
|
|
|
// const existingTableName = newTableSchema.tableNameOld
|
|
// ? newTableSchema.tableNameOld
|
|
// : newTableSchema.tableName;
|
|
|
|
const newTableEntry: DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
|
|
user_id: recordedDbEntry.user_id,
|
|
db_id: recordedDbEntry.id,
|
|
db_slug: recordedDbEntry.db_slug,
|
|
table_name: slugToNormalText(newTableSchema.tableName),
|
|
table_slug: newTableSchema.tableName,
|
|
child_table: newTableSchema.childTable ? 1 : 0,
|
|
child_table_parent_database_schema_id: newTableSchema.childTableDbId
|
|
? numberfy(newTableSchema.childTableDbId)
|
|
: 0,
|
|
child_table_parent_table_schema_id: newTableSchema.childTableId
|
|
? numberfy(newTableSchema.childTableId)
|
|
: 0,
|
|
table_schema_id: newTableSchema.id
|
|
? numberfy(newTableSchema.id)
|
|
: 0,
|
|
active_data: newTableSchema.updateData ? 1 : 0,
|
|
};
|
|
|
|
const existingTable = await varDatabaseDbHandler({
|
|
queryString: `SELECT * FROM ${targetDatabase}.${targetTableName} WHERE db_id = ? AND table_slug = ?`,
|
|
queryValuesArray: [
|
|
String(recordedDbEntry.id),
|
|
String(newTableSchema.tableName),
|
|
],
|
|
});
|
|
|
|
const table: DSQL_DATASQUIREL_USER_DATABASE_TABLES = existingTable?.[0];
|
|
|
|
if (table?.id) {
|
|
tableId = table.id;
|
|
if (update) {
|
|
await updateDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
|
data: newTableEntry,
|
|
identifierColumnName: "id",
|
|
identifierValue: table.id,
|
|
tableName: targetTableName,
|
|
dbFullName: targetDatabase,
|
|
});
|
|
}
|
|
} else {
|
|
const newTableEntryRes =
|
|
await addDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
|
data: newTableEntry,
|
|
tableName: targetTableName,
|
|
dbFullName: targetDatabase,
|
|
});
|
|
|
|
if (newTableEntryRes?.payload?.insertId) {
|
|
tableId = newTableEntryRes.payload.insertId;
|
|
}
|
|
}
|
|
|
|
if (newTableSchema.tableNameOld) {
|
|
}
|
|
|
|
return tableId;
|
|
} catch (error) {
|
|
return undefined;
|
|
}
|
|
}
|