feat(mongodb): support safe index editing

This commit is contained in:
kirito authored and GitHub committed 2026-08-14 12:58:03 +08:00
1 parent 880d635961
commit 799bdd1a62
27 files changed
+2754 -10

No files matched your search

@@ -0,0 +1,262 @@
<script setup lang="ts">
import { toRefs, watch } from "vue";
import { Loader2, Pencil, Plus, RefreshCw, Trash2 } from "@lucide/vue";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
const props = defineProps<{ controller: Record<string, any> }>();
const emit = defineEmits<{ closed: [] }>();
const {
node,
t,
showMongoIndexManagerDialog,
mongoIndexManagerRows,
mongoIndexManagerLoading,
mongoIndexManagerError,
mongoIndexManagerSelectedName,
mongoIndexManagerMode,
mongoIndexManagerSelected,
mongoIndexManagerCollectionName,
loadMongoIndexManagerRows,
selectMongoIndexRow,
startCreateMongoIndexDraft,
startEditMongoIndexDraft,
cancelMongoIndexDraft,
dropSelectedMongoIndexRow,
canDropSelectedMongoIndexRow,
canEditSelectedMongoIndexRow,
confirmEditMongoIndex,
mongoCreateIndexForm,
mongoCreateIndexFieldOptions,
mongoCreateIndexError,
mongoCreateIndexLoading,
mongoIndexKeyTypes,
mongoCreateIndexCanSubmit,
mongoCreateIndexCanAddField,
addMongoCreateIndexField,
removeMongoCreateIndexField,
confirmCreateMongoIndex,
} = toRefs(props.controller);
/** Render the transport values (1 / -1) with their human-readable direction. */
function mongoIndexTypeLabel(type: string): string {
if (type === "1") return t.value("contextMenu.mongoIndexAscending");
if (type === "-1") return t.value("contextMenu.mongoIndexDescending");
return type;
}
watch(showMongoIndexManagerDialog, (open) => {
if (!open) emit("closed");
});
</script>
<template>
<Dialog v-model:open="showMongoIndexManagerDialog">
<DialogContent class="flex min-w-0 flex-col gap-0 p-0 sm:max-w-[760px]">
<DialogHeader class="border-b px-5 py-4">
<DialogTitle>{{ t("contextMenu.manageMongoIndexesTitle", { collection: mongoIndexManagerCollectionName }) }}</DialogTitle>
<p class="font-mono text-xs text-muted-foreground">{{ node.database }} / {{ mongoIndexManagerCollectionName }}</p>
</DialogHeader>
<div class="flex items-center gap-2 border-b px-5 py-2">
<Button type="button" variant="outline" size="sm" :disabled="mongoIndexManagerLoading || mongoCreateIndexLoading || mongoIndexManagerMode === 'create' || mongoIndexManagerMode === 'edit'" @click="startCreateMongoIndexDraft">
<Plus class="mr-1 h-4 w-4" />
{{ t("contextMenu.createMongoIndex") }}
</Button>
<Button type="button" variant="outline" size="sm" :disabled="mongoIndexManagerLoading || mongoCreateIndexLoading || !canEditSelectedMongoIndexRow" @click="startEditMongoIndexDraft">
<Pencil class="mr-1 h-4 w-4" />
{{ t("contextMenu.editMongoIndex") }}
</Button>
<Button type="button" variant="outline" size="sm" class="text-destructive hover:text-destructive" :disabled="mongoIndexManagerLoading || mongoCreateIndexLoading || !canDropSelectedMongoIndexRow" @click="dropSelectedMongoIndexRow">
<Trash2 class="mr-1 h-4 w-4" />
{{ t("contextMenu.dropIndex") }}
</Button>
<Button
type="button"
variant="ghost"
size="sm"
class="ml-auto"
:disabled="mongoIndexManagerLoading || mongoCreateIndexLoading || mongoIndexManagerMode === 'create' || mongoIndexManagerMode === 'edit'"
:title="t('contextMenu.refreshChildren')"
:aria-label="t('contextMenu.refreshChildren')"
@click="loadMongoIndexManagerRows"
>
<RefreshCw class="h-4 w-4" :class="mongoIndexManagerLoading ? 'animate-spin' : ''" />
</Button>
</div>
<section class="min-w-0">
<div class="flex min-w-0 gap-2 border-b bg-muted/40 px-5 py-1.5 text-xs text-muted-foreground">
<span class="min-w-0 flex-1">{{ t("structureEditor.indexName") }}</span>
<span class="min-w-0 flex-1">{{ t("contextMenu.mongoIndexKeys") }}</span>
</div>
<div class="h-[200px] min-w-0 overflow-auto">
<div v-if="mongoIndexManagerLoading" class="flex items-center justify-center gap-2 py-10 text-sm text-muted-foreground">
<Loader2 class="h-4 w-4 animate-spin" />
{{ t("common.loading") }}
</div>
<p v-else-if="mongoIndexManagerError" class="min-w-0 whitespace-pre-wrap break-all px-5 py-6 text-sm text-destructive">{{ mongoIndexManagerError }}</p>
<p v-else-if="mongoIndexManagerRows.length === 0" class="px-5 py-10 text-center text-sm text-muted-foreground">{{ t("contextMenu.mongoIndexEmpty") }}</p>
<button
v-for="row in mongoIndexManagerRows"
v-else
:key="row.name"
type="button"
class="flex w-full min-w-0 gap-2 px-5 py-1.5 text-left text-sm hover:bg-muted/60"
:class="row.name === mongoIndexManagerSelectedName ? 'bg-primary/10' : ''"
:disabled="mongoIndexManagerMode === 'create' || mongoIndexManagerMode === 'edit'"
@click="selectMongoIndexRow(row.name)"
>
<span class="min-w-0 flex-1 truncate font-mono">{{ row.name }}</span>
<span class="min-w-0 flex-1 truncate font-mono text-muted-foreground">{{ row.keys }}</span>
</button>
</div>
</section>
<section class="min-w-0 border-t bg-muted/20 px-5 py-4">
<div v-if="mongoIndexManagerMode === 'create' || mongoIndexManagerMode === 'edit'" class="grid min-w-0 gap-4">
<div class="grid min-w-0 gap-2">
<div class="flex items-center justify-between gap-3">
<span class="text-sm font-medium">{{ t("contextMenu.createMongoIndexFields") }}</span>
<Button type="button" variant="outline" size="sm" :disabled="mongoCreateIndexLoading || !mongoCreateIndexCanAddField" @click="addMongoCreateIndexField">
<Plus class="mr-1 h-4 w-4" />
{{ t("mongo.addField") }}
</Button>
</div>
<div v-for="field in mongoCreateIndexForm.fields" :key="field.id" class="flex min-w-0 items-center gap-2">
<Input v-model="field.path" :list="`mongo-index-manager-fields-${field.id}`" :disabled="mongoCreateIndexLoading" :placeholder="t('mongo.fieldPlaceholder')" :aria-label="t('mongo.field')" class="h-8 min-w-0 flex-1" autocomplete="off" />
<datalist :id="`mongo-index-manager-fields-${field.id}`">
<option v-for="option in mongoCreateIndexFieldOptions" :key="option" :value="option"></option>
</datalist>
<Select v-model="field.type" :disabled="mongoCreateIndexLoading">
<SelectTrigger class="h-8 w-36 shrink-0" :aria-label="t('structureEditor.indexType')">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="type in mongoIndexKeyTypes" :key="type" :value="type">{{ mongoIndexTypeLabel(type) }}</SelectItem>
</SelectContent>
</Select>
<Button type="button" variant="ghost" size="icon" class="h-8 w-8" :disabled="mongoCreateIndexLoading || mongoCreateIndexForm.fields.length === 1" :title="t('structureEditor.remove')" :aria-label="t('structureEditor.remove')" @click="removeMongoCreateIndexField(field.id)">
<Trash2 class="h-4 w-4" />
</Button>
</div>
</div>
<label class="grid min-w-0 gap-1.5 text-sm font-medium">
{{ t("structureEditor.indexName") }}
<Input v-model="mongoCreateIndexForm.name" :disabled="mongoCreateIndexLoading" :placeholder="t('contextMenu.createMongoIndexNamePlaceholder')" :aria-label="t('structureEditor.indexName')" class="h-8" />
</label>
<div class="grid min-w-0 gap-2.5">
<label class="flex w-fit cursor-pointer items-center gap-2 text-sm">
<Switch v-model="mongoCreateIndexForm.unique" :disabled="mongoCreateIndexLoading" :aria-label="t('structureEditor.unique')" />
{{ t("structureEditor.unique") }}
</label>
<label class="flex w-fit cursor-pointer items-center gap-2 text-sm">
<Switch v-model="mongoCreateIndexForm.sparse" :disabled="mongoCreateIndexLoading" :aria-label="t('contextMenu.createMongoIndexSparse')" />
{{ t("contextMenu.createMongoIndexSparse") }}
</label>
<label v-if="mongoIndexManagerMode === 'edit' || mongoCreateIndexForm.hidden" class="flex w-fit cursor-pointer items-center gap-2 text-sm">
<Switch v-model="mongoCreateIndexForm.hidden" :disabled="mongoCreateIndexLoading" :aria-label="t('mongo.indexHidden')" />
{{ t("mongo.indexHidden") }}
</label>
<label class="grid min-w-0 gap-1.5 text-sm">
{{ t("mongo.indexExpireAfterSeconds") }}
<Input v-model="mongoCreateIndexForm.expireAfterSeconds" :disabled="mongoCreateIndexLoading" inputmode="numeric" :placeholder="t('mongo.indexExpireAfterSecondsPlaceholder')" :aria-label="t('mongo.indexExpireAfterSeconds')" class="h-8 max-w-[260px]" />
<span class="text-xs text-muted-foreground">{{ t("mongo.indexExpireAfterSecondsHint") }}</span>
</label>
<label class="grid min-w-0 gap-1.5 text-sm">
{{ t("mongo.indexPartialFilter") }}
<Input v-model="mongoCreateIndexForm.partialFilterExpression" :disabled="mongoCreateIndexLoading" placeholder='{ "status": "active" }' :aria-label="t('mongo.indexPartialFilter')" class="h-8 font-mono" />
</label>
</div>
<details class="min-w-0 rounded-md border bg-background px-3 py-2">
<summary class="cursor-pointer text-sm font-medium">{{ t("mongo.indexLegacyOptions") }}</summary>
<div class="grid min-w-0 gap-2.5 pt-3">
<p class="text-xs text-muted-foreground">{{ t("mongo.indexLegacyOptionsHint") }}</p>
<label class="flex w-fit cursor-pointer items-center gap-2 text-sm">
<Switch v-model="mongoCreateIndexForm.background" :disabled="mongoCreateIndexLoading" :aria-label="t('mongo.indexBackground')" />
{{ t("mongo.indexBackground") }}
</label>
<label class="grid min-w-0 gap-1.5 text-sm">
{{ t("mongo.indexBucketSize") }}
<Input v-model="mongoCreateIndexForm.bucketSize" :disabled="mongoCreateIndexLoading" inputmode="numeric" :aria-label="t('mongo.indexBucketSize')" class="h-8 max-w-[260px]" />
</label>
</div>
</details>
<p v-if="mongoIndexManagerMode === 'edit' && mongoIndexManagerSelected?.extraOptions" class="text-xs text-muted-foreground">{{ t("contextMenu.mongoIndexExtraOptionsPreserved") }}</p>
<p v-if="mongoIndexManagerMode === 'edit'" class="text-xs text-muted-foreground">{{ t("contextMenu.mongoIndexRebuildHint") }}</p>
<p v-if="mongoCreateIndexError" class="min-w-0 max-w-full whitespace-pre-wrap break-all text-sm text-destructive">{{ mongoCreateIndexError }}</p>
<div class="flex items-center justify-end gap-2">
<Button variant="outline" size="sm" :disabled="mongoCreateIndexLoading" @click="cancelMongoIndexDraft">{{ t("dangerDialog.cancel") }}</Button>
<Button size="sm" :disabled="mongoCreateIndexLoading || !mongoCreateIndexCanSubmit" @click="mongoIndexManagerMode === 'edit' ? confirmEditMongoIndex() : confirmCreateMongoIndex()">
<Loader2 v-if="mongoCreateIndexLoading" class="mr-2 h-4 w-4 animate-spin" />
{{ mongoIndexManagerMode === "edit" ? t("contextMenu.saveMongoIndex") : t("contextMenu.createMongoIndex") }}
</Button>
</div>
</div>
<div v-else class="grid min-w-0 gap-3">
<p v-if="!mongoIndexManagerSelected" class="text-sm text-muted-foreground">{{ t("contextMenu.mongoIndexSelectHint") }}</p>
<template v-else>
<div class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("structureEditor.indexName") }}</span>
<span class="min-w-0 break-all font-mono text-sm">{{ mongoIndexManagerSelected.name }}</span>
</div>
<div class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("contextMenu.mongoIndexKeys") }}</span>
<span class="min-w-0 break-all font-mono text-sm">{{ mongoIndexManagerSelected.keys }}</span>
</div>
<!-- MongoDB has no ALTER INDEX, so existing properties stay read-only. -->
<div v-if="mongoIndexManagerMode !== 'edit'" class="flex min-w-0 flex-wrap gap-x-6 gap-y-2">
<label class="flex w-fit items-center gap-2 text-sm text-muted-foreground">
<Switch :model-value="mongoIndexManagerSelected.isUnique" disabled :aria-label="t('structureEditor.unique')" />
{{ t("structureEditor.unique") }}
</label>
<template v-if="mongoIndexManagerSelected.propertiesComplete">
<label class="flex w-fit items-center gap-2 text-sm text-muted-foreground">
<Switch :model-value="mongoIndexManagerSelected.isSparse" disabled :aria-label="t('mongo.indexSparse')" />
{{ t("mongo.indexSparse") }}
</label>
<label class="flex w-fit items-center gap-2 text-sm text-muted-foreground">
<Switch :model-value="mongoIndexManagerSelected.background" disabled :aria-label="t('mongo.indexBackground')" />
{{ t("mongo.indexBackground") }}
</label>
<label v-if="mongoIndexManagerSelected.hidden" class="flex w-fit items-center gap-2 text-sm text-muted-foreground">
<Switch :model-value="true" disabled :aria-label="t('mongo.indexHidden')" />
{{ t("mongo.indexHidden") }}
</label>
</template>
</div>
<template v-if="mongoIndexManagerSelected.propertiesComplete">
<div class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("mongo.indexExpireAfterSeconds") }}</span>
<span class="min-w-0 break-all font-mono text-sm">{{ mongoIndexManagerSelected.expireAfterSeconds ?? t("mongo.indexNoExpiry") }}</span>
</div>
<div v-if="mongoIndexManagerSelected.bucketSize !== undefined" class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("mongo.indexBucketSize") }}</span>
<span class="min-w-0 break-all font-mono text-sm">{{ mongoIndexManagerSelected.bucketSize }}</span>
</div>
</template>
<div v-if="mongoIndexManagerSelected.partialFilterExpression" class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("mongo.indexPartialFilter") }}</span>
<span class="min-w-0 whitespace-pre-wrap break-all font-mono text-xs">{{ mongoIndexManagerSelected.partialFilterExpression }}</span>
</div>
<div v-if="mongoIndexManagerSelected.extraOptions" class="grid min-w-0 gap-1.5">
<span class="text-xs text-muted-foreground">{{ t("mongo.indexOtherOptions") }}</span>
<span class="min-w-0 whitespace-pre-wrap break-all font-mono text-xs">{{ mongoIndexManagerSelected.extraOptions }}</span>
</div>
<!-- The Legacy driver cannot report sparse/TTL/background, so say so
instead of rendering the defaults as if the server had sent them. -->
<p v-if="!mongoIndexManagerSelected.propertiesComplete" class="text-xs text-muted-foreground">{{ t("mongo.indexPropertiesUnavailable") }}</p>
<p v-if="mongoIndexManagerMode !== 'edit'" class="text-xs text-muted-foreground">{{ t("contextMenu.mongoIndexReadOnlyHint") }}</p>
</template>
</div>
</section>
</DialogContent>
</Dialog>
</template>
@@ -7,6 +7,7 @@ import { Input } from "@/components/ui/input";
import { SearchableSelect } from "@/components/ui/searchable-select";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { SidebarMongoIndexManagerDialog } from "./sidebarAsyncDialogs";
const props = defineProps<{ controller: Record<string, any> }>();
const emit = defineEmits<{ closed: [] }>();
@@ -112,6 +113,7 @@ const {
addMongoCreateIndexField,
removeMongoCreateIndexField,
confirmCreateMongoIndex,
showMongoIndexManagerDialog,
showRedisDatabaseAliasDialog,
redisDatabaseAliasInput,
redisDatabaseAliasSaving,
@@ -183,6 +185,7 @@ watch(
showRenameMongoCollectionDialog,
showCloneMongoCollectionDialog,
showCreateMongoIndexDialog,
showMongoIndexManagerDialog,
showRedisDatabaseAliasDialog,
showCreateSchemaDialog,
showEditSchemaCommentDialog,
@@ -755,4 +758,6 @@ watch(
</DialogFooter>
</DialogContent>
</Dialog>
<SidebarMongoIndexManagerDialog v-if="showMongoIndexManagerDialog" :controller="controller" />
</template>
@@ -258,6 +258,13 @@ import {
mongoCreateIndexFieldOptions,
mongoCreateIndexError,
mongoCreateIndexLoading,
showMongoIndexManagerDialog,
mongoIndexManagerRows,
mongoIndexManagerLoading,
mongoIndexManagerError,
mongoIndexManagerSelectedName,
mongoIndexManagerMode,
mongoEditIndexOriginalName,
showFlushRedisDbConfirm,
showCreateSchemaDialog,
createSchemaName,
@@ -444,6 +451,19 @@ const {
addMongoCreateIndexField,
removeMongoCreateIndexField,
confirmCreateMongoIndex,
canManageMongoIndexes,
prepareMongoIndexManagerDialog,
loadMongoIndexManagerRows,
mongoIndexManagerSelected,
mongoIndexManagerCollectionName,
selectMongoIndexRow,
startCreateMongoIndexDraft,
startEditMongoIndexDraft,
cancelMongoIndexDraft,
dropSelectedMongoIndexRow,
canDropSelectedMongoIndexRow,
canEditSelectedMongoIndexRow,
confirmEditMongoIndex,
openCreateNacosNamespaceDialog,
confirmCreateNacosNamespace,
openEditNacosNamespaceDialog,
@@ -1136,6 +1156,12 @@ function openCreateMongoIndexDialog() {
prepareCreateMongoIndexDialog();
}
function openMongoIndexManagerDialog() {
claimTreeItemDialogOwnership();
routeTreeItemDialogController();
prepareMongoIndexManagerDialog();
}
function openRedisDatabaseAliasDialog() {
claimTreeItemDialogOwnership();
routeTreeItemDialogController();
@@ -4190,6 +4216,24 @@ function databaseSpecificDialogCapabilities() {
addMongoCreateIndexField,
removeMongoCreateIndexField,
confirmCreateMongoIndex,
showMongoIndexManagerDialog,
mongoIndexManagerRows,
mongoIndexManagerLoading,
mongoIndexManagerError,
mongoIndexManagerSelectedName,
mongoIndexManagerMode,
mongoIndexManagerSelected,
mongoIndexManagerCollectionName,
selectMongoIndexRow,
startCreateMongoIndexDraft,
startEditMongoIndexDraft,
cancelMongoIndexDraft,
dropSelectedMongoIndexRow,
canDropSelectedMongoIndexRow,
canEditSelectedMongoIndexRow,
confirmEditMongoIndex,
mongoEditIndexOriginalName,
loadMongoIndexManagerRows,
showRedisDatabaseAliasDialog,
redisDatabaseAliasInput,
redisDatabaseAliasSaving,
@@ -4797,6 +4841,11 @@ function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean {
items.push({ label: "", separator: true });
items.push({ label: t("contextMenu.viewData"), action: toggle, icon: TableProperties });
items.push({ label: t("contextMenu.newQuery"), action: newQuery, icon: TerminalSquare });
// Creating and dropping indexes stay on the Indexes group node; the collection
// only opens the manager panel, which offers creation from inside itself.
if (canManageMongoIndexes.value) {
items.push({ label: t("contextMenu.manageMongoIndexes"), action: openMongoIndexManagerDialog, icon: PencilRuler });
}
if (canRenameMongoCollection.value) {
items.push({
label: t("contextMenu.renameObject"),
@@ -23,3 +23,4 @@ export const SidebarVisibleSchemasDialog = lazySidebarDialog(() => import("@/com
export const SidebarDdlViewDialog = lazySidebarDialog(() => import("@/components/objects/DdlViewDialog.vue"));
export const SidebarObjectSourceDialog = lazySidebarDialog(() => import("@/components/objects/ObjectSourceDialog.vue"));
export const SidebarProcedureExecutionDialog = lazySidebarDialog(() => import("@/components/objects/ProcedureExecutionDialog.vue"));
export const SidebarMongoIndexManagerDialog = lazySidebarDialog(() => import("@/components/sidebar/MongoIndexManagerDialog.vue"));
@@ -4,7 +4,7 @@ import type { PasteTableMode } from "@/lib/table/tableClipboard";
import { fallbackCreateDatabaseCharsetMetadata } from "@/lib/database/createDatabaseCharsetOptions";
import type { DatabaseUserIdentity } from "@/lib/database/databaseUserAdmin";
import type { AuthorizationPlan, AuthorizationStepResult } from "@/lib/database/databaseAuthorizationPlan";
import type { MongoCreateIndexForm } from "@/lib/sidebar/mongoCollectionMutation";
import type { MongoCreateIndexForm, MongoIndexRow } from "@/lib/sidebar/mongoCollectionMutation";
export type DuplicateStructureSource = TreeNode & { connectionId: string; database: string };
type ConnectionDeleteTarget = TreeNode & { connectionId: string };
@@ -106,17 +106,50 @@ export const dropMongoIndexLoading = ref(false);
export const showDropAllMongoIndexesConfirm = ref(false);
export const dropAllMongoIndexesLoading = ref(false);
export const showCreateMongoIndexDialog = ref(false);
export const mongoCreateIndexForm = ref<MongoCreateIndexForm>({ name: "", fields: [{ id: 1, path: "", type: "1" }], unique: false, sparse: false });
function emptyMongoCreateIndexForm(): MongoCreateIndexForm {
return {
name: "",
fields: [{ id: 1, path: "", type: "1" }],
unique: false,
sparse: false,
expireAfterSeconds: "",
partialFilterExpression: "",
background: false,
bucketSize: "",
hidden: false,
};
}
export const mongoCreateIndexForm = ref<MongoCreateIndexForm>(emptyMongoCreateIndexForm());
export const mongoCreateIndexFieldOptions = ref<string[]>([]);
export const mongoCreateIndexError = ref("");
export const mongoCreateIndexLoading = ref(false);
export function resetMongoCreateIndexForm() {
mongoCreateIndexForm.value = { name: "", fields: [{ id: 1, path: "", type: "1" }], unique: false, sparse: false };
mongoCreateIndexForm.value = emptyMongoCreateIndexForm();
mongoCreateIndexFieldOptions.value = [];
mongoCreateIndexError.value = "";
mongoCreateIndexLoading.value = false;
}
export const showMongoIndexManagerDialog = ref(false);
export const mongoIndexManagerRows = ref<MongoIndexRow[]>([]);
export const mongoIndexManagerLoading = ref(false);
export const mongoIndexManagerError = ref("");
export const mongoIndexManagerSelectedName = ref("");
export const mongoIndexManagerMode = ref<"view" | "create" | "edit">("view");
/** Name of the index being edited, so the confirm step knows which one to drop. */
export const mongoEditIndexOriginalName = ref("");
export function resetMongoIndexManager() {
mongoIndexManagerRows.value = [];
mongoIndexManagerLoading.value = false;
mongoIndexManagerError.value = "";
mongoIndexManagerSelectedName.value = "";
mongoIndexManagerMode.value = "view";
mongoEditIndexOriginalName.value = "";
}
export const showFlushRedisDbConfirm = ref(false);
export const showRedisDatabaseAliasDialog = ref(false);
export const redisDatabaseAliasInput = ref("");
@@ -165,6 +198,7 @@ const openFlags = [
showDropMongoIndexConfirm,
showDropAllMongoIndexesConfirm,
showCreateMongoIndexDialog,
showMongoIndexManagerDialog,
showFlushRedisDbConfirm,
showRedisDatabaseAliasDialog,
showCreateSchemaDialog,
@@ -190,6 +224,7 @@ export function resetSidebarTreeDialogState() {
cloneMongoCollectionError.value = "";
cloneMongoCollectionLoading.value = false;
resetMongoCreateIndexForm();
resetMongoIndexManager();
sidebarTreeDialogOwner.value = null;
sidebarDangerTarget.value = null;
sidebarFormTarget.value = null;
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { shallowRef } from "vue";
import { nextTick, shallowRef } from "vue";
import type { TreeNode } from "@/types/database";
import {
cloneMongoCollectionError,
@@ -8,19 +8,28 @@ import {
cloneMongoCollectionName,
mongoCreateIndexError,
mongoCreateIndexForm,
mongoEditIndexOriginalName,
mongoIndexManagerError,
mongoIndexManagerLoading,
mongoIndexManagerMode,
mongoIndexManagerRows,
mongoIndexManagerSelectedName,
resetMongoCreateIndexForm,
resetMongoIndexManager,
sidebarDangerTarget,
sidebarFormTarget,
showCloneMongoCollectionDialog,
showCreateMongoIndexDialog,
showDropAllMongoIndexesConfirm,
showDropMongoCollectionConfirm,
showMongoIndexManagerDialog,
} from "@/components/sidebar/sidebarTreeDialogState";
const mocks = vi.hoisted(() => ({
toast: vi.fn(),
ensureConnected: vi.fn().mockResolvedValue(undefined),
loadIndexes: vi.fn().mockResolvedValue(undefined),
mongoListIndexSpecs: vi.fn().mockResolvedValue([]),
listMongoCompletionFields: vi.fn().mockResolvedValue([{ name: "email", type: "string" }]),
loadMongoCollections: vi.fn().mockResolvedValue(undefined),
loadMongoDatabases: vi.fn().mockResolvedValue(undefined),
@@ -48,6 +57,7 @@ vi.mock("@/stores/connectionStore", () => ({
}));
vi.mock("@/lib/backend/api", () => ({
mongoListIndexSpecs: (...args: unknown[]) => mocks.mongoListIndexSpecs(...args),
mongoCreateIndex: (...args: unknown[]) => mocks.mongoCreateIndex(...args),
mongoCloneCollection: (...args: unknown[]) => mocks.mongoCloneCollection(...args),
mongoDropCollection: (...args: unknown[]) => mocks.mongoDropCollection(...args),
@@ -129,6 +139,13 @@ function mongoIndexNode(name: string, kind: "collection" | "view" | "timeseries"
};
}
/** Let the panel's fire-and-forget index load settle before asserting. */
async function flush(): Promise<void> {
await Promise.resolve();
await Promise.resolve();
await nextTick();
}
function runtime(activeNode: TreeNode) {
const indexesGroup = activeNode.type === "group-indexes" ? activeNode : mongoIndexesGroupNode();
return useSidebarDatabaseSpecificMutationRuntime({
@@ -153,6 +170,23 @@ describe("MongoDB sidebar mutation runtime", () => {
mocks.getConfig.mockReturnValue(mongoConfig());
mocks.ensureConnected.mockResolvedValue(undefined);
mocks.loadIndexes.mockResolvedValue(undefined);
mocks.mongoListIndexSpecs.mockResolvedValue([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
{
name: "email_1",
keys: [{ field: "email", direction: "1" }],
is_unique: false,
is_primary: false,
is_sparse: true,
expire_after_seconds: 3600,
partial_filter_expression: '{"archived":false}',
background: false,
bucket_size: null,
hidden: false,
properties_complete: true,
extra_options: null,
},
]);
mocks.listMongoCompletionFields.mockResolvedValue([{ name: "email", type: "string" }]);
mocks.loadMongoCollections.mockResolvedValue(undefined);
mocks.loadMongoDatabases.mockResolvedValue(undefined);
@@ -167,7 +201,9 @@ describe("MongoDB sidebar mutation runtime", () => {
showCloneMongoCollectionDialog.value = false;
showDropAllMongoIndexesConfirm.value = false;
showDropMongoCollectionConfirm.value = false;
showMongoIndexManagerDialog.value = false;
resetMongoCreateIndexForm();
resetMongoIndexManager();
cloneMongoCollectionName.value = "";
cloneMongoCollectionError.value = "";
cloneMongoCollectionLoading.value = false;
@@ -306,6 +342,10 @@ describe("MongoDB sidebar mutation runtime", () => {
fields: [{ id: 7, path: "location", type: "2dsphere" }],
unique: true,
sparse: true,
expireAfterSeconds: "600",
partialFilterExpression: '{"active":true}',
background: true,
bucketSize: "32",
};
mongoCreateIndexError.value = "previous failure";
@@ -316,6 +356,11 @@ describe("MongoDB sidebar mutation runtime", () => {
fields: [{ id: 1, path: "", type: "1" }],
unique: false,
sparse: false,
expireAfterSeconds: "",
partialFilterExpression: "",
background: false,
bucketSize: "",
hidden: false,
});
expect(mongoCreateIndexError.value).toBe("");
expect(showCreateMongoIndexDialog.value).toBe(true);
@@ -333,6 +378,446 @@ describe("MongoDB sidebar mutation runtime", () => {
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
});
it("opens the index manager panel from a collection node and lists its indexes", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
expect(feature.canManageMongoIndexes.value).toBe(true);
feature.prepareMongoIndexManagerDialog();
await flush();
expect(showMongoIndexManagerDialog.value).toBe(true);
expect(mocks.mongoListIndexSpecs).toHaveBeenCalledWith("conn-1", "app", "users");
expect(mongoIndexManagerRows.value).toEqual([
{
name: "_id_",
keys: "_id ASC",
isUnique: true,
isProtected: true,
isSparse: false,
expireAfterSeconds: undefined,
partialFilterExpression: undefined,
background: false,
bucketSize: undefined,
hidden: false,
propertiesComplete: true,
extraOptions: undefined,
},
{
name: "email_1",
keys: "email ASC",
isUnique: false,
isProtected: false,
isSparse: true,
expireAfterSeconds: 3600,
partialFilterExpression: '{"archived":false}',
background: false,
bucketSize: undefined,
hidden: false,
propertiesComplete: true,
extraOptions: undefined,
},
]);
// The first row is auto-selected so the property pane is never blank.
expect(mongoIndexManagerSelectedName.value).toBe("_id_");
expect(feature.canDropSelectedMongoIndexRow.value).toBe(false);
});
it("creates an index from the panel opened on a collection node and returns to view mode", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.startCreateMongoIndexDraft();
expect(mongoIndexManagerMode.value).toBe("create");
mongoCreateIndexForm.value = { ...mongoCreateIndexForm.value, fields: [{ id: 1, path: "email", type: "1" }], expireAfterSeconds: "3600" };
await feature.confirmCreateMongoIndex();
await flush();
expect(mocks.mongoCreateIndex).toHaveBeenCalledWith("conn-1", "app", "users", '{"email":1}', '{"expireAfterSeconds":3600}');
// The panel stays open, drops back to read-only, and selects the new index.
expect(showMongoIndexManagerDialog.value).toBe(true);
expect(mongoIndexManagerMode.value).toBe("view");
expect(mongoIndexManagerSelectedName.value).toBe("email_1");
});
it("drops the selected index from the panel while protecting the default index", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
expect(feature.canDropSelectedMongoIndexRow.value).toBe(true);
await feature.dropSelectedMongoIndexRow();
expect(mocks.mongoDropIndexes).toHaveBeenCalledWith("conn-1", "app", "users", '"email_1"', true);
feature.selectMongoIndexRow("_id_");
mocks.mongoDropIndexes.mockClear();
await feature.dropSelectedMongoIndexRow();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
});
it("edits the selected index by dropping then creating and returns to view mode", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
expect(feature.canEditSelectedMongoIndexRow.value).toBe(true);
feature.startEditMongoIndexDraft();
expect(mongoIndexManagerMode.value).toBe("edit");
expect(mongoEditIndexOriginalName.value).toBe("email_1");
// The form is prefilled from the selected row.
expect(mongoCreateIndexForm.value.name).toBe("email_1");
expect(mongoCreateIndexForm.value.fields).toEqual([{ id: 1, path: "email", type: "1" }]);
expect(mongoCreateIndexForm.value.sparse).toBe(true);
expect(mongoCreateIndexForm.value.expireAfterSeconds).toBe("3600");
// Rename: create new first, then drop old.
mongoCreateIndexForm.value.name = "email_renamed";
await feature.confirmEditMongoIndex();
await flush();
// Re-read happens before any mutation; then create(new) before drop(old).
expect(mocks.mongoListIndexSpecs).toHaveBeenCalledWith("conn-1", "app", "users");
expect(mocks.mongoCreateIndex).toHaveBeenCalledWith("conn-1", "app", "users", '{"email":1}', '{"name":"email_renamed","sparse":true,"expireAfterSeconds":3600,"partialFilterExpression":{"archived":false}}');
expect(mocks.mongoDropIndexes).toHaveBeenCalledWith("conn-1", "app", "users", '"email_1"', true);
expect(mongoIndexManagerMode.value).toBe("view");
expect(mongoEditIndexOriginalName.value).toBe("");
expect(mongoIndexManagerSelectedName.value).toBe("email_1");
});
it("does not edit the protected _id index", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("_id_");
expect(feature.canEditSelectedMongoIndexRow.value).toBe(false);
feature.startEditMongoIndexDraft();
expect(mongoIndexManagerMode.value).toBe("view");
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
});
it("does not edit or rename an index without a complete server specification", async () => {
mocks.mongoListIndexSpecs.mockResolvedValueOnce([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
{
name: "email_1",
keys: [{ field: "email", direction: "1" }],
is_unique: false,
is_primary: false,
is_sparse: false,
expire_after_seconds: null,
partial_filter_expression: null,
background: false,
bucket_size: null,
hidden: false,
properties_complete: false,
extra_options: null,
},
]);
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
expect(feature.canEditSelectedMongoIndexRow.value).toBe(false);
feature.startEditMongoIndexDraft();
expect(mongoIndexManagerMode.value).toBe("view");
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
});
it("creates the new index first when renaming and does not drop if create fails", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.name = "email_v2";
mocks.mongoCreateIndex.mockRejectedValueOnce(new Error("duplicate key"));
await feature.confirmEditMongoIndex();
await flush();
// Create was attempted; drop was never called because create failed.
expect(mocks.mongoCreateIndex).toHaveBeenCalled();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
// The error surfaces in the panel.
expect(mongoCreateIndexError.value).toContain("duplicate key");
});
it("restores the complete original specification when a same-name rebuild fails", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.unique = true;
mocks.mongoCreateIndex.mockRejectedValueOnce(new Error("index build failed")).mockResolvedValueOnce({ name: "email_1" });
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoDropIndexes).toHaveBeenCalledWith("conn-1", "app", "users", '"email_1"', true);
expect(mocks.mongoCreateIndex).toHaveBeenNthCalledWith(1, "conn-1", "app", "users", '{"email":1}', '{"name":"email_1","unique":true,"sparse":true,"expireAfterSeconds":3600,"partialFilterExpression":{"archived":false}}');
expect(mocks.mongoCreateIndex).toHaveBeenNthCalledWith(2, "conn-1", "app", "users", '{"email":1}', '{"name":"email_1","sparse":true,"expireAfterSeconds":3600,"partialFilterExpression":{"archived":false}}');
expect(mongoCreateIndexError.value).toContain("email_1");
expect(mongoCreateIndexError.value).toContain("index build failed");
expect(mongoCreateIndexError.value).toContain("mongoEditIndexCreateFailedRolledBack");
});
it("reports both replacement and rollback errors when restoring a same-name index fails", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.unique = true;
mocks.mongoCreateIndex.mockRejectedValueOnce(new Error("replacement failed")).mockRejectedValueOnce(new Error("rollback failed"));
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoCreateIndex).toHaveBeenCalledTimes(2);
expect(mongoCreateIndexError.value).toContain("replacement failed");
expect(mongoCreateIndexError.value).toContain("rollback failed");
});
it("aborts the edit when the index no longer exists on the server", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
// Simulate the index being deleted by another session before confirm.
mocks.mongoListIndexSpecs.mockResolvedValueOnce([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
]);
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
expect(mongoCreateIndexError.value).toContain("email_1");
});
it("aborts the edit when the index keys changed since the dialog was opened", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
// Simulate the index being modified by another session (direction changed).
mocks.mongoListIndexSpecs.mockResolvedValueOnce([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
{ name: "email_1", keys: [{ field: "email", direction: "-1" }], is_unique: false, is_primary: false, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
]);
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
expect(mongoCreateIndexError.value).toContain("email_1");
});
it("aborts the edit when the keys match but the complete server options changed", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mocks.mongoListIndexSpecs.mockResolvedValueOnce([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
{
name: "email_1",
keys: [{ field: "email", direction: "1" }],
is_unique: true,
is_primary: false,
is_sparse: true,
expire_after_seconds: 3600,
partial_filter_expression: '{"archived":false}',
background: false,
bucket_size: null,
hidden: true,
properties_complete: true,
extra_options: '{"collation":{"locale":"fr"}}',
},
]);
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoDropIndexes).not.toHaveBeenCalled();
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
expect(mongoCreateIndexError.value).toContain("mongoEditIndexStale");
});
it("does not report a rename as successful when drop returns structured failures", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.name = "email_v2";
mocks.mongoCreateIndex.mockResolvedValueOnce({ name: "email_v2" });
mocks.mongoDropIndexes.mockResolvedValueOnce({
dropped_names: [],
affected_rows: 0,
failures: [{ name: "email_1", message: "not authorized" }],
});
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoCreateIndex).toHaveBeenCalledOnce();
expect(mocks.mongoDropIndexes).toHaveBeenCalledOnce();
expect(mongoIndexManagerMode.value).toBe("edit");
expect(mongoCreateIndexError.value).toContain("not authorized");
expect(mongoCreateIndexError.value).toContain("mongoEditIndexDropFailedAfterCreate");
expect(mocks.toast).not.toHaveBeenCalledWith(expect.stringContaining("mongoEditIndexSuccess"), 3000);
});
it("does not create or report rollback when a same-name drop returns structured failures", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("email_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.unique = true;
mocks.mongoDropIndexes.mockResolvedValueOnce({
dropped_names: [],
affected_rows: 0,
failures: [{ name: "email_1", message: "index is busy" }],
});
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoCreateIndex).not.toHaveBeenCalled();
expect(mongoIndexManagerMode.value).toBe("edit");
expect(mongoCreateIndexError.value).toContain("index is busy");
expect(mongoCreateIndexError.value).toContain("mongoEditIndexDropFailed");
expect(mongoCreateIndexError.value).not.toContain("mongoEditIndexCreateFailedRolledBack");
});
it("preserves extra options (collation, wildcardProjection) through the edit", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
// Seed the list with an index that has extraOptions.
mocks.mongoListIndexSpecs.mockReturnValue([
{ name: "_id_", keys: [{ field: "_id", direction: "1" }], is_unique: true, is_primary: true, is_sparse: false, expire_after_seconds: null, partial_filter_expression: null, background: false, bucket_size: null, hidden: false, properties_complete: true, extra_options: null },
{
name: "wild_1",
keys: [{ field: "x", direction: "1" }],
is_unique: false,
is_primary: false,
is_sparse: false,
expire_after_seconds: null,
partial_filter_expression: null,
background: false,
bucket_size: null,
hidden: false,
properties_complete: true,
extra_options: '{"wildcardProjection":{"x":1}}',
},
]);
feature.prepareMongoIndexManagerDialog();
await flush();
feature.selectMongoIndexRow("wild_1");
feature.startEditMongoIndexDraft();
mongoCreateIndexForm.value.name = "wild_v2";
await feature.confirmEditMongoIndex();
await flush();
expect(mocks.mongoCreateIndex).toHaveBeenCalled();
const createArgs = mocks.mongoCreateIndex.mock.calls[0];
const optionsJson = createArgs[4];
expect(optionsJson).toContain("wildcardProjection");
});
it("surfaces an index-list failure in the panel instead of showing a stale list", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
sidebarFormTarget.value = node;
mocks.mongoListIndexSpecs.mockRejectedValueOnce(new Error("listIndexes failed"));
feature.prepareMongoIndexManagerDialog();
await flush();
expect(mongoIndexManagerRows.value).toEqual([]);
expect(mongoIndexManagerSelectedName.value).toBe("");
expect(mongoIndexManagerError.value).toContain("listIndexes failed");
expect(mongoIndexManagerLoading.value).toBe(false);
});
it("does not open the index manager panel for read-only MongoDB connections", () => {
mocks.getConfig.mockReturnValue({ ...mongoConfig(), read_only: true });
const node = mongoCollectionNode();
const feature = runtime(node);
feature.prepareMongoIndexManagerDialog();
expect(showMongoIndexManagerDialog.value).toBe(false);
expect(mocks.mongoListIndexSpecs).not.toHaveBeenCalled();
});
it("does not clear indexes through a collection node", async () => {
const node = mongoCollectionNode();
const feature = runtime(node);
@@ -13,8 +13,11 @@ import {
isCloneableMongoCollection,
isProtectedMongoIndex,
isRenamableMongoCollection,
mergeExtraOptionsIntoRequest,
mongoCollectionKindFromNode,
mongoCloneCollectionPreview,
mongoCreateIndexRequestFromSpec,
mongoCreateIndexFormFromRow,
mongoCreateIndexPreview,
mongoDropAllIndexesPreview,
mongoDropCollectionPreview,
@@ -22,6 +25,12 @@ import {
mongoDropDatabasePreview,
mongoDropIndexPreview,
mongoRenameCollectionPreview,
mongoReplaceIndexPreview,
preflightEditIndexSpec,
snapshotMongoIndexSpec,
toMongoIndexRow,
type MongoCreateIndexRequest,
type MongoIndexSpecSnapshot,
} from "@/lib/sidebar/mongoCollectionMutation";
import { supportsMongoAllDriverMutations, supportsMongoIndexMutations, supportsNativeMongoDriverMutations } from "@/lib/mongo/mongoCapabilities";
import { runMongoSidebarMutation } from "@/lib/sidebar/runMongoSidebarMutation";
@@ -66,6 +75,14 @@ import {
mongoCreateIndexError,
mongoCreateIndexLoading,
resetMongoCreateIndexForm,
showMongoIndexManagerDialog,
mongoIndexManagerRows,
mongoIndexManagerLoading,
mongoIndexManagerError,
mongoIndexManagerSelectedName,
mongoIndexManagerMode,
mongoEditIndexOriginalName,
resetMongoIndexManager,
} from "@/components/sidebar/sidebarTreeDialogState";
interface SidebarDatabaseSpecificMutationRuntimeOptions {
@@ -84,6 +101,8 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
const { t } = useI18n();
const { toast } = useToast();
const { activeNode, connectionStore } = options;
let mongoIndexSpecsByName = new Map<string, MongoIndexSpecSnapshot>();
let mongoEditIndexOriginalSpec: MongoIndexSpecSnapshot | undefined;
function usesAnyMongoDriver(node: Pick<TreeNode, "connectionId">): boolean {
return !!node.connectionId && supportsMongoAllDriverMutations(connectionStore.getConfig(node.connectionId));
@@ -240,11 +259,25 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
return node.type === "group-indexes" ? node.tableName || "" : "";
}
/**
* The manager panel is also reachable from the collection node, while direct
* create / drop-all actions stay scoped to the Indexes group.
*/
function mongoIndexPanelCollectionName(node: TreeNode): string {
if (node.type === "mongo-collection") return node.label;
return mongoIndexCollectionName(node);
}
function canManageMongoIndexPanelNode(node: TreeNode): boolean {
return !!mongoIndexPanelCollectionName(node) && !!node.database && canMutateMongoIndexes(node);
}
function canManageMongoIndexesNode(node: TreeNode): boolean {
return !!mongoIndexCollectionName(node) && !!node.database && canMutateMongoIndexes(node);
}
const canDropAllMongoIndexes = computed(() => canManageMongoIndexesNode(activeNode.value));
const canManageMongoIndexes = computed(() => canManageMongoIndexPanelNode(activeNode.value));
function mongoIndexDropPreview(node: Pick<TreeNode, "database" | "tableName">, indexName: string): string {
return mongoDropIndexPreview(node.database || "", node.tableName || "", indexName);
@@ -259,7 +292,8 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
}
const canCreateMongoIndex = computed(() => canCreateMongoIndexNode(activeNode.value));
const mongoCreateIndexCanSubmit = computed(() => mongoCreateIndexForm.value.fields.length > 0 && mongoCreateIndexForm.value.fields.every((field) => !!field.path.trim()));
/** Mirror the full request validation so the button never invites a known error. */
const mongoCreateIndexCanSubmit = computed(() => buildMongoCreateIndexRequest(mongoCreateIndexForm.value).valid);
const mongoCreateIndexCanAddField = computed(() => mongoCreateIndexForm.value.fields.every((field) => !!field.path.trim()));
watch(
@@ -286,6 +320,155 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
});
}
async function loadMongoIndexManagerRows() {
const node = sidebarFormTarget.value ?? activeNode.value;
const connectionId = node.connectionId;
const database = node.database;
const collection = mongoIndexPanelCollectionName(node);
if (!connectionId || !database || !collection) return;
mongoIndexManagerLoading.value = true;
mongoIndexManagerError.value = "";
try {
await connectionStore.ensureConnected(connectionId);
// MongoDB-specific read: the shared IndexInfo shape cannot carry sparse,
// TTL, background or bucketSize. The backend degrades to the generic
// listing for Legacy Agent connections and flags it there.
const specs = await api.mongoListIndexSpecs(connectionId, database, collection);
mongoIndexSpecsByName = new Map(specs.map((spec) => [spec.name, snapshotMongoIndexSpec(spec)] as const));
mongoIndexManagerRows.value = specs.map(toMongoIndexRow);
const selected = mongoIndexManagerSelectedName.value;
if (!selected || !mongoIndexManagerRows.value.some((row) => row.name === selected)) {
mongoIndexManagerSelectedName.value = mongoIndexManagerRows.value[0]?.name ?? "";
}
} catch (error) {
mongoIndexSpecsByName.clear();
mongoIndexManagerRows.value = [];
mongoIndexManagerSelectedName.value = "";
mongoIndexManagerError.value = translateBackendError(t, errorMessage(error));
} finally {
mongoIndexManagerLoading.value = false;
}
}
function prepareMongoIndexManagerDialog() {
const node = activeNode.value;
if (!canManageMongoIndexPanelNode(node) || !node.connectionId || !node.database) return;
resetMongoIndexManager();
resetMongoCreateIndexForm();
mongoIndexSpecsByName.clear();
mongoEditIndexOriginalSpec = undefined;
showMongoIndexManagerDialog.value = true;
void loadMongoIndexManagerRows();
}
const mongoIndexManagerSelected = computed(() => mongoIndexManagerRows.value.find((row) => row.name === mongoIndexManagerSelectedName.value));
/** Resolve against the dialog's own target so the header survives tree selection changes. */
const mongoIndexManagerCollectionName = computed(() => mongoIndexPanelCollectionName(sidebarFormTarget.value ?? activeNode.value));
function selectMongoIndexRow(name: string) {
if (mongoIndexManagerMode.value === "create" || mongoIndexManagerMode.value === "edit") return;
mongoIndexManagerSelectedName.value = name;
}
/** Switch the property pane into an editable draft for a brand new index. */
function startCreateMongoIndexDraft() {
const node = sidebarFormTarget.value ?? activeNode.value;
resetMongoCreateIndexForm();
mongoIndexManagerMode.value = "create";
mongoIndexManagerError.value = "";
if (!node.connectionId || !node.database) return;
const collection = mongoIndexPanelCollectionName(node);
if (!collection) return;
void connectionStore
.listMongoCompletionFields(node.connectionId, node.database, collection)
.then((fields) => {
if (showMongoIndexManagerDialog.value) mongoCreateIndexFieldOptions.value = fields.map((field) => field.name);
})
.catch(() => {
// MongoDB is schemaless; a field that was never sampled is still valid.
});
}
function cancelMongoIndexDraft() {
mongoIndexManagerMode.value = "view";
mongoEditIndexOriginalName.value = "";
mongoEditIndexOriginalSpec = undefined;
resetMongoCreateIndexForm();
}
/** The default _id index is never droppable, so the panel must not offer it. */
const canDropSelectedMongoIndexRow = computed(() => {
if (mongoIndexManagerMode.value === "create" || mongoIndexManagerMode.value === "edit") return false;
const row = mongoIndexManagerSelected.value;
return !!row && !row.isProtected;
});
/** Editing requires a complete server specification so no unmodeled options are lost. */
const canEditSelectedMongoIndexRow = computed(() => {
if (mongoIndexManagerMode.value === "create" || mongoIndexManagerMode.value === "edit") return false;
const row = mongoIndexManagerSelected.value;
return !!row && !row.isProtected && row.propertiesComplete;
});
/** Prefill the create form from the selected index row and enter edit mode. */
function startEditMongoIndexDraft() {
const node = sidebarFormTarget.value ?? activeNode.value;
const row = mongoIndexManagerSelected.value;
if (!canEditSelectedMongoIndexRow.value || !row || !node.connectionId || !node.database) return;
const originalSpec = mongoIndexSpecsByName.get(row.name);
if (!originalSpec) return;
mongoCreateIndexForm.value = mongoCreateIndexFormFromRow(row);
mongoEditIndexOriginalName.value = row.name;
mongoEditIndexOriginalSpec = originalSpec;
mongoIndexManagerMode.value = "edit";
mongoIndexManagerError.value = "";
const collection = mongoIndexPanelCollectionName(node);
if (!collection) return;
void connectionStore
.listMongoCompletionFields(node.connectionId, node.database, collection)
.then((fields) => {
if (showMongoIndexManagerDialog.value) mongoCreateIndexFieldOptions.value = fields.map((field) => field.name);
})
.catch(() => {
// MongoDB is schemaless; a field that was never sampled is still valid.
});
}
async function dropSelectedMongoIndexRow() {
const node = sidebarFormTarget.value ?? activeNode.value;
const connectionId = node.connectionId;
const database = node.database;
const collection = mongoIndexPanelCollectionName(node);
const row = mongoIndexManagerSelected.value;
if (!canDropSelectedMongoIndexRow.value || !row || !connectionId || !database || !collection) return;
await runMongoSidebarMutation({
connection: connectionStore.getConfig(connectionId),
database,
reviewText: mongoDropIndexPreview(database, collection, row.name),
source: t("production.sourceSidebar"),
loading: mongoIndexManagerLoading,
beforeExecute: () => connectionStore.ensureConnected(connectionId),
execute: () => api.mongoDropIndexes(connectionId, database, collection, JSON.stringify(row.name), true),
onSuccess: async (result) => {
const failed = mongoDropIndexFailureCount(result);
if (failed > 0) {
toast(t("contextMenu.dropIndexesPartialFailure", { success: result.dropped_names.length, failed }), 5000);
} else {
toast(t("contextMenu.dropTableChildObjectSuccess", { name: row.name }), 3000);
}
mongoIndexManagerSelectedName.value = "";
await refreshMongoIndexTreeAfterMutation({ ...node, tableName: collection });
await loadMongoIndexManagerRows();
},
onError: (error) => {
mongoIndexManagerError.value = translateBackendError(t, errorMessage(error));
},
});
}
function addMongoCreateIndexField() {
if (!mongoCreateIndexCanAddField.value) return;
const nextId = Math.max(0, ...mongoCreateIndexForm.value.fields.map((field) => field.id)) + 1;
@@ -297,16 +480,34 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
mongoCreateIndexForm.value.fields = mongoCreateIndexForm.value.fields.filter((field) => field.id !== id);
}
function mongoCreateIndexRequestErrorText(request: Extract<MongoCreateIndexRequest, { valid: false }>): string {
switch (request.error) {
case "field-duplicate":
return t("mongo.duplicateField", { field: request.field });
case "ttl-invalid":
return t("contextMenu.createMongoIndexTtlInvalid");
case "filter-invalid":
return t("contextMenu.createMongoIndexFilterInvalid");
case "bucket-size-invalid":
return t("contextMenu.createMongoIndexBucketSizeInvalid");
default:
return t("contextMenu.createMongoIndexFieldRequired");
}
}
async function confirmCreateMongoIndex() {
const node = sidebarFormTarget.value ?? activeNode.value;
const connectionId = node.connectionId;
const database = node.database;
const collectionName = mongoIndexCollectionName(node);
if (!canCreateMongoIndexNode(node) || !connectionId || !database || !collectionName) return;
// When called from the manager panel the node may be a collection node;
// when called from the standalone create dialog it is always a group-indexes node.
const collectionName = showMongoIndexManagerDialog.value ? mongoIndexPanelCollectionName(node) : mongoIndexCollectionName(node);
const canProceed = showMongoIndexManagerDialog.value ? canManageMongoIndexPanelNode(node) : canCreateMongoIndexNode(node);
if (!canProceed || !connectionId || !database || !collectionName) return;
const request = buildMongoCreateIndexRequest(mongoCreateIndexForm.value);
if (!request.valid) {
mongoCreateIndexError.value = request.error === "field-duplicate" ? t("mongo.duplicateField", { field: request.field }) : t("contextMenu.createMongoIndexFieldRequired");
mongoCreateIndexError.value = mongoCreateIndexRequestErrorText(request);
return;
}
@@ -322,6 +523,126 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
onSuccess: async (created) => {
showCreateMongoIndexDialog.value = false;
toast(t("contextMenu.createMongoIndexSuccess", { name: created.name, collection: collectionName }), 3000);
// In the manager panel the dialog stays open: return the property pane to
// read-only and reload the list so the new index shows up selected.
if (showMongoIndexManagerDialog.value) {
mongoIndexManagerMode.value = "view";
mongoIndexManagerSelectedName.value = created.name;
resetMongoCreateIndexForm();
await loadMongoIndexManagerRows();
}
await refreshMongoIndexTreeAfterMutation({ ...node, tableName: collectionName });
},
onError: (error) => {
mongoCreateIndexError.value = translateBackendError(t, errorMessage(error));
},
});
}
/**
* Edit an existing index by drop + recreate. The form was prefilled by
* {@link startEditMongoIndexDraft}; this runs the two backend calls in
* sequence inside the production-gated mutation shell.
*
* Safety strategy:
* 1. Merge `extraOptions` (collation, wildcardProjection, weights, …) back
* into the create request so no server-reported option is silently lost.
* 2. Re-read the current index spec from the server before any destructive
* operation and compare it with the complete immutable opening snapshot.
* 3. When the user renamed the index, create the new one first, then drop
* the old one — if create fails the original index is untouched.
* 4. When the name is unchanged (same-name rebuild), preserve a complete
* rollback request and recreate the original index if the new build fails.
*/
async function confirmEditMongoIndex() {
const node = sidebarFormTarget.value ?? activeNode.value;
const connectionId = node.connectionId;
const database = node.database;
const collectionName = showMongoIndexManagerDialog.value ? mongoIndexPanelCollectionName(node) : mongoIndexCollectionName(node);
const originalName = mongoEditIndexOriginalName.value;
const originalSpec = mongoEditIndexOriginalSpec;
if (!connectionId || !database || !collectionName || !originalName || !originalSpec || mongoIndexManagerMode.value !== "edit") return;
const request = buildMongoCreateIndexRequest(mongoCreateIndexForm.value);
if (!request.valid) {
mongoCreateIndexError.value = mongoCreateIndexRequestErrorText(request);
return;
}
// Preserve server-reported options the form does not model (collation,
// wildcardProjection, weights, text defaults, geo options, …).
const merged = mergeExtraOptionsIntoRequest(request, originalSpec.extraOptions);
const newName = mongoCreateIndexForm.value.name.trim();
const isRename = newName && newName !== originalName;
if (!isRename && !originalSpec.propertiesComplete) {
mongoCreateIndexError.value = t("contextMenu.mongoEditIndexIncompleteSameName", { name: originalName });
return;
}
mongoCreateIndexError.value = "";
await runMongoSidebarMutation({
connection: connectionStore.getConfig(connectionId),
database,
// Review text shows both commands so production confirmation is explicit.
reviewText: mongoReplaceIndexPreview(database, collectionName, originalName, merged.keysJson, merged.optionsJson),
source: t("production.sourceSidebar"),
loading: mongoCreateIndexLoading,
beforeExecute: () => connectionStore.ensureConnected(connectionId),
execute: async () => {
// Re-read the current spec from the server to guard against concurrent
// modifications between when the dialog was opened and when the user
// confirmed the edit.
const specs = await api.mongoListIndexSpecs(connectionId, database, collectionName);
const preflight = preflightEditIndexSpec(specs, originalSpec);
if (!preflight.safe) {
throw new Error(preflight.reason === "not-found" ? t("contextMenu.mongoEditIndexNotFound", { name: originalName }) : t("contextMenu.mongoEditIndexStale", { name: originalName }));
}
if (isRename) {
// Safe rename: create the new index first. If create fails, the
// original index is untouched and the error surfaces cleanly.
const created = await api.mongoCreateIndex(connectionId, database, collectionName, merged.keysJson, merged.optionsJson);
const dropResult = await api.mongoDropIndexes(connectionId, database, collectionName, JSON.stringify(originalName), true);
if (mongoDropIndexFailureCount(dropResult) > 0) {
const error = dropResult.failures?.map((failure) => `${failure.name}: ${failure.message}`).join("; ") || originalName;
throw new Error(t("contextMenu.mongoEditIndexDropFailedAfterCreate", { oldName: originalName, newName: created.name, error }));
}
return created;
}
const rollbackRequest = mongoCreateIndexRequestFromSpec(originalSpec);
const dropResult = await api.mongoDropIndexes(connectionId, database, collectionName, JSON.stringify(originalName), true);
if (mongoDropIndexFailureCount(dropResult) > 0) {
const error = dropResult.failures?.map((failure) => `${failure.name}: ${failure.message}`).join("; ") || originalName;
throw new Error(t("contextMenu.mongoEditIndexDropFailed", { name: originalName, error }));
}
try {
return await api.mongoCreateIndex(connectionId, database, collectionName, merged.keysJson, merged.optionsJson);
} catch (createError) {
const createErrorMessage = errorMessage(createError);
try {
await api.mongoCreateIndex(connectionId, database, collectionName, rollbackRequest.keysJson, rollbackRequest.optionsJson);
} catch (rollbackError) {
throw new Error(
t("contextMenu.mongoEditIndexCreateAndRollbackFailed", {
name: originalName,
createError: createErrorMessage,
rollbackError: errorMessage(rollbackError),
}),
);
}
throw new Error(t("contextMenu.mongoEditIndexCreateFailedRolledBack", { name: originalName, error: createErrorMessage }));
}
},
onSuccess: async (created) => {
toast(t("contextMenu.mongoEditIndexSuccess", { oldName: originalName, newName: created.name, collection: collectionName }), 3000);
mongoIndexManagerMode.value = "view";
mongoEditIndexOriginalName.value = "";
mongoEditIndexOriginalSpec = undefined;
mongoIndexManagerSelectedName.value = created.name;
resetMongoCreateIndexForm();
await loadMongoIndexManagerRows();
await refreshMongoIndexTreeAfterMutation({ ...node, tableName: collectionName });
},
onError: (error) => {
@@ -640,6 +961,7 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
mongoDropAllIndexesPreview: mongoDropAllIndexesPreviewForNode,
refreshMongoIndexTreeAfterMutation,
canCreateMongoIndex,
canManageMongoIndexes,
mongoIndexKeyTypes: MONGO_INDEX_KEY_TYPES,
mongoCreateIndexCanSubmit,
mongoCreateIndexCanAddField,
@@ -647,6 +969,18 @@ export function useSidebarDatabaseSpecificMutationRuntime(options: SidebarDataba
addMongoCreateIndexField,
removeMongoCreateIndexField,
confirmCreateMongoIndex,
prepareMongoIndexManagerDialog,
loadMongoIndexManagerRows,
mongoIndexManagerSelected,
mongoIndexManagerCollectionName,
selectMongoIndexRow,
startCreateMongoIndexDraft,
startEditMongoIndexDraft,
cancelMongoIndexDraft,
dropSelectedMongoIndexRow,
canDropSelectedMongoIndexRow,
canEditSelectedMongoIndexRow,
confirmEditMongoIndex,
openCreateNacosNamespaceDialog,
confirmCreateNacosNamespace,
openEditNacosNamespaceDialog,
+34
View File
@@ -2726,6 +2726,27 @@ export default {
createMongoIndexNamePlaceholder: "Generated automatically",
createMongoIndexNameHint: "Leave blank to let MongoDB generate the name from the fields and types.",
createMongoIndexSparse: "Sparse",
createMongoIndexTtlInvalid: "Expire after must be a whole number of seconds.",
createMongoIndexFilterInvalid: 'Partial filter must be an object, for example { "status": "active" }.',
createMongoIndexBucketSizeInvalid: "Bucket size must be a whole number.",
manageMongoIndexes: "Manage Indexes",
manageMongoIndexesTitle: 'Indexes: "{collection}"',
mongoIndexKeys: "Keys",
mongoIndexEmpty: "This collection has no indexes yet.",
mongoIndexSelectHint: "Select an index to see its properties.",
mongoIndexReadOnlyHint: "MongoDB cannot alter an existing index. Drop it and create a new one to change its properties.",
editMongoIndex: "Edit Index",
saveMongoIndex: "Save",
mongoIndexRebuildHint: "Saving rebuilds the index: the old index is dropped and a new one is created.",
mongoEditIndexSuccess: 'Index "{oldName}" rebuilt as "{newName}" on "{collection}"',
mongoEditIndexNotFound: 'Index "{name}" no longer exists on the server. The list may be stale — refresh and try again.',
mongoEditIndexStale: 'Index "{name}" was modified by another session since the dialog was opened. Refresh the list and retry.',
mongoEditIndexIncompleteSameName: 'Index "{name}" does not have a complete server specification, so it cannot be rebuilt destructively with the same name.',
mongoEditIndexDropFailed: 'Index "{name}" could not be dropped; no replacement was created: {error}',
mongoEditIndexDropFailedAfterCreate: 'Index "{newName}" was created, but the original index "{oldName}" could not be dropped: {error}',
mongoEditIndexCreateFailedRolledBack: 'The replacement for index "{name}" could not be created, so the original index was restored: {error}',
mongoEditIndexCreateAndRollbackFailed: 'The replacement for index "{name}" could not be created ({createError}), and restoring the original index also failed ({rollbackError}).',
mongoIndexExtraOptionsPreserved: "Server-reported options (collation, wildcardProjection, weights, …) are preserved on rebuild.",
dropForeignKey: "Drop Foreign Key",
dropTrigger: "Drop Trigger",
batchDrop: "Drop selected ({count})",
@@ -4395,6 +4416,19 @@ export default {
unsupportedJsonNumber: "JSON contains a number outside MongoDB int64 range",
duplicateJsonKey: "Duplicate JSON key: {field}",
duplicateField: "Duplicate field name: {field}",
indexExpireAfterSeconds: "Expire after (seconds)",
indexExpireAfterSecondsPlaceholder: "Never",
indexExpireAfterSecondsHint: "Single-field indexes only. MongoDB removes documents once the indexed date passes this age.",
indexPartialFilter: "Partial filter",
indexLegacyOptions: "Compatibility options",
indexLegacyOptionsHint: "Only useful on older servers: background is ignored from MongoDB 4.2, and bucket size stopped applying when geoHaystack indexes were removed in 4.4.",
indexBackground: "Background",
indexBucketSize: "Bucket size",
indexSparse: "Sparse",
indexHidden: "Hidden",
indexNoExpiry: "Never",
indexOtherOptions: "Other options",
indexPropertiesUnavailable: "The MongoDB Legacy driver cannot report sparse, expiry, background or bucket size. Connect with the native driver to see them.",
edit: "Edit",
editModeFields: "Fields",
editModeJson: "JSON",
+34
View File
@@ -2673,6 +2673,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "Generado automáticamente",
createMongoIndexNameHint: "Déjalo vacío para que MongoDB genere el nombre a partir de los campos y tipos.",
createMongoIndexSparse: "Disperso",
createMongoIndexTtlInvalid: "El tiempo de expiración debe ser un número entero de segundos.",
createMongoIndexFilterInvalid: 'El filtro parcial debe ser un objeto, por ejemplo { "status": "active" }.',
createMongoIndexBucketSizeInvalid: "El tamaño del bucket debe ser un número entero.",
manageMongoIndexes: "Gestionar índices",
manageMongoIndexesTitle: 'Índices: "{collection}"',
mongoIndexKeys: "Claves",
mongoIndexEmpty: "Esta colección no tiene índices.",
mongoIndexSelectHint: "Selecciona un índice para ver sus propiedades.",
mongoIndexReadOnlyHint: "MongoDB no puede modificar un índice existente. Elimínalo y crea uno nuevo para cambiar sus propiedades.",
editMongoIndex: "Editar índice",
saveMongoIndex: "Guardar",
mongoIndexRebuildHint: "Guardar reconstruye el índice: se elimina el índice antiguo y se crea uno nuevo.",
mongoEditIndexSuccess: 'Índice "{oldName}" reconstruido como "{newName}" en "{collection}"',
mongoEditIndexNotFound: 'El índice "{name}" ya no existe en el servidor. La lista puede estar desactualizada; actualízala e inténtalo de nuevo.',
mongoEditIndexStale: 'El índice "{name}" fue modificado por otra sesión desde que se abrió el diálogo. Actualiza la lista e inténtalo de nuevo.',
mongoEditIndexIncompleteSameName: 'El índice "{name}" no tiene una especificación completa del servidor y no se puede reconstruir de forma destructiva con el mismo nombre.',
mongoEditIndexDropFailed: 'No se pudo eliminar el índice "{name}"; no se creó ningún reemplazo: {error}',
mongoEditIndexDropFailedAfterCreate: 'Se creó el índice "{newName}", pero no se pudo eliminar el índice original "{oldName}": {error}',
mongoEditIndexCreateFailedRolledBack: 'No se pudo crear el reemplazo del índice "{name}", por lo que se restauró el índice original: {error}',
mongoEditIndexCreateAndRollbackFailed: 'No se pudo crear el reemplazo del índice "{name}" ({createError}) y también falló la restauración del índice original ({rollbackError}).',
mongoIndexExtraOptionsPreserved: "Las opciones del servidor (collation, wildcardProjection, weights, …) se conservan al reconstruir.",
batchDropIndexes: "Eliminar índices ({count})",
confirmDropViewTitle: "Eliminar vista",
confirmDropViewMessage: '¿Seguro que deseas eliminar la vista "{name}"?',
@@ -4245,6 +4266,19 @@ export default withEnglishFallback({
nativeJsonHint: "Para los valores BSON, use JSON extendido de MongoDB, por ejemplo $oid, $date, $numberLong.",
invalidNativeJson: "Formato JSON nativo no válido: {error}",
nativeDocumentRequired: "El nivel superior del JSON nativo debe ser un objeto documento.",
indexExpireAfterSeconds: "Expirar después de (segundos)",
indexExpireAfterSecondsPlaceholder: "Nunca expira",
indexExpireAfterSecondsHint: "Solo índices de un solo campo. MongoDB elimina los documentos cuando la fecha indexada supera esta antigüedad.",
indexPartialFilter: "Filtro de índice parcial",
indexLegacyOptions: "Opciones de compatibilidad",
indexLegacyOptionsHint: "Solo relevante para versiones antiguas del servidor: MongoDB 4.2 ignora «background», y desde 4.4, tras eliminar los índices geoHaystack, «bucket size» ya no tiene efecto.",
indexBackground: "Background",
indexBucketSize: "Tamaño del bucket",
indexSparse: "Disperso",
indexHidden: "Oculto",
indexNoExpiry: "Nunca expira",
indexOtherOptions: "Otras opciones",
indexPropertiesUnavailable: "El controlador Legacy de MongoDB no puede informar disperso, expiración, background o tamaño de bucket. Conéctese con el controlador nativo para verlos.",
},
vector: {
collectionFallback: "colección",
+34
View File
@@ -2644,6 +2644,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "Generato automaticamente",
createMongoIndexNameHint: "Lascia vuoto per fare generare il nome a MongoDB dai campi e dai tipi.",
createMongoIndexSparse: "Sparso",
createMongoIndexTtlInvalid: "Il tempo di scadenza deve essere un numero intero di secondi.",
createMongoIndexFilterInvalid: 'Il filtro parziale deve essere un oggetto, ad esempio { "status": "active" }.',
createMongoIndexBucketSizeInvalid: "La dimensione del bucket deve essere un numero intero.",
manageMongoIndexes: "Gestisci indici",
manageMongoIndexesTitle: 'Indici: "{collection}"',
mongoIndexKeys: "Chiavi",
mongoIndexEmpty: "Questa raccolta non ha indici.",
mongoIndexSelectHint: "Seleziona un indice per vedere le sue proprietà.",
mongoIndexReadOnlyHint: "MongoDB non può modificare un indice esistente. Eliminalo e creane uno nuovo per cambiarne le proprietà.",
editMongoIndex: "Modifica indice",
saveMongoIndex: "Salva",
mongoIndexRebuildHint: "Il salvataggio ricostruisce l'indice: l'indice precedente viene eliminato e ne viene creato uno nuovo.",
mongoEditIndexSuccess: 'Indice "{oldName}" ricostruito come "{newName}" in "{collection}"',
mongoEditIndexNotFound: "L'indice \"{name}\" non esiste più sul server. L'elenco potrebbe non essere aggiornato; aggiorna e riprova.",
mongoEditIndexStale: "L'indice \"{name}\" è stato modificato da un'altra sessione da quando è stata aperta la finestra. Aggiorna l'elenco e riprova.",
mongoEditIndexIncompleteSameName: 'L\'indice "{name}" non dispone di una specifica completa del server e non può essere ricostruito in modo distruttivo con lo stesso nome.',
mongoEditIndexDropFailed: 'Impossibile eliminare l\'indice "{name}"; non è stato creato alcun sostituto: {error}',
mongoEditIndexDropFailedAfterCreate: 'L\'indice "{newName}" è stato creato, ma non è stato possibile eliminare l\'indice originale "{oldName}": {error}',
mongoEditIndexCreateFailedRolledBack: "Impossibile creare il sostituto dell'indice \"{name}\", quindi l'indice originale è stato ripristinato: {error}",
mongoEditIndexCreateAndRollbackFailed: "Impossibile creare il sostituto dell'indice \"{name}\" ({createError}) e anche il ripristino dell'indice originale non è riuscito ({rollbackError}).",
mongoIndexExtraOptionsPreserved: "Le opzioni del server (collation, wildcardProjection, weights, …) vengono preservate nella ricostruzione.",
dropForeignKey: "Elimina Chiave Esterna",
dropTrigger: "Elimina Trigger",
batchDrop: "Elimina selezionati ({count})",
@@ -4243,6 +4264,19 @@ export default withEnglishFallback({
nativeJsonHint: "Per i valori BSON, utilizzare MongoDB Extended JSON, ad esempio $oid, $date, $numberLong.",
invalidNativeJson: "Formato JSON nativo non valido: {error}",
nativeDocumentRequired: "Il livello superiore del JSON nativo deve essere un oggetto documento.",
indexExpireAfterSeconds: "Scadenza dopo (secondi)",
indexExpireAfterSecondsPlaceholder: "Mai",
indexExpireAfterSecondsHint: "Solo indici a campo singolo. MongoDB rimuove i documenti quando la data indicizzata supera questa età.",
indexPartialFilter: "Filtro parziale",
indexLegacyOptions: "Opzioni di compatibilità",
indexLegacyOptionsHint: "Rilevante solo per versioni vecchie del server: background è ignorato da MongoDB 4.2, e bucket size non si applica più dalla rimozione degli indici geoHaystack in 4.4.",
indexBackground: "Background",
indexBucketSize: "Dimensione bucket",
indexSparse: "Sparso",
indexHidden: "Nascosto",
indexNoExpiry: "Mai",
indexOtherOptions: "Altre opzioni",
indexPropertiesUnavailable: "Il driver Legacy di MongoDB non può riportare sparsità, scadenza, background o dimensione bucket. Connettersi con il driver nativo per vederli.",
},
vector: {
collectionFallback: "collezione",
+34
View File
@@ -2694,6 +2694,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "自動生成",
createMongoIndexNameHint: "空欄の場合、MongoDB がフィールドとタイプから名前を生成します。",
createMongoIndexSparse: "スパース",
createMongoIndexTtlInvalid: "有効期限は秒単位の整数で指定してください。",
createMongoIndexFilterInvalid: '部分フィルターはオブジェクトである必要があります。例: { "status": "active" }',
createMongoIndexBucketSizeInvalid: "バケットサイズは整数である必要があります。",
manageMongoIndexes: "インデックスを管理",
manageMongoIndexesTitle: 'インデックス: "{collection}"',
mongoIndexKeys: "キー",
mongoIndexEmpty: "このコレクションにはインデックスがありません。",
mongoIndexSelectHint: "インデックスを選択してプロパティを表示します。",
mongoIndexReadOnlyHint: "MongoDB は既存のインデックスを変更できません。プロパティを変更するには削除して作成し直してください。",
editMongoIndex: "インデックスを編集",
saveMongoIndex: "保存",
mongoIndexRebuildHint: "保存するとインデックスが再構築されます: 古いインデックスが削除され、新しいインデックスが作成されます。",
mongoEditIndexSuccess: "インデックス「{oldName}」を「{newName}」として「{collection}」に再構築しました",
mongoEditIndexNotFound: "インデックス「{name}」はサーバーに存在しません。リストが古い可能性があります。更新して再試行してください。",
mongoEditIndexStale: "インデックス「{name}」はダイアログを開いてから別のセッションによって変更されました。リストを更新して再試行してください。",
mongoEditIndexIncompleteSameName: "インデックス「{name}」の完全なサーバー仕様を取得できないため、同じ名前で破壊的に再構築できません。",
mongoEditIndexDropFailed: "インデックス「{name}」を削除できなかったため、置換インデックスは作成されませんでした:{error}",
mongoEditIndexDropFailedAfterCreate: "新しいインデックス「{newName}」は作成されましたが、元のインデックス「{oldName}」を削除できませんでした:{error}",
mongoEditIndexCreateFailedRolledBack: "インデックス「{name}」の置換を作成できなかったため、元のインデックスを復元しました:{error}",
mongoEditIndexCreateAndRollbackFailed: "インデックス「{name}」の置換作成に失敗し({createError})、元のインデックスの復元にも失敗しました({rollbackError})。",
mongoIndexExtraOptionsPreserved: "サーバーから報告されたオプション(collation、wildcardProjection、weights など)は再構築時に保持されます。",
batchDropIndexes: "インデックスを削除 ({count})",
confirmDropViewTitle: "ビューを削除",
confirmDropViewMessage: "本当にビュー「{name}」を削除しますか?",
@@ -4273,6 +4294,19 @@ export default withEnglishFallback({
nativeJsonHint: "BSON 値には MongoDB Extended JSON を使用してください。例: $oid、$date、$numberLong。",
invalidNativeJson: "ネイティブJSONの形式が無効です: {error}",
nativeDocumentRequired: "ネイティブJSONの最上位はドキュメントオブジェクトである必要があります。",
indexExpireAfterSeconds: "有効期限(秒)",
indexExpireAfterSecondsPlaceholder: "期限なし",
indexExpireAfterSecondsHint: "単一フィールドインデックスのみ。インデックスの日付フィールドがこの経過時間を超えると、MongoDB は対応するドキュメントを削除します。",
indexPartialFilter: "部分フィルター",
indexLegacyOptions: "互換オプション",
indexLegacyOptionsHint: "旧バージョンのサーバーにのみ有効: MongoDB 4.2 以降は background を無視し、4.4 で geoHaystack インデックスが削除されてから bucket size は適用されなくなりました。",
indexBackground: "バックグラウンド",
indexBucketSize: "バケットサイズ",
indexSparse: "スパース",
indexHidden: "非表示",
indexNoExpiry: "期限なし",
indexOtherOptions: "その他のオプション",
indexPropertiesUnavailable: "MongoDB Legacy ドライバーはスパース、有効期限、バックグラウンド、バケットサイズを報告できません。ネイティブドライバーで接続して確認してください。",
},
vector: {
collectionFallback: "コレクション",
+34
View File
@@ -2572,6 +2572,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "자동 생성",
createMongoIndexNameHint: "비워 두면 MongoDB가 필드와 유형으로 이름을 생성합니다.",
createMongoIndexSparse: "희소",
createMongoIndexTtlInvalid: "만료 시간은 초 단위의 정수여야 합니다.",
createMongoIndexFilterInvalid: '부분 필터는 객체여야 합니다. 예: { "status": "active" }',
createMongoIndexBucketSizeInvalid: "버킷 크기는 정수여야 합니다.",
manageMongoIndexes: "인덱스 관리",
manageMongoIndexesTitle: '인덱스: "{collection}"',
mongoIndexKeys: "키",
mongoIndexEmpty: "이 컬렉션에 인덱스가 없습니다.",
mongoIndexSelectHint: "인덱스를 선택하여 속성을 확인하세요.",
mongoIndexReadOnlyHint: "MongoDB는 기존 인덱스를 수정할 수 없습니다. 속성을 변경하려면 삭제 후 새로 만드세요.",
editMongoIndex: "인덱스 편집",
saveMongoIndex: "저장",
mongoIndexRebuildHint: "저장하면 인덱스가 재구축됩니다: 기존 인덱스가 삭제되고 새 인덱스가 생성됩니다.",
mongoEditIndexSuccess: '인덱스 "{oldName}"이(가) "{collection}"에서 "{newName}"(으)로 재구축되었습니다',
mongoEditIndexNotFound: '인덱스 "{name}"이(가) 서버에 더 이상 존재하지 않습니다. 목록이 만료되었을 수 있습니다. 새로고침 후 다시 시도하세요.',
mongoEditIndexStale: '인덱스 "{name}"이(가) 대화상자를 연 이후 다른 세션에 의해 수정되었습니다. 목록을 새로고침 후 다시 시도하세요.',
mongoEditIndexIncompleteSameName: '인덱스 "{name}"의 전체 서버 사양을 가져올 수 없어 같은 이름으로 파괴적 재구축을 수행할 수 없습니다.',
mongoEditIndexDropFailed: '인덱스 "{name}"을(를) 삭제하지 못해 대체 인덱스를 생성하지 않았습니다: {error}',
mongoEditIndexDropFailedAfterCreate: '새 인덱스 "{newName}"을(를) 생성했지만 원래 인덱스 "{oldName}"을(를) 삭제하지 못했습니다: {error}',
mongoEditIndexCreateFailedRolledBack: '인덱스 "{name}"의 대체 인덱스를 생성하지 못해 원래 인덱스를 복원했습니다: {error}',
mongoEditIndexCreateAndRollbackFailed: '인덱스 "{name}"의 대체 인덱스 생성에 실패했고({createError}) 원래 인덱스 복원에도 실패했습니다({rollbackError}).',
mongoIndexExtraOptionsPreserved: "서버에서 보고한 옵션(collation, wildcardProjection, weights 등)은 재구축 시 보존됩니다.",
dropForeignKey: "외래 키 삭제",
dropTrigger: "트리거 삭제",
batchDrop: "선택 항목 삭제 ({count})",
@@ -3900,6 +3921,19 @@ export default withEnglishFallback({
tableView: "테이블 보기",
filterPlaceholder: "필터...",
sortPlaceholder: "정렬...",
indexExpireAfterSeconds: "만료 시간(초)",
indexExpireAfterSecondsPlaceholder: "만료 안 함",
indexExpireAfterSecondsHint: "단일 필드 인덱스만 해당. 인덱스 날짜 필드가 이 시간을 초과하면 MongoDB가 해당 문서를 삭제합니다.",
indexPartialFilter: "부분 필터",
indexLegacyOptions: "호환 옵션",
indexLegacyOptionsHint: "이전 서버 버전에만 의미: MongoDB 4.2부터 background가 무시되고, 4.4에서 geoHaystack 인덱스가 제거된 후 bucket size는 더 이상 적용되지 않습니다.",
indexBackground: "백그라운드",
indexBucketSize: "버킷 크기",
indexSparse: "희소",
indexHidden: "숨김",
indexNoExpiry: "만료 안 함",
indexOtherOptions: "기타 옵션",
indexPropertiesUnavailable: "MongoDB Legacy 드라이버는 희소, 만료, 백그라운드 또는 버킷 크기를 보고할 수 없습니다. 네이티브 드라이버로 연결하여 확인하세요.",
},
vector: {
collectionFallback: "컬렉션",
+34
View File
@@ -2673,6 +2673,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "Gerado automaticamente",
createMongoIndexNameHint: "Deixe em branco para o MongoDB gerar o nome com base nos campos e tipos.",
createMongoIndexSparse: "Esparso",
createMongoIndexTtlInvalid: "O tempo de expiração deve ser um número inteiro de segundos.",
createMongoIndexFilterInvalid: 'O filtro parcial deve ser um objeto, por exemplo { "status": "active" }.',
createMongoIndexBucketSizeInvalid: "O tamanho do bucket deve ser um número inteiro.",
manageMongoIndexes: "Gerenciar índices",
manageMongoIndexesTitle: 'Índices: "{collection}"',
mongoIndexKeys: "Chaves",
mongoIndexEmpty: "Esta coleção não tem índices.",
mongoIndexSelectHint: "Selecione um índice para ver suas propriedades.",
mongoIndexReadOnlyHint: "O MongoDB não pode alterar um índice existente. Remova-o e crie um novo para alterar suas propriedades.",
editMongoIndex: "Editar índice",
saveMongoIndex: "Salvar",
mongoIndexRebuildHint: "Salvar reconstrói o índice: o índice antigo é removido e um novo é criado.",
mongoEditIndexSuccess: 'Índice "{oldName}" reconstruído como "{newName}" em "{collection}"',
mongoEditIndexNotFound: 'O índice "{name}" não existe mais no servidor. A lista pode estar desatualizada; atualize e tente novamente.',
mongoEditIndexStale: 'O índice "{name}" foi modificado por outra sessão desde que a caixa de diálogo foi aberta. Atualize a lista e tente novamente.',
mongoEditIndexIncompleteSameName: 'O índice "{name}" não possui uma especificação completa do servidor e não pode ser reconstruído de forma destrutiva com o mesmo nome.',
mongoEditIndexDropFailed: 'Não foi possível remover o índice "{name}"; nenhum substituto foi criado: {error}',
mongoEditIndexDropFailedAfterCreate: 'O índice "{newName}" foi criado, mas não foi possível remover o índice original "{oldName}": {error}',
mongoEditIndexCreateFailedRolledBack: 'Não foi possível criar o substituto do índice "{name}", portanto o índice original foi restaurado: {error}',
mongoEditIndexCreateAndRollbackFailed: 'Não foi possível criar o substituto do índice "{name}" ({createError}) e a restauração do índice original também falhou ({rollbackError}).',
mongoIndexExtraOptionsPreserved: "As opções relatadas pelo servidor (collation, wildcardProjection, weights, …) são preservadas na reconstrução.",
batchDropIndexes: "Remover índices ({count})",
confirmDropViewTitle: "Remover Visão",
confirmDropViewMessage: 'Tem certeza de que deseja remover a visão "{name}"?',
@@ -4245,6 +4266,19 @@ export default withEnglishFallback({
nativeJsonHint: "Para valores BSON, use MongoDB Extended JSON, por exemplo $oid, $date, $numberLong.",
invalidNativeJson: "Formato de JSON nativo inválido: {error}",
nativeDocumentRequired: "O objeto de nível superior do JSON nativo deve ser um documento.",
indexExpireAfterSeconds: "Expirar após (segundos)",
indexExpireAfterSecondsPlaceholder: "Nunca",
indexExpireAfterSecondsHint: "Apenas índices de campo único. O MongoDB remove documentos quando a data indexada ultrapassa esse tempo.",
indexPartialFilter: "Filtro parcial",
indexLegacyOptions: "Opções de compatibilidade",
indexLegacyOptionsHint: "Relevante apenas para versões antigas do servidor: o background é ignorado a partir do MongoDB 4.2, e o bucket size deixou de se aplicar quando os índices geoHaystack foram removidos no 4.4.",
indexBackground: "Background",
indexBucketSize: "Tamanho do bucket",
indexSparse: "Esparso",
indexHidden: "Oculto",
indexNoExpiry: "Nunca",
indexOtherOptions: "Outras opções",
indexPropertiesUnavailable: "O driver Legacy do MongoDB não pode informar esparso, expiração, background ou tamanho do bucket. Conecte-se com o driver nativo para vê-los.",
},
vector: {
collectionFallback: "coleção",
+34
View File
@@ -2650,6 +2650,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "自动生成",
createMongoIndexNameHint: "留空时由 MongoDB 根据字段和类型自动生成名称。",
createMongoIndexSparse: "稀疏",
createMongoIndexTtlInvalid: "过期时间必须是整数秒。",
createMongoIndexFilterInvalid: '部分索引过滤器必须是对象,例如 { "status": "active" }。',
createMongoIndexBucketSizeInvalid: "存储桶大小必须是整数。",
manageMongoIndexes: "管理索引",
manageMongoIndexesTitle: "索引:「{collection}」",
mongoIndexKeys: "键",
mongoIndexEmpty: "该集合暂无索引。",
mongoIndexSelectHint: "选择一个索引查看其属性。",
mongoIndexReadOnlyHint: "MongoDB 无法修改已有索引。如需更改属性,请删除后重新创建。",
editMongoIndex: "编辑索引",
saveMongoIndex: "保存",
mongoIndexRebuildHint: "保存会重建索引:旧索引将被删除并创建新索引。",
mongoEditIndexSuccess: "已在「{collection}」将索引「{oldName}」重建为「{newName}」",
mongoEditIndexNotFound: "索引「{name}」在服务器上已不存在。列表可能已过期,请刷新后重试。",
mongoEditIndexStale: "索引「{name}」自打开对话框以来已被其他会话修改。请刷新列表后重试。",
mongoEditIndexIncompleteSameName: "索引「{name}」缺少完整的服务器规格,无法执行同名破坏性重建。",
mongoEditIndexDropFailed: "索引「{name}」删除失败,未创建替代索引:{error}",
mongoEditIndexDropFailedAfterCreate: "新索引「{newName}」已创建,但原索引「{oldName}」删除失败:{error}",
mongoEditIndexCreateFailedRolledBack: "索引「{name}」的替代索引创建失败,已恢复原索引:{error}",
mongoEditIndexCreateAndRollbackFailed: "索引「{name}」的替代索引创建失败({createError}),恢复原索引也失败({rollbackError})。",
mongoIndexExtraOptionsPreserved: "服务器报告的选项(collation、wildcardProjection、weights 等)在重建时会被保留。",
dropForeignKey: "删除外键",
dropTrigger: "删除触发器",
batchDrop: "删除所选({count})",
@@ -4383,6 +4404,19 @@ export default withEnglishFallback({
unsupportedJsonNumber: "JSON 中包含超出 MongoDB int64 范围的数字",
duplicateJsonKey: "JSON 字段重复:{field}",
duplicateField: "字段名重复:{field}",
indexExpireAfterSeconds: "在此之后过期(秒)",
indexExpireAfterSecondsPlaceholder: "永不过期",
indexExpireAfterSecondsHint: "仅支持单字段索引。索引的日期字段超过该时长后,MongoDB 会删除对应文档。",
indexPartialFilter: "部分索引过滤器",
indexLegacyOptions: "兼容选项",
indexLegacyOptionsHint: "仅对旧版本服务器有意义:MongoDB 4.2 起忽略「背景」,4.4 移除 geoHaystack 索引后「存储桶大小」也不再生效。",
indexBackground: "背景",
indexBucketSize: "存储桶大小",
indexSparse: "稀疏",
indexHidden: "隐藏",
indexNoExpiry: "永不过期",
indexOtherOptions: "其他选项",
indexPropertiesUnavailable: "MongoDB 旧版驱动无法读取稀疏、过期时间、背景或存储桶大小,请使用原生驱动连接以查看这些属性。",
edit: "编辑",
editModeFields: "字段",
editModeJson: "JSON",
+34
View File
@@ -2672,6 +2672,27 @@ export default withEnglishFallback({
createMongoIndexNamePlaceholder: "自動產生",
createMongoIndexNameHint: "留空時由 MongoDB 根據欄位和類型自動產生名稱。",
createMongoIndexSparse: "稀疏",
createMongoIndexTtlInvalid: "過期時間必須是整數秒。",
createMongoIndexFilterInvalid: '部分索引篩選條件必須是物件,例如 { "status": "active" }。',
createMongoIndexBucketSizeInvalid: "儲存桶大小必須是整數。",
manageMongoIndexes: "管理索引",
manageMongoIndexesTitle: "索引:「{collection}」",
mongoIndexKeys: "鍵",
mongoIndexEmpty: "該集合尚無索引。",
mongoIndexSelectHint: "選擇一個索引檢視其屬性。",
mongoIndexReadOnlyHint: "MongoDB 無法修改既有索引。如需變更屬性,請刪除後重新建立。",
editMongoIndex: "編輯索引",
saveMongoIndex: "儲存",
mongoIndexRebuildHint: "儲存會重建索引:舊索引將被刪除並建立新索引。",
mongoEditIndexSuccess: "已在「{collection}」將索引「{oldName}」重建為「{newName}」",
mongoEditIndexNotFound: "索引「{name}」在伺服器上已不存在。清單可能已過期,請重新整理後重試。",
mongoEditIndexStale: "索引「{name}」自開啟對話框以來已被其他工作階段修改。請重新整理清單後重試。",
mongoEditIndexIncompleteSameName: "索引「{name}」缺少完整的伺服器規格,無法執行同名破壞性重建。",
mongoEditIndexDropFailed: "索引「{name}」刪除失敗,未建立替代索引:{error}",
mongoEditIndexDropFailedAfterCreate: "新索引「{newName}」已建立,但原索引「{oldName}」刪除失敗:{error}",
mongoEditIndexCreateFailedRolledBack: "索引「{name}」的替代索引建立失敗,已還原原索引:{error}",
mongoEditIndexCreateAndRollbackFailed: "索引「{name}」的替代索引建立失敗({createError}),還原原索引也失敗({rollbackError})。",
mongoIndexExtraOptionsPreserved: "伺服器回報的選項(collation、wildcardProjection、weights 等)在重建時會被保留。",
batchDropIndexes: "刪除索引({count})",
confirmDropViewTitle: "刪除檢視",
confirmDropViewMessage: "確定要刪除檢視「{name}」嗎?",
@@ -3573,6 +3594,19 @@ export default withEnglishFallback({
unsupportedJsonNumber: "JSON 包含超出 MongoDB int64 範圍的數字",
duplicateJsonKey: "JSON 欄位重複:{field}",
duplicateField: "欄位名重複:{field}",
indexExpireAfterSeconds: "在此之後過期(秒)",
indexExpireAfterSecondsPlaceholder: "永不過期",
indexExpireAfterSecondsHint: "僅支援單欄位索引。索引的日期欄位超過該時長後,MongoDB 會刪除對應文件。",
indexPartialFilter: "部分索引篩選條件",
indexLegacyOptions: "相容選項",
indexLegacyOptionsHint: "僅對舊版伺服器有意義:MongoDB 4.2 起忽略「背景」,4.4 移除 geoHaystack 索引後「儲存桶大小」也不再生效。",
indexBackground: "背景",
indexBucketSize: "儲存桶大小",
indexSparse: "稀疏",
indexHidden: "隱藏",
indexNoExpiry: "永不過期",
indexOtherOptions: "其他選項",
indexPropertiesUnavailable: "MongoDB 舊版驅動無法讀取稀疏、過期時間、背景或儲存桶大小,請改用原生驅動連線以查看這些屬性。",
edit: "編輯",
editModeFields: "欄位",
editModeJson: "JSON",
+1
View File
@@ -689,6 +689,7 @@ export const mongoServerVersion = forward("mongoServerVersion");
export const mongoAggregateDocuments = forward("mongoAggregateDocuments");
export const mongoDistinct = forward("mongoDistinct");
export const mongoCollectionStats = forward("mongoCollectionStats");
export const mongoListIndexSpecs = forward("mongoListIndexSpecs");
export const mongoCreateIndex = forward("mongoCreateIndex");
export const mongoCreateUser = forward("mongoCreateUser");
export const mongoRunCommand = forward("mongoRunCommand");
+9
View File
@@ -102,6 +102,7 @@ import type {
MongoCollectionStatsResult,
MongoCloneCollectionResult,
MongoDropIndexesResult,
MongoIndexSpec,
MongoGridFsBucketInfo,
HistoryEntry,
HistorySearchRequest,
@@ -3625,6 +3626,14 @@ export async function mongoCollectionStats(connectionId: string, database: strin
});
}
export async function mongoListIndexSpecs(connectionId: string, database: string, collection: string): Promise<MongoIndexSpec[]> {
return post("/api/mongo/list-index-specs", {
connectionId,
database,
collection,
});
}
export async function mongoCreateIndex(connectionId: string, database: string, collection: string, keysJson: string, optionsJson?: string): Promise<{ name: string }> {
return post("/api/mongo/create-index", {
connectionId,
+34
View File
@@ -3286,6 +3286,32 @@ export interface MongoDropIndexesResult {
failures?: MongoDropIndexFailure[];
}
export interface MongoIndexKey {
field: string;
/** `1`, `-1`, or a MongoDB key type such as `text` / `2dsphere` / `hashed`. */
direction: string;
}
/** Full MongoDB index specification, carrying the options `IndexInfo` cannot hold. */
export interface MongoIndexSpec {
name: string;
keys: MongoIndexKey[];
is_unique: boolean;
is_primary: boolean;
is_sparse: boolean;
/** TTL in seconds; null when the index does not expire. */
expire_after_seconds: number | null;
partial_filter_expression: string | null;
/** Ignored by MongoDB 4.2+, still reported by older servers. */
background: boolean;
/** Only meaningful for geoHaystack indexes, removed in MongoDB 4.4+. */
bucket_size: number | null;
hidden: boolean;
/** False when the driver could not report the properties above (Legacy Agent). */
properties_complete: boolean;
extra_options: string | null;
}
export interface MongoCloneCollectionResult {
documents_copied: number;
indexes_copied: number;
@@ -3538,6 +3564,14 @@ export async function mongoCollectionStats(connectionId: string, database: strin
});
}
export async function mongoListIndexSpecs(connectionId: string, database: string, collection: string): Promise<MongoIndexSpec[]> {
return invoke("mongo_list_index_specs", {
connectionId,
database,
collection,
});
}
export async function mongoCreateIndex(connectionId: string, database: string, collection: string, keysJson: string, optionsJson?: string): Promise<{ name: string }> {
return invoke("mongo_create_index", {
connectionId,
@@ -4,21 +4,48 @@ import {
isCloneableMongoCollection,
isProtectedMongoIndex,
isRenamableMongoCollection,
mergeExtraOptionsIntoRequest,
mongoCollectionKindFromNode,
mongoCollectionTableTypeFromNode,
mongoCloneCollectionPreview,
mongoCreateIndexFormFromRow,
mongoCreateIndexRequestFromSpec,
mongoCreateIndexPreview,
mongoDropCollectionPreview,
mongoDropAllIndexesPreview,
mongoDropIndexFailureCount,
mongoDropIndexPreview,
mongoIndexKeyLabel,
mongoRenameCollectionPreview,
mongoReplaceIndexPreview,
preflightEditIndexSpec,
snapshotMongoIndexSpec,
toMongoCollectionKind,
toMongoIndexRow,
type MongoCreateIndexForm,
type MongoIndexSpecSource,
} from "../mongoCollectionMutation";
function indexForm(fields: MongoCreateIndexForm["fields"], options: Partial<Omit<MongoCreateIndexForm, "fields">> = {}): MongoCreateIndexForm {
return { name: "", unique: false, sparse: false, ...options, fields };
return { name: "", unique: false, sparse: false, expireAfterSeconds: "", partialFilterExpression: "", background: false, bucketSize: "", ...options, fields };
}
function serverIndexSpec(overrides: Partial<MongoIndexSpecSource> = {}): MongoIndexSpecSource {
return {
name: "email_1",
keys: [{ field: "email", direction: "1" }],
is_unique: false,
is_primary: false,
is_sparse: true,
expire_after_seconds: 3600,
partial_filter_expression: '{"archived":false}',
background: false,
bucket_size: null,
hidden: false,
properties_complete: true,
extra_options: '{"collation":{"locale":"en"}}',
...overrides,
};
}
describe("isRenamableMongoCollection", () => {
@@ -156,4 +183,319 @@ describe("mongo shell previews", () => {
),
).toEqual({ valid: false, error: "field-duplicate", field: "email" });
});
it("emits TTL and partial-filter options in a stable order", () => {
const request = buildMongoCreateIndexRequest(
indexForm([{ id: 1, path: "createdAt", type: "1" }], {
name: "ttl_idx",
unique: true,
sparse: true,
expireAfterSeconds: "3600",
partialFilterExpression: '{"archived":false}',
}),
);
expect(request).toEqual({
valid: true,
keysJson: '{"createdAt":1}',
optionsJson: '{"name":"ttl_idx","unique":true,"sparse":true,"expireAfterSeconds":3600,"partialFilterExpression":{"archived":false}}',
});
});
it("keeps a zero-second TTL distinct from an empty box", () => {
const withZero = buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "createdAt", type: "1" }], { expireAfterSeconds: "0" }));
expect(withZero).toEqual({ valid: true, keysJson: '{"createdAt":1}', optionsJson: '{"expireAfterSeconds":0}' });
const withBlank = buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "createdAt", type: "1" }], { expireAfterSeconds: " " }));
expect(withBlank).toEqual({ valid: true, keysJson: '{"createdAt":1}', optionsJson: undefined });
});
it("passes legacy options through only when enabled", () => {
const request = buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "email", type: "1" }], { background: true, bucketSize: "10" }));
expect(request).toEqual({ valid: true, keysJson: '{"email":1}', optionsJson: '{"background":true,"bucketSize":10}' });
});
it("rejects a non-numeric TTL, a bad bucket size, and a non-object filter", () => {
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "a", type: "1" }], { expireAfterSeconds: "-5" }))).toEqual({ valid: false, error: "ttl-invalid" });
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "a", type: "1" }], { expireAfterSeconds: "1.5" }))).toEqual({ valid: false, error: "ttl-invalid" });
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "a", type: "1" }], { bucketSize: "abc" }))).toEqual({ valid: false, error: "bucket-size-invalid" });
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "a", type: "1" }], { partialFilterExpression: "{not valid" }))).toEqual({ valid: false, error: "filter-invalid" });
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "a", type: "1" }], { partialFilterExpression: "[1,2]" }))).toEqual({ valid: false, error: "filter-invalid" });
});
it("reports a missing field before a malformed option", () => {
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "", type: "1" }], { expireAfterSeconds: "nope" }))).toEqual({ valid: false, error: "field-required" });
});
});
describe("mongoIndexKeyLabel", () => {
it("maps sort directions to readable labels and passes other types through", () => {
expect(mongoIndexKeyLabel(1)).toBe("ASC");
expect(mongoIndexKeyLabel("1")).toBe("ASC");
expect(mongoIndexKeyLabel(-1)).toBe("DESC");
expect(mongoIndexKeyLabel("-1")).toBe("DESC");
expect(mongoIndexKeyLabel("text")).toBe("text");
expect(mongoIndexKeyLabel(undefined)).toBe("");
});
});
describe("toMongoIndexRow", () => {
it("describes compound keys from a spec with directions", () => {
expect(
toMongoIndexRow({
name: "account_created",
keys: [
{ field: "account", direction: "1" },
{ field: "createTime", direction: "-1" },
],
}),
).toEqual({
name: "account_created",
keys: "account ASC, createTime DESC",
isUnique: false,
isProtected: false,
isSparse: false,
expireAfterSeconds: undefined,
partialFilterExpression: undefined,
background: false,
bucketSize: undefined,
hidden: false,
propertiesComplete: true,
extraOptions: undefined,
});
});
it("keeps non-numeric directions literal", () => {
expect(
toMongoIndexRow({
name: "content_text",
keys: [{ field: "content", direction: "text" }],
}).keys,
).toBe("content text");
});
it("maps spec properties and flags the default index as protected", () => {
const row = toMongoIndexRow({
name: "_id_",
keys: [{ field: "_id", direction: "1" }],
is_unique: true,
is_primary: true,
is_sparse: true,
expire_after_seconds: 3600,
partial_filter_expression: '{"archived":false}',
background: true,
bucket_size: 32,
hidden: true,
properties_complete: true,
extra_options: '{"collation":{"locale":"en"}}',
});
expect(row.isProtected).toBe(true);
expect(row.isSparse).toBe(true);
expect(row.expireAfterSeconds).toBe(3600);
expect(row.partialFilterExpression).toBe('{"archived":false}');
expect(row.background).toBe(true);
expect(row.bucketSize).toBe(32);
expect(row.hidden).toBe(true);
expect(row.propertiesComplete).toBe(true);
expect(row.extraOptions).toBe('{"collation":{"locale":"en"}}');
});
it("exposes the Legacy Agent's incomplete property set", () => {
const row = toMongoIndexRow({ name: "email_1", keys: [{ field: "email", direction: "1" }], properties_complete: false });
expect(row.propertiesComplete).toBe(false);
expect(row.isSparse).toBe(false);
expect(row.expireAfterSeconds).toBeUndefined();
});
});
describe("mongoCreateIndexFormFromRow", () => {
it("parses compound keys back into form fields with directions", () => {
const row = toMongoIndexRow({
name: "account_created",
keys: [
{ field: "account", direction: "1" },
{ field: "createTime", direction: "-1" },
],
});
const form = mongoCreateIndexFormFromRow(row);
expect(form.name).toBe("account_created");
expect(form.fields).toEqual([
{ id: 1, path: "account", type: "1" },
{ id: 2, path: "createTime", type: "-1" },
]);
});
it("round-trips non-numeric key types through the label parser", () => {
const row = toMongoIndexRow({ name: "content_text", keys: [{ field: "content", direction: "text" }] });
const form = mongoCreateIndexFormFromRow(row);
expect(form.fields).toEqual([{ id: 1, path: "content", type: "text" }]);
});
it("maps every option field back onto the form", () => {
const row = toMongoIndexRow({
name: "ttl_idx",
keys: [{ field: "createdAt", direction: "1" }],
is_unique: true,
is_sparse: true,
expire_after_seconds: 3600,
partial_filter_expression: '{"archived":false}',
background: true,
bucket_size: 32,
});
const form = mongoCreateIndexFormFromRow(row);
expect(form.unique).toBe(true);
expect(form.sparse).toBe(true);
expect(form.expireAfterSeconds).toBe("3600");
expect(form.partialFilterExpression).toBe('{"archived":false}');
expect(form.background).toBe(true);
expect(form.bucketSize).toBe("32");
});
it("falls back to one ascending field when the keys description is empty", () => {
const row = toMongoIndexRow({ name: "empty" });
const form = mongoCreateIndexFormFromRow(row);
expect(form.fields).toEqual([{ id: 1, path: "", type: "1" }]);
});
});
describe("mongoReplaceIndexPreview", () => {
it("joins the drop and create commands for review", () => {
const preview = mongoReplaceIndexPreview("app", "users", "email_1", '{"email":1}', '{"unique":true}');
const drop = mongoDropIndexPreview("app", "users", "email_1");
const create = mongoCreateIndexPreview("app", "users", '{"email":1}', '{"unique":true}');
expect(preview).toBe(`${drop}\n${create}`);
expect(preview).toContain('dropIndex("email_1")');
expect(preview).toContain('createIndex({"email":1}, {"unique":true})');
});
});
describe("mongoCreateIndexFormFromRow hidden", () => {
it("preserves the hidden flag from the row", () => {
const row = toMongoIndexRow({ name: "hidden_idx", keys: [{ field: "x", direction: "1" }], hidden: true });
const form = mongoCreateIndexFormFromRow(row);
expect(form.hidden).toBe(true);
});
it("defaults hidden to false when the row does not report it", () => {
const row = toMongoIndexRow({ name: "visible_idx", keys: [{ field: "x", direction: "1" }] });
const form = mongoCreateIndexFormFromRow(row);
expect(form.hidden).toBe(false);
});
});
describe("buildMongoCreateIndexRequest hidden", () => {
it("emits hidden:true when the form sets it", () => {
const request = buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "x", type: "1" }], { hidden: true }));
expect(request).toEqual({ valid: true, keysJson: '{"x":1}', optionsJson: '{"hidden":true}' });
});
it("omits hidden when the form leaves it false", () => {
const request = buildMongoCreateIndexRequest(indexForm([{ id: 1, path: "x", type: "1" }], { hidden: false }));
expect(request).toEqual({ valid: true, keysJson: '{"x":1}', optionsJson: undefined });
});
});
describe("mergeExtraOptionsIntoRequest", () => {
it("merges collation and wildcardProjection from extraOptions into the request", () => {
const request = { valid: true as const, keysJson: '{"x":1}', optionsJson: '{"name":"x_1"}' };
const extra = '{"collation":{"locale":"en"},"wildcardProjection":{"x":1}}';
const merged = mergeExtraOptionsIntoRequest(request, extra);
const parsed = JSON.parse(merged.optionsJson!);
expect(parsed.collation).toEqual({ locale: "en" });
expect(parsed.wildcardProjection).toEqual({ x: 1 });
expect(parsed.name).toBe("x_1");
});
it("form options take precedence over extraOptions for the same key", () => {
const request = { valid: true as const, keysJson: '{"x":1}', optionsJson: '{"name":"renamed"}' };
const extra = '{"name":"original","weights":{"content":2}}';
const merged = mergeExtraOptionsIntoRequest(request, extra);
const parsed = JSON.parse(merged.optionsJson!);
expect(parsed.name).toBe("renamed");
expect(parsed.weights).toEqual({ content: 2 });
});
it("filters out a stray key field from extraOptions", () => {
const request = { valid: true as const, keysJson: '{"x":1}', optionsJson: undefined };
const extra = '{"key":{"y":1},"collation":{"locale":"ja"}}';
const merged = mergeExtraOptionsIntoRequest(request, extra);
const parsed = JSON.parse(merged.optionsJson!);
expect(parsed.key).toBeUndefined();
expect(parsed.collation).toEqual({ locale: "ja" });
});
it("falls back to the form-only request when extraOptions is malformed", () => {
const request = { valid: true as const, keysJson: '{"x":1}', optionsJson: '{"unique":true}' };
const merged = mergeExtraOptionsIntoRequest(request, "{not valid json");
expect(merged.optionsJson).toBe('{"unique":true}');
});
it("returns the form request unchanged when extraOptions is empty", () => {
const request = { valid: true as const, keysJson: '{"x":1}', optionsJson: '{"unique":true}' };
const merged = mergeExtraOptionsIntoRequest(request, "");
expect(merged.optionsJson).toBe('{"unique":true}');
});
});
describe("preflightEditIndexSpec", () => {
it("reports safe when the complete normalized specification matches", () => {
const original = snapshotMongoIndexSpec(serverIndexSpec());
const current = serverIndexSpec({
partial_filter_expression: '{ "archived": false }',
extra_options: '{"collation":{"locale":"en"}}',
});
expect(preflightEditIndexSpec([current], original)).toEqual({ safe: true });
});
it("reports not-found when the index no longer exists", () => {
const original = snapshotMongoIndexSpec(serverIndexSpec());
expect(preflightEditIndexSpec([serverIndexSpec({ name: "other" })], original)).toEqual({ safe: false, reason: "not-found" });
});
it("reports stale when the keys have changed since the dialog was opened", () => {
const original = snapshotMongoIndexSpec(serverIndexSpec());
expect(preflightEditIndexSpec([serverIndexSpec({ keys: [{ field: "email", direction: "-1" }] })], original)).toEqual({ safe: false, reason: "stale" });
});
it("reports stale when the keys match but any complete option changed", () => {
const original = snapshotMongoIndexSpec(serverIndexSpec());
expect(preflightEditIndexSpec([serverIndexSpec({ is_unique: true, hidden: true })], original)).toEqual({ safe: false, reason: "stale" });
expect(preflightEditIndexSpec([serverIndexSpec({ extra_options: '{"collation":{"locale":"fr"}}' })], original)).toEqual({ safe: false, reason: "stale" });
});
});
describe("mongoCreateIndexRequestFromSpec", () => {
it("builds a complete rollback request from the opening server specification", () => {
const snapshot = snapshotMongoIndexSpec(
serverIndexSpec({
is_unique: true,
background: true,
bucket_size: 16,
hidden: true,
extra_options: '{"collation":{"locale":"en"},"wildcardProjection":{"email":1}}',
}),
);
const request = mongoCreateIndexRequestFromSpec(snapshot);
expect(request.keysJson).toBe('{"email":1}');
expect(JSON.parse(request.optionsJson)).toEqual({
collation: { locale: "en" },
wildcardProjection: { email: 1 },
name: "email_1",
unique: true,
sparse: true,
expireAfterSeconds: 3600,
partialFilterExpression: { archived: false },
background: true,
bucketSize: 16,
hidden: true,
});
});
});
@@ -15,6 +15,16 @@ export interface MongoCreateIndexForm {
fields: MongoCreateIndexField[];
unique: boolean;
sparse: boolean;
/** TTL in seconds. Kept as text so an empty box stays distinct from `0`. */
expireAfterSeconds: string;
/** Partial index condition, entered as an object literal. */
partialFilterExpression: string;
/** Ignored by MongoDB 4.2+; retained for older servers. */
background: boolean;
/** Only meaningful for geoHaystack indexes, removed in MongoDB 4.4+. */
bucketSize: string;
/** Hide the index from the query planner (MongoDB 4.4+). */
hidden: boolean;
}
export type MongoCreateIndexRequest =
@@ -25,7 +35,7 @@ export type MongoCreateIndexRequest =
}
| {
valid: false;
error: "field-required" | "field-duplicate";
error: "field-required" | "field-duplicate" | "ttl-invalid" | "filter-invalid" | "bucket-size-invalid";
field?: string;
};
@@ -108,6 +118,28 @@ export function isProtectedMongoIndex(index: { name: string; is_primary?: boolea
return index.name === "_id_" || !!index.is_primary;
}
/** Parse an optional non-negative integer box; `null` marks a malformed entry. */
function optionalNonNegativeInteger(raw: string | undefined): number | undefined | null {
const value = raw?.trim();
if (!value) return undefined;
if (!/^\d+$/.test(value)) return null;
const parsed = Number(value);
return Number.isSafeInteger(parsed) ? parsed : null;
}
/** Parse the partial filter box; `null` marks anything that is not an object literal. */
function optionalFilterObject(raw: string | undefined): Record<string, unknown> | undefined | null {
const value = raw?.trim();
if (!value) return undefined;
try {
const parsed = JSON.parse(value);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
return parsed as Record<string, unknown>;
} catch {
return null;
}
}
/** Convert the visual form into the driver's JSON transport format. */
export function buildMongoCreateIndexRequest(form: MongoCreateIndexForm): MongoCreateIndexRequest {
const fields = form.fields.map((field) => ({ ...field, path: field.path.trim() }));
@@ -121,15 +153,177 @@ export function buildMongoCreateIndexRequest(form: MongoCreateIndexForm): MongoC
// Build the object text directly so compound indexes retain their visual row order,
// including when a field name looks like an integer.
const keysJson = `{${fields.map((field) => `${JSON.stringify(field.path)}:${JSON.stringify(field.type === "1" ? 1 : field.type === "-1" ? -1 : field.type)}`).join(",")}}`;
const expireAfterSeconds = optionalNonNegativeInteger(form.expireAfterSeconds);
if (expireAfterSeconds === null) return { valid: false, error: "ttl-invalid" };
const bucketSize = optionalNonNegativeInteger(form.bucketSize);
if (bucketSize === null) return { valid: false, error: "bucket-size-invalid" };
const partialFilterExpression = optionalFilterObject(form.partialFilterExpression);
if (partialFilterExpression === null) return { valid: false, error: "filter-invalid" };
const options = {
...(form.name.trim() ? { name: form.name.trim() } : {}),
...(form.unique ? { unique: true } : {}),
...(form.sparse ? { sparse: true } : {}),
...(expireAfterSeconds === undefined ? {} : { expireAfterSeconds }),
...(partialFilterExpression === undefined ? {} : { partialFilterExpression }),
...(form.background ? { background: true } : {}),
...(bucketSize === undefined ? {} : { bucketSize }),
...(form.hidden ? { hidden: true } : {}),
};
const optionsJson = Object.keys(options).length ? JSON.stringify(options) : undefined;
return { valid: true, keysJson, optionsJson };
}
/**
* Merge the server-reported `extraOptions` (collation, wildcardProjection,
* weights, text defaults, geo options, …) into the form-built request so the
* drop + recreate preserves every option the form does not model. Form values
* take precedence: if the user explicitly set `unique` in the form, that wins
* over any `unique` in `extraOptions` (which shouldn't appear there anyway
* since it's modeled, but the principle holds for overlapping future fields).
*/
export function mergeExtraOptionsIntoRequest(request: Extract<MongoCreateIndexRequest, { valid: true }>, extraOptions?: string): { keysJson: string; optionsJson?: string } {
if (!extraOptions?.trim()) return { keysJson: request.keysJson, optionsJson: request.optionsJson };
let extra: Record<string, unknown>;
try {
const parsed = JSON.parse(extraOptions);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return request;
extra = parsed as Record<string, unknown>;
} catch {
// Malformed extra options should not block the edit; fall back to form-only.
return request;
}
// `key` is the index key spec, never an option — filter it defensively.
delete extra.key;
const formOptions = request.optionsJson ? (JSON.parse(request.optionsJson) as Record<string, unknown>) : {};
// Form values take precedence over extraOptions for the same key.
const merged = { ...extra, ...formOptions };
const optionsJson = Object.keys(merged).length ? JSON.stringify(merged) : undefined;
return { keysJson: request.keysJson, optionsJson };
}
export type MongoIndexSpecSource = {
name: string;
keys?: readonly { field: string; direction: string }[] | null;
is_unique?: boolean;
is_primary?: boolean;
is_sparse?: boolean;
expire_after_seconds?: number | null;
partial_filter_expression?: string | null;
background?: boolean;
bucket_size?: number | null;
hidden?: boolean;
properties_complete?: boolean;
extra_options?: string | null;
};
/** Immutable, normalized server specification captured when index editing starts. */
export interface MongoIndexSpecSnapshot {
name: string;
keys: { field: string; direction: string }[];
isUnique: boolean;
isPrimary: boolean;
isSparse: boolean;
expireAfterSeconds: number | null;
partialFilterExpression?: string;
background: boolean;
bucketSize: number | null;
hidden: boolean;
propertiesComplete: boolean;
extraOptions?: string;
}
/** Copy a server result into the stable shape used by preflight and rollback. */
export function snapshotMongoIndexSpec(source: MongoIndexSpecSource): MongoIndexSpecSnapshot {
return {
name: source.name,
keys: (source.keys ?? []).map((key) => ({ field: key.field, direction: String(key.direction) })),
isUnique: !!source.is_unique,
isPrimary: !!source.is_primary,
isSparse: !!source.is_sparse,
expireAfterSeconds: source.expire_after_seconds ?? null,
partialFilterExpression: source.partial_filter_expression?.trim() || undefined,
background: !!source.background,
bucketSize: source.bucket_size ?? null,
hidden: !!source.hidden,
propertiesComplete: source.properties_complete !== false,
extraOptions: source.extra_options?.trim() || undefined,
};
}
function canonicalJsonValue(value: unknown): unknown {
if (Array.isArray(value)) return value.map(canonicalJsonValue);
if (!value || typeof value !== "object") return value;
return Object.fromEntries(
Object.entries(value as Record<string, unknown>)
.sort(([left], [right]) => left.localeCompare(right))
.map(([key, child]) => [key, canonicalJsonValue(child)]),
);
}
function canonicalJsonText(raw?: string): string | undefined {
if (!raw) return undefined;
try {
return JSON.stringify(canonicalJsonValue(JSON.parse(raw)));
} catch {
return raw.trim() || undefined;
}
}
function comparableMongoIndexSpec(spec: MongoIndexSpecSnapshot): unknown {
return {
...spec,
partialFilterExpression: canonicalJsonText(spec.partialFilterExpression),
extraOptions: canonicalJsonText(spec.extraOptions),
};
}
/**
* Preflight check before a destructive edit: verify the complete normalized
* server specification still matches the immutable snapshot captured when the
* editor opened.
*/
export function preflightEditIndexSpec(currentSpecs: readonly MongoIndexSpecSource[], originalSpec: MongoIndexSpecSnapshot): { safe: boolean; reason?: string } {
const current = currentSpecs.find((spec) => spec.name === originalSpec.name);
if (!current) return { safe: false, reason: "not-found" };
const currentSnapshot = snapshotMongoIndexSpec(current);
if (JSON.stringify(comparableMongoIndexSpec(currentSnapshot)) !== JSON.stringify(comparableMongoIndexSpec(originalSpec))) {
return { safe: false, reason: "stale" };
}
return { safe: true };
}
function parseIndexOptionObject(raw: string | undefined, label: string): Record<string, unknown> {
if (!raw) return {};
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
throw new Error(`${label} must be a JSON object`);
}
return parsed as Record<string, unknown>;
}
/** Rebuild the exact create-index transport request used to restore an original index. */
export function mongoCreateIndexRequestFromSpec(spec: MongoIndexSpecSnapshot): { keysJson: string; optionsJson: string } {
const keysJson = `{${spec.keys.map((key) => `${JSON.stringify(key.field)}:${JSON.stringify(key.direction === "1" ? 1 : key.direction === "-1" ? -1 : key.direction)}`).join(",")}}`;
const extraOptions = parseIndexOptionObject(spec.extraOptions, "Index extra options");
for (const modeledField of ["key", "name", "unique", "sparse", "expireAfterSeconds", "partialFilterExpression", "background", "bucketSize", "hidden"]) {
delete extraOptions[modeledField];
}
const partialFilterExpression = spec.partialFilterExpression ? parseIndexOptionObject(spec.partialFilterExpression, "Partial filter expression") : undefined;
const options = {
...extraOptions,
name: spec.name,
...(spec.isUnique ? { unique: true } : {}),
...(spec.isSparse ? { sparse: true } : {}),
...(spec.expireAfterSeconds === null ? {} : { expireAfterSeconds: spec.expireAfterSeconds }),
...(partialFilterExpression ? { partialFilterExpression } : {}),
...(spec.background ? { background: true } : {}),
...(spec.bucketSize === null ? {} : { bucketSize: spec.bucketSize }),
...(spec.hidden ? { hidden: true } : {}),
};
return { keysJson, optionsJson: JSON.stringify(options) };
}
/**
* Keep the preview and production confirmation byte-for-byte aligned with
* the JSON passed to the backend. Re-serializing parsed JSON could reorder a
@@ -139,3 +333,127 @@ export function mongoCreateIndexPreview(database: string, collection: string, ke
const args = [keysJson, ...(optionsJson ? [optionsJson] : [])];
return `db.getSiblingDB(${JSON.stringify(database)}).getCollection(${JSON.stringify(collection)}).createIndex(${args.join(", ")})`;
}
/** Render a key direction the way index management tools label it. */
export function mongoIndexKeyLabel(value: unknown): string {
if (value === 1 || value === "1") return "ASC";
if (value === -1 || value === "-1") return "DESC";
return String(value ?? "");
}
/**
* Reverse {@link mongoIndexKeyLabel} so an edited index can re-enter the form
* with the same direction the server reported. Anything that is not ASC/DESC
* (text, 2dsphere, hashed, …) is kept verbatim.
*/
function mongoIndexKeyTypeFromLabel(label: string): MongoIndexKeyType {
if (label === "ASC") return "1";
if (label === "DESC") return "-1";
// Non-numeric key types (text, 2dsphere, hashed, 2d) round-trip as-is when
// they are part of the known set, otherwise default to ascending.
return (MONGO_INDEX_KEY_TYPES as readonly string[]).includes(label) ? (label as MongoIndexKeyType) : "1";
}
/**
* Parse the human-readable keys description produced by {@link mongoIndexKeyDescription}
* back into the visual form's field rows. Each comma-separated segment is
* `field DIRECTION`; a segment with no direction defaults to ascending.
*/
function parseMongoIndexKeyDescription(description: string): { field: string; type: MongoIndexKeyType }[] {
return description
.split(",")
.map((segment) => segment.trim())
.filter((segment) => segment.length > 0)
.map((segment) => {
const spaceIndex = segment.lastIndexOf(" ");
// Segments like `account ASC` split into field + label; a bare field name
// (no direction) falls back to ascending.
if (spaceIndex <= 0) return { field: segment, type: "1" as MongoIndexKeyType };
const field = segment.slice(0, spaceIndex).trim();
const label = segment.slice(spaceIndex + 1).trim();
return { field, type: mongoIndexKeyTypeFromLabel(label) };
});
}
/** One row of the index management panel. */
export interface MongoIndexRow {
name: string;
/** Per-key description, e.g. `account ASC`. */
keys: string;
isUnique: boolean;
isProtected: boolean;
isSparse: boolean;
/** TTL in seconds; undefined when the index does not expire. */
expireAfterSeconds?: number;
partialFilterExpression?: string;
background: boolean;
bucketSize?: number;
hidden: boolean;
/**
* False when the driver could not report the properties above, so the panel
* hides them instead of presenting defaults as if the server had said so.
*/
propertiesComplete: boolean;
extraOptions?: string;
}
/** Describe every key as `field LABEL`, e.g. `account ASC, createTime DESC`. */
function mongoIndexKeyDescription(keys: readonly { field: string; direction: string }[]): string {
return keys
.map((key) => {
const label = mongoIndexKeyLabel(key.direction);
return label ? `${key.field} ${label}` : key.field;
})
.join(", ");
}
/** Adapt a backend index spec into the management panel's row model. */
export function toMongoIndexRow(source: MongoIndexSpecSource): MongoIndexRow {
return {
name: source.name,
keys: mongoIndexKeyDescription(source.keys ?? []),
isUnique: !!source.is_unique,
isProtected: isProtectedMongoIndex({ name: source.name, is_primary: source.is_primary }),
isSparse: !!source.is_sparse,
expireAfterSeconds: source.expire_after_seconds ?? undefined,
partialFilterExpression: source.partial_filter_expression?.trim() || undefined,
background: !!source.background,
bucketSize: source.bucket_size ?? undefined,
hidden: !!source.hidden,
// Absent means the caller did not say, and only the Legacy Agent path says false.
propertiesComplete: source.properties_complete !== false,
extraOptions: source.extra_options?.trim() || undefined,
};
}
/**
* Prefill the create-index form from an existing index row so the user can
* edit it as a drop + recreate. The keys description is parsed back into the
* visual field rows; options map one-to-one.
*/
export function mongoCreateIndexFormFromRow(row: MongoIndexRow): MongoCreateIndexForm {
const parsed = parseMongoIndexKeyDescription(row.keys);
const fields = parsed.length > 0 ? parsed.map((entry, index) => ({ id: index + 1, path: entry.field, type: entry.type })) : [{ id: 1, path: "", type: "1" as MongoIndexKeyType }];
return {
name: row.name,
fields,
unique: row.isUnique,
sparse: row.isSparse,
expireAfterSeconds: row.expireAfterSeconds === undefined ? "" : String(row.expireAfterSeconds),
partialFilterExpression: row.partialFilterExpression ?? "",
background: row.background,
bucketSize: row.bucketSize === undefined ? "" : String(row.bucketSize),
hidden: row.hidden,
};
}
/**
* Build the review/preview text for an index edit (drop + recreate).
* Mirrors the two-step shell commands the backend runs, so production
* confirmation shows exactly what happens.
*/
export function mongoReplaceIndexPreview(database: string, collection: string, oldName: string, keysJson: string, optionsJson?: string): string {
const drop = mongoDropIndexPreview(database, collection, oldName);
const create = mongoCreateIndexPreview(database, collection, keysJson, optionsJson);
return `${drop}\n${create}`;
}