This commit is contained in:
Benjamin Toby 2025-12-03 11:45:32 +01:00
parent 4a6da06580
commit 9ab5543c5c
14 changed files with 70 additions and 10 deletions

View File

@ -287,7 +287,6 @@ function sqlGenerator({ tableName, genObject, dbFullName, count }) {
orderFields.push(genObject.order.field); orderFields.push(genObject.order.field);
} }
orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`; orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`;
console.log("orderSrt", orderSrt);
queryString += ` ${orderSrt}`; queryString += ` ${orderSrt}`;
} }
if ((genObject === null || genObject === void 0 ? void 0 : genObject.limit) && !count) if ((genObject === null || genObject === void 0 ? void 0 : genObject.limit) && !count)

View File

@ -95,7 +95,7 @@ function createDbFromSchema(_a) {
}); });
if (!((_b = dbCheck === null || dbCheck === void 0 ? void 0 : dbCheck[0]) === null || _b === void 0 ? void 0 : _b.dbFullName)) { if (!((_b = dbCheck === null || dbCheck === void 0 ? void 0 : dbCheck[0]) === null || _b === void 0 ? void 0 : _b.dbFullName)) {
const newDatabase = yield (0, dbHandler_1.default)({ const newDatabase = yield (0, dbHandler_1.default)({
query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`, query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE ${database.collation || "utf8mb4_bin"}`,
}); });
} }
const allTables = (yield (0, dbHandler_1.default)({ const allTables = (yield (0, dbHandler_1.default)({

View File

@ -48,7 +48,7 @@ function createTable(_a) {
})(); })();
createTableQueryArray.push(" " + fieldEntryText + comma); createTableQueryArray.push(" " + fieldEntryText + comma);
} }
createTableQueryArray.push(`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`); createTableQueryArray.push(`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=${(tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.collation) || "utf8mb4_bin"};`);
const createTableQuery = createTableQueryArray.join("\n"); const createTableQuery = createTableQueryArray.join("\n");
const newTable = yield (0, dbHandler_1.default)({ const newTable = yield (0, dbHandler_1.default)({
query: createTableQuery, query: createTableQuery,

View File

@ -63,6 +63,26 @@ function updateTable(_a) {
if (!tableID && !isMain) { if (!tableID && !isMain) {
throw new Error("Recorded Table entry not found!"); throw new Error("Recorded Table entry not found!");
} }
/**
* Handle Table Default Collation
*
* @description Update Column Collation
*/
if (tableSchema.collation) {
try {
const existingCollation = (yield (0, dbHandler_1.default)({
query: `SHOW TABLE STATUS LIKE '${tableName}'`,
config: { database: dbFullName },
}));
const existingCollationStr = existingCollation === null || existingCollation === void 0 ? void 0 : existingCollation[0].Collation;
if (existingCollationStr !== tableSchema.collation) {
yield (0, dbHandler_1.default)({
query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE ${tableSchema.collation}`,
});
}
}
catch (error) { }
}
/** /**
* Handle Table updates * Handle Table updates
* *

View File

@ -101,6 +101,7 @@ export type DSQL_DATASQUIREL_USER_DATABASES = {
active_clone_parent_db?: string; active_clone_parent_db?: string;
active_clone_parent_db_id?: number; active_clone_parent_db_id?: number;
active_data?: number; active_data?: number;
collation?: string;
last_checked_date_code?: number; last_checked_date_code?: number;
date_created?: string; date_created?: string;
date_created_code?: number; date_created_code?: number;
@ -124,6 +125,7 @@ export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
child_table_parent_database_schema_id?: number; child_table_parent_database_schema_id?: number;
child_table_parent_table_schema_id?: number; child_table_parent_table_schema_id?: number;
active_data?: 0 | 1; active_data?: 0 | 1;
collation?: string;
last_checked_date_code?: number; last_checked_date_code?: number;
date_created?: string; date_created?: string;
date_created_code?: number; date_created_code?: number;

View File

@ -39,10 +39,12 @@ export interface DSQL_DatabaseSchemaType {
childDatabase?: boolean; childDatabase?: boolean;
childDatabaseDbId?: string | number; childDatabaseDbId?: string | number;
updateData?: boolean; updateData?: boolean;
collation?: (typeof MariaDBCollations)[number];
} }
export interface DSQL_ChildrenDatabaseObject { export interface DSQL_ChildrenDatabaseObject {
dbId?: string | number; dbId?: string | number;
} }
export declare const MariaDBCollations: readonly ["utf8mb4_bin", "utf8mb4_unicode_520_ci"];
export interface DSQL_TableSchemaType { export interface DSQL_TableSchemaType {
id?: string | number; id?: string | number;
tableName: string; tableName: string;
@ -55,6 +57,7 @@ export interface DSQL_TableSchemaType {
childTableId?: string | number; childTableId?: string | number;
tableNameOld?: string; tableNameOld?: string;
childTableDbId?: string | number; childTableDbId?: string | number;
collation?: (typeof MariaDBCollations)[number];
} }
export interface DSQL_ChildrenTablesType { export interface DSQL_ChildrenTablesType {
tableId?: string | number; tableId?: string | number;

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.AIOptions = exports.OpsActions = exports.TimeParadigms = exports.UserAPIAuthActions = exports.UserAPIParadigms = exports.TargetMediaParadigms = exports.EnvKeys = exports.AppVersions = exports.APIParadigms = exports.UserSQLPermissions = exports.SQLPermissions = exports.InvitedUserSelectFields = exports.DelegatedUserSelectFields = exports.UserSelectFields = exports.IndexTypes = exports.DefaultSQLValuesLiteral = exports.CurrentlyEditedFieldActions = exports.FileMimeTypes = exports.VideoMimeTypes = exports.ImageMimeTypes = exports.MediaTypes = exports.DockerComposeServices = exports.DatasquirelWindowEvents = exports.WebSocketEvents = exports.QueueJobTypes = exports.SignUpParadigms = exports.UserTypes = exports.QueryFields = exports.DsqlCrudActions = exports.DataCrudRequestMethodsLowerCase = exports.DataCrudRequestMethods = exports.ServerQueryEqualities = exports.ServerQueryOperators = exports.TextFieldTypesArray = exports.UsersOmitedFields = void 0; exports.AIOptions = exports.OpsActions = exports.TimeParadigms = exports.UserAPIAuthActions = exports.UserAPIParadigms = exports.TargetMediaParadigms = exports.EnvKeys = exports.AppVersions = exports.APIParadigms = exports.UserSQLPermissions = exports.SQLPermissions = exports.InvitedUserSelectFields = exports.DelegatedUserSelectFields = exports.UserSelectFields = exports.IndexTypes = exports.DefaultSQLValuesLiteral = exports.CurrentlyEditedFieldActions = exports.FileMimeTypes = exports.VideoMimeTypes = exports.ImageMimeTypes = exports.MediaTypes = exports.DockerComposeServices = exports.DatasquirelWindowEvents = exports.WebSocketEvents = exports.QueueJobTypes = exports.SignUpParadigms = exports.UserTypes = exports.QueryFields = exports.DsqlCrudActions = exports.DataCrudRequestMethodsLowerCase = exports.DataCrudRequestMethods = exports.ServerQueryEqualities = exports.ServerQueryOperators = exports.TextFieldTypesArray = exports.MariaDBCollations = exports.UsersOmitedFields = void 0;
exports.UsersOmitedFields = [ exports.UsersOmitedFields = [
"password", "password",
"social_id", "social_id",
@ -12,6 +12,10 @@ exports.UsersOmitedFields = [
"date_updated_code", "date_updated_code",
"date_updated_timestamp", "date_updated_timestamp",
]; ];
exports.MariaDBCollations = [
"utf8mb4_bin",
"utf8mb4_unicode_520_ci",
];
exports.TextFieldTypesArray = [ exports.TextFieldTypesArray = [
{ title: "Plain Text", value: "plain" }, { title: "Plain Text", value: "plain" },
{ title: "Rich Text", value: "richText" }, { title: "Rich Text", value: "richText" },

View File

@ -386,8 +386,6 @@ export default function sqlGenerator<
orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`; orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`;
console.log("orderSrt", orderSrt);
queryString += ` ${orderSrt}`; queryString += ` ${orderSrt}`;
} }

View File

@ -1,4 +1,3 @@
import fs from "fs";
import createTable from "../utils/createTable"; import createTable from "../utils/createTable";
import updateTable from "../utils/updateTable"; import updateTable from "../utils/updateTable";
import { DSQL_DatabaseSchemaType } from "../../types"; import { DSQL_DatabaseSchemaType } from "../../types";
@ -74,7 +73,9 @@ export default async function createDbFromSchema({
if (!dbCheck?.[0]?.dbFullName) { if (!dbCheck?.[0]?.dbFullName) {
const newDatabase = await dbHandler({ const newDatabase = await dbHandler({
query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`, query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE ${
database.collation || "utf8mb4_bin"
}`,
}); });
} }

View File

@ -63,7 +63,9 @@ export default async function createTable({
} }
createTableQueryArray.push( createTableQueryArray.push(
`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;` `) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=${
tableSchema?.collation || "utf8mb4_bin"
};`
); );
const createTableQuery = createTableQueryArray.join("\n"); const createTableQuery = createTableQueryArray.join("\n");

View File

@ -92,6 +92,28 @@ export default async function updateTable({
throw new Error("Recorded Table entry not found!"); throw new Error("Recorded Table entry not found!");
} }
/**
* Handle Table Default Collation
*
* @description Update Column Collation
*/
if (tableSchema.collation) {
try {
const existingCollation = (await dbHandler({
query: `SHOW TABLE STATUS LIKE '${tableName}'`,
config: { database: dbFullName },
})) as any[];
const existingCollationStr = existingCollation?.[0].Collation;
if (existingCollationStr !== tableSchema.collation) {
await dbHandler({
query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE ${tableSchema.collation}`,
});
}
} catch (error) {}
}
/** /**
* Handle Table updates * Handle Table updates
* *

View File

@ -128,6 +128,7 @@ export type DSQL_DATASQUIREL_USER_DATABASES = {
active_clone_parent_db?: string; active_clone_parent_db?: string;
active_clone_parent_db_id?: number; active_clone_parent_db_id?: number;
active_data?: number; active_data?: number;
collation?: string;
last_checked_date_code?: number; last_checked_date_code?: number;
date_created?: string; date_created?: string;
date_created_code?: number; date_created_code?: number;
@ -152,6 +153,7 @@ export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
child_table_parent_database_schema_id?: number; child_table_parent_database_schema_id?: number;
child_table_parent_table_schema_id?: number; child_table_parent_table_schema_id?: number;
active_data?: 0 | 1; active_data?: 0 | 1;
collation?: string;
last_checked_date_code?: number; last_checked_date_code?: number;
date_created?: string; date_created?: string;
date_created_code?: number; date_created_code?: number;

View File

@ -80,12 +80,18 @@ export interface DSQL_DatabaseSchemaType {
childDatabase?: boolean; childDatabase?: boolean;
childDatabaseDbId?: string | number; childDatabaseDbId?: string | number;
updateData?: boolean; updateData?: boolean;
collation?: (typeof MariaDBCollations)[number];
} }
export interface DSQL_ChildrenDatabaseObject { export interface DSQL_ChildrenDatabaseObject {
dbId?: string | number; dbId?: string | number;
} }
export const MariaDBCollations = [
"utf8mb4_bin",
"utf8mb4_unicode_520_ci",
] as const;
export interface DSQL_TableSchemaType { export interface DSQL_TableSchemaType {
id?: string | number; id?: string | number;
tableName: string; tableName: string;
@ -98,6 +104,7 @@ export interface DSQL_TableSchemaType {
childTableId?: string | number; childTableId?: string | number;
tableNameOld?: string; tableNameOld?: string;
childTableDbId?: string | number; childTableDbId?: string | number;
collation?: (typeof MariaDBCollations)[number];
} }
export interface DSQL_ChildrenTablesType { export interface DSQL_ChildrenTablesType {

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "5.4.9", "version": "5.5.0",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {