perf: save last selected database and restore for next connection

This commit is contained in:
Lykin 2023-12-02 02:03:29 +08:00
parent 2d3225dbcf
commit 7934fc275a
6 changed files with 52 additions and 5 deletions

View File

@ -198,8 +198,9 @@ func (b *browserService) OpenConnection(name string) (resp types.JSResp) {
resp.Success = true resp.Success = true
resp.Data = map[string]any{ resp.Data = map[string]any{
"db": dbs, "db": dbs,
"view": selConn.KeyView, "view": selConn.KeyView,
"lastDB": selConn.LastDB,
} }
return return
} }

View File

@ -358,3 +358,20 @@ func (c *connectionService) DeleteGroup(name string, includeConn bool) (resp typ
resp.Success = true resp.Success = true
return return
} }
// SaveLastDB save last selected database index
func (c *connectionService) SaveLastDB(name string, db int) (resp types.JSResp) {
param := c.conns.GetConnection(name)
if param == nil {
resp.Msg = "no connection named \"" + name + "\""
return
}
param.LastDB = db
if err := c.conns.UpdateConnection(name, param.ConnectionConfig); err != nil {
resp.Msg = "save connection fail:" + err.Error()
return
}
resp.Success = true
return
}

View File

@ -5,6 +5,7 @@ type ConnectionCategory int
type ConnectionConfig struct { type ConnectionConfig struct {
Name string `json:"name" yaml:"name"` Name string `json:"name" yaml:"name"`
Group string `json:"group,omitempty" yaml:"-"` Group string `json:"group,omitempty" yaml:"-"`
LastDB int `json:"lastDB" yaml:"last_db"`
Addr string `json:"addr,omitempty" yaml:"addr,omitempty"` Addr string `json:"addr,omitempty" yaml:"addr,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"` Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"` Username string `json:"username,omitempty" yaml:"username,omitempty"`

View File

@ -3,7 +3,7 @@ import { useThemeVars } from 'naive-ui'
import BrowserTree from './BrowserTree.vue' import BrowserTree from './BrowserTree.vue'
import IconButton from '@/components/common/IconButton.vue' import IconButton from '@/components/common/IconButton.vue'
import useTabStore from 'stores/tab.js' import useTabStore from 'stores/tab.js'
import { computed, onMounted, reactive, ref, unref, watch } from 'vue' import { computed, nextTick, onMounted, reactive, ref, unref, watch } from 'vue'
import { get, map } from 'lodash' import { get, map } from 'lodash'
import Refresh from '@/components/icons/Refresh.vue' import Refresh from '@/components/icons/Refresh.vue'
import useDialogStore from 'stores/dialog.js' import useDialogStore from 'stores/dialog.js'
@ -19,6 +19,7 @@ import { useRender } from '@/utils/render.js'
import RedisTypeSelector from '@/components/common/RedisTypeSelector.vue' import RedisTypeSelector from '@/components/common/RedisTypeSelector.vue'
import { types } from '@/consts/support_redis_type.js' import { types } from '@/consts/support_redis_type.js'
import Plus from '@/components/icons/Plus.vue' import Plus from '@/components/icons/Plus.vue'
import useConnectionStore from 'stores/connections.js'
const themeVars = useThemeVars() const themeVars = useThemeVars()
const i18n = useI18n() const i18n = useI18n()
@ -26,6 +27,7 @@ const dialogStore = useDialogStore()
// const prefStore = usePreferencesStore() // const prefStore = usePreferencesStore()
const tabStore = useTabStore() const tabStore = useTabStore()
const browserStore = useBrowserStore() const browserStore = useBrowserStore()
const connectionStore = useConnectionStore()
const render = useRender() const render = useRender()
const currentName = computed(() => get(tabStore.currentTab, 'name', '')) const currentName = computed(() => get(tabStore.currentTab, 'name', ''))
const browserTreeRef = ref(null) const browserTreeRef = ref(null)
@ -159,6 +161,8 @@ watch(
browserTreeRef.value?.resetExpandKey(currentName.value, db) browserTreeRef.value?.resetExpandKey(currentName.value, db)
fullyLoaded.value = await browserStore.loadMoreKeys(currentName.value, db) fullyLoaded.value = await browserStore.loadMoreKeys(currentName.value, db)
browserTreeRef.value?.refreshTree() browserTreeRef.value?.refreshTree()
nextTick().then(() => connectionStore.saveLastDB(currentName.value, db))
} catch (e) { } catch (e) {
$message.error(e.message) $message.error(e.message)
} finally { } finally {

View File

@ -255,11 +255,12 @@ const useBrowserStore = defineStore('browser', {
// if (connNode == null) { // if (connNode == null) {
// throw new Error('no such connection') // throw new Error('no such connection')
// } // }
const { db, view = KeyViewType.Tree } = data const { db, view = KeyViewType.Tree, lastDB } = data
if (isEmpty(db)) { if (isEmpty(db)) {
throw new Error('no db loaded') throw new Error('no db loaded')
} }
const dbs = [] const dbs = []
let containLastDB = false
for (let i = 0; i < db.length; i++) { for (let i = 0; i < db.length; i++) {
this._getNodeMap(name, i).clear() this._getNodeMap(name, i).clear()
this._getKeySet(name, i).clear() this._getKeySet(name, i).clear()
@ -274,10 +275,18 @@ const useBrowserStore = defineStore('browser', {
isLeaf: false, isLeaf: false,
children: undefined, children: undefined,
}) })
if (db[i].index === lastDB) {
containLastDB = true
}
} }
this.databases[name] = dbs this.databases[name] = dbs
this.viewType[name] = view this.viewType[name] = view
this.openedDB[name] = get(dbs, '0.db', 0) // get last selected db
if (containLastDB) {
this.openedDB[name] = lastDB
} else {
this.openedDB[name] = get(dbs, '0.db', 0)
}
}, },
/** /**

View File

@ -8,6 +8,7 @@ import {
ListConnection, ListConnection,
RenameGroup, RenameGroup,
SaveConnection, SaveConnection,
SaveLastDB,
SaveSortedConnection, SaveSortedConnection,
} from 'wailsjs/go/services/connectionService.js' } from 'wailsjs/go/services/connectionService.js'
import { ConnectionType } from '@/consts/connection_type.js' import { ConnectionType } from '@/consts/connection_type.js'
@ -333,6 +334,20 @@ const useConnectionStore = defineStore('connections', {
return { success: true } return { success: true }
}, },
/**
* save last selected database
* @param {string} name
* @param {number} db
* @return {Promise<{success: boolean, [msg]: string}>}
*/
async saveLastDB(name, db) {
const { success, msg } = await SaveLastDB(name, db)
if (!success) {
return { success: false, msg }
}
return { success: true }
},
/** /**
* get default key filter pattern by server name * get default key filter pattern by server name
* @param name * @param name