datasquirel/package-shared/shell/utils/generateColumnDescription.ts

84 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
type Param = {
columnData: import("../../types").DSQL_FieldSchemaType;
primaryKeySet?: boolean;
};
type Return = {
fieldEntryText: string;
newPrimaryKeySet: boolean;
};
/**
* # Generate Table Column Description
*/
export default function generateColumnDescription({
columnData,
primaryKeySet,
}: Param): Return {
/**
* Format tableInfoArray
*
* @description Format tableInfoArray
*/
const {
fieldName,
dataType,
nullValue,
primaryKey,
autoIncrement,
defaultValue,
defaultValueLiteral,
onUpdateLiteral,
notNullValue,
} = columnData;
let fieldEntryText = "";
fieldEntryText += `\`${fieldName}\` ${dataType}`;
////////////////////////////////////////
if (nullValue) {
fieldEntryText += " DEFAULT NULL";
} else if (defaultValueLiteral) {
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
} else if (defaultValue) {
if (String(defaultValue).match(/uuid\(\)/i)) {
fieldEntryText += ` DEFAULT UUID()`;
} else {
fieldEntryText += ` DEFAULT '${defaultValue}'`;
}
} else if (notNullValue) {
fieldEntryText += ` NOT NULL`;
}
////////////////////////////////////////
if (onUpdateLiteral) {
fieldEntryText += ` ON UPDATE ${onUpdateLiteral}`;
}
////////////////////////////////////////
if (primaryKey && !primaryKeySet) {
fieldEntryText += " PRIMARY KEY";
primaryKeySet = true;
}
////////////////////////////////////////
if (autoIncrement) {
fieldEntryText += " AUTO_INCREMENT";
primaryKeySet = true;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return {
fieldEntryText,
newPrimaryKeySet: primaryKeySet || false,
};
}