formatSql.ts20 lines · main
| 1 | import { type SafeSqlFragment } from '@supabase/pg-meta' |
| 2 | import { format } from 'sql-formatter' |
| 3 | |
| 4 | /** |
| 5 | * Util function for formatting SQL. It wraps the `sql-formatter` library with a preset format options so that the |
| 6 | * formatting is consistent across the app. It also has a try/catch block which returns the original SQL in case of |
| 7 | * an error. |
| 8 | */ |
| 9 | export function formatSql(sql: SafeSqlFragment): SafeSqlFragment |
| 10 | export function formatSql(sql: string): string |
| 11 | export function formatSql(sql: string): string { |
| 12 | try { |
| 13 | return format(sql, { |
| 14 | language: 'postgresql', |
| 15 | keywordCase: 'lower', |
| 16 | }) |
| 17 | } catch { |
| 18 | return sql |
| 19 | } |
| 20 | } |