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
+38
-11
@@ -53,17 +53,7 @@ afterEach(() => {
|
||||
|
||||
describe("useFileDrop SQL target", () => {
|
||||
it("uses only an active SQL tab for browser-dropped files", async () => {
|
||||
let setupFileDrop!: ReturnType<typeof useFileDrop>["setupFileDrop"];
|
||||
app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
({ setupFileDrop } = useFileDrop());
|
||||
return () => h("div");
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.mount(document.createElement("div"));
|
||||
await setupFileDrop();
|
||||
await mountFileDrop();
|
||||
|
||||
mocks.queryStore.tabs = [{ id: "sql-tab", connectionId: "postgres-1", database: "analytics", catalog: "hive", schema: "reporting" }];
|
||||
mocks.queryStore.activeTabId = "sql-tab";
|
||||
@@ -80,9 +70,46 @@ describe("useFileDrop SQL target", () => {
|
||||
|
||||
await vi.waitFor(() => expect(mocks.queryStore.createTab).toHaveBeenCalledTimes(2));
|
||||
expect(mocks.queryStore.createTab).toHaveBeenNthCalledWith(2, "", "", "non-sql.sql", "query", undefined, undefined, undefined);
|
||||
|
||||
mocks.queryStore.tabs = [{ id: "mongo-tab", connectionId: "mongo-1", database: "admin", schema: undefined }];
|
||||
mocks.queryStore.activeTabId = "mongo-tab";
|
||||
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => (connectionId === "mongo-1" ? { id: connectionId, db_type: "mongodb" } : undefined));
|
||||
dispatchSqlDrop("script.js");
|
||||
|
||||
await vi.waitFor(() => expect(mocks.queryStore.createTab).toHaveBeenCalledTimes(3));
|
||||
expect(mocks.queryStore.createTab).toHaveBeenNthCalledWith(3, "mongo-1", "admin", "script.js", "query", undefined, undefined, undefined);
|
||||
});
|
||||
|
||||
it("ignores dropped .js files that do not resolve to a MongoDB target", async () => {
|
||||
await mountFileDrop();
|
||||
|
||||
mocks.queryStore.tabs = [{ id: "sql-tab", connectionId: "postgres-1", database: "analytics" }];
|
||||
mocks.queryStore.activeTabId = "sql-tab";
|
||||
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => (connectionId === "postgres-1" ? { id: connectionId, db_type: "postgres" } : undefined));
|
||||
dispatchSqlDrop("script.js");
|
||||
|
||||
await vi.waitFor(() => expect(mocks.readBrowserSqlFile).toHaveBeenCalled());
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
expect(mocks.queryStore.createTab).not.toHaveBeenCalled();
|
||||
expect(mocks.queryStore.openExternalSqlFile).not.toHaveBeenCalled();
|
||||
expect(mocks.toast).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
async function mountFileDrop() {
|
||||
let setupFileDrop!: ReturnType<typeof useFileDrop>["setupFileDrop"];
|
||||
app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
({ setupFileDrop } = useFileDrop());
|
||||
return () => h("div");
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.mount(document.createElement("div"));
|
||||
await setupFileDrop();
|
||||
}
|
||||
|
||||
function dispatchSqlDrop(name: string) {
|
||||
const event = new Event("drop", { bubbles: true, cancelable: true });
|
||||
Object.defineProperty(event, "dataTransfer", { value: { files: [new File(["select 1"], name)] } });
|
||||
|
||||
@@ -9,13 +9,9 @@ import { useLargeSqlFileStreamingFallback } from "@/composables/useLargeSqlFileF
|
||||
import * as api from "@/lib/backend/api";
|
||||
import type { ConnectionConfig, ExternalSqlFileVersion } from "@/types/database";
|
||||
import { detectDatabaseFileType } from "@/lib/database/databaseFileDetection";
|
||||
import { externalSqlEditorMaxBytes, externalSqlFileOpenErrorMessage, readBrowserSqlFile } from "@/lib/sql/sqlFileOpen";
|
||||
import { externalSqlEditorMaxBytes, externalSqlFileOpenErrorMessage, isScriptFilePath, isSqlFilePath, readBrowserSqlFile } from "@/lib/sql/sqlFileOpen";
|
||||
import { activeTabExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab } from "@/lib/sql/externalSqlFileTarget";
|
||||
|
||||
function isSqlFilePath(path: string): boolean {
|
||||
return /\.sql$/i.test(path);
|
||||
}
|
||||
|
||||
function getDataFileQuery(path: string): Promise<string | undefined> {
|
||||
return api.buildDroppedFilePreviewSql({ path });
|
||||
}
|
||||
@@ -29,11 +25,16 @@ export function useFileDrop() {
|
||||
const { openInStreamingExecutorOnTooLarge } = useLargeSqlFileStreamingFallback();
|
||||
|
||||
async function openDroppedSqlFile(name: string, content: string, path?: string, version?: ExternalSqlFileVersion) {
|
||||
const options = { allowMongoScripts: !isSqlFilePath(name) };
|
||||
const target = path
|
||||
? resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId), options)
|
||||
: activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId), options);
|
||||
// Dropped .js files only open when they resolve to a MongoDB target so they
|
||||
// never bind to a relational SQL tab.
|
||||
if (!isSqlFilePath(name) && connectionStore.getConfig(target.connectionId)?.db_type !== "mongodb") return;
|
||||
if (path) {
|
||||
const target = resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId));
|
||||
queryStore.openExternalSqlFile(target.connectionId, target.database, path, content, version, target.catalog, target.schema);
|
||||
} else {
|
||||
const target = activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId));
|
||||
const tabId = queryStore.createTab(target.connectionId, target.database, name, "query", target.schema, undefined, target.catalog);
|
||||
queryStore.updateSql(tabId, content);
|
||||
}
|
||||
@@ -85,7 +86,7 @@ export function useFileDrop() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isSqlFilePath(path)) {
|
||||
if (isScriptFilePath(path)) {
|
||||
try {
|
||||
const snapshot = await api.readExternalSqlFileSnapshot(path, externalSqlEditorMaxBytes(settingsStore.editorSettings.externalSqlEditorMaxMb));
|
||||
await openDroppedSqlFile(name, snapshot.content, path, snapshot.version);
|
||||
@@ -127,7 +128,7 @@ export function useFileDrop() {
|
||||
event.preventDefault();
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
if (!isSqlFilePath(file.name)) continue;
|
||||
if (!isScriptFilePath(file.name)) continue;
|
||||
void readBrowserSqlFile(file, externalSqlEditorMaxBytes(settingsStore.editorSettings.externalSqlEditorMaxMb))
|
||||
.then((content) => openDroppedSqlFile(file.name, content))
|
||||
.catch((e: any) => {
|
||||
@@ -139,7 +140,7 @@ export function useFileDrop() {
|
||||
const files = event.dataTransfer?.files;
|
||||
if (!files || files.length === 0) return;
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (isSqlFilePath(files[i].name)) {
|
||||
if (isScriptFilePath(files[i].name)) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user