請先閱讀這位做者的文章,基本是按照他的思路作的。 https://molunerfinn.com/electron-vue-3/#lowdb%E5%AE%9E%E9%99%85%E4%BD%BF%E7%94%A8%E7%9A%84%E5%9D%91vue
lodash 中文文檔 https://www.html.cn/doc/lodash/ http://lodash.think2011.net/assigngit
lodash-id 中文文檔 https://www.helplib.com/GitHub/article_106283github
核心代碼以下:sql
import Datastore from 'lowdb' import LodashId from 'lodash-id' import FileSync from 'lowdb/adapters/FileSync' import path from 'path' import fs from 'fs-extra' // 引入remote模塊 import { app, remote } from 'electron' // package.json import pkg from '../../package.json' // 根據process.type來分辨在哪一種模式使用哪一種模塊 const APP = process.type === 'renderer' ? remote.app : app // 獲取electron應用的用戶目錄 const STORE_PATH = APP.getPath('userData') // console.log(STORE_PATH) // C:\Users\JD\AppData\Roaming\Electron if (process.type !== 'renderer') { // 若是不存在路徑 if (!fs.pathExistsSync(STORE_PATH)) { // 就建立 fs.mkdirpSync(STORE_PATH) } } // 以同步的方式初始化lowdb讀寫的json文件名以及存儲路徑 const adapter = new FileSync(path.join(STORE_PATH, `/${pkg.name}_lowdb.json`)) // lowdb接管該文件 const db = Datastore(adapter) // 經過._mixin()引入lodash_id惟一id插件 db._.mixin(LodashId) // 初始化數據 if(!db.has('user').value()) { db.set('user',[{userId: 'admin', password: '123456'}]).write() } if(!db.has('category').value()) { db.set('category', []).write() } if(!db.has('assets').value()) { db.set('assets', []).write() } if(!db.has('incomeAndExpenditure').value()) { db.set('incomeAndExpenditure', []).write() } export default db // 暴露出去
效果: 數據庫
結合vuetify的datatable組件作了個CRUD的例子,具體請查看源碼。json
文件結構以下,api也能夠放到renderer裏,由於我主進程也可能會用到,因此我這邊主要把它拿出來了,不必定合適,看我的習慣吧。api
核心代碼以下,insert、upsert、removeWhere等其實都是lodash-id這個模塊封裝的,你能夠點擊看看,其實裏面的代碼仍是lodash的,你也能夠封裝本身的方法。數組
import db from '../datastore' export function getCategoryById(id) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const category = collection.getById(id).value() resolve({ code: 200, data: category }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function getCategoryWhere(attrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const categoryList = collection.filter(attrs).value() resolve({ code: 200, data: categoryList }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function getCategoryAll() { return new Promise((resolve, reject) => { try { const collection = db.get('category') const categoryAll = collection.value() resolve({ code: 200, data: categoryAll }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function getCategoryPagination(pagination, whereAttrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const total = collection.size().value() const categoryList = collection .filter(o => { // 模糊查詢 return o.category.match(whereAttrs.category) }) .orderBy(pagination.sortBy, pagination.descending ? 'desc' : 'asc') .chunk(pagination.rowsPerPage === -1 ? total : pagination.rowsPerPage) .take(pagination.page) .last() // 由於上面用了chunk,是個二維數組,因此這裏取最後一個 .value() resolve({ code: 200, data: {total: total, list: categoryList} }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function postCategory(document) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const category = collection.find({category: document.category}).value() if(category) { return reject({ code: 400, message: 'This classification already exists' }) } const newCategory = collection.insert(document).write() resolve({ code: 200, data: newCategory }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function postOrPutCategory(document) { return new Promise((resolve, reject) => { try { const collection = db.get('category') if(collection.find({category: document.category}).value()) { return reject({ code: 400, message: 'This classification already exists' }) } const newCategory = collection.upsert(document).write() resolve({ code: 200, data: newCategory }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function putCategoryById(id, attrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') if(collection.find({category: attrs.category}).value()) { return reject({ code: 400, message: 'This classification already exists' }) } const newCategory = collection.updateById(id, attrs).write() resolve({ code: 200, data: newCategory }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function putCategoryWhere(whereAttrs, attrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') if(collection.find({category: attrs.category}).value()) { return reject({ code: 400, message: 'This classification already exists' }) } const newCategory = collection.updateWhere(whereAttrs, attrs).write() resolve({ code: 200, data: newCategory }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function replaceCategoryById(id, attrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const newCategory = collection.replaceById(id, attrs).write() resolve({ code: 200, data: newCategory }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function deleteCategoryById(id) { return new Promise((resolve, reject) => { try { const collection = db.get('category') collection.removeById(id).write() resolve({ code: 200 }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function deleteCategoryByIds(ids) { return new Promise((resolve, reject) => { try { const collection = db.get('category') ids.forEach(id => { collection.removeById(id).write() }) resolve({ code: 200 }) } catch (err) { return reject({ code: 400, message: err.message }) } }) } export function deleteCategoryWhere(whereAttrs) { return new Promise((resolve, reject) => { try { const collection = db.get('category') const categoryList = collection.removeWhere(whereAttrs).write() resolve({ code: 200, data: categoryList }) } catch (err) { return reject({ code: 400, message: err.message }) } }) }