feat(mongo): open and save js files in mongo tabs
This commit is contained in:
1 parent
906f5c271c
commit
51b8342f5d
7 files changed
+152
-36
No files matched your search
@@ -56,6 +56,18 @@ describe("external SQL file targets", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("reuses the active MongoDB tab context for a script file only when explicitly allowed", () => {
|
||||
const tabs = [{ id: "mongo-tab", connectionId: "mongo-connection", database: "app_db", mode: "query" as const }];
|
||||
const lookup = () => ({ db_type: "mongodb" as const });
|
||||
const target = { connectionId: "mongo-connection", database: "app_db", catalog: undefined, schema: undefined };
|
||||
|
||||
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", lookup)).toEqual(unassociatedExternalSqlFileTarget());
|
||||
expect(resolveExternalSqlFileTargetForActiveTab("/work/query.js", tabs, "mongo-tab", lookup)).toEqual(unassociatedExternalSqlFileTarget());
|
||||
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", lookup, { allowMongoScripts: true })).toEqual(target);
|
||||
expect(resolveExternalSqlFileTargetForActiveTab("/work/query.js", tabs, "mongo-tab", lookup, { allowMongoScripts: true })).toEqual(target);
|
||||
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", () => ({ db_type: "redis" }), { allowMongoScripts: true })).toEqual(unassociatedExternalSqlFileTarget());
|
||||
});
|
||||
|
||||
it("keeps the fallback unassociated without an active tab", () => {
|
||||
const tabs = [{ id: "inactive-tab", connectionId: "sql-connection", database: "analytics", mode: "query" as const }];
|
||||
|
||||
@@ -63,7 +75,7 @@ describe("external SQL file targets", () => {
|
||||
expect(activeTabExternalSqlFileTarget(tabs, "missing-tab", () => ({ db_type: "postgres" }))).toEqual(unassociatedExternalSqlFileTarget());
|
||||
});
|
||||
|
||||
it.each(["mq", "zookeeper", "plugin", "redis"] as const)("does not reuse a non-SQL %s tab target", (dbType) => {
|
||||
it.each(["mq", "zookeeper", "plugin", "redis", "mongodb"] as const)("does not reuse a non-SQL %s tab target", (dbType) => {
|
||||
const tabs = [{ id: "active-tab", connectionId: "non-sql-connection", database: "invalid", catalog: "invalid", schema: "invalid", mode: "query" as const }];
|
||||
|
||||
expect(activeTabExternalSqlFileTarget(tabs, "active-tab", () => ({ db_type: dbType }))).toEqual(unassociatedExternalSqlFileTarget());
|
||||
|
||||
@@ -2,18 +2,22 @@
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
defaultSavedQueryFileName,
|
||||
ExternalSqlFileTooLargeError,
|
||||
clampExternalSqlEditorMaxMbInput,
|
||||
externalSqlEditorMaxBytes,
|
||||
externalSqlFileDisplayTitles,
|
||||
externalSqlFileOpenErrorMessage,
|
||||
formatSqlFileSize,
|
||||
isScriptFilePath,
|
||||
isSqlFilePath,
|
||||
MAX_EXTERNAL_SQL_EDITOR_FILE_BYTES,
|
||||
MAX_EXTERNAL_SQL_EDITOR_FILE_MB,
|
||||
MIN_EXTERNAL_SQL_EDITOR_FILE_MB,
|
||||
normalizeExternalSqlEditorMaxMb,
|
||||
normalizeExternalSqlPath,
|
||||
queryEditorOpenFileAccept,
|
||||
queryEditorOpenFileFilters,
|
||||
readBrowserSqlFile,
|
||||
} from "@/lib/sql/sqlFileOpen";
|
||||
|
||||
@@ -27,6 +31,12 @@ describe("external SQL file paths", () => {
|
||||
expect(isSqlFilePath("/work/script.py")).toBe(false);
|
||||
});
|
||||
|
||||
it("distinguishes script files (.sql and .js) from unsupported files", () => {
|
||||
expect(isScriptFilePath("C:\\work\\demo.SQL")).toBe(true);
|
||||
expect(isScriptFilePath("/work/mongo_query.js")).toBe(true);
|
||||
expect(isScriptFilePath("/work/script.py")).toBe(false);
|
||||
});
|
||||
|
||||
it("uses the shortest unique parent path for duplicate filenames", () => {
|
||||
expect(externalSqlFileDisplayTitles(["/work/demo/create.sql", "/work/learn/create.sql", "/work/query.sql"])).toEqual(["demo/create.sql", "learn/create.sql", "query.sql"]);
|
||||
});
|
||||
@@ -36,6 +46,33 @@ describe("external SQL file paths", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("query editor script file filters and names", () => {
|
||||
it("provides MongoDB script filters for MongoDB database type", () => {
|
||||
const filters = queryEditorOpenFileFilters("mongodb");
|
||||
expect(filters).toEqual([
|
||||
{ name: "MongoDB Script", extensions: ["js"] },
|
||||
{ name: "SQL", extensions: ["sql"] },
|
||||
{ name: "All Files", extensions: ["*"] },
|
||||
]);
|
||||
expect(queryEditorOpenFileAccept("mongodb")).toBe(".js,.sql");
|
||||
});
|
||||
|
||||
it("keeps non-MongoDB pickers SQL-only", () => {
|
||||
expect(queryEditorOpenFileFilters("postgres")).toEqual([{ name: "SQL", extensions: ["sql"] }]);
|
||||
expect(queryEditorOpenFileFilters(undefined)).toEqual([{ name: "SQL", extensions: ["sql"] }]);
|
||||
expect(queryEditorOpenFileAccept("postgres")).toBe(".sql");
|
||||
expect(queryEditorOpenFileAccept(undefined)).toBe(".sql");
|
||||
});
|
||||
|
||||
it("generates correct default save file names for MongoDB vs SQL", () => {
|
||||
expect(defaultSavedQueryFileName("query_1", "mongodb")).toBe("query_1.js");
|
||||
expect(defaultSavedQueryFileName("users list", "mongodb")).toBe("users_list.js");
|
||||
expect(defaultSavedQueryFileName("existing.js", "mongodb")).toBe("existing.js");
|
||||
expect(defaultSavedQueryFileName("query_1", "mysql")).toBe("query_1.sql");
|
||||
expect(defaultSavedQueryFileName("query_1")).toBe("query_1.sql");
|
||||
});
|
||||
});
|
||||
|
||||
describe("external SQL file editor limit", () => {
|
||||
it("accepts the exact browser editor limit", async () => {
|
||||
const file = new Blob(["select 1;"]);
|
||||
|
||||
@@ -15,15 +15,16 @@ export interface ExternalSqlFileTarget {
|
||||
type ExternalSqlFileTargetTab = Pick<QueryTab, "id" | "connectionId" | "database" | "catalog" | "schema" | "mode">;
|
||||
type ExternalSqlFileTargetConnection = Pick<ConnectionConfig, "db_type">;
|
||||
type ExternalSqlFileConnectionLookup = (connectionId: string) => ExternalSqlFileTargetConnection | undefined;
|
||||
type ExternalSqlFileTargetOptions = { allowMongoScripts?: boolean };
|
||||
|
||||
export function unassociatedExternalSqlFileTarget(): ExternalSqlFileTarget {
|
||||
return { connectionId: "", database: "", catalog: undefined, schema: undefined };
|
||||
}
|
||||
|
||||
export function activeTabExternalSqlFileTarget(tabs: readonly ExternalSqlFileTargetTab[], activeTabId: string | null | undefined, getConnection: ExternalSqlFileConnectionLookup): ExternalSqlFileTarget {
|
||||
export function activeTabExternalSqlFileTarget(tabs: readonly ExternalSqlFileTargetTab[], activeTabId: string | null | undefined, getConnection: ExternalSqlFileConnectionLookup, options: ExternalSqlFileTargetOptions = {}): ExternalSqlFileTarget {
|
||||
const activeTab = activeTabId ? tabs.find((tab) => tab.id === activeTabId) : undefined;
|
||||
const connection = activeTab?.connectionId ? getConnection(activeTab.connectionId) : undefined;
|
||||
if (!activeTab || activeTab.mode === "plugin-workbench" || activeTab.mode === "plugin-filesystem" || !connection || !supportsSqlFileExecution(connection.db_type)) return unassociatedExternalSqlFileTarget();
|
||||
if (!activeTab || activeTab.mode === "plugin-workbench" || activeTab.mode === "plugin-filesystem" || !connection || !(supportsSqlFileExecution(connection.db_type) || (options.allowMongoScripts === true && connection.db_type === "mongodb"))) return unassociatedExternalSqlFileTarget();
|
||||
return {
|
||||
connectionId: activeTab.connectionId,
|
||||
database: activeTab.database,
|
||||
@@ -103,6 +104,6 @@ export function resolveExternalSqlFileTarget(path: string, connectionExists: (co
|
||||
return { connectionId: saved.connectionId, database: saved.database, catalog: saved.catalog, schema: saved.schema };
|
||||
}
|
||||
|
||||
export function resolveExternalSqlFileTargetForActiveTab(path: string, tabs: readonly ExternalSqlFileTargetTab[], activeTabId: string | null | undefined, getConnection: ExternalSqlFileConnectionLookup): ExternalSqlFileTarget {
|
||||
return resolveExternalSqlFileTarget(path, (connectionId) => !!getConnection(connectionId), activeTabExternalSqlFileTarget(tabs, activeTabId, getConnection));
|
||||
export function resolveExternalSqlFileTargetForActiveTab(path: string, tabs: readonly ExternalSqlFileTargetTab[], activeTabId: string | null | undefined, getConnection: ExternalSqlFileConnectionLookup, options: ExternalSqlFileTargetOptions = {}): ExternalSqlFileTarget {
|
||||
return resolveExternalSqlFileTarget(path, (connectionId) => !!getConnection(connectionId), activeTabExternalSqlFileTarget(tabs, activeTabId, getConnection, options));
|
||||
}
|
||||
@@ -2,6 +2,38 @@ export function isSqlFilePath(path: string): boolean {
|
||||
return /\.sql$/i.test(path.trim());
|
||||
}
|
||||
|
||||
export function isScriptFilePath(path: string): boolean {
|
||||
return /\.(sql|js)$/i.test(path.trim());
|
||||
}
|
||||
|
||||
export interface QueryEditorFileDialogFilter {
|
||||
name: string;
|
||||
extensions: string[];
|
||||
}
|
||||
|
||||
export function queryEditorOpenFileFilters(databaseType?: string): QueryEditorFileDialogFilter[] {
|
||||
if (databaseType === "mongodb") {
|
||||
return [
|
||||
{ name: "MongoDB Script", extensions: ["js"] },
|
||||
{ name: "SQL", extensions: ["sql"] },
|
||||
{ name: "All Files", extensions: ["*"] },
|
||||
];
|
||||
}
|
||||
return [{ name: "SQL", extensions: ["sql"] }];
|
||||
}
|
||||
|
||||
export function queryEditorOpenFileAccept(databaseType?: string): string {
|
||||
return databaseType === "mongodb" ? ".js,.sql" : ".sql";
|
||||
}
|
||||
|
||||
export function defaultSavedQueryFileName(title: string, databaseType?: string): string {
|
||||
const defaultExt = databaseType === "mongodb" ? "js" : "sql";
|
||||
const trimmed = title.trim() || "query";
|
||||
const normalized = trimmed.replace(/\s+/g, "_");
|
||||
const extSuffix = `.${defaultExt}`;
|
||||
return normalized.toLowerCase().endsWith(extSuffix) ? normalized : `${normalized}${extSuffix}`;
|
||||
}
|
||||
|
||||
export function sqlFileTitleFromPath(path: string): string {
|
||||
const normalized = normalizeExternalSqlPath(path);
|
||||
const name = normalized.split("/").filter(Boolean).pop();
|
||||
|
||||
Reference in new issue
Block a user