perf: use custom separator set by each server profile

fix: add duplicated key to tree view when update value
This commit is contained in:
tiny-craft 2023-07-17 11:49:49 +08:00
parent d976cd1731
commit ea2c24a5f9
3 changed files with 54 additions and 6 deletions

View File

@ -142,7 +142,7 @@ const onSaveValue = async () => {
props.name, props.name,
props.db, props.db,
props.keyPath, props.keyPath,
toLower(props.keyType), toLower(keyType),
editValue.value, editValue.value,
-1 -1
) )

View File

@ -263,8 +263,9 @@ const handleSelectContextMenu = (key) => {
case 'group_delete': case 'group_delete':
removeGroup(label) removeGroup(label)
break break
default:
console.warn('TODO: handle context menu:' + key)
} }
console.warn('TODO: handle context menu:' + key)
} }
const findSiblingsAndIndex = (node, nodes) => { const findSiblingsAndIndex = (node, nodes) => {

View File

@ -32,8 +32,6 @@ import {
import { ConnectionType } from '../consts/connection_type.js' import { ConnectionType } from '../consts/connection_type.js'
import useTabStore from './tab.js' import useTabStore from './tab.js'
const separator = ':'
const useConnectionStore = defineStore('connections', { const useConnectionStore = defineStore('connections', {
/** /**
* @typedef {Object} ConnectionItem * @typedef {Object} ConnectionItem
@ -61,6 +59,7 @@ const useConnectionStore = defineStore('connections', {
* @typedef {Object} ConnectionState * @typedef {Object} ConnectionState
* @property {string[]} groups * @property {string[]} groups
* @property {ConnectionItem[]} connections * @property {ConnectionItem[]} connections
* @property {Object.<string, ConnectionProfile>} serverProfile
* @property {Object.<string, DatabaseItem[]>} databases * @property {Object.<string, DatabaseItem[]>} databases
* @property {Object.<string, Map<string, DatabaseItem>>} nodeMap key format likes 'server#db', children key format likes 'key#type' * @property {Object.<string, Map<string, DatabaseItem>>} nodeMap key format likes 'server#db', children key format likes 'key#type'
*/ */
@ -72,6 +71,13 @@ const useConnectionStore = defineStore('connections', {
* @property {string} cmd * @property {string} cmd
*/ */
/**
* @typedef {Object} ConnectionProfile
* @property {string} defaultFilter
* @property {string} keySeparator
* @property {string} markColor
*/
/** /**
* *
* @returns {ConnectionState} * @returns {ConnectionState}
@ -80,6 +86,7 @@ const useConnectionStore = defineStore('connections', {
groups: [], // all group name set groups: [], // all group name set
connections: [], // all connections connections: [], // all connections
serverStats: {}, // current server status info serverStats: {}, // current server status info
serverProfile: {}, // all server profile
databases: {}, // all databases in opened connections group by server name databases: {}, // all databases in opened connections group by server name
nodeMap: {}, // all node in opened connections group by server+db and key+type nodeMap: {}, // all node in opened connections group by server+db and key+type
}), }),
@ -100,6 +107,7 @@ const useConnectionStore = defineStore('connections', {
} }
const conns = [] const conns = []
const groups = [] const groups = []
const profiles = {}
const { data = [{ groupName: '', connections: [] }] } = await ListConnection() const { data = [{ groupName: '', connections: [] }] } = await ListConnection()
for (const conn of data) { for (const conn of data) {
if (conn.type !== 'group') { if (conn.type !== 'group') {
@ -111,6 +119,11 @@ const useConnectionStore = defineStore('connections', {
type: ConnectionType.Server, type: ConnectionType.Server,
// isLeaf: false, // isLeaf: false,
}) })
profiles[conn.name] = {
defaultFilter: conn.defaultFilter,
keySeparator: conn.keySeparator,
markColor: conn.markColor,
}
} else { } else {
// custom group // custom group
groups.push(conn.name) groups.push(conn.name)
@ -132,9 +145,15 @@ const useConnectionStore = defineStore('connections', {
type: ConnectionType.Group, type: ConnectionType.Group,
children, children,
}) })
profiles[conn.name] = {
defaultFilter: conn.defaultFilter,
keySeparator: conn.keySeparator,
markColor: conn.markColor,
}
} }
} }
this.connections = conns this.connections = conns
this.serverProfile = profiles
this.groups = uniq(groups) this.groups = uniq(groups)
}, },
@ -147,6 +166,11 @@ const useConnectionStore = defineStore('connections', {
try { try {
const { data, success } = await GetConnection(name) const { data, success } = await GetConnection(name)
if (success) { if (success) {
this.serverProfile[name] = {
defaultFilter: data.defaultFilter,
keySeparator: data.keySeparator,
markColor: data.markColor,
}
return data return data
} }
} finally { } finally {
@ -517,6 +541,7 @@ const useConnectionStore = defineStore('connections', {
if (isEmpty(scanPrefix)) { if (isEmpty(scanPrefix)) {
scanPrefix = '*' scanPrefix = '*'
} else { } else {
const separator = this._getSeparator(connName)
if (!endsWith(prefix, separator + '*')) { if (!endsWith(prefix, separator + '*')) {
scanPrefix = prefix + separator + '*' scanPrefix = prefix + separator + '*'
} }
@ -541,6 +566,7 @@ const useConnectionStore = defineStore('connections', {
* @private * @private
*/ */
_deleteKeyNodes(connName, db, prefix) { _deleteKeyNodes(connName, db, prefix) {
const separator = this._getSeparator(connName)
const dbs = this.databases[connName] const dbs = this.databases[connName]
let node = dbs[db] let node = dbs[db]
const prefixPart = split(prefix, separator) const prefixPart = split(prefix, separator)
@ -562,6 +588,20 @@ const useConnectionStore = defineStore('connections', {
return false return false
}, },
/**
* get custom separator of connection
* @param server
* @returns {string}
* @private
*/
_getSeparator(server) {
const { keySeparator } = this.serverProfile[server] || { keySeparator: ':' }
if (isEmpty(keySeparator)) {
return ':'
}
return keySeparator
},
/** /**
* remove keys in db * remove keys in db
* @param {string} connName * @param {string} connName
@ -570,6 +610,7 @@ const useConnectionStore = defineStore('connections', {
* @private * @private
*/ */
_addKeyNodes(connName, db, keys) { _addKeyNodes(connName, db, keys) {
const separator = this._getSeparator(connName)
const dbs = this.databases[connName] const dbs = this.databases[connName]
if (dbs[db].children == null) { if (dbs[db].children == null) {
dbs[db].children = [] dbs[db].children = []
@ -612,6 +653,7 @@ const useConnectionStore = defineStore('connections', {
} else { } else {
// key // key
const nodeKey = `#${ConnectionType.RedisValue}/${handlePath}` const nodeKey = `#${ConnectionType.RedisValue}/${handlePath}`
const replaceKey = nodeMap.has(nodeKey)
const selectedNode = { const selectedNode = {
key: `${connName}/db${db}${nodeKey}`, key: `${connName}/db${db}${nodeKey}`,
label: keyPart[i], label: keyPart[i],
@ -622,7 +664,9 @@ const useConnectionStore = defineStore('connections', {
isLeaf: true, isLeaf: true,
} }
nodeMap.set(nodeKey, selectedNode) nodeMap.set(nodeKey, selectedNode)
children.push(selectedNode) if (!replaceKey) {
children.push(selectedNode)
}
} }
} }
} }
@ -667,13 +711,14 @@ const useConnectionStore = defineStore('connections', {
* @param {string} connName * @param {string} connName
* @param {number} db * @param {number} db
* @param {string} key * @param {string} key
* @param {number} keyType * @param {string} keyType
* @param {any} value * @param {any} value
* @param {number} ttl * @param {number} ttl
* @returns {Promise<{[msg]: string, success: boolean}>} * @returns {Promise<{[msg]: string, success: boolean}>}
*/ */
async setKey(connName, db, key, keyType, value, ttl) { async setKey(connName, db, key, keyType, value, ttl) {
try { try {
console.log(connName, db, key, keyType, value, ttl)
const { data, success, msg } = await SetKeyValue(connName, db, key, keyType, value, ttl) const { data, success, msg } = await SetKeyValue(connName, db, key, keyType, value, ttl)
if (success) { if (success) {
// update tree view data // update tree view data
@ -1027,6 +1072,7 @@ const useConnectionStore = defineStore('connections', {
_deleteKeyNode(connName, db, key) { _deleteKeyNode(connName, db, key) {
const dbs = this.databases[connName] const dbs = this.databases[connName]
const dbDetail = get(dbs, db, {}) const dbDetail = get(dbs, db, {})
const separator = this._getSeparator(connName)
if (dbDetail == null) { if (dbDetail == null) {
return return
@ -1124,6 +1170,7 @@ const useConnectionStore = defineStore('connections', {
// for (const key of keys) { // for (const key of keys) {
// await this._deleteKeyNode(connName, db, key) // await this._deleteKeyNode(connName, db, key)
// } // }
const separator = this._getSeparator(connName)
if (endsWith(prefix, '*')) { if (endsWith(prefix, '*')) {
prefix = prefix.substring(0, prefix.length - 1) prefix = prefix.substring(0, prefix.length - 1)
} }