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

Package detail

monaco-sql-languages

DTStack32.6kMIT0.15.1TypeScript support: included

SQL languages for the Monaco Editor, based on monaco-languages.

monaco-editor, SQL, code-completion, hive, spark, mysql, postgresql, flink, trino, impala

readme

Monaco SQL Languages

NPM version NPM downloads

English | 简体中文

This project is based on the SQL language project of Monaco Editor, which was forked from the monaco-languages.The difference is that Monaco SQL Languages supports various SQL languages and the corresponding advanced features for the Big Data field.


Feature highlights

  • Code Highlighting
  • Syntax Validation
  • Code Completion
  • Built-in SQL Snippets

Powered By dt-sql-parser


Online Preview

https://dtstack.github.io/monaco-sql-languages/

Powered By molecule.


Supported SQL Languages

  • MySQL
  • Flink
  • Spark
  • Hive
  • Trino (Presto)
  • PostgreSQL
  • Impala

Installing

npm install monaco-sql-languages

Tips: Monaco SQL Languages is only guaranteed to work stably on `monaco-editor@0.31.0` for now.


Integrating


Usage

  1. Import language contributions

    Tips: If integrated via MonacoEditorWebpackPlugin, it will help us to import contribution files automatically. Otherwise, you need to import the contribution files manually.

     import 'monaco-sql-languages/esm/languages/mysql/mysql.contribution';
     import 'monaco-sql-languages/esm/languages/flink/flink.contribution';
     import 'monaco-sql-languages/esm/languages/spark/spark.contribution';
     import 'monaco-sql-languages/esm/languages/hive/hive.contribution';
     import 'monaco-sql-languages/esm/languages/trino/trino.contribution';
     import 'monaco-sql-languages/esm/languages/pgsql/pgsql.contribution';
     import 'monaco-sql-languages/esm/languages/impala/impala.contribution';
    
     // Or you can import all language contributions at once.
     // import 'monaco-sql-languages/esm/all.contributions';
  2. Setup language features

    You can setup language features via setupLanguageFeatures. For example, setup code completion feature of flinkSQL language.

     import { LanguageIdEnum, setupLanguageFeatures } from 'monaco-sql-languages';
    
     setupLanguageFeatures(LanguageIdEnum.FLINK, {
         completionItems: {
             enable: true,
             triggerCharacters: [' ', '.'],
             completionService: //... ,
         }
     });

    By default, Monaco SQL Languages only provides keyword autocompletion and built-in SQL snippets, and you can customize your completionItem list via completionService.

     import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
     import {
         setupLanguageFeatures,
         LanguageIdEnum,
         CompletionService,
         ICompletionItem,
         EntityContextType
      } from 'monaco-sql-languages';
    
     const completionService: CompletionService = function (
         model,
         position,
         completionContext,
         suggestions, // syntax context info at caretPosition
         entities, // tables, columns in the syntax context of the editor text
         snippets // SQL snippets
     ) {
         return new Promise((resolve, reject) => {
             if (!suggestions) {
                 return Promise.resolve([]);
             }
             const { keywords, syntax } = suggestions;
             const keywordsCompletionItems: ICompletionItem[] = keywords.map((kw) => ({
                 label: kw,
                 kind: languages.CompletionItemKind.Keyword,
                 detail: 'keyword',
                 sortText: '2' + kw
             }));
    
             let syntaxCompletionItems: ICompletionItem[] = [];
    
             syntax.forEach((item) => {
                 if (item.syntaxContextType === EntityContextType.DATABASE) {
                     const databaseCompletions: ICompletionItem[] = []; // some completions about databaseName
                     syntaxCompletionItems = [...syntaxCompletionItems, ...databaseCompletions];
                 }
                 if (item.syntaxContextType === EntityContextType.TABLE) {
                     const tableCompletions: ICompletionItem[] = []; // some completions about tableName
                     syntaxCompletionItems = [...syntaxCompletionItems, ...tableCompletions];
                 }
             });
    
             resolve([...syntaxCompletionItems, ...keywordsCompletionItems]);
         });
     };
    
     setupLanguageFeatures(LanguageIdEnum.FLINK, {
         completionItems: {
             enable: true,
             completionService: //... ,
         }
     });
  3. Create the Monaco Editor instance and specify the language you need

     import { LanguageIdEnum } from 'monaco-sql-languages';
    
     monaco.editor.create(document.getElementById('container'), {
         value: 'select * from tb_test',
         language: LanguageIdEnum.FLINK // languageId
     });

SQL Snippets

We provide some built-in SQL snippets for each SQL language, which helps us to write SQL quickly.

How to customize SQL snippets?

When setting language features, you can customize SQL snippets via snippets configuration. When snippets is passed in as an empty array, the built-in SQL snippets are disabled.

import { snippets, CompletionSnippetOption } from 'monaco-sql-languages/esm/main.js';

const customSnippets: CompletionSnippetOption[] = [
    {
        label: 'INSERT',
        prefix: 'insert',
        // Will join the line with `\n`
        body: [
            'INSERT INTO ${1:table_name}',
            'SELECT ${3:column1}, ${4:column2}',
            'FROM ${2:source_table}',
            'WHERE ${5:conditions};\n$6'
        ],
        description: "This is an 'insert into select' snippet"
    }
];

Snippets syntax can refer to vscode-snippet. But it is different from vscode code snippets, we only provide snippets completions at the beginning of the SQL statement.

Note: If you provide a custom completionService method, you need to manually return the snippets as completions, as shown in the following example:

const completionService: CompletionService = async function (
    model,
    position,
    completionContext,
    suggestions,
    entities,
    snippets
) {
    const { keywords } = suggestions;

    const keywordsCompletionItems: ICompletionItem[] = keywords.map((kw) => ({
        label: kw,
        kind: languages.CompletionItemKind.Keyword,
        detail: 'keyword',
        sortText: '2' + kw
    }));

    const snippetCompletionItems: ICompletionItem[] =
        snippets?.map((item) => ({
            label: item.label || item.prefix,
            kind: languages.CompletionItemKind.Snippet,
            filterText: item.prefix,
            insertText: item.insertText,
            insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
            sortText: '3' + item.prefix,
            detail: item.description !== undefined ? item.description : 'SQL Snippet',
            documentation: item.insertText
        })) || [];

    return [...keywordsCompletionItems, ...snippetCompletionItems];
};

Other Notes:

When in code snippet context, you can use Tab key to move to the next input position, but the keywords completions is also triggered by Tab key, which will cause a shortcut key conflict. So Monaco-Editor stipulates that when in code snippet context, it will not trigger completion. snippet-prevent-completion If you want to still support intelligent completion in code snippet context, you can set the Monaco-Editor configuration item suggest.snippetsPreventQuickSuggestions to false to achieve it.

editor.create(editorElement, {
    suggest: {
        snippetsPreventQuickSuggestions: false
    }
})

snippet-no-prevent-completion

Monaco Theme

Monaco SQL Languages plan to support more themes in the future.

Monaco SQL Languages provides built-in Monaco Theme that is named vsPlusTheme. vsPlusTheme inspired by vscode default plus colorTheme and it contains three styles of themes inside:

  • darkTheme: Inherited from monaco built-in theme vs-dark;
  • lightTheme: Inherited from monaco built-in theme vs;
  • hcBlackTheme: Inherited from monaco built-in theme hc-black;

Use Monaco SQL Languages built-in vsPlusTheme

import { vsPlusTheme } from 'monaco-sql-languages';
import { editor } from 'monaco-editor';

// import themeData and defineTheme, you can customize the theme name, e.g. sql-dark
editor.defineTheme('sql-dark', vsPlusTheme.darkThemeData);
editor.defineTheme('sql-light', vsPlusTheme.lightThemeData);
editor.defineTheme('sql-hc', vsPlusTheme.hcBlackThemeData);

// specify the theme you have defined
editor.create(null as any, {
    theme: 'sql-dark',
    language: 'flinksql'
});

Customize your own Monaco theme

import { TokenClassConsts, postfixTokenClass } from 'monaco-sql-languages';

// Customize the various tokens style
const myThemeData: editor.IStandaloneThemeData = {
    base: 'vs-dark',
    inherit: true,
    rules: [
        { token: postfixTokenClass(TokenClassConsts.COMMENT), foreground: '6a9955' },
        { token: postfixTokenClass(TokenClassConsts.IDENTIFIER), foreground: '9cdcfe' },
        { token: postfixTokenClass(TokenClassConsts.KEYWORD), foreground: '569cd6' },
        { token: postfixTokenClass(TokenClassConsts.NUMBER), foreground: 'b5cea8' },
        { token: postfixTokenClass(TokenClassConsts.STRING), foreground: 'ce9178' },
        { token: postfixTokenClass(TokenClassConsts.TYPE), foreground: '4ec9b0' }
    ],
    colors: {}
};

// Define the monaco theme
editor.defineTheme('my-theme', myThemeData);

postfixTokenClass is not required in most cases, but since Monaco SQL Languages has tokenPostfix: 'sql' internally set for all SQL languages, in some cases your custom style may not work if you don't use postfixTokenClassClass to handle TokenClassConsts.*.


Dev: cheat sheet

  • initial setup

    pnpm install
  • open the dev web

    pnpm watch-esm
    cd website
    pnpm install
    pnpm dev
  • build

    pnpm build
  • run test

    pnpm test

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.


License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.15.1 (2025-06-06)

Bug Fixes

0.15.0 (2025-05-16)

Features

0.14.0 (2025-05-09)

Features

Bug Fixes

  • scripts: resolve configuration conflicts (76d728c)

0.13.1 (2025-02-17)

Features

0.13.0 (2025-02-13)

Features

Bug Fixes

0.12.2 (2024-06-19)

Bug Fixes

0.12.1 (2024-06-19)

Bug Fixes

0.12.0 (2024-04-30)

Features

0.12.0-beta.14 (2024-04-26)

Features

Bug Fixes

0.12.0-beta.13 (2024-04-22)

Features

Bug Fixes

0.12.0-beta.12 (2024-04-19)

Features

Bug Fixes

0.12.0-beta.11 (2024-04-01)

Features

0.12.0-beta.10 (2024-01-04)

Features

  • disabled commmon sql diagnostics feature (0a64586)
  • upgrade dt-sql-parser to v4.0.0-beta.4.11 (4d680f4)

0.12.0-beta.9 (2023-12-15)

Features

  • allow the monaco version to be greater than 0.31.0 (708f38c)
  • update lock file (8f6ae1c)

0.12.0-beta.8 (2023-12-15)

Features

Bug Fixes

  • trino/hive keywords exclude nonReserved (#88) (d9a7447)

0.12.0-beta.7 (2023-11-24)

Features

  • upgrade dt-sql-parser to v4.0.0-beta.4.7 (a1caa33)

0.12.0-beta.6 (2023-11-20)

Features

  • upgrade dt-sql-parser to v4.0.0-beta.4.6 (f79285c)

0.12.0-beta.5 (2023-11-10)

Features

0.12.0-beta.4 (2023-11-07)

Features

  • upgrade dt-sql-parser to v4.0.0-beta.4.5 (23b90fe)

0.12.0-beta.3 (2023-11-06)

Features

  • add vscode plus colorTheme (#65) (f7a7ea8)
  • highlight keywords of condition expression (#66) (a9f6578)
  • hive and spark support code completion (#54) (6d0eabf)
  • improve flinksql highlight (#58) (c204a2c)
  • improve highlighting of hive lanuguage (#59) (43c14c8)
  • improve highlighting of spark language (#61) (4de9815)
  • improve highlighting of trinosql language (#64) (f24e4ff)
  • support trinosql language (#56) (adbd3df)

Bug Fixes

0.12.0-beta.2 (2023-09-01)

Features

  • upgrade dt-sql-parser to v4.0.0-beta.4.2 (2a11750)

Bug Fixes

  • correct diag position (#49) (560e05b)
  • correct main field in package.json (0f70a6e)
  • correct module field in package.json (83880a2)

0.12.0-beta.1 (2023-06-14)

0.12.0-beta (2023-06-14)

Features

  • allow flink sql language register completionService (520a02e)
  • flinksql completion (1fba38b)
  • integrated dt-sql-parser auto complete (c0d5ecf)
  • remove lstypes about because it is for node-lsp (3b5c06e)
  • support syntax completions (7e00463)

Bug Fixes

0.11.0 (2023-01-09)

Features

  • upgrade the molecule to latest version (377590d)

0.10.0 (2022-12-16)

0.9.5 (2022-08-19)

0.9.4 (2021-12-08)