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
@@ -1239,6 +1239,57 @@ describe("connectionStore completion assistant", () => {
|
||||
expect(getColumns.mock.calls.map((call) => call[3])).toEqual(["users", "orders", "users"]);
|
||||
});
|
||||
|
||||
it("caches Mongo completion indexes and invalidates them via invalidateCompletionTableCache and invalidateCompletionCache", async () => {
|
||||
const mongoListIndexSpecs = vi.fn().mockResolvedValue([
|
||||
{
|
||||
name: "email_1",
|
||||
keys: [{ field: "email", direction: "1" }],
|
||||
is_unique: true,
|
||||
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,
|
||||
},
|
||||
]);
|
||||
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||
mongoListIndexSpecs,
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [
|
||||
{
|
||||
...postgresConnection(),
|
||||
id: "mongo-1",
|
||||
name: "MongoDB",
|
||||
db_type: "mongodb",
|
||||
},
|
||||
];
|
||||
store.connectedIds.add("mongo-1");
|
||||
|
||||
const first = await store.listMongoCompletionIndexes("mongo-1", "app", "users");
|
||||
expect(first).toEqual([{ name: "email_1", keyPattern: "{ email: 1 }" }]);
|
||||
const second = await store.listMongoCompletionIndexes("mongo-1", "app", "users");
|
||||
expect(second).toEqual([{ name: "email_1", keyPattern: "{ email: 1 }" }]);
|
||||
expect(mongoListIndexSpecs).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(store.invalidateCompletionTableCache("mongo-1", "app", "users")).toBeGreaterThan(0);
|
||||
await store.listMongoCompletionIndexes("mongo-1", "app", "users");
|
||||
expect(mongoListIndexSpecs).toHaveBeenCalledTimes(2);
|
||||
|
||||
store.invalidateCompletionCache("mongo-1", "app");
|
||||
await store.listMongoCompletionIndexes("mongo-1", "app", "users");
|
||||
expect(mongoListIndexSpecs).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("does not let an invalidated column request overwrite fresh metadata", async () => {
|
||||
const staleColumns = deferred<unknown[]>();
|
||||
const freshColumns = deferred<unknown[]>();
|
||||
|
||||
@@ -166,7 +166,7 @@ import { useSettingsStore } from "@/stores/settingsStore";
|
||||
import { useSavedSqlStore } from "@/stores/savedSqlStore";
|
||||
import { decorateDatabaseSavedSqlTreeNodes, indexSavedSqlFilesByDatabase, stripDatabaseSavedSqlTreeNodes, withDatabaseSavedSqlRoot } from "@/lib/savedSql/savedSqlDatabaseTree";
|
||||
import { encodeSqlServerLinkedSchema, parseSqlServerLinkedSchema } from "@/lib/database/sqlServerLinkedServers";
|
||||
import { inferMongoCompletionFields, type MongoCompletionField } from "@/lib/mongo/mongoCompletion";
|
||||
import { formatMongoIndexKeyPattern, inferMongoCompletionFields, type MongoCompletionField, type MongoCompletionIndex } from "@/lib/mongo/mongoCompletion";
|
||||
import type { SoqlCompletionField, SoqlCompletionObject } from "@/lib/soql/soqlCompletion";
|
||||
import type { SalesforceCurrentUser } from "@/types/salesforce";
|
||||
import { flattenElasticsearchMappingFields, type ElasticsearchCompletionField } from "@/lib/elasticsearch/elasticsearchCompletion";
|
||||
@@ -537,6 +537,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
const redisCommandDocsCacheGeneration = new Map<string, number>();
|
||||
const mongoCompletionCollectionsCache = ref<Record<string, string[]>>({});
|
||||
const mongoCompletionFieldsCache = ref<Record<string, MongoCompletionField[]>>({});
|
||||
const mongoCompletionIndexesCache = ref<Record<string, MongoCompletionIndex[]>>({});
|
||||
const soqlCompletionObjectsCache = ref<Record<string, SoqlCompletionObject[]>>({});
|
||||
const soqlCompletionFieldsCache = ref<Record<string, SoqlCompletionField[]>>({});
|
||||
// One entry per connection: the authenticated Salesforce user never changes for
|
||||
@@ -3940,6 +3941,9 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
for (const key of Object.keys(mongoCompletionFieldsCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete mongoCompletionFieldsCache.value[key];
|
||||
}
|
||||
for (const key of Object.keys(mongoCompletionIndexesCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete mongoCompletionIndexesCache.value[key];
|
||||
}
|
||||
for (const key of Object.keys(soqlCompletionObjectsCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete soqlCompletionObjectsCache.value[key];
|
||||
}
|
||||
@@ -7901,7 +7905,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
if (!normalizedKey.startsWith(prefix)) return false;
|
||||
const tableToken = `:${tableName.toLowerCase()}`;
|
||||
const tableOffset = normalizedKey.lastIndexOf(tableToken);
|
||||
if (tableOffset < prefix.length) return false;
|
||||
if (tableOffset < prefix.length - 1) return false;
|
||||
const trailing = normalizedKey.slice(tableOffset + tableToken.length);
|
||||
if (trailing && !trailing.startsWith(":")) return false;
|
||||
const normalizedSchema = schema?.trim().toLowerCase();
|
||||
@@ -7919,7 +7923,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
bumpCompletionCacheRevision(connectionId, database);
|
||||
const matches = (key: string) => completionTableCacheKeyMatches(key, connectionId, database, tableName, schema, catalog);
|
||||
let removed = 0;
|
||||
for (const cache of [completionColumnsCache.value, completionForeignKeysCache.value]) {
|
||||
for (const cache of [completionColumnsCache.value, completionForeignKeysCache.value, mongoCompletionFieldsCache.value, mongoCompletionIndexesCache.value]) {
|
||||
for (const key of Object.keys(cache)) {
|
||||
if (!matches(key)) continue;
|
||||
delete cache[key];
|
||||
@@ -8772,6 +8776,24 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
});
|
||||
}
|
||||
|
||||
async function listMongoCompletionIndexes(connectionId: string, database: string, collection: string): Promise<MongoCompletionIndex[]> {
|
||||
if (!database || !collection) return [];
|
||||
const cacheKey = `${connectionId}:${database}:${collection}`;
|
||||
const cached = mongoCompletionIndexesCache.value[cacheKey];
|
||||
if (cached) return cached;
|
||||
return withCompletionInFlight(`${cacheKey}:mongo-indexes`, async () => {
|
||||
await ensureConnected(connectionId);
|
||||
const specs = (await api.mongoListIndexSpecs(connectionId, database, collection)) ?? [];
|
||||
const indexes = specs.map((spec) => ({
|
||||
name: spec.name,
|
||||
keyPattern: formatMongoIndexKeyPattern(spec.keys),
|
||||
}));
|
||||
mongoCompletionIndexesCache.value[cacheKey] = indexes;
|
||||
evictOldestCacheEntries(mongoCompletionIndexesCache.value, COMPLETION_CACHE_MAX);
|
||||
return indexes;
|
||||
});
|
||||
}
|
||||
|
||||
// Map a Salesforce describe column to a SOQL completion field. The backend packs
|
||||
// relationshipName / referenceTo / label into ColumnInfo.extra (JSON) and active
|
||||
// picklist values into enum_values; parse defensively since extra may be absent.
|
||||
@@ -10330,6 +10352,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
listRedisCompletionCommandDocs,
|
||||
listMongoCompletionCollections,
|
||||
listMongoCompletionFields,
|
||||
listMongoCompletionIndexes,
|
||||
listSoqlCompletionObjects,
|
||||
listSoqlCompletionFields,
|
||||
loadSalesforceCurrentUser,
|
||||
|
||||
Reference in new issue
Block a user