From 37b19838d5a9a36734e8f45a63aaf4f734927dc5 Mon Sep 17 00:00:00 2001 From: Timur Moziev Date: Tue, 19 Nov 2024 21:39:30 +0000 Subject: [PATCH] add displayAllTables setting add wide table display --- src-web/style.css | 9 ++++- src/config/file.ts | 58 +++++++++++++++++++++++--------- src/services/getTableEntities.ts | 2 +- src/types/config.ts | 9 ++--- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src-web/style.css b/src-web/style.css index d78a7a7..4200f1b 100644 --- a/src-web/style.css +++ b/src-web/style.css @@ -19,13 +19,19 @@ div { border: 1px solid #ddd; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); - max-width: 800px; + max-width: 640px; +} + +div.chonk { + width: 100%; + max-width: 100%; } table { border-collapse: collapse; width: 100%; margin: 20px auto; + table-layout: fixed; } th, @@ -33,6 +39,7 @@ td { border: 1px solid #ddd; padding: 10px; text-align: center; + word-wrap: break-word; } th { diff --git a/src/config/file.ts b/src/config/file.ts index 13e2e3a..ff67a9d 100644 --- a/src/config/file.ts +++ b/src/config/file.ts @@ -1,7 +1,9 @@ import { parseCli } from "clivo"; import fs from "fs/promises"; +import knex from "../config/database.js"; import { Config } from "../types/config.js"; +import { generateTypes } from "../utils/db.js"; let config: Config; // eslint-disable-next-line sonarjs/no-unused-collection @@ -38,7 +40,7 @@ export async function getConfig(): Promise { try { config = JSON.parse(await fs.readFile(configPath, "utf8")); if (config == null) { - config = { tables: [] }; + config = { }; } if (config.tables == null) { config.tables = []; @@ -47,27 +49,53 @@ export async function getConfig(): Promise { throw new Error("can't open config file " + configPath); } - try { - tables = []; - tableColumns = {}; - for (const table of config.tables) { - tables.push(table.name); - tableColumns[table.name] = []; - if (table.visibleColumns) { - for (const column of table.visibleColumns) { - tableColumns[table.name].push(column); + tables = []; + tableColumns = {}; + + if (config.displayAllTables) { + try { + const tableNames = await knex.raw("SHOW TABLES"); + const tablesList = tableNames[0].map( + (row: any) => row["Tables_in_" + process.env.SNAPCRUD_DB_NAME], + ); + + for (const tableName of tablesList) { + console.log(tableName); + const columns = await generateTypes(knex, tableName); + if (!columns) { + continue; + } + config.tables.push({ displayAllColumns: true, name: tableName }); + tables.push(tableName); + tableColumns[tableName] = []; + for (const column of columns) { + tableColumns[tableName].push(column.Field); } } - if (table.editableColumns) { - for (const column of table.editableColumns) { - if (!tableColumns[table.name].includes(column)) { + } catch { + throw new Error("file opened, but cannot be parsed " + configPath); + } + } else { + try { + for (const table of config.tables) { + tables.push(table.name); + tableColumns[table.name] = []; + if (table.visibleColumns) { + for (const column of table.visibleColumns) { tableColumns[table.name].push(column); } } + if (table.editableColumns) { + for (const column of table.editableColumns) { + if (!tableColumns[table.name].includes(column)) { + tableColumns[table.name].push(column); + } + } + } } + } catch { + throw new Error("file opened, but cannot be parsed " + configPath); } - } catch { - throw new Error("file opened, but cannot be parsed " + configPath); } return config; diff --git a/src/services/getTableEntities.ts b/src/services/getTableEntities.ts index 27892bf..a39a84f 100644 --- a/src/services/getTableEntities.ts +++ b/src/services/getTableEntities.ts @@ -49,7 +49,7 @@ export async function getTableEntities(

Back to tables

-
+
diff --git a/src/types/config.ts b/src/types/config.ts index d278d93..27ca8fb 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -1,8 +1,9 @@ export interface Config { - tables: { - displayAllColumns: boolean; - editableColumns: string[]; + displayAllTables?: boolean, + tables?: { + displayAllColumns?: boolean; + editableColumns?: string[]; name: string; - visibleColumns: string[]; + visibleColumns?: string[]; }[]; }