130 lines
3.8 KiB
TypeScript
130 lines
3.8 KiB
TypeScript
import generateColumnDescription from "./generateColumnDescription";
|
|
import supplementTable from "./supplementTable";
|
|
import {
|
|
DSQL_FieldSchemaType,
|
|
DSQL_IndexSchemaType,
|
|
DSQL_TableSchemaType,
|
|
DSQL_UniqueConstraintSchemaType,
|
|
} from "../../types";
|
|
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
|
import handleTableForeignKey from "./handle-table-foreign-key";
|
|
import createTableHandleTableRecord from "./create-table-handle-table-record";
|
|
import dbHandler from "../../functions/backend/dbHandler";
|
|
import handleIndexescreateDbFromSchema from "../createDbFromSchema/handle-indexes";
|
|
import handleUniqueConstraintsCreateDbFromSchema from "../createDbFromSchema/handle-unique-constraints";
|
|
|
|
type Param = {
|
|
dbFullName: string;
|
|
tableName: string;
|
|
fields: DSQL_FieldSchemaType[];
|
|
tableSchema?: DSQL_TableSchemaType;
|
|
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
|
isMain?: boolean;
|
|
indexes?: DSQL_IndexSchemaType[];
|
|
uniqueConstraints?: DSQL_UniqueConstraintSchemaType[];
|
|
};
|
|
|
|
/**
|
|
* # Create Table Functions
|
|
*/
|
|
export default async function createTable({
|
|
dbFullName,
|
|
tableName,
|
|
fields: passedFields,
|
|
tableSchema,
|
|
recordedDbEntry,
|
|
isMain,
|
|
indexes,
|
|
uniqueConstraints,
|
|
}: Param) {
|
|
const fields = supplementTable({ tableInfoArray: passedFields });
|
|
|
|
let tableId = await createTableHandleTableRecord({
|
|
recordedDbEntry,
|
|
tableSchema,
|
|
isMain,
|
|
});
|
|
|
|
if (!tableId && !isMain) throw new Error(`Couldn't grab table ID`);
|
|
|
|
const createTableQueryArray = [];
|
|
|
|
createTableQueryArray.push(
|
|
`CREATE TABLE IF NOT EXISTS \`${dbFullName}\`.\`${tableName}\` (`
|
|
);
|
|
|
|
let primaryKeySet = false;
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
const column = fields[i];
|
|
|
|
let { fieldEntryText, newPrimaryKeySet } = generateColumnDescription({
|
|
columnData: column,
|
|
primaryKeySet: primaryKeySet,
|
|
});
|
|
|
|
primaryKeySet = newPrimaryKeySet;
|
|
|
|
const comma = (() => {
|
|
if (i === fields.length - 1) return "";
|
|
return ",";
|
|
})();
|
|
|
|
createTableQueryArray.push(" " + fieldEntryText + comma);
|
|
}
|
|
|
|
createTableQueryArray.push(
|
|
`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=${
|
|
tableSchema?.collation || "utf8mb4_bin"
|
|
};`
|
|
);
|
|
|
|
const createTableQuery = createTableQueryArray.join("\n");
|
|
|
|
const newTable = await dbHandler({
|
|
query: createTableQuery,
|
|
});
|
|
|
|
/**
|
|
* Handle MYSQL Foreign Keys
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
await handleTableForeignKey({
|
|
dbFullName,
|
|
fields,
|
|
tableName,
|
|
});
|
|
|
|
/**
|
|
* Handle DATASQUIREL Table Indexes
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
if (indexes?.[0]) {
|
|
handleIndexescreateDbFromSchema({
|
|
dbFullName,
|
|
indexes,
|
|
tableName,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handle DATASQUIREL Table Unique Indexes
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table unique constraint(if available), and perform operations
|
|
*/
|
|
if (uniqueConstraints?.[0]) {
|
|
handleUniqueConstraintsCreateDbFromSchema({
|
|
dbFullName,
|
|
tableUniqueConstraints: uniqueConstraints,
|
|
tableName,
|
|
});
|
|
}
|
|
|
|
return tableId;
|
|
}
|