feat(redis): 修复全库键浏览与二进制展示

This commit is contained in:
Bacon2994
2026-05-08 10:06:16 +08:00
parent 68c0639e3a
commit 0fb6aebd59
13 changed files with 879 additions and 174 deletions
+30 -18
View File
@@ -450,40 +450,52 @@ export async function redisScanKeys(
return post("/api/redis/scan-keys", { connectionId, db, cursor, pattern, count });
}
export async function redisGetValue(connectionId: string, key: string): Promise<RedisValue> {
return post("/api/redis/get-value", { connectionId, key });
export async function redisGetValue(connectionId: string, db: number, keyRaw: string): Promise<RedisValue> {
return post("/api/redis/get-value", { connectionId, db, keyRaw });
}
export async function redisSetString(connectionId: string, key: string, value: string, ttl?: number): Promise<void> {
return post("/api/redis/set-string", { connectionId, key, value, ttl });
export async function redisSetString(
connectionId: string,
db: number,
keyRaw: string,
value: string,
ttl?: number,
): Promise<void> {
return post("/api/redis/set-string", { connectionId, db, keyRaw, value, ttl });
}
export async function redisDeleteKey(connectionId: string, key: string): Promise<void> {
return post("/api/redis/delete-key", { connectionId, key });
export async function redisDeleteKey(connectionId: string, db: number, keyRaw: string): Promise<void> {
return post("/api/redis/delete-key", { connectionId, db, keyRaw });
}
export async function redisHashSet(connectionId: string, key: string, field: string, value: string): Promise<void> {
return post("/api/redis/hash-set", { connectionId, key, field, value });
export async function redisHashSet(
connectionId: string,
db: number,
keyRaw: string,
field: string,
value: string,
): Promise<void> {
return post("/api/redis/hash-set", { connectionId, db, keyRaw, field, value });
}
export async function redisHashDel(connectionId: string, key: string, field: string): Promise<void> {
return post("/api/redis/hash-del", { connectionId, key, field });
export async function redisHashDel(connectionId: string, db: number, keyRaw: string, field: string): Promise<void> {
return post("/api/redis/hash-del", { connectionId, db, keyRaw, field });
}
export async function redisListPush(connectionId: string, key: string, value: string): Promise<void> {
return post("/api/redis/list-push", { connectionId, key, value });
export async function redisListPush(connectionId: string, db: number, keyRaw: string, value: string): Promise<void> {
return post("/api/redis/list-push", { connectionId, db, keyRaw, value });
}
export async function redisListRemove(connectionId: string, key: string, index: number): Promise<void> {
return post("/api/redis/list-remove", { connectionId, key, index });
export async function redisListRemove(connectionId: string, db: number, keyRaw: string, index: number): Promise<void> {
return post("/api/redis/list-remove", { connectionId, db, keyRaw, index });
}
export async function redisSetAdd(connectionId: string, key: string, member: string): Promise<void> {
return post("/api/redis/set-add", { connectionId, key, member });
export async function redisSetAdd(connectionId: string, db: number, keyRaw: string, member: string): Promise<void> {
return post("/api/redis/set-add", { connectionId, db, keyRaw, member });
}
export async function redisSetRemove(connectionId: string, key: string, member: string): Promise<void> {
return post("/api/redis/set-remove", { connectionId, key, member });
export async function redisSetRemove(connectionId: string, db: number, keyRaw: string, member: string): Promise<void> {
return post("/api/redis/set-remove", { connectionId, db, keyRaw, member });
}
// ---------------------------------------------------------------------------
+141
View File
@@ -0,0 +1,141 @@
import type { RedisKeyInfo } from "./api";
export interface RedisKeyTreeLeafNode {
kind: "leaf";
id: string;
label: string;
fullKeyDisplay: string;
keyRaw: string;
db: number;
keyType: string;
ttl: number;
pathSegments: string[];
}
export interface RedisKeyTreeGroupNode {
kind: "group";
id: string;
label: string;
pathSegments: string[];
children: RedisKeyTreeNode[];
}
export type RedisKeyTreeNode = RedisKeyTreeLeafNode | RedisKeyTreeGroupNode;
export interface RedisKeyTreeRow {
node: RedisKeyTreeNode;
depth: number;
}
function buildGroupId(db: number, pathSegments: string[]): string {
return `group:${db}:${pathSegments.join("\u0000")}`;
}
function buildLeafId(db: number, keyRaw: string): string {
return `leaf:${db}:${keyRaw}`;
}
function compareRedisTreeNodes(a: RedisKeyTreeNode, b: RedisKeyTreeNode): number {
if (a.kind !== b.kind) return a.kind === "group" ? -1 : 1;
return a.label.localeCompare(b.label);
}
function sortRedisTreeNodes(nodes: RedisKeyTreeNode[]): RedisKeyTreeNode[] {
return [...nodes].sort(compareRedisTreeNodes).map((node) =>
node.kind === "group"
? {
...node,
children: sortRedisTreeNodes(node.children),
}
: node,
);
}
export function buildRedisKeyTree(keys: RedisKeyInfo[], db: number): RedisKeyTreeNode[] {
const root: RedisKeyTreeNode[] = [];
const groupMap = new Map<string, RedisKeyTreeGroupNode>();
for (const key of keys) {
const pathSegments = key.key_display.split(":");
if (pathSegments.length === 1) {
root.push({
kind: "leaf",
id: buildLeafId(db, key.key_raw),
label: pathSegments[0],
fullKeyDisplay: key.key_display,
keyRaw: key.key_raw,
db,
keyType: key.key_type,
ttl: key.ttl,
pathSegments,
});
continue;
}
let currentLevel = root;
const groupSegments: string[] = [];
for (const segment of pathSegments.slice(0, -1)) {
groupSegments.push(segment);
const groupId = buildGroupId(db, groupSegments);
let group = groupMap.get(groupId);
if (!group) {
group = {
kind: "group",
id: groupId,
label: segment,
pathSegments: [...groupSegments],
children: [],
};
groupMap.set(groupId, group);
currentLevel.push(group);
}
currentLevel = group.children;
}
currentLevel.push({
kind: "leaf",
id: buildLeafId(db, key.key_raw),
label: pathSegments[pathSegments.length - 1],
fullKeyDisplay: key.key_display,
keyRaw: key.key_raw,
db,
keyType: key.key_type,
ttl: key.ttl,
pathSegments,
});
}
return sortRedisTreeNodes(root);
}
export function collectExpandedGroupIds(nodes: RedisKeyTreeNode[]): Set<string> {
const ids = new Set<string>();
const visit = (entries: RedisKeyTreeNode[]) => {
for (const node of entries) {
if (node.kind !== "group") continue;
ids.add(node.id);
visit(node.children);
}
};
visit(nodes);
return ids;
}
export function flattenVisibleRedisKeyTree(
nodes: RedisKeyTreeNode[],
expandedGroupIds: ReadonlySet<string>,
depth = 0,
): RedisKeyTreeRow[] {
const rows: RedisKeyTreeRow[] = [];
for (const node of nodes) {
rows.push({ node, depth });
if (node.kind === "group" && expandedGroupIds.has(node.id)) {
rows.push(...flattenVisibleRedisKeyTree(node.children, expandedGroupIds, depth + 1));
}
}
return rows;
}
+35 -20
View File
@@ -258,15 +258,18 @@ export async function getAppVersion(): Promise<string> {
// --- Redis ---
export interface RedisKeyInfo {
key: string;
key_display: string;
key_raw: string;
key_type: string;
ttl: number;
}
export interface RedisValue {
key: string;
key_display: string;
key_raw: string;
key_type: string;
ttl: number;
value_is_binary: boolean;
value: any;
}
@@ -289,40 +292,52 @@ export async function redisScanKeys(
return invoke("redis_scan_keys", { connectionId, db, cursor, pattern, count });
}
export async function redisGetValue(connectionId: string, key: string): Promise<RedisValue> {
return invoke("redis_get_value", { connectionId, key });
export async function redisGetValue(connectionId: string, db: number, keyRaw: string): Promise<RedisValue> {
return invoke("redis_get_value", { connectionId, db, keyRaw });
}
export async function redisSetString(connectionId: string, key: string, value: string, ttl?: number): Promise<void> {
return invoke("redis_set_string", { connectionId, key, value, ttl });
export async function redisSetString(
connectionId: string,
db: number,
keyRaw: string,
value: string,
ttl?: number,
): Promise<void> {
return invoke("redis_set_string", { connectionId, db, keyRaw, value, ttl });
}
export async function redisDeleteKey(connectionId: string, key: string): Promise<void> {
return invoke("redis_delete_key", { connectionId, key });
export async function redisDeleteKey(connectionId: string, db: number, keyRaw: string): Promise<void> {
return invoke("redis_delete_key", { connectionId, db, keyRaw });
}
export async function redisHashSet(connectionId: string, key: string, field: string, value: string): Promise<void> {
return invoke("redis_hash_set", { connectionId, key, field, value });
export async function redisHashSet(
connectionId: string,
db: number,
keyRaw: string,
field: string,
value: string,
): Promise<void> {
return invoke("redis_hash_set", { connectionId, db, keyRaw, field, value });
}
export async function redisHashDel(connectionId: string, key: string, field: string): Promise<void> {
return invoke("redis_hash_del", { connectionId, key, field });
export async function redisHashDel(connectionId: string, db: number, keyRaw: string, field: string): Promise<void> {
return invoke("redis_hash_del", { connectionId, db, keyRaw, field });
}
export async function redisListPush(connectionId: string, key: string, value: string): Promise<void> {
return invoke("redis_list_push", { connectionId, key, value });
export async function redisListPush(connectionId: string, db: number, keyRaw: string, value: string): Promise<void> {
return invoke("redis_list_push", { connectionId, db, keyRaw, value });
}
export async function redisListRemove(connectionId: string, key: string, index: number): Promise<void> {
return invoke("redis_list_remove", { connectionId, key, index });
export async function redisListRemove(connectionId: string, db: number, keyRaw: string, index: number): Promise<void> {
return invoke("redis_list_remove", { connectionId, db, keyRaw, index });
}
export async function redisSetAdd(connectionId: string, key: string, member: string): Promise<void> {
return invoke("redis_set_add", { connectionId, key, member });
export async function redisSetAdd(connectionId: string, db: number, keyRaw: string, member: string): Promise<void> {
return invoke("redis_set_add", { connectionId, db, keyRaw, member });
}
export async function redisSetRemove(connectionId: string, key: string, member: string): Promise<void> {
return invoke("redis_set_remove", { connectionId, key, member });
export async function redisSetRemove(connectionId: string, db: number, keyRaw: string, member: string): Promise<void> {
return invoke("redis_set_remove", { connectionId, db, keyRaw, member });
}
// --- MongoDB ---