forked from atums.world/backend
add clearTable command, simple webpage start
This commit is contained in:
parent
f14daf041a
commit
9a93e6269d
10 changed files with 156 additions and 11 deletions
|
@ -205,3 +205,23 @@ export function supportsExif(mimeType: string, extension: string): boolean {
|
|||
export function supportsThumbnail(mimeType: string): boolean {
|
||||
return /^(image\/(?!svg+xml)|video\/)/i.test(mimeType);
|
||||
}
|
||||
|
||||
// Commands
|
||||
export function parseArgs(): Record<string, string | boolean> {
|
||||
const args: string[] = process.argv.slice(2);
|
||||
const parsedArgs: Record<string, string | boolean> = {};
|
||||
|
||||
for (let i: number = 0; i < args.length; i++) {
|
||||
if (args[i].startsWith("--")) {
|
||||
const key: string = args[i].slice(2);
|
||||
const value: string | boolean =
|
||||
args[i + 1] && !args[i + 1].startsWith("--")
|
||||
? args[i + 1]
|
||||
: true;
|
||||
parsedArgs[key] = value;
|
||||
if (value !== true) i++;
|
||||
}
|
||||
}
|
||||
|
||||
return parsedArgs;
|
||||
}
|
||||
|
|
40
src/helpers/commands/clearTable.ts
Normal file
40
src/helpers/commands/clearTable.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { parseArgs } from "@helpers/char";
|
||||
import { type ReservedSQL, sql } from "bun";
|
||||
|
||||
(async (): Promise<void> => {
|
||||
try {
|
||||
const args: Record<string, string | boolean> = parseArgs();
|
||||
const table: string | undefined = args.table as string | undefined;
|
||||
const cascade: boolean = args.cascade === true;
|
||||
|
||||
if (!table) {
|
||||
throw new Error("Missing required argument: --table <table_name>");
|
||||
}
|
||||
|
||||
const reservation: ReservedSQL = await sql.reserve();
|
||||
|
||||
try {
|
||||
await reservation`TRUNCATE TABLE ${sql(table)} ${cascade ? sql`CASCADE` : sql``};`;
|
||||
console.log(
|
||||
`Table ${table} has been cleared${cascade ? " with CASCADE" : ""}.`,
|
||||
);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes("foreign key constraint")
|
||||
) {
|
||||
console.error(
|
||||
`Could not clear table "${table}" due to foreign key constraints.\n` +
|
||||
"Try using --cascade if you want to remove dependent records.",
|
||||
);
|
||||
} else {
|
||||
console.error("Could not clear table:", error);
|
||||
}
|
||||
} finally {
|
||||
reservation.release();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Unexpected error:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue