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

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Generate SQL text for Field
* ==============================================================================
* @param {object} params - Single object params
2024-12-06 10:31:24 +00:00
* @param {import("../../types").DSQL_FieldSchemaType} params.columnData - Field object
2023-09-21 14:00:04 +00:00
* @param {boolean} [params.primaryKeySet] - Table Name(slug)
*
2024-12-06 10:31:24 +00:00
* @returns {{ fieldEntryText: string, newPrimaryKeySet: boolean }}
2023-09-21 14:00:04 +00:00
*/
2024-10-14 06:49:01 +00:00
module.exports = function generateColumnDescription({
columnData,
primaryKeySet,
}) {
2023-09-21 14:00:04 +00:00
/**
* Format tableInfoArray
*
* @description Format tableInfoArray
*/
2024-10-14 06:49:01 +00:00
const {
fieldName,
dataType,
nullValue,
primaryKey,
autoIncrement,
defaultValue,
defaultValueLiteral,
2024-12-06 10:31:24 +00:00
foreignKey,
updatedField,
onUpdate,
onUpdateLiteral,
onDelete,
onDeleteLiteral,
defaultField,
encrypted,
json,
newTempField,
2024-10-14 06:49:01 +00:00
notNullValue,
2024-12-06 10:31:24 +00:00
originName,
plainText,
pattern,
patternFlags,
richText,
2024-10-14 06:49:01 +00:00
} = columnData;
2023-09-21 14:00:04 +00:00
let fieldEntryText = "";
fieldEntryText += `\`${fieldName}\` ${dataType}`;
////////////////////////////////////////
2024-12-06 10:31:24 +00:00
// if (String(fieldEntryText).match(/ UUID$/)) {
// fieldEntryText += ` DEFAULT UUID()`;
// } else
2023-09-21 14:00:04 +00:00
if (nullValue) {
fieldEntryText += " DEFAULT NULL";
} else if (defaultValueLiteral) {
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
} else if (defaultValue) {
2024-12-06 10:31:24 +00:00
if (String(defaultValue).match(/uuid\(\)/i)) {
fieldEntryText += ` DEFAULT UUID()`;
} else {
fieldEntryText += ` DEFAULT '${defaultValue}'`;
}
2023-09-21 14:00:04 +00:00
} else if (notNullValue) {
fieldEntryText += ` NOT NULL`;
}
////////////////////////////////////////
2024-12-06 10:31:24 +00:00
if (onUpdateLiteral) {
fieldEntryText += ` ON UPDATE ${onUpdateLiteral}`;
}
////////////////////////////////////////
2023-09-21 14:00:04 +00:00
if (primaryKey && !primaryKeySet) {
fieldEntryText += " PRIMARY KEY";
primaryKeySet = true;
}
////////////////////////////////////////
if (autoIncrement) {
fieldEntryText += " AUTO_INCREMENT";
primaryKeySet = true;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return { fieldEntryText, newPrimaryKeySet: primaryKeySet || false };
};
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */