Skip to main content

SQL editor

SQL editor with catalog completion, run and format.

Guidance​

The SQL editor is the Query workspace's editor: mm-code-editor with the SQL grammar for the source's dialect, autocomplete from catalog metadata, and the run and format keys.

  • Ctrl+Enter (⌘+Enter on Apple) fires mm-run with the selection if there is one, otherwise the whole statement.
  • Shift+Alt+F fires a cancelable mm-format; unless you cancel it, keywords are upper-cased and trailing spaces trimmed. Nothing else changes, so formatting never alters meaning.
  • Completions come from schema ({ 'shop.public.orders': ['order_id', …] }) and from the async completer, whose results show their asset type and provider ("table · PostgreSQL") with the provider's logo. The popup sits on the code ground; Enter accepts, Escape closes.
  • error puts the server's error inline at its line and column, with a status line under the editor.

Dialects: postgresql (default), mysql, sqlite and standard. Trino maps to standard SQL, Snowflake, Redshift and DuckDB to PostgreSQL, BigQuery and ClickHouse to MySQL.

Properties​

PropertyAttributeTypeDefaultDescription
dialectdialectSqlDialectName | string'postgresql'postgresql (default), mysql, sqlite, standard, or a Marmot provider mapped to the closest one.
schemaSqlSchema{}Catalog metadata for completion, keyed by dotted table name.
defaultSchemadefault-schemastring''Schema whose tables complete without a prefix, such as shop.public.
completerSqlCompleter | nullnullAsync completion hook, called as people type. Return completions with assetType and provider for the detail.
errorSqlError | nullnullThe server's error for the last run: { line, column, message }, 1-based.
hideShortcuthide-shortcutbooleanfalseHides the run shortcut hint under the editor.
languagelanguageCodeEditorLanguage'sql'sql, json, yaml or text.
valuevaluestring''The text.
labellabelstring''Accessible name of the editor ("Role document", "Pipeline config").
placeholderplaceholderstring''Shown when the editor is empty. Never a substitute for label.
readonlyreadonlybooleanfalseSelectable and copyable but not editable.
diagnosticsEditorDiagnostic[][]Inline diagnostics: [{ from, to, severity, message }] with document offsets.
resizeresize'vertical' | 'none''vertical'vertical (default) lets people drag the editor taller; none fixes the height.
lazylazy'load' | 'interaction''load'load (default) loads CodeMirror after first render; interaction waits for the first focus or press.
readybooleanfalseTrue once CodeMirror is mounted.
viewThe CodeMirror view, once loaded.
selectedTextstringThe selected text, or the empty string when nothing is selected or the editor has not loaded.
namenamestring''Name submitted with the form.
disableddisabledbooleanfalseDisables the control and excludes it from submission.
requiredrequiredbooleanfalseRequires a value before the form submits.
formHTMLFormElement | null
validityValidityState
validationMessagestring
willValidateboolean
isDisabledbooleanTrue when disabled directly or by a disabled ancestor fieldset.

Methods​

MethodDescription
run()Fires mm-run with the selection, or the whole editor when nothing is selected.
format()Fires mm-format and, unless it is cancelled, upper-cases keywords and trims trailing spaces.
load()Loads CodeMirror now. Resolves when the editor is interactive.
formResetCallback()Restore the initial value when the owning form resets.

Events​

EventDetailDescription
mm-runCustomEvent<{ sql: string, selection: boolean }>Ctrl/⌘+Enter, or run(). sql is the selection when there is one.
mm-formatCustomEvent<{ sql: string }>Shift+Alt+F, or format(). Cancel it to format on your side and set value.
mm-inputCustomEvent<{ value: string }>The text changed.
mm-changeCustomEvent<{ value: string }>The text changed and the editor lost focus.
mm-readyCustomEvent<Record<string, never>>CodeMirror loaded and the editor is interactive.
inputEvent
changeEvent

Slots​

SlotDescription
badgeStatus under the editor, such as "Read only" or "Read-only replica". Defaults to "Read only" when readonly.

CSS parts​

PartDescription
frameThe code ground holding the editor.
fallbackThe static highlighted copy shown before CodeMirror loads.
footerThe row under the editor with the error, badge and run shortcut.
messagesThe list of diagnostic messages.
shortcutThe run shortcut hint.

CSS custom properties​

PropertyDescription
--mm-code-editor-heightInitial height of the editor. Defaults to 240px.

Examples​

Every state of mm-sql-editor in Storybook, with keyboard and screen reader tests.