Skip to content

Commit

Permalink
add displayAllTables setting
Browse files Browse the repository at this point in the history
add wide table display
  • Loading branch information
TimurRin committed Nov 19, 2024
1 parent 1a65e5c commit 37b1983
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src-web/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ 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,
td {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
word-wrap: break-word;
}

th {
Expand Down
58 changes: 43 additions & 15 deletions src/config/file.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,7 +40,7 @@ export async function getConfig(): Promise<Config> {
try {
config = JSON.parse(await fs.readFile(configPath, "utf8"));
if (config == null) {
config = { tables: [] };
config = { };
}
if (config.tables == null) {
config.tables = [];
Expand All @@ -47,27 +49,53 @@ export async function getConfig(): Promise<Config> {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/services/getTableEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getTableEntities(
<div>
<p><a href="/">Back to tables</a></p>
</div>
<div>
<div class="chonk">
<table>
<thead>
<tr>
Expand Down
9 changes: 5 additions & 4 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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[];
}[];
}

0 comments on commit 37b1983

Please sign in to comment.