feat(completion): complete mongo dropIndex and hint indexes
This commit is contained in:
1 parent
0f81dcdf40
commit
80bd37ebaa
6 files changed
+296
-7
No files matched your search
+16
@@ -60,6 +60,7 @@ function createHarness(overrides: Partial<QueryEditorProps> = {}, configureMetad
|
||||
listRedisCompletionKeys: vi.fn(async () => ["user:1"]),
|
||||
listMongoCompletionCollections: vi.fn(async () => ["users"]),
|
||||
listMongoCompletionFields: vi.fn(async () => []),
|
||||
listMongoCompletionIndexes: vi.fn(async () => []),
|
||||
listElasticsearchCompletionIndices: vi.fn(async () => ["users"]),
|
||||
listElasticsearchCompletionFields: vi.fn(async () => []),
|
||||
};
|
||||
@@ -275,6 +276,21 @@ describe("QueryEditor completion provider ownership", () => {
|
||||
expect(await accept("db.users.find({ na })", 18)).toEqual({ doc: "db.users.find({ name: })", cursor: 22 });
|
||||
});
|
||||
|
||||
it("completes MongoDB index names in dropIndex and replaces closing quote", async () => {
|
||||
const doc = 'db.users.dropIndex("")';
|
||||
const cursor = doc.indexOf('""') + 1;
|
||||
const { store, provide, currentView } = createHarness({ databaseType: "mongodb", modelValue: doc });
|
||||
store.listMongoCompletionIndexes.mockResolvedValue([{ name: "email_1", keyPattern: "{ email: 1 }" }]);
|
||||
currentView.dispatch({ selection: { anchor: cursor } });
|
||||
const result = await provide();
|
||||
expect(store.listMongoCompletionIndexes).toHaveBeenCalledWith("connection", "demo", "users");
|
||||
const option = result?.options.find((candidate) => candidate.displayLabel === "email_1" || candidate.label === "email_1");
|
||||
expect(option).toBeDefined();
|
||||
expect(option?.detail).toBe("{ email: 1 }");
|
||||
(option!.apply as (view: EditorView, completion: unknown, from: number, to: number) => void)(currentView, option, result!.from, cursor);
|
||||
expect(currentView.state.doc.toString()).toBe('db.users.dropIndex("email_1")');
|
||||
});
|
||||
|
||||
it.each(["redis", "mongodb", "mysql"] as const)("does not query %s metadata without a connection", async (databaseType) => {
|
||||
const { store, provide } = createHarness({ databaseType, connectionId: undefined });
|
||||
expect(await provide()).toBeNull();
|
||||
|
||||
@@ -20,7 +20,17 @@ import {
|
||||
import { originForSqlCompletionProvider, originForTypedSqlCompletionStart, shouldAllowSqlCompletionTrigger, type SqlCompletionTriggerFacts, type SqlCompletionTriggerOrigin } from "@/lib/sql/sqlCompletionTriggerPolicy";
|
||||
import { driverProfileHasCompletionCandidates } from "@/lib/database/driverProfileExtensions";
|
||||
import { buildElasticsearchCompletionItemsFromContext, elasticsearchCompletionNeedsFields, getElasticsearchCompletionContext, getElasticsearchCompletionResultValidFor, shouldAutoOpenElasticsearchCompletion, type ElasticsearchCompletionField } from "@/lib/elasticsearch/elasticsearchCompletion";
|
||||
import { buildMongoCompletionItemsFromContext, foldMongoKeySeparator, getMongoCompletionContext, getMongoCompletionResultValidFor, mongoCompletionNeedsCollections, mongoCompletionNeedsDatabases, mongoCompletionNeedsFields, shouldAutoOpenMongoCompletion } from "@/lib/mongo/mongoCompletion";
|
||||
import {
|
||||
buildMongoCompletionItemsFromContext,
|
||||
foldMongoKeySeparator,
|
||||
getMongoCompletionContext,
|
||||
getMongoCompletionResultValidFor,
|
||||
mongoCompletionNeedsCollections,
|
||||
mongoCompletionNeedsDatabases,
|
||||
mongoCompletionNeedsFields,
|
||||
mongoCompletionNeedsIndexes,
|
||||
shouldAutoOpenMongoCompletion,
|
||||
} from "@/lib/mongo/mongoCompletion";
|
||||
import { buildSoqlCompletionItems, getSoqlCompletionContext, getSoqlCompletionResultValidFor, resolveSoqlFieldCandidates, resolveSoqlValueField, shouldAutoOpenSoqlCompletion, soqlCompletionNeedsObjects, type SoqlCompletionField, type SoqlCompletionObject } from "@/lib/soql/soqlCompletion";
|
||||
import {
|
||||
buildSqlServerUseDatabaseCompletionItems,
|
||||
@@ -572,6 +582,7 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
|
||||
let databases: string[] = [];
|
||||
let collections: string[] = [];
|
||||
let fields: Awaited<ReturnType<typeof connectionStore.listMongoCompletionFields>> = [];
|
||||
let indexes: Awaited<ReturnType<typeof connectionStore.listMongoCompletionIndexes>> = [];
|
||||
|
||||
// `use` and `getSiblingDB` name a database, which does not depend on the tab having one selected.
|
||||
if (mongoCompletionNeedsDatabases(completionContext.mode)) {
|
||||
@@ -598,12 +609,22 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
|
||||
}
|
||||
}
|
||||
|
||||
const indexDatabase = completionContext.database ?? props.database;
|
||||
if (indexDatabase && mongoCompletionNeedsIndexes(completionContext.mode) && completionContext.collection) {
|
||||
try {
|
||||
indexes = await connectionStore.listMongoCompletionIndexes(props.connectionId, indexDatabase, completionContext.collection);
|
||||
} catch {
|
||||
indexes = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (epoch !== completionEpoch) return null;
|
||||
|
||||
const items = buildMongoCompletionItemsFromContext(completionContext, {
|
||||
databases,
|
||||
collections,
|
||||
fields,
|
||||
indexes,
|
||||
});
|
||||
if (items.length === 0) return null;
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user