Kaynağa Gözat

支持数据库目录切换

gaoshuaixing 2 yıl önce
ebeveyn
işleme
7433f1f956

+ 0 - 3
electron/addon/autoUpdater/index.js

@@ -55,9 +55,6 @@ class AutoUpdaterAddon {
   
     // 是否后台自动下载
     autoUpdater.autoDownload = updateConfig.force ? true : false;
-    // if (Utils.getEnv() == 'local') {
-    //   autoUpdater.updateConfigPath = path.join(__dirname, '../../out/dev-app-update.yml')
-    // }
   
     try {
       autoUpdater.setFeedURL(updateConfig.options);

+ 6 - 0
electron/controller/example.js

@@ -107,6 +107,12 @@ class ExampleController extends Controller {
       case 'get' :
         data.result = await service.storage.getTestDataSqlite(paramsObj.search_age);
         break;
+      case 'getDataDir' :
+        data.result = await service.storage.getDataDir();
+        break;
+      case 'setDataDir' :
+        data.result = await service.storage.setCustomDataDir(paramsObj.data_dir);
+        break;            
     }
 
     data.all_list = await service.storage.getAllTestDataSqlite();

+ 36 - 6
electron/service/storage.js

@@ -3,6 +3,7 @@
 const Service = require('ee-core').Service;
 const Storage = require('ee-core').Storage;
 const _ = require('lodash');
+const path = require('path');
 
 /**
  * 数据存储
@@ -15,19 +16,17 @@ class StorageService extends Service {
 
     // lowdb数据库
     this.systemDB = Storage.JsonDB.connection('system');
+
     let lowdbOptions = {
       driver: 'lowdb'
     }
     this.demoDB = Storage.JsonDB.connection('demo', lowdbOptions);  
-    this.systemDBKey = {
-      cache: 'cache'
-    };
     this.demoDBKey = {
-      preferences: 'preferences',
       test_data: 'test_data'
     };
 
     // sqlite数据库
+    this.sqliteFile = 'sqlite-demo.db';
     let sqliteOptions = {
       driver: 'sqlite',
       default: {
@@ -35,7 +34,7 @@ class StorageService extends Service {
         verbose: console.log // 打印sql语法
       }
     }
-    this.demoSqliteDB = Storage.JsonDB.connection('sqlite-demo.db', sqliteOptions);
+    this.demoSqliteDB = Storage.JsonDB.connection(this.sqliteFile, sqliteOptions);
   }
 
   /*
@@ -225,7 +224,38 @@ class StorageService extends Service {
     const allUser =  selectAllUser.all();
     //console.log("select allUser:", allUser);
     return allUser;
-  }  
+  }
+  
+  /*
+   * get data dir (sqlite)
+   */
+  async getDataDir() {
+    const dir = this.demoSqliteDB.getStorageDir();    
+
+    return dir;
+  } 
+
+  /*
+   * set custom data dir (sqlite)
+   */
+  async setCustomDataDir(dir) {
+    if (_.isEmpty(dir)) {
+      return;
+    }
+
+    // the absolute path of the db file
+    const dbFile = path.join(dir, this.sqliteFile);
+    const sqliteOptions = {
+      driver: 'sqlite',
+      default: {
+        timeout: 6000,
+        verbose: console.log
+      }
+    }
+    this.demoSqliteDB = Storage.JsonDB.connection(dbFile, sqliteOptions);    
+
+    return;
+  }
 }
 
 StorageService.toString = () => '[class StorageService]';

+ 46 - 4
frontend/src/views/base/sqlitedb/Index.vue

@@ -20,7 +20,24 @@
     </div>
     <div class="one-block-1">
       <span>
-        2. 测试数据
+        2. 数据目录
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="12">
+          <a-input v-model="data_dir" :value="data_dir" addon-before="数据目录" />
+        </a-col>
+        <a-col :span="12">
+          <a-button @click="selectDir">
+            修改目录
+          </a-button>
+        </a-col>
+      </a-row>
+    </div>     
+    <div class="one-block-1">
+      <span>
+        3. 测试数据
       </span>
     </div>  
     <div class="one-block-2">
@@ -32,7 +49,7 @@
     </div>    
     <div class="one-block-1">
       <span>
-        3. 添加数据
+        4. 添加数据
       </span>
     </div>  
     <div class="one-block-2">
@@ -144,26 +161,51 @@ export default {
       update_name: '李四',
       update_age: 31,
       delete_name: '李四',
-      all_list: ['空']
+      all_list: ['空'],
+      data_dir: ''
     };
   },
   mounted () {
+    this.init();
     this.getAllTestData();
   },
   methods: {
+    init() {
+      const params = {
+        action: 'getDataDir',
+      }
+      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+        this.data_dir = res.result;
+      }) 
+    },
     getAllTestData () {
       const self = this;
       const params = {
         action: 'all',
       }
       this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
-        console.log('res:', res);
         if (res.all_list.length == 0) {
           return false;
         }
         self.all_list = res.all_list;
       }) 
     },
+    selectDir() {
+      this.$ipcInvoke(ipcApiRoute.selectFolder, '').then(r => {
+        this.data_dir = r;
+        // 修改数据目录
+        this.modifyDataDir(r);
+      })
+    },
+    modifyDataDir(dir) {
+      const params = {
+        action: 'setDataDir',
+        data_dir: dir
+      }
+      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+        this.all_list = res.all_list;
+      }) 
+    },
     sqlitedbOperation (ac) {
       const self = this;
       const params = {