Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-editor): add the format sql action (#301)
Yunwei Xiao3dcb68a7df20
diff --git a/frontend/package.json b/frontend/package.jsonindex c812b581b6..a73c7dd91c 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -27,6 +27,7 @@"postcss": "^8.3.5","slug": "^4.0.2","splitpanes": "^3.0.6",+ "sql-formatter": "^4.0.2","tailwindcss": "^2.2.2","uuid": "^8.3.2","v-code-diff": "^0.3.4",@@ -41,6 +42,7 @@"@intlify/vite-plugin-vue-i18n": "^3.2.1","@types/lodash-es": "^4.17.4","@types/node": "^16.11.9",+ "@types/prettier": "^2.4.2","@types/slug": "^0.9.1","@types/splitpanes": "^2.2.1","@types/uuid": "^8.3.0",diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vueindex b7e63bd296..69e460b889 100644--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue@@ -3,12 +3,23 @@</template><script lang="ts" setup>-import { onMounted, ref, toRef, toRaw } from "vue";+import { onMounted, ref, toRef, toRaw, PropType, nextTick } from "vue";+import type { editor as Editor } from "monaco-editor";+import setupMonaco from "./setupMonaco";+import sqlFormatter from "./sqlFormatter";++import { SqlLanguage } from "../../types";const props = defineProps({- modelValue: { type: String, default: "" },- language: { type: String, default: "mysql" },+ modelValue: {+ type: String,+ required: true,+ },+ language: {+ type: String as PropType<SqlLanguage>,+ default: "mysql",+ },});const emit = defineEmits<{@@ -22,22 +33,26 @@ const editorRef = ref();const sqlCode = toRef(props, "modelValue");const language = toRef(props, "language");-// let editorInstance: Editor.IStandaloneCodeEditor+let editorInstance: Editor.IStandaloneCodeEditor;-// const setContent = (content: string) => {-// if (editorInstance) editorInstance.setValue(content)-// }+const setContent = (content: string) => {+ if (editorInstance) editorInstance.setValue(content);+};-// const formatContent = () => {-// if (editorInstance) editorInstance.getAction('editor.action.formatDocument').run()-// }+const formatContent = () => {+ if (editorInstance) {+ const sql = editorInstance.getValue();+ const { data } = sqlFormatter(sql, language.value);+ setContent(data);+ }+};const init = async () => {const { monaco } = await setupMonaco(language.value);const model = monaco.editor.createModel(sqlCode.value, toRaw(language.value));- const editorInstance = monaco.editor.create(editorRef.value, {+ editorInstance = monaco.editor.create(editorRef.value, {model,tabSize: 2,insertSpaces: true,@@ -55,17 +70,16 @@ const init = async () => {// add the run query action in context menueditorInstance.addAction({- id: "Bytebase",+ id: "RunQuery",label: "Run Query",keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],contextMenuGroupId: "operation",contextMenuOrder: 0,run: async () => {- console.log("run query");-const typedValue = editorInstance.getValue();const selectedValue = editorInstance.getModel()+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-expect-error?.getValueInRange(editorInstance.getSelection()) as string;@@ -75,6 +89,28 @@ const init = async () => {},});+ editorInstance.addAction({+ id: "FormatSQL",+ label: "Format SQL",+ keybindings: [+ monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,+ ],+ contextMenuGroupId: "operation",+ contextMenuOrder: 1,+ run: () => {+ formatContent();+ nextTick(() => {+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment+ // @ts-expect-error+ const range = editorInstance.getModel().getFullModelRange();+ editorInstance.setPosition({+ lineNumber: range?.endLineNumber,+ column: range?.endColumn,+ });+ });+ },+ });+// typed something, change the texteditorInstance.onDidChangeModelContent(() => {const value = editorInstance.getValue();diff --git a/frontend/src/components/MonacoEditor/sqlFormatter.ts b/frontend/src/components/MonacoEditor/sqlFormatter.tsnew file mode 100644index 0000000000..acd6fb38a5--- /dev/null+++ b/frontend/src/components/MonacoEditor/sqlFormatter.ts@@ -0,0 +1,22 @@+import { format, FormatOptions } from "sql-formatter";++import { SqlDialect } from "../../types";++type FormatResult = {+ data: string;+ error: Error | null;+};++const formatSQL = (sql: string, dialect: SqlDialect): FormatResult => {+ const options: FormatOptions = {+ language: dialect,+ };+ try {+ const formatted = format(sql, options);+ return { data: formatted, error: null };+ } catch (error) {+ return { data: "", error: error as Error };+ }+};++export default formatSQL;diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.tsindex 825f7a1226..2203720a93 100644--- a/frontend/src/types/sqlEditor.ts+++ b/frontend/src/types/sqlEditor.ts@@ -7,6 +7,7 @@ export type EditorPosition = monaco.Position;export type CompletionItems = monaco.languages.CompletionItem[];export type ConnectionAtomType = "instance" | "database" | "table" | "view";+export type SqlDialect = "mysql" | "postgresql";export interface ConnectionAtom {parentId: InstanceId | DatabaseId | TableId | ViewId;diff --git a/frontend/yarn.lock b/frontend/yarn.lockindex ce58169623..8fef6cfedc 100644--- a/frontend/yarn.lock+++ b/frontend/yarn.lock@@ -389,6 +389,11 @@resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==+"@types/prettier@^2.4.2":+ version "2.4.2"+ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.2.tgz#4c62fae93eb479660c3bd93f9d24d561597a8281"+ integrity sha512-ekoj4qOQYp7CvjX8ZDBgN86w3MqQhLE1hczEJbEIjgFEumDy+na/4AJAbLXfgEWFNB2pKadM5rPFtuSGMWK7xA==+"@types/responselike@*":version "1.0.0"resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29"@@ -4150,6 +4155,13 @@ splitpanes@^3.0.6:resolved "https://registry.yarnpkg.com/splitpanes/-/splitpanes-3.0.6.tgz#45636c36972d3391f837073807726a2dcc49af15"integrity sha512-Ogxc+e2te4Tp5IpMwjOPO1ZvXUf2PsuEoyZhF9H5Hp6XOB0mLg/OnEZFaK2MxT7Q3O+WSEKE7CeOoTC+4zsQ6A==+sql-formatter@^4.0.2:+ version "4.0.2"+ resolved "https://registry.yarnpkg.com/sql-formatter/-/sql-formatter-4.0.2.tgz#2b359e5a4c611498d327b9659da7329d71724607"+ integrity sha512-R6u9GJRiXZLr/lDo8p56L+OyyN2QFJPCDnsyEOsbdIpsnDKL8gubYFo7lNR7Zx7hfdWT80SfkoVS0CMaF/DE2w==+ dependencies:+ argparse "^2.0.1"+string-width@^1.0.1:version "1.0.2"resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"