92 lines
3.6 KiB
JavaScript
92 lines
3.6 KiB
JavaScript
import chalk from "chalk";
|
|
import { select, input } from "@inquirer/prompts";
|
|
import dbHandler from "../../lib/db-handler";
|
|
import MariaDBQuoteGen from "../../lib/schema/mariadb-quote-gen";
|
|
const LIMIT = 50;
|
|
export default async function showEntries({ tableName }) {
|
|
let page = 0;
|
|
let searchField = null;
|
|
let searchTerm = null;
|
|
const quotedTable = MariaDBQuoteGen(tableName);
|
|
while (true) {
|
|
const offset = page * LIMIT;
|
|
const rows = searchTerm
|
|
? (await dbHandler({
|
|
query: `SELECT * FROM ${quotedTable} WHERE ${MariaDBQuoteGen(searchField)} LIKE ? LIMIT ${LIMIT} OFFSET ${offset}`,
|
|
values: [`%${searchTerm}%`],
|
|
})).payload
|
|
: (await dbHandler({
|
|
query: `SELECT * FROM ${quotedTable} LIMIT ${LIMIT} OFFSET ${offset}`,
|
|
})).payload;
|
|
const countRow = searchTerm
|
|
? (await dbHandler({
|
|
query: `SELECT COUNT(*) as count FROM ${quotedTable} WHERE ${MariaDBQuoteGen(searchField)} LIKE ?`,
|
|
values: [`%${searchTerm}%`],
|
|
})).payload
|
|
: (await dbHandler({
|
|
query: `SELECT COUNT(*) as count FROM ${quotedTable}`,
|
|
})).payload;
|
|
const total = Number(countRow?.[0]?.count || 0);
|
|
const searchInfo = searchTerm
|
|
? chalk.dim(` · searching "${searchField}" = "${searchTerm}"`)
|
|
: "";
|
|
if (!rows) {
|
|
return;
|
|
}
|
|
console.log(`\n${chalk.bold(tableName)} — Page ${page + 1}${searchInfo} (${rows.length} of ${total}):\n`);
|
|
if (rows.length) {
|
|
console.table(rows);
|
|
}
|
|
else {
|
|
console.log(chalk.yellow("No rows found."));
|
|
console.log();
|
|
}
|
|
const choices = [];
|
|
if (page > 0)
|
|
choices.push({ name: "← Previous Page", value: "prev" });
|
|
if (offset + rows.length < total)
|
|
choices.push({ name: "Next Page →", value: "next" });
|
|
choices.push({ name: "Search by Field", value: "search" });
|
|
if (searchTerm)
|
|
choices.push({ name: "Clear Search", value: "clear_search" });
|
|
choices.push({ name: chalk.dim("← Go Back"), value: "__back__" });
|
|
choices.push({ name: chalk.dim("✕ Exit"), value: "__exit__" });
|
|
const action = await select({ message: "Navigate:", choices });
|
|
if (action === "__back__")
|
|
break;
|
|
if (action === "__exit__")
|
|
return "__exit__";
|
|
if (action === "next")
|
|
page++;
|
|
if (action === "prev")
|
|
page--;
|
|
if (action === "clear_search") {
|
|
searchField = null;
|
|
searchTerm = null;
|
|
page = 0;
|
|
}
|
|
if (action === "search") {
|
|
const columns = (await dbHandler({
|
|
query: `SELECT COLUMN_NAME 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("No columns found for search."));
|
|
continue;
|
|
}
|
|
searchField = await select({
|
|
message: "Search by field:",
|
|
choices: columns.map((c) => ({
|
|
name: c.COLUMN_NAME,
|
|
value: c.COLUMN_NAME,
|
|
})),
|
|
});
|
|
searchTerm = await input({
|
|
message: `Search term for "${searchField}":`,
|
|
validate: (v) => v.trim().length > 0 || "Cannot be empty",
|
|
});
|
|
page = 0;
|
|
}
|
|
}
|
|
}
|