feat(mongodb): improve sidebar index management

This commit is contained in:
onenewcode authored and GitHub committed 2026-08-02 22:45:33 +08:00
1 parent 22055e081f
commit 8e7b74d0ee
56 files changed
+3558 -246

No files matched your search

@@ -1,5 +1,23 @@
import { describe, expect, it } from "vitest";
import { isRenamableMongoCollection, mongoCollectionKindFromNode, mongoDropAllIndexesPreview, mongoDropCollectionPreview, mongoDropIndexPreview, mongoRenameCollectionPreview, toMongoCollectionKind } from "../mongoCollectionMutation";
import {
buildMongoCreateIndexRequest,
isProtectedMongoIndex,
isRenamableMongoCollection,
mongoCollectionKindFromNode,
mongoCollectionTableTypeFromNode,
mongoCreateIndexPreview,
mongoDropCollectionPreview,
mongoDropAllIndexesPreview,
mongoDropIndexFailureCount,
mongoDropIndexPreview,
mongoRenameCollectionPreview,
toMongoCollectionKind,
type MongoCreateIndexForm,
} from "../mongoCollectionMutation";
function indexForm(fields: MongoCreateIndexForm["fields"], options: Partial<Omit<MongoCreateIndexForm, "fields">> = {}): MongoCreateIndexForm {
return { name: "", unique: false, sparse: false, ...options, fields };
}
describe("isRenamableMongoCollection", () => {
it("allows ordinary collections and defaults", () => {
@@ -21,6 +39,12 @@ describe("mongoCollectionKindFromNode", () => {
expect(mongoCollectionKindFromNode({ meta: { collectionKind: "collection" } })).toBe("collection");
expect(mongoCollectionKindFromNode({})).toBe("collection");
});
it("maps collection kinds to data-tab table types", () => {
expect(mongoCollectionTableTypeFromNode({ meta: { collectionKind: "collection" } })).toBe("TABLE");
expect(mongoCollectionTableTypeFromNode({ meta: { collectionKind: "view" } })).toBe("VIEW");
expect(mongoCollectionTableTypeFromNode({ meta: { collectionKind: "timeseries" } })).toBe("TIMESERIES");
});
});
describe("toMongoCollectionKind", () => {
@@ -32,6 +56,14 @@ describe("toMongoCollectionKind", () => {
});
});
describe("isProtectedMongoIndex", () => {
it("protects the default index by name or primary metadata", () => {
expect(isProtectedMongoIndex({ name: "_id_", is_primary: false })).toBe(true);
expect(isProtectedMongoIndex({ name: "unexpected", is_primary: true })).toBe(true);
expect(isProtectedMongoIndex({ name: "email_1", is_primary: false })).toBe(false);
});
});
describe("mongo shell previews", () => {
it("preserves identifier whitespace in rename preview", () => {
expect(mongoRenameCollectionPreview("app", " users ", " renamed ")).toBe('db.getSiblingDB("app").getCollection(" users ").renameCollection(" renamed ")');
@@ -42,4 +74,66 @@ describe("mongo shell previews", () => {
expect(mongoDropIndexPreview("app", "users", "idx_name")).toBe('db.getSiblingDB("app").getCollection("users").dropIndex("idx_name")');
expect(mongoDropAllIndexesPreview("app", "users")).toBe('db.getSiblingDB("app").getCollection("users").dropIndexes()');
});
it("counts per-index failures in partial batch results", () => {
expect(mongoDropIndexFailureCount({})).toBe(0);
expect(mongoDropIndexFailureCount({ failures: [{ name: "missing_1", message: "index not found" }] })).toBe(1);
});
it("builds a create-index request and shell preview from the visual form", () => {
const request = buildMongoCreateIndexRequest(
indexForm(
[
{ id: 1, path: "email", type: "1" },
{ id: 2, path: "createdAt", type: "-1" },
],
{ name: "email_created_at", unique: true, sparse: true },
),
);
expect(request).toMatchObject({
valid: true,
keysJson: '{"email":1,"createdAt":-1}',
optionsJson: '{"name":"email_created_at","unique":true,"sparse":true}',
});
if (!request.valid) throw new Error("expected valid index form");
expect(mongoCreateIndexPreview("app", "users", request.keysJson, request.optionsJson)).toBe('db.getSiblingDB("app").getCollection("users").createIndex({"email":1,"createdAt":-1}, {"name":"email_created_at","unique":true,"sparse":true})');
});
it("keeps visual compound-field order, including integer-like names", () => {
const request = buildMongoCreateIndexRequest(
indexForm([
{ id: 1, path: "10", type: "1" },
{ id: 2, path: "2", type: "-1" },
]),
);
if (!request.valid) throw new Error("expected valid index form");
expect(request.optionsJson).toBeUndefined();
expect(mongoCreateIndexPreview("app", "events", request.keysJson, request.optionsJson)).toBe('db.getSiblingDB("app").getCollection("events").createIndex({"10":1,"2":-1})');
});
it("serializes MongoDB-specific key types without exposing JSON inputs", () => {
const request = buildMongoCreateIndexRequest(
indexForm([
{ id: 1, path: "content", type: "text" },
{ id: 2, path: "location", type: "2dsphere" },
]),
);
expect(request).toEqual({ valid: true, keysJson: '{"content":"text","location":"2dsphere"}', optionsJson: undefined });
});
it("requires every field and rejects duplicate field paths", () => {
expect(buildMongoCreateIndexRequest(indexForm([]))).toEqual({ valid: false, error: "field-required" });
expect(buildMongoCreateIndexRequest(indexForm([{ id: 1, path: " ", type: "1" }]))).toEqual({ valid: false, error: "field-required" });
expect(
buildMongoCreateIndexRequest(
indexForm([
{ id: 1, path: "email", type: "1" },
{ id: 2, path: "email", type: "-1" },
]),
),
).toEqual({ valid: false, error: "field-duplicate", field: "email" });
});
});
@@ -65,6 +65,31 @@ describe("runMongoSidebarMutation", () => {
expect(onSuccess).toHaveBeenCalledWith(result);
});
it("waits for asynchronous success work before clearing loading", async () => {
const loading = ref(false);
let releaseSuccess: () => void = () => {};
const successWork = new Promise<void>((resolve) => {
releaseSuccess = resolve;
});
const onSuccess = vi.fn(async () => successWork);
const pending = runMongoSidebarMutation({
connection: connection(),
database: "app",
reviewText: 'db.getCollection("users").createIndex({"email":1})',
source: "Object tree",
loading,
execute: async () => ({ name: "email_1" }),
onSuccess,
});
await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledOnce());
expect(loading.value).toBe(true);
releaseSuccess();
await pending;
expect(loading.value).toBe(false);
});
it("does not execute or call onSuccess when production confirmation is cancelled", async () => {
const loading = ref(false);
const execute = vi.fn().mockResolvedValue(undefined);
@@ -1,5 +1,34 @@
import type { MongoCollectionKind, TreeNode } from "@/types/database";
export const MONGO_INDEX_KEY_TYPES = ["1", "-1", "text", "hashed", "2dsphere", "2d"] as const;
export type MongoIndexKeyType = (typeof MONGO_INDEX_KEY_TYPES)[number];
export interface MongoCreateIndexField {
id: number;
path: string;
type: MongoIndexKeyType;
}
export interface MongoCreateIndexForm {
name: string;
fields: MongoCreateIndexField[];
unique: boolean;
sparse: boolean;
}
export type MongoCreateIndexRequest =
| {
valid: true;
keysJson: string;
optionsJson?: string;
}
| {
valid: false;
error: "field-required" | "field-duplicate";
field?: string;
};
/**
* MongoDB only supports renameCollection for ordinary, non-system collections.
* Views, time-series collections, and reserved system namespaces must not expose a rename action.
@@ -17,6 +46,11 @@ export function mongoCollectionKindFromNode(node: Pick<TreeNode, "meta">): Mongo
return "collection";
}
export function mongoCollectionTableTypeFromNode(node: Pick<TreeNode, "meta">): "TABLE" | "VIEW" | "TIMESERIES" {
const kind = mongoCollectionKindFromNode(node);
return kind === "view" ? "VIEW" : kind === "timeseries" ? "TIMESERIES" : "TABLE";
}
export function toMongoCollectionKind(kind?: string | null): MongoCollectionKind {
const normalized = (kind || "collection").toLowerCase();
if (normalized === "view") return "view";
@@ -43,3 +77,44 @@ export function mongoDropIndexPreview(database: string, collection: string, inde
export function mongoDropAllIndexesPreview(database: string, collection: string): string {
return `db.getSiblingDB(${JSON.stringify(database)}).getCollection(${JSON.stringify(collection)}).dropIndexes()`;
}
export function mongoDropIndexFailureCount(result: { failures?: readonly unknown[] }): number {
return result.failures?.length ?? 0;
}
/** MongoDB always protects its default _id index, even when metadata is incomplete. */
export function isProtectedMongoIndex(index: { name: string; is_primary?: boolean }): boolean {
return index.name === "_id_" || !!index.is_primary;
}
/** 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() }));
if (fields.length === 0 || fields.some((field) => !field.path)) return { valid: false, error: "field-required" };
const seen = new Set<string>();
for (const field of fields) {
if (seen.has(field.path)) return { valid: false, error: "field-duplicate", field: field.path };
seen.add(field.path);
}
// 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 options = {
...(form.name.trim() ? { name: form.name.trim() } : {}),
...(form.unique ? { unique: true } : {}),
...(form.sparse ? { sparse: true } : {}),
};
const optionsJson = Object.keys(options).length ? JSON.stringify(options) : undefined;
return { valid: true, keysJson, optionsJson };
}
/**
* Keep the preview and production confirmation byte-for-byte aligned with
* the JSON passed to the backend. Re-serializing parsed JSON could reorder a
* compound key or round a large numeric value before the user reviews it.
*/
export function mongoCreateIndexPreview(database: string, collection: string, keysJson: string, optionsJson?: string): string {
const args = [keysJson, ...(optionsJson ? [optionsJson] : [])];
return `db.getSiblingDB(${JSON.stringify(database)}).getCollection(${JSON.stringify(collection)}).createIndex(${args.join(", ")})`;
}
@@ -11,7 +11,7 @@ export type RunMongoSidebarMutationOptions<T> = {
/** Runs after production confirmation and after loading is set (e.g. ensureConnected). */
beforeExecute?: () => Promise<void>;
execute: () => Promise<T>;
onSuccess: (result: T) => void;
onSuccess: (result: T) => void | Promise<void>;
onError?: (error: unknown) => void;
};
@@ -41,10 +41,13 @@ export async function runMongoSidebarMutation<T>(options: RunMongoSidebarMutatio
});
// Cancel only: guard returns undefined when the user declines production confirmation.
if (executed === undefined) return;
options.onSuccess(executed.result);
await options.onSuccess(executed.result);
} catch (error: unknown) {
options.onError?.(error);
} finally {
options.loading.value = false;
}
}
// DataGrid and sidebar mutations intentionally share the same production-gated lifecycle.
export const runMongoMutation = runMongoSidebarMutation;