SQLEditor.constants.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { untrustedSql } from '@supabase/pg-meta'
  2. import { IS_PLATFORM } from 'common'
  3. import type { SqlSnippets, UserContent } from '@/types'
  4. const SQL_SNIPPET_SCHEMA_VERSION = '1.0'
  5. export const NEW_SQL_SNIPPET_SKELETON: UserContent<SqlSnippets.Content> = {
  6. name: 'New Query',
  7. description: '',
  8. type: 'sql',
  9. visibility: 'user', // default to user scope
  10. favorite: false,
  11. content: {
  12. schema_version: SQL_SNIPPET_SCHEMA_VERSION,
  13. content_id: '',
  14. unchecked_sql: untrustedSql(''),
  15. },
  16. }
  17. export const sqlAiDisclaimerComment = `
  18. -- Briven AI is experimental and may produce incorrect answers
  19. -- Always verify the output before executing
  20. `.trim()
  21. // Should only be used for comparisons. If you need a new title, use generateSnippetTitle()
  22. export const untitledSnippetTitle = 'Untitled query'
  23. /**
  24. * Generates a snippet title. If the platform is self-hosted, it will return a random number to avoid conflicts.
  25. */
  26. export const generateSnippetTitle = () => {
  27. if (IS_PLATFORM) {
  28. return untitledSnippetTitle
  29. } else {
  30. return `${untitledSnippetTitle} ${Math.floor(Math.random() * 900) + 100}`
  31. }
  32. }
  33. export const destructiveSqlRegex = [
  34. /^(.*;)?\s*(drop|delete|truncate|alter\s+table\s+.*\s+drop\s+column)\s/is,
  35. ]
  36. // Matches `UPDATE <table> SET ...` where <table> is any combination of bareword
  37. // or double-quoted identifiers, optionally schema-qualified. Quoted identifiers
  38. // can contain any character (including spaces) and use `""` to escape an inner
  39. // quote, mirroring Postgres syntax.
  40. export const updateWithoutWhereRegex =
  41. /(?:^|;)\s*update\s+(?:"(?:[^"]|"")+"|[\w]+)(?:\.(?:"(?:[^"]|"")+"|[\w]+))?\s+set\s+[\w\W]+?(?!\s*where\s)/is
  42. export const alterDatabasePreventConnectionStatements = [
  43. 'alter database postgres connection limit 0',
  44. 'alter database postgres allow_connections false',
  45. ]
  46. export const ASSISTANT_TEMPLATES = [
  47. {
  48. name: 'Twitter clone',
  49. description: 'Simplified schema that mimics the Twitter application',
  50. prompt: 'Create a twitter clone',
  51. },
  52. {
  53. name: 'Chat application',
  54. description: 'Send messages through channels or direct messages',
  55. prompt:
  56. 'Create a chat application that supports sending messages either through channels or directly between users',
  57. },
  58. {
  59. name: 'User management schema',
  60. description: 'With role based access control',
  61. prompt: 'Create a simple user management schema that supports role based access control',
  62. },
  63. {
  64. name: 'Countries and Cities',
  65. description: 'With each city belonging to a country',
  66. prompt:
  67. 'Create a table of countries and a table of cities, with each city belonging to a country',
  68. },
  69. ]
  70. export const ROWS_PER_PAGE_OPTIONS = [
  71. { value: -1, label: 'No limit' },
  72. { value: 100, label: '100 rows' },
  73. { value: 500, label: '500 rows' },
  74. { value: 1000, label: '1,000 rows' },
  75. ]