27 lines
1.0 KiB
JavaScript
27 lines
1.0 KiB
JavaScript
import { Database } from "bun:sqlite";
|
|
import { input } from "@inquirer/prompts";
|
|
import chalk from "chalk";
|
|
import dbHandler from "../../lib/db-handler";
|
|
export default async function runSQL(params) {
|
|
const sql = await input({
|
|
message: "Enter SQL query:",
|
|
validate: (val) => val.trim().length > 0 || "Query cannot be empty",
|
|
});
|
|
try {
|
|
const res = await dbHandler({ query: sql });
|
|
if (res.payload) {
|
|
console.log(`\n${chalk.bold(`Result (${res.payload.length} row${res.payload.length !== 1 ? "s" : ""}):`)} \n`);
|
|
if (res.payload.length)
|
|
console.table(res.payload);
|
|
else
|
|
console.log(chalk.yellow("No res returned.\n"));
|
|
}
|
|
else if (res.single_res) {
|
|
console.log(chalk.green(`\nSuccess! Affected rows: ${res.single_res.changes}, Last insert ID: ${res.single_res.lastInsertRowid}\n`));
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error(chalk.red(`\nSQL Error: ${error.message}\n`));
|
|
}
|
|
}
|