61 lines
3.2 KiB
JavaScript
61 lines
3.2 KiB
JavaScript
import chalk from "chalk";
|
|
import { select } from "@inquirer/prompts";
|
|
import dbHandler from "../../lib/db-handler";
|
|
export default async function showFields({ tableName, }) {
|
|
const columns = (await dbHandler({
|
|
query: `SELECT ORDINAL_POSITION, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_KEY, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION`,
|
|
values: [tableName],
|
|
})).payload;
|
|
if (!columns?.length) {
|
|
console.log(chalk.yellow(`\nNo columns found for "${tableName}".\n`));
|
|
return;
|
|
}
|
|
const indexes = (await dbHandler({
|
|
query: `SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE, INDEX_TYPE FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? ORDER BY INDEX_NAME, SEQ_IN_INDEX`,
|
|
values: [tableName],
|
|
})).payload;
|
|
const foreignKeys = (await dbHandler({
|
|
query: `SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND REFERENCED_TABLE_NAME IS NOT NULL`,
|
|
values: [tableName],
|
|
})).payload;
|
|
while (true) {
|
|
const fieldName = await select({
|
|
message: `"${tableName}" — select a field:`,
|
|
choices: [
|
|
...columns.map((c) => ({
|
|
name: c.COLUMN_NAME,
|
|
value: c.COLUMN_NAME,
|
|
})),
|
|
{ name: chalk.dim("← Go Back"), value: "__back__" },
|
|
{ name: chalk.dim("✕ Exit"), value: "__exit__" },
|
|
],
|
|
});
|
|
if (fieldName === "__back__")
|
|
break;
|
|
if (fieldName === "__exit__")
|
|
return "__exit__";
|
|
const col = columns.find((c) => c.COLUMN_NAME === fieldName);
|
|
const colIndexes = indexes?.filter((i) => i.COLUMN_NAME === fieldName) || [];
|
|
const fk = foreignKeys?.find((f) => f.COLUMN_NAME === fieldName);
|
|
console.log(`\n${chalk.bold(`Field: "${fieldName}"`)}\n`);
|
|
console.log(` ${chalk.dim("Table")} ${tableName}`);
|
|
console.log(` ${chalk.dim("Column #")} ${col.ORDINAL_POSITION}`);
|
|
console.log(` ${chalk.dim("Type")} ${col.COLUMN_TYPE}`);
|
|
console.log(` ${chalk.dim("Primary Key")} ${col.COLUMN_KEY === "PRI" ? chalk.green("YES") : "NO"}`);
|
|
console.log(` ${chalk.dim("Not Null")} ${col.IS_NULLABLE === "NO" ? chalk.yellow("YES") : "NO"}`);
|
|
console.log(` ${chalk.dim("Default")} ${col.COLUMN_DEFAULT ?? chalk.italic("(none)")}`);
|
|
console.log(` ${chalk.dim("Extra")} ${col.EXTRA || chalk.italic("(none)")}`);
|
|
console.log(` ${chalk.dim("Indexed")} ${colIndexes.length ? chalk.cyan(colIndexes.map((i) => i.INDEX_NAME).join(", ")) : "NO"}`);
|
|
if (fk) {
|
|
console.log(` ${chalk.dim("Foreign Key")} ${chalk.magenta(`${fk.REFERENCED_TABLE_NAME}(${fk.REFERENCED_COLUMN_NAME})`)}`);
|
|
}
|
|
else {
|
|
console.log(` ${chalk.dim("Foreign Key")} NO`);
|
|
}
|
|
if (col.COLUMN_COMMENT) {
|
|
console.log(` ${chalk.dim("Comment")} ${col.COLUMN_COMMENT}`);
|
|
}
|
|
console.log();
|
|
}
|
|
}
|