diff --git a/apps/desktop/src/components/redis/RedisKeyBrowser.vue b/apps/desktop/src/components/redis/RedisKeyBrowser.vue index 97db860ae6..ad58b4eef2 100644 --- a/apps/desktop/src/components/redis/RedisKeyBrowser.vue +++ b/apps/desktop/src/components/redis/RedisKeyBrowser.vue @@ -25,6 +25,7 @@ import { Badge } from "@/components/ui/badge"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { Switch } from "@/components/ui/switch"; import DangerConfirmDialog from "@/components/editor/DangerConfirmDialog.vue"; import RedisValueViewer from "./RedisValueViewer.vue"; import * as api from "@/lib/api"; @@ -54,7 +55,14 @@ const settingsStore = useSettingsStore(); const editorFontFamilyStyle = useEditorFontFamilyStyle(); type RedisSearchMode = "key" | "value"; -type RedisCreateKeyType = "string" | "hash" | "list" | "set" | "zset"; +type RedisCreateKeyType = "string" | "hash" | "list" | "set" | "zset" | "stream" | "json"; + +interface CreateKeyEntry { + id: number; + value: string; + field?: string; + score?: string; +} type RedisSidePanel = "detail" | "command"; type RedisCommandHistoryEntry = { id: number; @@ -101,6 +109,13 @@ const createKeyValue = ref(""); const createKeyField = ref(""); const createKeyScore = ref("0"); const createKeyError = ref(""); +const createKeyTtl = ref(""); +const createKeyEntries = ref([]); +const createKeyRawMode = ref(false); +const createKeyEntryId = ref("*"); +const jsonModuleAvailable = ref(null); +const checkingJsonModule = ref(false); +let nextEntryId = 0; let searchRequestId = 0; let redisBrowserIsActive = true; let redisDbFlushedListenerRegistered = false; @@ -152,6 +167,8 @@ const createKeyTypeOptions = computed<{ value: RedisCreateKeyType; label: string { value: "list", label: "List" }, { value: "set", label: "Set" }, { value: "zset", label: "Sorted Set" }, + { value: "stream", label: "Stream" }, + { value: "json", label: "JSON" }, ]); const visibleRows = computed(() => flattenVisibleRedisKeyTree(treeKeys.value, expandedGroupIds.value).map((row) => ({ @@ -445,6 +462,24 @@ async function openCommandPanel() { getCommandInput()?.focus(); } +function makeEntry(): CreateKeyEntry { + return { id: nextEntryId++, value: "", field: "", score: "0" }; +} + +function resetEntries() { + createKeyEntries.value = [makeEntry()]; +} + +function addEntry() { + createKeyEntries.value.push(makeEntry()); +} + +function removeEntry(idx: number) { + if (createKeyEntries.value.length > 1) { + createKeyEntries.value.splice(idx, 1); + } +} + function resetCreateKeyForm() { createKeyName.value = ""; createKeyType.value = "string"; @@ -452,6 +487,41 @@ function resetCreateKeyForm() { createKeyField.value = ""; createKeyScore.value = "0"; createKeyError.value = ""; + createKeyTtl.value = ""; + createKeyRawMode.value = false; + createKeyEntryId.value = "*"; + jsonModuleAvailable.value = null; + checkingJsonModule.value = false; + resetEntries(); +} + +function onCreateKeyTypeChange(type: any) { + createKeyType.value = (type || "string") as RedisCreateKeyType; + createKeyRawMode.value = false; + jsonModuleAvailable.value = null; + checkingJsonModule.value = false; + resetEntries(); + if (createKeyType.value === "json") { + createKeyError.value = ""; + checkingJsonModule.value = true; + api + .redisCheckJsonModule(props.connectionId, props.db) + .then((ok) => { + jsonModuleAvailable.value = ok; + if (!ok) { + createKeyError.value = t("redis.jsonModuleNotAvailable"); + } + }) + .catch(() => { + jsonModuleAvailable.value = false; + createKeyError.value = t("redis.jsonModuleNotAvailable"); + }) + .finally(() => { + checkingJsonModule.value = false; + }); + } else { + createKeyError.value = ""; + } } function openCreateKeyDialog() { @@ -498,33 +568,72 @@ async function createRedisKey() { toast(t("redis.createKeyNameRequired"), 3000); return; } - if (createKeyType.value === "hash" && !createKeyField.value.trim()) { - createKeyError.value = t("redis.createFieldRequired"); - toast(t("redis.createFieldRequired"), 3000); - return; - } - const score = Number.parseFloat(createKeyScore.value || "0"); - if (createKeyType.value === "zset" && Number.isNaN(score)) { - createKeyError.value = t("redis.createScoreInvalid"); - toast(t("redis.createScoreInvalid"), 3000); - return; - } creatingKey.value = true; createKeyError.value = ""; try { const keyRaw = redisKeyTextToRaw(keyName); - if (createKeyType.value === "string") { - await api.redisSetString(props.connectionId, props.db, keyRaw, createKeyValue.value); - } else if (createKeyType.value === "hash") { - await api.redisHashSet(props.connectionId, props.db, keyRaw, createKeyField.value, createKeyValue.value); - } else if (createKeyType.value === "list") { - await api.redisListPush(props.connectionId, props.db, keyRaw, createKeyValue.value); - } else if (createKeyType.value === "set") { - await api.redisSetAdd(props.connectionId, props.db, keyRaw, createKeyValue.value); - } else if (createKeyType.value === "zset") { - await api.redisZadd(props.connectionId, props.db, keyRaw, createKeyValue.value, score); + const ttl = createKeyTtl.value ? Number.parseInt(createKeyTtl.value) || undefined : undefined; + + if (createKeyType.value === "string" || createKeyType.value === "json" || createKeyRawMode.value) { + // Raw text/JSON mode — single value + if (createKeyType.value === "string") { + await api.redisSetString(props.connectionId, props.db, keyRaw, createKeyValue.value, ttl); + } else if (createKeyType.value === "json") { + await api.redisJsonSet(props.connectionId, props.db, keyRaw, createKeyValue.value, ttl); + } else if (createKeyType.value === "hash") { + await api.redisHashSet(props.connectionId, props.db, keyRaw, createKeyField.value, createKeyValue.value, ttl); + } else if (createKeyType.value === "list") { + await api.redisListPush(props.connectionId, props.db, keyRaw, createKeyValue.value, ttl); + } else if (createKeyType.value === "set") { + await api.redisSetAdd(props.connectionId, props.db, keyRaw, createKeyValue.value, ttl); + } else if (createKeyType.value === "zset") { + const score = Number.parseFloat(createKeyScore.value || "0"); + await api.redisZadd(props.connectionId, props.db, keyRaw, createKeyValue.value, score, ttl); + } + } else { + // Structured entries mode — insert each entry, then set TTL once + if (createKeyType.value === "hash") { + for (const entry of createKeyEntries.value) { + if (entry.field && entry.field.trim()) { + await api.redisHashSet(props.connectionId, props.db, keyRaw, entry.field, entry.value); + } + } + } else if (createKeyType.value === "list") { + for (const entry of createKeyEntries.value) { + if (entry.value) { + await api.redisListPush(props.connectionId, props.db, keyRaw, entry.value); + } + } + } else if (createKeyType.value === "set") { + for (const entry of createKeyEntries.value) { + if (entry.value) { + await api.redisSetAdd(props.connectionId, props.db, keyRaw, entry.value); + } + } + } else if (createKeyType.value === "zset") { + for (const entry of createKeyEntries.value) { + if (entry.value) { + const s = Number.parseFloat(entry.score || "0"); + if (!Number.isNaN(s)) { + await api.redisZadd(props.connectionId, props.db, keyRaw, entry.value, s); + } + } + } + } else if (createKeyType.value === "stream") { + const fields: [string, string][] = createKeyEntries.value + .filter((e) => e.field && e.field.trim()) + .map((e) => [e.field!.trim(), e.value]); + if (fields.length > 0) { + const entryId = createKeyEntryId.value.trim() || "*"; + await api.redisStreamAdd(props.connectionId, props.db, keyRaw, entryId, fields, ttl); + } + } + if (ttl) { + await api.redisSetTtl(props.connectionId, props.db, keyRaw, ttl); + } } + const created = await api.redisGetValue(props.connectionId, props.db, keyRaw); upsertCreatedKey(created); showCreateKeyDialog.value = false; @@ -1018,10 +1127,7 @@ defineExpose({ focusSearch }); -