Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@codemirror/lang-sql

codemirror2mMIT6.9.0TypeScript support: included

SQL language support for the CodeMirror code editor

editor, code

readme

@codemirror/lang-sql NPM version

[ WEBSITE | ISSUES | FORUM | CHANGELOG ]

This package implements SQL language support for the CodeMirror code editor.

The project page has more information, a number of examples and the documentation.

This code is released under an MIT license.

We aim to be an inclusive, welcoming community. To make that explicit, we have a code of conduct that applies to communication around the project.

Usage

import {EditorView, basicSetup} from "codemirror"
import {sql} from "@codemirror/lang-sql"

const view = new EditorView({
  parent: document.body,
  doc: `select * from users where age > 20`,
  extensions: [basicSetup, sql()]
})

Use sql({dialect: PostgreSQL}) or similar to select a specific SQL dialect.

API Reference

sql(config⁠?: SQLConfig = {}) → LanguageSupport

SQL language support for the given SQL dialect, with keyword completion, and, if provided, schema-based completion as extra extensions.

interface SQLConfig

Options used to configure an SQL extension.

dialect⁠?: SQLDialect

The dialect to use. Defaults to StandardSQL.

schema⁠?: SQLNamespace

You can use this to define the schemas, tables, and their fields for autocompletion.

defaultTable⁠?: string

When given, columns from the named table can be completed directly at the top level.

defaultSchema⁠?: string

When given, tables prefixed with this schema name can be completed directly at the top level.

upperCaseKeywords⁠?: boolean

When set to true, keyword completions will be upper-case.

keywordCompletion⁠?: fn(label: string, type: string) → Completion

Can be used to customize the completions generated for keywords.

type SQLNamespace = Object<SQLNamespace> | {self: Completion, children: SQLNamespace} | readonly (string | Completion)[]

The type used to describe a level of the schema for completion. Can be an array of options (columns), an object mapping table or schema names to deeper levels, or a {self, children} object that assigns a completion option to use for its parent property, when the default option (its name as label and type "type") isn't suitable.

class SQLDialect

Represents an SQL dialect.

language: LRLanguage

The language for this dialect.

spec: SQLDialectSpec

The spec used to define this dialect.

extension: Extension

Returns the language for this dialect as an extension.

static define(spec: SQLDialectSpec) → SQLDialect

Define a new dialect.

type SQLDialectSpec

Configuration for an SQL Dialect.

keywords⁠?: string

A space-separated list of keywords for the dialect.

builtin⁠?: string

A space-separated string of built-in identifiers for the dialect.

types⁠?: string

A space-separated string of type names for the dialect.

backslashEscapes⁠?: boolean

Controls whether regular strings allow backslash escapes.

hashComments⁠?: boolean

Controls whether # creates a line comment.

slashComments⁠?: boolean

Controls whether // creates a line comment.

spaceAfterDashes⁠?: boolean

When enabled -- comments are only recognized when there's a space after the dashes.

doubleDollarQuotedStrings⁠?: boolean

When enabled, things quoted with "$" are treated as strings, rather than identifiers.

doubleQuotedStrings⁠?: boolean

When enabled, things quoted with double quotes are treated as strings, rather than identifiers.

charSetCasts⁠?: boolean

Enables strings like _utf8'str' or N'str'.

plsqlQuotingMechanism⁠?: boolean

Enables string quoting syntax like q'[str]', as used in PL/SQL.

operatorChars⁠?: string

The set of characters that make up operators. Defaults to "*+\-%<>!=&|~^/".

specialVar⁠?: string

The set of characters that start a special variable name. Defaults to "?".

identifierQuotes⁠?: string

The characters that can be used to quote identifiers. Defaults to "\"".

caseInsensitiveIdentifiers⁠?: boolean

Controls whether identifiers are case-insensitive. Identifiers with upper-case letters are quoted when set to false (which is the default).

unquotedBitLiterals⁠?: boolean

Controls whether bit values can be defined as 0b1010. Defaults to false.

treatBitsAsBytes⁠?: boolean

Controls whether bit values can contain other characters than 0 and 1. Defaults to false.

StandardSQL: SQLDialect

The standard SQL dialect.

PostgreSQL: SQLDialect

Dialect for PostgreSQL.

MySQL: SQLDialect

MySQL dialect.

MariaSQL: SQLDialect

Variant of MySQL for MariaDB.

MSSQL: SQLDialect

SQL dialect for Microsoft SQL Server.

SQLite: SQLDialect

SQLite dialect.

Cassandra: SQLDialect

Dialect for Cassandra's SQL-ish query language.

PLSQL: SQLDialect

PL/SQL dialect.

keywordCompletionSource(dialect: SQLDialect, upperCase⁠?: boolean = false, build⁠?: fn(label: string, type: string) → Completion) → CompletionSource

Returns a completion source that provides keyword completion for the given SQL dialect.

schemaCompletionSource(config: SQLConfig) → CompletionSource

Returns a completion sources that provides schema-based completion for the given configuration.

changelog

6.9.0 (2025-05-30)

New features

The new SQLDialect.configureLanguage method can be used to configure the language (and it's syntax node props) used by a dialect.

6.8.0 (2024-10-01)

New features

The new keywordCompletion option can be used to define what kind of completions are generated for keywords.

6.7.1 (2024-08-21)

Bug fixes

Remove single-letter words from the list of Postgres keywords, since they interfere with alias-based autocompletion.

6.7.0 (2024-06-24)

New features

Dialects can now disable quoting of identifiers containing upper-case characters with the caseInsensitiveIdentifiers option.

6.6.5 (2024-06-17)

Bug fixes

Fix a bug that broke tokenizing of e'\n'-style strings.

6.6.4 (2024-05-04)

Bug fixes

Make statement folding leave the entire first line visible.

Fix a null dereference in schema-based autocompletion.

6.6.3 (2024-04-08)

Bug fixes

Fix a bug where Postgres-style dollar-quoted strings were enabled for all dialects, and the doubleDollarQuotedStrings options was ignored.

6.6.2 (2024-03-23)

Bug fixes

Properly support tags in PostgreSQL 4073 quoted strings.

6.6.1 (2024-03-04)

Bug fixes

Fix an issue that caused completions to be missing when using the defaultSchema option.

6.6.0 (2024-02-29)

Bug fixes

Don't tokenize identifiers after periods as anything but plain identifiers.

New features

The schema option now allows nested objects to define multiple levels of completions, as well as self completion options for specific levels. The old format (using tables/schemas) continues to work but is deprecated.

6.5.5 (2023-12-28)

Bug fixes

Make sure table and column completions with upper-case characters are quoted.

Tag comments and strings as isolating for the purpose of bidirectional text.

6.5.4 (2023-08-10)

Bug fixes

Remove use of negative lookbehind in a regular expression, which recent versions of Safari still don't support.

6.5.3 (2023-08-05)

Bug fixes

The PL/SQL dialect now correctly handles q'[]'-quoting syntax.

6.5.2 (2023-06-23)

Bug fixes

Allow table names to contain multiple dots in the schema passed to schemaCompletionSource.

6.5.1 (2023-06-21)

Bug fixes

schemaCompletionSource now adds quotes around non-word identifiers even if the user didn't type a starting quote.

6.5.0 (2023-05-16)

New features

Dialect objects now have a public spec property holding their configuration.

6.4.1 (2023-04-13)

Bug fixes

Fix a bug where tokenizing of block comments got confused when nested comment start/end markers appeared directly next to each other.

6.4.0 (2023-01-23)

Bug fixes

Fix syntax tree node names for curly and square brackets, which had their names swapped.

New features

The new schemas config option can be used to provide custom completion objects for schema completions.

6.3.3 (2022-11-14)

Bug fixes

Fix tokenizing of double-$ strings in SQL dialects that support them.

6.3.2 (2022-10-24)

Bug fixes

Make sure the language object has a name.

6.3.1 (2022-10-17)

Bug fixes

Fix tokenizing of -- line comments.

6.3.0 (2022-08-23)

New features

Schema-based completion now understands basic table alias syntax, and will take it into account when looking up completions.

6.2.0 (2022-08-14)

New features

The new unquotedBitLiterals dialect option controls whether 0b01 syntax is recognized.

Dialects now allow a treatBitsAsBytes option to allow any characters inside quoted strings prefixed with b.

6.1.0 (2022-08-05)

New features

The new doubleDollarQuotedStrings options to SQL dialects allows parsing of text delimited by $$ as strings. Regenerate readme

6.0.0 (2022-06-08)

Breaking changes

Update dependencies to 6.0.0

0.20.4 (2022-05-30)

New features

Schema completion descriptions may now include dots in table names to indicate nested schemas.

0.20.3 (2022-05-27)

Bug fixes

Fix a bug where the slash at the end of block comments wasn't considered part of the comment token.

0.20.2 (2022-05-24)

Bug fixes

Fix an infinite recursion bug in schemaCompletionSource.

0.20.1 (2022-05-24)

Breaking changes

The schemaCompletion and keywordCompletion exports, which returned extensions, have been replaced with schemaCompletionSource and keywordCompletionSource, which return completion sources. The old exports will remain available until the next major version.

0.20.0 (2022-04-20)

Bug fixes

Fix autocompletion on columns when the table name is written with upper-case letters. Move to @lezer/highlight

0.19.4 (2021-10-28)

Bug fixes

Remove duplicate keywords/types in dialect configurations.

Fix a bug that caused characters directly before a space to be tokenized incorrectly.

0.19.3 (2021-08-21)

Bug fixes

Fix a bug that broke tokenization of keywords.

0.19.2 (2021-08-11)

0.19.1 (2021-08-11)

Bug fixes

Fix incorrect versions for @lezer dependencies.

0.19.0 (2021-08-11)

Breaking changes

Update dependencies to 0.19.0

0.18.0 (2021-03-03)

Breaking changes

Update dependencies to 0.18.

0.17.2 (2021-02-01)

Bug fixes

Fix bad syntax tree creation when the input ends with an unfinished quoted identifier.

0.17.1 (2021-01-06)

New features

The package now also exports a CommonJS module.

0.17.0 (2020-12-29)

Breaking changes

First numbered release.