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
@@ -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