Browse Source

fix: set database when connect, refresh page (#394)

Signed-off-by: nameczz <zizhao.chen@zilliz.com>
nameczz 1 year ago
parent
commit
b1f9bba0e7

+ 4 - 2
client/src/context/Data.tsx

@@ -1,7 +1,7 @@
 import { createContext, useEffect, useState, useContext } from 'react';
 import { Database, User, MilvusService } from '@/http';
 import { parseJson, getNode, getSystemConfigs } from '@/utils';
-import { MILVUS_NODE_TYPE } from '@/consts';
+import { LAST_TIME_DATABASE, MILVUS_NODE_TYPE } from '@/consts';
 import { authContext } from '@/context';
 import { DataContextType } from './Types';
 
@@ -16,7 +16,9 @@ export const dataContext = createContext<DataContextType>({
 const { Provider } = dataContext;
 export const DataProvider = (props: { children: React.ReactNode }) => {
   const { isAuth } = useContext(authContext);
-  const [database, setDatabase] = useState<string>('default');
+  const [database, setDatabase] = useState<string>(
+    window.localStorage.getItem(LAST_TIME_DATABASE) || 'default'
+  );
   const [databases, setDatabases] = useState<string[]>(['default']);
   const [data, setData] = useState<any>({});
 

+ 3 - 2
client/src/pages/connect/AuthForm.tsx

@@ -247,7 +247,8 @@ export const AuthForm = (props: any) => {
     window.localStorage.setItem(LOGIN_USERNAME, form.username);
     // store address for next time using
     window.localStorage.setItem(LAST_TIME_ADDRESS, form.address);
-    window.localStorage.setItem(LAST_TIME_DATABASE, form.database);
+    window.localStorage.setItem(LAST_TIME_DATABASE, result.database);
+
     // redirect to homepage
     navigate('/');
   };
@@ -306,7 +307,7 @@ export const AuthForm = (props: any) => {
             component={GitHubIcon}
             className={classes.icon}
           />
-           {btnTrans('star')}
+          {btnTrans('star')}
         </a>
       </section>
     </form>

+ 9 - 17
server/src/milvus/milvus.service.ts

@@ -13,6 +13,7 @@ import { clientCache } from '../app';
 
 export class MilvusService {
   private databaseService: DatabasesService;
+  private DEFAULT_DATABASE = 'default';
 
   constructor() {
     this.databaseService = new DatabasesService();
@@ -35,7 +36,12 @@ export class MilvusService {
     database?: string;
   }) {
     // Destructure the data object to get the connection details
-    const { address, username, password, database } = data;
+    const {
+      address,
+      username,
+      password,
+      database = this.DEFAULT_DATABASE,
+    } = data;
     // Format the address to remove the http prefix
     const milvusAddress = MilvusService.formatAddress(address);
 
@@ -95,26 +101,12 @@ export class MilvusService {
         }),
       });
 
-      // Create a new database service and check if the specified database exists
-      let hasDatabase = false;
-      try {
-        hasDatabase = await this.databaseService.hasDatabase(
-          milvusClient.clientId,
-          database
-        );
-      } catch (_) {
-        // ignore error
-      }
-
-      // if database exists, use this db
-      if (hasDatabase) {
-        await this.databaseService.use(milvusClient.clientId, database);
-      }
+      await this.databaseService.use(milvusClient.clientId, database);
 
       // Return the address and the database (if it exists, otherwise return 'default')
       return {
         address,
-        database: hasDatabase ? database : 'default',
+        database: database || this.DEFAULT_DATABASE,
         clientId: milvusClient.clientId,
       };
     } catch (error) {