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: add MongoDB document/table result views (#19539)
h3n4l2feadd37df97
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.tsindex 89c0224446..3ffe73827e 100644--- a/frontend/src/composables/useExecuteSQL.ts+++ b/frontend/src/composables/useExecuteSQL.ts@@ -324,10 +324,7 @@ const useExecuteSQL = () => {});const instanceResource = getInstanceResource(database);- if (- instanceResource.engine === Engine.MONGODB ||- instanceResource.engine === Engine.COSMOSDB- ) {+ if (instanceResource.engine === Engine.COSMOSDB) {flattenNoSQLResult(resultSet);}diff --git a/frontend/src/composables/utils.test.ts b/frontend/src/composables/utils.test.tsnew file mode 100644index 0000000000..859cace514--- /dev/null+++ b/frontend/src/composables/utils.test.ts@@ -0,0 +1,66 @@+import { create as createProto } from "@bufbuild/protobuf";+import { describe, expect, test } from "vitest";+import {+ QueryResultSchema,+ QueryRowSchema,+ RowValueSchema,+} from "@/types/proto-es/v1/sql_service_pb";+import { flattenNoSQLQueryResult } from "./utils";++describe("flattenNoSQLQueryResult", () => {+ test("flattens raw MongoDB document rows into table columns on demand", () => {+ const result = createProto(QueryResultSchema, {+ columnNames: ["result"],+ columnTypeNames: ["TEXT"],+ rows: [+ createProto(QueryRowSchema, {+ values: [+ createProto(RowValueSchema, {+ kind: {+ case: "stringValue",+ value: JSON.stringify({+ _id: { $oid: "507f1f77bcf86cd799439011" },+ name: "Ada",+ profile: {+ age: { $numberInt: "36" },+ },+ }),+ },+ }),+ ],+ }),+ ],+ });++ const flattened = flattenNoSQLQueryResult(result);++ expect(flattened?.columnNames).toEqual(["_id", "name", "profile"]);+ expect(flattened?.columnTypeNames).toEqual(["TEXT", "TEXT", "TEXT"]);+ expect(flattened?.rows[0]?.values[0]?.kind.value).toBe(+ "507f1f77bcf86cd799439011"+ );+ expect(flattened?.rows[0]?.values[1]?.kind.value).toBe("Ada");+ expect(flattened?.rows[0]?.values[2]?.kind.value).toBe('{"age":36}');+ });++ test("returns undefined for non-document result sets", () => {+ const result = createProto(QueryResultSchema, {+ columnNames: ["name"],+ columnTypeNames: ["TEXT"],+ rows: [+ createProto(QueryRowSchema, {+ values: [+ createProto(RowValueSchema, {+ kind: {+ case: "stringValue",+ value: "Ada",+ },+ }),+ ],+ }),+ ],+ });++ expect(flattenNoSQLQueryResult(result)).toBeUndefined();+ });+});diff --git a/frontend/src/composables/utils.ts b/frontend/src/composables/utils.tsindex 9c2bd24b9c..513ef396b1 100644--- a/frontend/src/composables/utils.ts+++ b/frontend/src/composables/utils.ts@@ -253,55 +253,82 @@ const convertAnyToRowValue = (export const flattenNoSQLResult = (resultSet: SQLResultSetV1) => {for (const result of resultSet.results) {- const { columns, columnIndexMap } = getNoSQLColumns(result.rows);-- const rows: QueryRow[] = [];- const columnTypeNames: string[] = Array.from({- length: columns.length,- }).map((_) => "TEXT");-- for (const row of result.rows) {- if (- row.values.length !== 1 ||- row.values[0].kind.case !== "stringValue"- ) {- continue;- }- // Use lossless-json to preserve precision for large integers (> 2^53-1)- const data = losslessParse(- row.values[0].kind.value,- null,- losslessReviver- ) as Record<string, unknown>;- const values: RowValue[] = Array.from({ length: columns.length }).map(- (_) =>- createProto(RowValueSchema, {- kind: {- case: "nullValue",- value: NullValue.NULL_VALUE,- },- })- );+ const flattened = flattenNoSQLQueryResult(result);+ if (!flattened) {+ continue;+ }- for (const [key, value] of Object.entries(data)) {- const index = columnIndexMap.get(key) ?? 0;+ result.rows = flattened.rows;+ result.columnNames = flattened.columnNames;+ result.columnTypeNames = flattened.columnTypeNames;+ }+};- const { value: formatted, type } = convertAnyToRowValue(value, true);- values[index] = formatted;- columnTypeNames[index] = type;- }+export const flattenNoSQLQueryResult = (+ result: QueryResult+): QueryResult | undefined => {+ if (+ result.columnNames.length !== 1 ||+ result.columnNames[0] !== "result" ||+ result.rows.length === 0+ ) {+ return undefined;+ }- rows.push(- createProto(QueryRowSchema, {- values: values,- })- );+ const { columns, columnIndexMap } = getNoSQLColumns(result.rows);+ if (columns.length === 0) {+ return undefined;+ }++ const rows: QueryRow[] = [];+ const columnTypeNames: string[] = Array.from({ length: columns.length }).map(+ () => "TEXT"+ );++ for (const row of result.rows) {+ if (row.values.length !== 1 || row.values[0].kind.case !== "stringValue") {+ return undefined;}- result.rows = rows;- result.columnNames = columns;- result.columnTypeNames = columnTypeNames;+ // Use lossless-json to preserve precision for large integers (> 2^53-1)+ const data = losslessParse(+ row.values[0].kind.value,+ null,+ losslessReviver+ ) as Record<string, unknown>;+ const values: RowValue[] = Array.from({ length: columns.length }).map(() =>+ createProto(RowValueSchema, {+ kind: {+ case: "nullValue",+ value: NullValue.NULL_VALUE,+ },+ })+ );++ for (const [key, value] of Object.entries(data)) {+ const index = columnIndexMap.get(key) ?? 0;+ const { value: formatted, type } = convertAnyToRowValue(value, true);+ values[index] = formatted;+ columnTypeNames[index] = type;+ }++ rows.push(+ createProto(QueryRowSchema, {+ values,+ })+ );}++ return createProto(QueryResultSchema, {+ columnNames: columns,+ columnTypeNames,+ rows,+ rowsCount: BigInt(rows.length),+ statement: result.statement,+ latency: result.latency,+ error: result.error,+ masked: result.masked,+ });};// Parses the hits array from an Elasticsearch _search QueryResult's "hits" column.diff --git a/frontend/src/utils/storage-keys.ts b/frontend/src/utils/storage-keys.tsindex d18c5c7c97..6ad01832b1 100644--- a/frontend/src/utils/storage-keys.ts+++ b/frontend/src/utils/storage-keys.ts@@ -26,7 +26,7 @@ export const STORAGE_KEY_SQL_EDITOR_DETAIL_FORMAT ="bb.sql-editor.detail-panel.format";export const STORAGE_KEY_SQL_EDITOR_DETAIL_LINE_WRAP ="bb.sql-editor.detail-panel.line-wrap";-export const STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW =+export const STORAGE_KEY_SQL_EDITOR_NOSQL_TABLE_VIEW ="bb.sql-editor.es-table-view";export const STORAGE_KEY_MY_ISSUES_TAB = "bb.components.MY_ISSUES.id";diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vueindex da55adb386..1d62658c8d 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue@@ -34,8 +34,8 @@</NTooltip></div><div class="flex justify-between items-center shrink-0 gap-x-2">- <div v-if="isESSearchResult" class="flex items-center">- <NSwitch v-model:value="esTableView" size="small" />+ <div v-if="supportsTableViewToggle" class="flex items-center">+ <NSwitch v-model:value="noSQLTableView" size="small" /><span class="ml-1 whitespace-nowrap text-sm text-gray-500">{{ $t("sql-editor.table-view") }}</span>@@ -286,7 +286,10 @@ import DataExportButton from "@/components/DataExportButton.vue";import EllipsisText from "@/components/EllipsisText.vue";import { CopyButton, Drawer, RichDatabaseName } from "@/components/v2";import { useExecuteSQL } from "@/composables/useExecuteSQL";-import { flattenElasticsearchSearchResult } from "@/composables/utils";+import {+ flattenElasticsearchSearchResult,+ flattenNoSQLQueryResult,+} from "@/composables/utils";import { DISMISS_PLACEHOLDER } from "@/plugins/ai/components/state";import { useSQLEditorStore, useSQLEditorTabStore } from "@/store";import type {@@ -309,7 +312,7 @@ import {isNullOrUndefined,type SearchParams,} from "@/utils";-import { STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW } from "@/utils/storage-keys";+import { STORAGE_KEY_SQL_EDITOR_NOSQL_TABLE_VIEW } from "@/utils/storage-keys";import {provideSQLResultViewContext,type SQLResultViewContext,@@ -406,23 +409,43 @@ const viewMode = computed((): ViewMode => {const engine = computed(() => getInstanceResource(props.database).engine);-const esTableView = useLocalStorage(STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW, true);+const noSQLTableView = useLocalStorage(+ STORAGE_KEY_SQL_EDITOR_NOSQL_TABLE_VIEW,+ true+);const flattenedESResult = computed(() => {if (engine.value !== Engine.ELASTICSEARCH) return undefined;return flattenElasticsearchSearchResult(props.result);});-const isESSearchResult = computed(() => flattenedESResult.value !== undefined);+const flattenedMongoResult = computed(() => {+ if (engine.value !== Engine.MONGODB) return undefined;+ return flattenNoSQLQueryResult(props.result);+});++const flattenedNoSQLResult = computed(() => {+ if (engine.value === Engine.ELASTICSEARCH) {+ return flattenedESResult.value;+ }+ if (engine.value === Engine.MONGODB) {+ return flattenedMongoResult.value;+ }+ return undefined;+});++const supportsTableViewToggle = computed(+ () => flattenedNoSQLResult.value !== undefined+);const activeResult = computed(() => {- if (isESSearchResult.value && esTableView.value) {- return flattenedESResult.value!;+ if (supportsTableViewToggle.value && noSQLTableView.value) {+ return flattenedNoSQLResult.value!;}return props.result;});-watch(esTableView, () => {+watch(noSQLTableView, () => {state.sortState = undefined;state.searchParams = { query: "", scopes: [] };state.searchCandidateActiveIndex = -1;@@ -661,8 +684,8 @@ const resultRowsText = computed(() => {});const isSensitiveColumn = (columnIndex: number): boolean => {- // Column indices don't match when showing flattened ES table view.- if (isESSearchResult.value && esTableView.value) return false;+ // Column indices don't match when showing flattened NoSQL table view.+ if (supportsTableViewToggle.value && noSQLTableView.value) return false;const maskingReason = props.result.masked?.[columnIndex];// Check if maskingReason exists and has actual content (not empty object)return (@@ -674,8 +697,8 @@ const isSensitiveColumn = (columnIndex: number): boolean => {};const getMaskingReason = (columnIndex: number) => {- // Column indices don't match when showing flattened ES table view.- if (isESSearchResult.value && esTableView.value) return undefined;+ // Column indices don't match when showing flattened NoSQL table view.+ if (supportsTableViewToggle.value && noSQLTableView.value) return undefined;if (!props.result.masked || columnIndex >= props.result.masked.length) {return undefined;}diff --git a/frontend/vite.config.ts b/frontend/vite.config.tsindex 88d039f308..7f27b8c254 100644--- a/frontend/vite.config.ts+++ b/frontend/vite.config.ts@@ -49,9 +49,13 @@ export default defineConfig({compiler: "vue3",}),yaml(),- CodeInspectorPlugin({- bundler: "vite",- }),+ ...(process.env.VITEST+ ? []+ : [+ CodeInspectorPlugin({+ bundler: "vite",+ }),+ ]),// Export CSP hashes from @vitejs/plugin-legacy for backend to useexportCspHashes(),],