From 87fad22a5175febe96a0c96a892ef989ca7d280b Mon Sep 17 00:00:00 2001 From: gaoshuaixing Date: Fri, 14 Jul 2023 18:01:36 +0800 Subject: [PATCH] demo db --- electron/controller/framework.js | 56 +++--- electron/service/database/jsondb.js | 112 ++++++++++++ electron/service/database/sqlitedb.js | 163 ++++++++++++++++++ frontend/src/api/main.js | 9 +- frontend/src/router/routerMap.js | 6 +- frontend/src/router/subMenu.js | 2 +- .../views/framework/{db => jsondb}/Index.vue | 4 +- .../src/views/framework/sqlitedb/Index.vue | 8 +- 8 files changed, 325 insertions(+), 35 deletions(-) create mode 100644 electron/service/database/jsondb.js create mode 100644 electron/service/database/sqlitedb.js rename frontend/src/views/framework/{db => jsondb}/Index.vue (97%) diff --git a/electron/controller/framework.js b/electron/controller/framework.js index 1cb0897..7ee327d 100644 --- a/electron/controller/framework.js +++ b/electron/controller/framework.js @@ -32,31 +32,31 @@ class FrameworkController extends Controller { /** * json数据库操作 */ - async dbOperation(args) { - const paramsObj = args; - //Log.info('eeeee paramsObj:', paramsObj); + async jsondbOperation(args) { + const { action, info, delete_name, update_name, update_age, search_age } = args; + const data = { - action: paramsObj.action, + action, result: null, all_list: [] }; - switch (paramsObj.action) { + switch (action) { case 'add' : - data.result = await Services.get('storage').addTestData(paramsObj.info); + data.result = await Services.get('database.jsondb').addTestData(info); break; case 'del' : - data.result = await Services.get('storage').delTestData(paramsObj.delete_name); + data.result = await Services.get('database.jsondb').delTestData(delete_name); break; case 'update' : - data.result = await Services.get('storage').updateTestData(paramsObj.update_name, paramsObj.update_age); + data.result = await Services.get('database.jsondb').updateTestData(update_name, update_age); break; case 'get' : - data.result = await Services.get('storage').getTestData(paramsObj.search_age); + data.result = await Services.get('database.jsondb').getTestData(search_age); break; } - data.all_list = await Services.get('storage').getAllTestData(); + data.all_list = await Services.get('database.jsondb').getAllTestData(); return data; } @@ -65,36 +65,46 @@ class FrameworkController extends Controller { * sqlite数据库操作 */ async sqlitedbOperation(args) { - const paramsObj = args; - //Log.info('eeeee paramsObj:', paramsObj); + const { action, info, delete_name, update_name, update_age, search_age } = args; + const data = { - action: paramsObj.action, + action, result: null, - all_list: [] + all_list: [], + code: 0 }; - - switch (paramsObj.action) { + + try { + // test + Services.get('database.sqlitedb').getDataDir(); + } catch (err) { + console.log(err); + data.code = -1; + return data; + } + + switch (action) { case 'add' : - data.result = await Services.get('storage').addTestDataSqlite(paramsObj.info);; + data.result = await Services.get('database.sqlitedb').addTestDataSqlite(info);; break; case 'del' : - data.result = await Services.get('storage').delTestDataSqlite(paramsObj.delete_name);; + data.result = await Services.get('database.sqlitedb').delTestDataSqlite(delete_name);; break; case 'update' : - data.result = await Services.get('storage').updateTestDataSqlite(paramsObj.update_name, paramsObj.update_age); + data.result = await Services.get('database.sqlitedb').updateTestDataSqlite(update_name, update_age); break; case 'get' : - data.result = await Services.get('storage').getTestDataSqlite(paramsObj.search_age); + data.result = await Services.get('database.sqlitedb').getTestDataSqlite(search_age); break; case 'getDataDir' : - data.result = await Services.get('storage').getDataDir(); + data.result = await Services.get('database.sqlitedb').getDataDir(); break; case 'setDataDir' : - data.result = await Services.get('storage').setCustomDataDir(paramsObj.data_dir); + data.result = await Services.get('database.sqlitedb').setCustomDataDir(data_dir); break; } - data.all_list = await Services.get('storage').getAllTestDataSqlite(); + data.all_list = await Services.get('database.sqlitedb').getAllTestDataSqlite(); return data; } diff --git a/electron/service/database/jsondb.js b/electron/service/database/jsondb.js new file mode 100644 index 0000000..724123b --- /dev/null +++ b/electron/service/database/jsondb.js @@ -0,0 +1,112 @@ +'use strict'; + +const { Service } = require('ee-core'); +const Storage = require('ee-core/storage'); +const _ = require('lodash'); + +/** + * json数据存储 + * @class + */ +class JsondbService extends Service { + + constructor (ctx) { + super(ctx); + + // jsondb数据库 + this.demoDB = Storage.connection('demo'); + this.demoDBKey = { + test_data: 'test_data' + }; + } + + /* + * 增 Test data + */ + async addTestData(user) { + const key = this.demoDBKey.test_data; + if (!this.demoDB.db.has(key).value()) { + this.demoDB.db.set(key, []).write(); + } + + const data = this.demoDB.db + .get(key) + .push(user) + .write(); + + return data; + } + + /* + * 删 Test data + */ + async delTestData(name = '') { + const key = this.demoDBKey.test_data; + const data = this.demoDB.db + .get(key) + .remove({name: name}) + .write(); + + return data; + } + + /* + * 改 Test data + */ + async updateTestData(name= '', age = 0) { + const key = this.demoDBKey.test_data; + const data = this.demoDB.db + .get(key) + .find({name: name}) // 修改找到的第一个数据,貌似无法批量修改 todo + .assign({age: age}) + .write(); + + return data; + } + + /* + * 查 Test data + */ + async getTestData(age = 0) { + const key = this.demoDBKey.test_data; + let data = this.demoDB.db + .get(key) + //.find({age: age}) 查找单个 + .filter(function(o) { + let isHas = true; + isHas = age === o.age ? true : false; + return isHas; + }) + //.orderBy(['age'], ['name']) 排序 + //.slice(0, 10) 分页 + .value(); + + if (_.isEmpty(data)) { + data = [] + } + + return data; + } + + /* + * all Test data + */ + async getAllTestData() { + const key = this.demoDBKey.test_data; + if (!this.demoDB.db.has(key).value()) { + this.demoDB.db.set(key, []).write(); + } + let data = this.demoDB.db + .get(key) + .value(); + + if (_.isEmpty(data)) { + data = [] + } + + return data; + } +} + +JsondbService.toString = () => '[class JsondbService]'; +module.exports = JsondbService; diff --git a/electron/service/database/sqlitedb.js b/electron/service/database/sqlitedb.js new file mode 100644 index 0000000..7a18d12 --- /dev/null +++ b/electron/service/database/sqlitedb.js @@ -0,0 +1,163 @@ +'use strict'; + +const { Service } = require('ee-core'); +const Storage = require('ee-core/storage'); +const _ = require('lodash'); +const path = require('path'); + +/** + * sqlite数据存储 + * @class + */ +class SqlitedbService extends Service { + + constructor (ctx) { + super(ctx); + + this.sqliteFile = 'sqlite-demo.db'; + let sqliteOptions = { + driver: 'sqlite', + default: { + timeout: 6000, + verbose: console.log // 打印sql语法 + } + } + this.demoSqliteDB = Storage.connection(this.sqliteFile, sqliteOptions); + } + + /* + * 检查并创建表 (sqlite) + */ + async checkAndCreateTableSqlite(tableName = '') { + if (_.isEmpty(tableName)) { + throw new Error(`table name is required`); + } + // 检查表是否存在 + const userTable = this.demoSqliteDB.db.prepare('SELECT * FROM sqlite_master WHERE type=? AND name = ?'); + const result = userTable.get('table', tableName); + //console.log('result:', result); + if (result) { + return; + } + + // 创建表 + const create_table_user = + `CREATE TABLE ${tableName} + ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name CHAR(50) NOT NULL, + age INT + );` + this.demoSqliteDB.db.exec(create_table_user); + + } + + /* + * 增 Test data (sqlite) + */ + async addTestDataSqlite(data) { + //console.log("add data:", data); + + let table = 'user'; + await this.checkAndCreateTableSqlite(table); + + const insert = this.demoSqliteDB.db.prepare(`INSERT INTO ${table} (name, age) VALUES (@name, @age)`); + insert.run(data); + + return true; + } + + /* + * 删 Test data (sqlite) + */ + async delTestDataSqlite(name = '') { + //console.log("delete name:", name); + + let table = 'user'; + await this.checkAndCreateTableSqlite(table); + + const delUser = this.demoSqliteDB.db.prepare(`DELETE FROM ${table} WHERE name = ?`); + delUser.run(name); + + return true; + } + + /* + * 改 Test data (sqlite) + */ + async updateTestDataSqlite(name= '', age = 0) { + //console.log("update :", {name, age}); + + let table = 'user'; + await this.checkAndCreateTableSqlite(table); + + const updateUser = this.demoSqliteDB.db.prepare(`UPDATE ${table} SET age = ? WHERE name = ?`); + updateUser.run(age, name); + + return true; + } + + /* + * 查 Test data (sqlite) + */ + async getTestDataSqlite(age = 0) { + //console.log("select :", {age}); + + let table = 'user'; + await this.checkAndCreateTableSqlite(table); + + const selectUser = this.demoSqliteDB.db.prepare(`SELECT * FROM ${table} WHERE age = @age`); + const users = selectUser.all({age: age}); + //console.log("select users:", users); + return users; + } + + /* + * all Test data (sqlite) + */ + async getAllTestDataSqlite() { + //console.log("select all user"); + + let table = 'user'; + await this.checkAndCreateTableSqlite(table); + + const selectAllUser = this.demoSqliteDB.db.prepare(`SELECT * FROM ${table} `); + 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.connection(dbFile, sqliteOptions); + + return; + } +} + +SqlitedbService.toString = () => '[class SqlitedbService]'; +module.exports = SqlitedbService; diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index a7deca1..a7838d6 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -1,5 +1,5 @@ -import storage from 'store2' import request from '@/utils/request' +import storage from 'store2' /** * 路由定义(主进程与渲染进程通信频道定义) @@ -9,7 +9,7 @@ const ipcApiRoute = { test: 'controller.example.test', checkForUpdater: 'controller.framework.checkForUpdater', downloadApp: 'controller.framework.downloadApp', - dbOperation: 'controller.framework.dbOperation', + jsondbOperation: 'controller.framework.jsondbOperation', sqlitedbOperation: 'controller.framework.sqlitedbOperation', uploadFile: 'controller.framework.uploadFile', checkHttpServer: 'controller.framework.checkHttpServer', @@ -82,7 +82,6 @@ const requestHttp = (uri, parameter) => { } export { - ipcApiRoute, - specialIpcRoute, - requestHttp, + ipcApiRoute, requestHttp, specialIpcRoute } + diff --git a/frontend/src/router/routerMap.js b/frontend/src/router/routerMap.js index 850efb7..63261dd 100644 --- a/frontend/src/router/routerMap.js +++ b/frontend/src/router/routerMap.js @@ -37,9 +37,9 @@ const constantRouterMap = [ component: () => import('@/views/framework/socket/SocketServer.vue') }, { - path: '/framework/db/index', - name: 'FrameworkDBIndex', - component: () => import('@/views/framework/db/Index.vue') + path: '/framework/jsondb/index', + name: 'FrameworkJsonDBIndex', + component: () => import('@/views/framework/jsondb/Index.vue') }, { path: '/framework/sqlitedb/index', diff --git a/frontend/src/router/subMenu.js b/frontend/src/router/subMenu.js index 9ead71b..f03ea9b 100644 --- a/frontend/src/router/subMenu.js +++ b/frontend/src/router/subMenu.js @@ -24,7 +24,7 @@ export default { 'menu_103' : { icon: 'profile', title: 'json数据库', - pageName: 'FrameworkDBIndex', + pageName: 'FrameworkJsonDBIndex', params: {} }, 'menu_104' : { diff --git a/frontend/src/views/framework/db/Index.vue b/frontend/src/views/framework/jsondb/Index.vue similarity index 97% rename from frontend/src/views/framework/db/Index.vue rename to frontend/src/views/framework/jsondb/Index.vue index 6cbeb70..7b3105f 100644 --- a/frontend/src/views/framework/db/Index.vue +++ b/frontend/src/views/framework/jsondb/Index.vue @@ -156,7 +156,7 @@ export default { const params = { action: 'all', } - ipc.invoke(ipcApiRoute.dbOperation, params).then(res => { + ipc.invoke(ipcApiRoute.jsondbOperation, params).then(res => { console.log('res:', res); if (res.all_list.length == 0) { return false; @@ -179,7 +179,7 @@ export default { if (ac == 'add' && this.name.length == 0) { this.$message.error(`请填写数据`); } - ipc.invoke(ipcApiRoute.dbOperation, params).then(res => { + ipc.invoke(ipcApiRoute.jsondbOperation, params).then(res => { console.log('res:', res); if (ac == 'get') { if (res.result.length == 0) { diff --git a/frontend/src/views/framework/sqlitedb/Index.vue b/frontend/src/views/framework/sqlitedb/Index.vue index 64eb28a..215f54b 100644 --- a/frontend/src/views/framework/sqlitedb/Index.vue +++ b/frontend/src/views/framework/sqlitedb/Index.vue @@ -175,7 +175,7 @@ export default { }, mounted () { this.init(); - this.getAllTestData(); + }, methods: { init() { @@ -183,7 +183,13 @@ export default { action: 'getDataDir', } ipc.invoke(ipcApiRoute.sqlitedbOperation, params).then(res => { + if (res.code == -1) { + this.$message.error('请检查sqlite数据库是否'); + return + } + this.data_dir = res.result; + this.getAllTestData(); }) }, getAllTestData () {