mirror of
https://github.com/t8y2/dbx.git
synced 2026-10-28 08:08:02 +00:00
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { DEFAULT_SQL_FORMATTER_SETTINGS, sqlFormatterOptions, type SqlFormatterSettings } from "@/lib/sqlFormatterConfig";
|
|
|
|
export type SqlFormatDialect = "mysql" | "postgres" | "sqlite" | "sqlserver" | "generic";
|
|
|
|
export const MAX_SQL_FORMAT_CHARS = 1_000_000;
|
|
|
|
function formatterLanguage(dialect: SqlFormatDialect) {
|
|
switch (dialect) {
|
|
case "mysql":
|
|
return "mysql";
|
|
case "postgres":
|
|
return "postgresql";
|
|
case "sqlite":
|
|
return "sqlite";
|
|
case "sqlserver":
|
|
return "transactsql";
|
|
default:
|
|
return "sql";
|
|
}
|
|
}
|
|
|
|
export async function formatSqlText(sql: string, dialect: SqlFormatDialect = "generic", settings: Partial<SqlFormatterSettings> = DEFAULT_SQL_FORMATTER_SETTINGS): Promise<string> {
|
|
if (!sql.trim()) return sql;
|
|
if (sql.length > MAX_SQL_FORMAT_CHARS) {
|
|
throw new Error("SQL is too large to format safely.");
|
|
}
|
|
|
|
const { format } = await import("sql-formatter");
|
|
return format(sql, {
|
|
language: formatterLanguage(dialect),
|
|
...sqlFormatterOptions(settings),
|
|
});
|
|
}
|