formatSql.ts 641 B

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