feat(mongodb): support createIndex shell command (#2187)
This commit is contained in:
1 parent
4e012a9dd9
commit
b90e4fd64e
17 files changed
+374
-18
No files matched your search
@@ -375,6 +375,7 @@ export const documentFindDocuments = forward("documentFindDocuments");
|
||||
export const mongoFindDocuments = forward("mongoFindDocuments");
|
||||
export const mongoServerVersion = forward("mongoServerVersion");
|
||||
export const mongoAggregateDocuments = forward("mongoAggregateDocuments");
|
||||
export const mongoCreateIndex = forward("mongoCreateIndex");
|
||||
export const mongoInsertDocument = forward("mongoInsertDocument");
|
||||
export const mongoInsertDocuments = forward("mongoInsertDocuments");
|
||||
export const mongoUpdateDocument = forward("mongoUpdateDocument");
|
||||
|
||||
@@ -1756,6 +1756,10 @@ export async function mongoAggregateDocuments(connectionId: string, database: st
|
||||
return post("/api/mongo/aggregate-documents", { connectionId, database, collection, pipelineJson, maxRows, executionId });
|
||||
}
|
||||
|
||||
export async function mongoCreateIndex(connectionId: string, database: string, collection: string, keysJson: string, optionsJson?: string): Promise<{ name: string }> {
|
||||
return post("/api/mongo/create-index", { connectionId, database, collection, keysJson, optionsJson });
|
||||
}
|
||||
|
||||
export async function mongoInsertDocument(connectionId: string, database: string, collection: string, docJson: string): Promise<string> {
|
||||
return post("/api/mongo/insert-document", { connectionId, database, collection, docJson });
|
||||
}
|
||||
|
||||
@@ -31,7 +31,11 @@ export interface MongoVersionCommand {
|
||||
kind: "version";
|
||||
}
|
||||
|
||||
export type MongoWriteCommand = { kind: "insert"; collection: string; docsJson: string } | { kind: "update"; collection: string; filter: string; update: string; many: boolean } | { kind: "delete"; collection: string; filter: string; many: boolean };
|
||||
export type MongoWriteCommand =
|
||||
| { kind: "insert"; collection: string; docsJson: string }
|
||||
| { kind: "update"; collection: string; filter: string; update: string; many: boolean }
|
||||
| { kind: "delete"; collection: string; filter: string; many: boolean }
|
||||
| { kind: "createIndex"; collection: string; keys: string; options?: string };
|
||||
|
||||
export interface MongoAggregateSafetyOptions {
|
||||
allowWrites?: boolean;
|
||||
@@ -201,6 +205,21 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null
|
||||
return { kind: "delete", collection: target.collection, filter, many: method === "deleteMany" };
|
||||
}
|
||||
|
||||
const createIndex = parseCollectionMethodTarget(source, "createIndex");
|
||||
if (createIndex) {
|
||||
const args = parseMethodArgs(source, createIndex.methodCallIndex);
|
||||
if (!args || args.length < 1 || args.length > 2) return null;
|
||||
const keys = normalizeJsonArgument(args[0]);
|
||||
if (!keys) return null;
|
||||
let options: string | undefined;
|
||||
if (args[1]?.trim()) {
|
||||
const parsedOptions = normalizeJsonArgument(args[1]);
|
||||
if (!parsedOptions) return null;
|
||||
options = parsedOptions;
|
||||
}
|
||||
return { kind: "createIndex", collection: createIndex.collection, keys, ...(options ? { options } : {}) };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -282,6 +301,15 @@ export function mongoWriteToQueryResult(affectedRows: number, executionTimeMs: n
|
||||
};
|
||||
}
|
||||
|
||||
export function mongoCreateIndexToQueryResult(name: string, executionTimeMs: number): QueryResult {
|
||||
return {
|
||||
columns: ["name"],
|
||||
rows: [[name]],
|
||||
affected_rows: 1,
|
||||
execution_time_ms: Math.max(0, Math.round(executionTimeMs)),
|
||||
};
|
||||
}
|
||||
|
||||
export function mongoUseToQueryResult(database: string, executionTimeMs: number): QueryResult {
|
||||
return {
|
||||
columns: ["message"],
|
||||
|
||||
@@ -1486,6 +1486,10 @@ export async function mongoAggregateDocuments(connectionId: string, database: st
|
||||
return invoke("mongo_aggregate_documents", { connectionId, database, collection, pipelineJson, maxRows, executionId });
|
||||
}
|
||||
|
||||
export async function mongoCreateIndex(connectionId: string, database: string, collection: string, keysJson: string, optionsJson?: string): Promise<{ name: string }> {
|
||||
return invoke("mongo_create_index", { connectionId, database, collection, keysJson, optionsJson });
|
||||
}
|
||||
|
||||
export async function mongoInsertDocument(connectionId: string, database: string, collection: string, docJson: string): Promise<string> {
|
||||
return invoke("mongo_insert_document", { connectionId, database, collection, docJson });
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { restoreOpenTabsState, serializeOpenTabs } from "@/lib/openTabsPersisten
|
||||
import {
|
||||
evaluateMongoAggregateSafety,
|
||||
mongoCountToQueryResult,
|
||||
mongoCreateIndexToQueryResult,
|
||||
mongoDocumentsToQueryResult,
|
||||
mongoIndexesToQueryResult,
|
||||
mongoUseToQueryResult,
|
||||
@@ -1756,6 +1757,29 @@ export const useQueryStore = defineStore("query", () => {
|
||||
} else if (mongoWrite.kind === "update") {
|
||||
const result = await api.mongoUpdateDocuments(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.filter, mongoWrite.update, mongoWrite.many);
|
||||
affectedRows = result.affected_rows;
|
||||
} else if (mongoWrite.kind === "createIndex") {
|
||||
const result = await api.mongoCreateIndex(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.keys, mongoWrite.options);
|
||||
console.info("[DBX][executeTabSql:mongo-write:done]", {
|
||||
traceId,
|
||||
indexName: result.name,
|
||||
elapsed: elapsed(),
|
||||
});
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.executionId === executionId) {
|
||||
current.results = undefined;
|
||||
current.activeResultIndex = undefined;
|
||||
current.result = markQueryResultRowsRaw(mongoCreateIndexToQueryResult(result.name, performance.now() - startedAt));
|
||||
touchResult(current);
|
||||
current.queryAnalysis = undefined;
|
||||
current.querySourceColumns = undefined;
|
||||
current.queryEditabilityReason = undefined;
|
||||
current.mongoEditTarget = undefined;
|
||||
current.tableMeta = undefined;
|
||||
current.resultBaseSql = options?.resultBaseSql ?? sql;
|
||||
current.resultSortedSql = options?.resultSortedSql;
|
||||
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
const result = await api.mongoDeleteDocuments(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.filter, mongoWrite.many);
|
||||
affectedRows = result.affected_rows;
|
||||
|
||||
Reference in new issue
Block a user