feat(redis): add configurable key search templates

This commit is contained in:
whoops authored and GitHub committed 2026-08-25 22:34:04 +08:00
1 parent bfd7c7def2
commit 5e307f56cc
47 files changed
+407 -11

No files matched your search

@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { filterRedisKeyTemplates, normalizeRedisKeyTemplates, redisKeyTemplatesToTextarea, resolveRedisKeyTemplates } from "@/lib/redis/redisKeyTemplates";
describe("redisKeyTemplates", () => {
it("normalizes arrays and newline text, trimming and deduping", () => {
expect(normalizeRedisKeyTemplates([" user:{$id} ", "", "v3:adui:{$positionID}", "user:{$id}", " "])).toEqual(["user:{$id}", "v3:adui:{$positionID}"]);
expect(normalizeRedisKeyTemplates("user:{$id}\n\nv3:adui\nuser:{$id}\n")).toEqual(["user:{$id}", "v3:adui"]);
expect(normalizeRedisKeyTemplates(null)).toEqual([]);
expect(normalizeRedisKeyTemplates(undefined)).toEqual([]);
expect(normalizeRedisKeyTemplates([1, "ok"] as unknown[])).toEqual(["ok"]);
});
it("resolves connection templates over global when connection list is non-empty", () => {
expect(resolveRedisKeyTemplates(["conn:a"], ["global:a", "global:b"])).toEqual(["conn:a"]);
expect(resolveRedisKeyTemplates([], ["global:a"])).toEqual(["global:a"]);
expect(resolveRedisKeyTemplates(undefined, ["global:a"])).toEqual(["global:a"]);
expect(resolveRedisKeyTemplates([" ", ""], ["global:a"])).toEqual(["global:a"]);
expect(resolveRedisKeyTemplates(undefined, undefined)).toEqual([]);
});
it("filters by case-insensitive substring and returns all when query is blank", () => {
const templates = ["v3:adui:{$positionID}", "v4:adui:creative_feed:{$arg1}", "cache:session"];
expect(filterRedisKeyTemplates(templates, "")).toEqual(templates);
expect(filterRedisKeyTemplates(templates, " ")).toEqual(templates);
expect(filterRedisKeyTemplates(templates, "ADUI")).toEqual(["v3:adui:{$positionID}", "v4:adui:creative_feed:{$arg1}"]);
expect(filterRedisKeyTemplates(templates, "session")).toEqual(["cache:session"]);
expect(filterRedisKeyTemplates(templates, "missing")).toEqual([]);
});
it("joins templates for textarea editing", () => {
expect(redisKeyTemplatesToTextarea(["a", "b"])).toBe("a\nb");
expect(redisKeyTemplatesToTextarea(undefined)).toBe("");
});
});
@@ -0,0 +1,35 @@
/**
* Helpers for configurable Redis key-search templates.
* Connection-level templates override the global list when non-empty.
*/
export function normalizeRedisKeyTemplates(value: unknown): string[] {
const rawLines = typeof value === "string" ? value.split(/\r?\n/) : Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : [];
const seen = new Set<string>();
const templates: string[] = [];
for (const raw of rawLines) {
const template = raw.trim();
if (!template || seen.has(template)) continue;
seen.add(template);
templates.push(template);
}
return templates;
}
/** Empty connection list means inherit global (not “no templates”). */
export function resolveRedisKeyTemplates(connectionTemplates: string[] | undefined | null, globalTemplates: string[] | undefined | null): string[] {
const fromConnection = normalizeRedisKeyTemplates(connectionTemplates);
if (fromConnection.length > 0) return fromConnection;
return normalizeRedisKeyTemplates(globalTemplates);
}
export function filterRedisKeyTemplates(templates: readonly string[], query: string): string[] {
const q = query.trim().toLowerCase();
if (!q) return [...templates];
return templates.filter((template) => template.toLowerCase().includes(q));
}
export function redisKeyTemplatesToTextarea(templates: readonly string[] | undefined | null): string {
return normalizeRedisKeyTemplates(templates).join("\n");
}
@@ -2,6 +2,7 @@ import type { EditorSettings } from "@/stores/settingsStore";
import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
import { normalizeQueryResultMaxRows } from "@/lib/dataGrid/queryResultRowLimit";
import { normalizeCompletionTriggerMode } from "@/lib/sql/sqlCompletionTriggerPolicy";
import { normalizeRedisKeyTemplates } from "@/lib/redis/redisKeyTemplates";
export const EDITOR_SETTINGS_DRAFT_KEYS = [
"fontFamily",
@@ -76,6 +77,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"sidebarIndent",
"sidebarFontSize",
"sidebarHiddenTablePrefixes",
"redisKeyTemplates",
"exportBatchSize",
"exportRowLimitEnabled",
"exportRowLimit",
@@ -114,6 +116,7 @@ function normalizedDraftValue(key: EditorSettingsDraftKey, value: unknown): unkn
if (key === "pageSize" || key === "tableOpenPageSize") return normalizeTableOpenPageSizeDraft(value);
if (key === "queryResultMaxRows") return normalizeQueryResultMaxRowsDraft(value);
if (key === "completionTriggerMode") return normalizeCompletionTriggerMode(value);
if (key === "redisKeyTemplates") return normalizeRedisKeyTemplates(value);
return value;
}
@@ -188,6 +188,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
{ id: "data-grid-compact-header-actions", category: "data", titleKey: "settings.compactColumnHeaderActions", descriptionKey: "settings.compactColumnHeaderActionsDescription", targetId: "data" },
{ id: "data-grid-auto-total", category: "data", titleKey: "settings.autoCalculateTotalRows", descriptionKey: "settings.autoCalculateTotalRowsDescription", targetId: "data" },
{ id: "data-grid-infinite-scroll", category: "data", titleKey: "settings.infiniteScroll", descriptionKey: "settings.infiniteScrollDescription", targetId: "data" },
{ id: "redis-key-templates", category: "data", titleKey: "settings.redisKeyTemplates", descriptionKey: "settings.redisKeyTemplatesDescription", targetId: "redis-key-templates" },
{ id: "data-grid-auto-transpose", category: "data", titleKey: "settings.dataGridAutoTransposeSingleRow", descriptionKey: "settings.dataGridAutoTransposeSingleRowDescription", targetId: "data" },
{ id: "data-grid-quick-entry", category: "data", titleKey: "settings.dataGridQuickEntry", descriptionKey: "settings.dataGridQuickEntryDescription", targetId: "data" },
{ id: "data-grid-filter-view", category: "data", titleKey: "settings.dataGridFilterView", descriptionKey: "settings.dataGridFilterViewDescription", targetId: "data" },