From 263d7c85b56ea250871ac29f0883e755c4cc4552 Mon Sep 17 00:00:00 2001 From: gaoshuaixing <530353222@qq.com> Date: Tue, 23 Nov 2021 20:33:34 +0800 Subject: [PATCH] add lowdb --- README.md | 2 +- app/controller/v1/example.js | 30 ++++ app/router/example.js | 2 + app/service/storage.js | 40 ++++- frontend/src/api/main.js | 1 + frontend/src/config/router.config.js | 5 + frontend/src/layouts/DemoMenu.vue | 6 + frontend/src/views/demo/db/Index.vue | 223 +++++++++++++++++++++++++++ package.json | 6 +- update.md | 5 + 10 files changed, 311 insertions(+), 9 deletions(-) create mode 100644 frontend/src/views/demo/db/Index.vue diff --git a/README.md b/README.md index 305fead..6c3aef8 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ 2. 安装,node推荐v14.16.0 ``` # 提升安装速度,使用国内镜像; - npm config set registry https://registry.npm.taobao.org + npm config set registry https://registry.npmmirror.com # 进入目录 ./electron-egg/ npm install ``` diff --git a/app/controller/v1/example.js b/app/controller/v1/example.js index 7d9f479..ec9f8d7 100644 --- a/app/controller/v1/example.js +++ b/app/controller/v1/example.js @@ -124,6 +124,36 @@ class ExampleController extends BaseController { self.sendSuccess(data); } + async dbOperation() { + const self = this; + const { ctx, service } = this; + const paramsObj = ctx.request.body; + const data = { + action: paramsObj.action, + result: null, + all_list: [] + }; + + switch (paramsObj.action) { + case 'add' : + data.result = await service.storage.addTestData(paramsObj.info);; + break; + case 'del' : + data.result = await service.storage.delTestData(paramsObj.delete_name);; + break; + case 'update' : + data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age); + break; + case 'get' : + data.result = await service.storage.getTestData(paramsObj.search_age); + break; + } + + data.all_list = await service.storage.getAllTestData(); + + self.sendSuccess(data); + } + async addTestData() { const self = this; const { service } = this; diff --git a/app/router/example.js b/app/router/example.js index d3cfa6b..4ba9ab7 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -41,4 +41,6 @@ module.exports = app => { router.post('/api/v1/example/messageShowConfirm', controller.v1.example.messageShowConfirm); // upload chrome extension router.post('/api/v1/example/uploadExtension', controller.v1.example.uploadExtension); + // db operation + router.post('/api/v1/example/dbOperation', controller.v1.example.dbOperation); }; \ No newline at end of file diff --git a/app/service/storage.js b/app/service/storage.js index 40ca522..d3f6b19 100644 --- a/app/service/storage.js +++ b/app/service/storage.js @@ -95,8 +95,8 @@ class StorageService extends BaseService { const key = storageKey.TEST_DATA; const data = this.instance() .get(key) - .find({name: name}) - .assign({ age: age}) + .find({name: name}) // 修改找到的第一个数据,貌似无法批量修改 todo + .assign({age: age}) .write(); return data; @@ -105,15 +105,45 @@ class StorageService extends BaseService { /* * 查 Test data */ - async getTestData(name = '') { + async getTestData(age = 0) { const key = storageKey.TEST_DATA; - const data = this.instance() + let data = this.instance() .get(key) - .find({name: name}) + //.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 = storageKey.TEST_DATA; + if (!this.instance().has(key).value()) { + this.instance().set(key, []).write(); + } + let data = this.instance() + .get(key) + .value(); + + if (_.isEmpty(data)) { + data = [] + } + + return data; + } } module.exports = StorageService; diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index 5447573..3e0cc83 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -13,6 +13,7 @@ const mainApi = { selectFileDir: '/api/v1/example/selectFileDir', messageShow: '/api/v1/example/messageShow', messageShowConfirm: '/api/v1/example/messageShowConfirm', + dbOperation: '/api/v1/example/dbOperation', testElectronApi: '/api/v1/example/testElectronApi', } diff --git a/frontend/src/config/router.config.js b/frontend/src/config/router.config.js index 10a75ad..b7a5b84 100644 --- a/frontend/src/config/router.config.js +++ b/frontend/src/config/router.config.js @@ -28,6 +28,11 @@ export const constantRouterMap = [ name: 'DemoSocketIndex', component: () => import('@/views/demo/socket/Index') }, + { + path: '/demo/db/index', + name: 'DemoDBIndex', + component: () => import('@/views/demo/db/Index') + }, { path: '/demo/windowview/index', name: 'DemoWindowViewIndex', diff --git a/frontend/src/layouts/DemoMenu.vue b/frontend/src/layouts/DemoMenu.vue index cb72353..acc1548 100644 --- a/frontend/src/layouts/DemoMenu.vue +++ b/frontend/src/layouts/DemoMenu.vue @@ -36,6 +36,12 @@ export default { pageName: 'DemoSocketIndex', params: {} }, + 'menu_301' : { + icon: 'profile', + title: '数据库', + pageName: 'DemoDBIndex', + params: {} + }, 'menu_400' : { icon: 'profile', title: '视图', diff --git a/frontend/src/views/demo/db/Index.vue b/frontend/src/views/demo/db/Index.vue new file mode 100644 index 0000000..fbb4813 --- /dev/null +++ b/frontend/src/views/demo/db/Index.vue @@ -0,0 +1,223 @@ + + + diff --git a/package.json b/package.json index 22916a5..d4efb52 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-egg", - "version": "1.15.0", + "version": "1.16.0", "description": "A fast, desktop software development framework", "main": "main.js", "scripts": { @@ -33,7 +33,7 @@ "to": "extraResources" }, "electronDownload": { - "mirror": "https://npm.taobao.org/mirrors/electron/" + "mirror": "https://npmmirror.com/mirrors/electron/" }, "nsis": { "oneClick": false, @@ -92,7 +92,7 @@ "egg-bin": "^4.12.3", "egg-ci": "^1.11.0", "egg-mock": "^3.21.0", - "electron": "^12.0.10", + "electron": "^12.2.3", "electron-builder": "22.10.4", "eslint": "^5.13.0", "eslint-config-egg": "^7.1.0", diff --git a/update.md b/update.md index b2172c6..c2c9244 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,8 @@ +## 1.16.0 +1. 增加lowdb数据库实例代码 +2. 更新npm源 +3. 更新electron版本 + ## 1.15.0 1. 增加chrome扩展程序(重点) 2. 增加web(html)内容嵌入