基础
特点
- 小数据量: 0~100M(单库)
- json数据库
- 兼容lodash语法
数据文件位置
打包前:项目根目录
electron-egg/data/xxx.json
打包后:软件缓存目录
# windows (例子)
C:\Users\Administrator\AppData\Roaming\ee\data\xxx.json
# macOS (例子)
Users/apple/Library/Application Support/ee/data/xxx.json
# Linux (例子)
$XDG_CONFIG_HOME or ~/.config/ee/data/xxx.json
示例
- 连接数据 ``` ‘use strict’;
const Service = require(‘ee-core’).Service; // 框架提供的数据库对象 const Storage = require(‘ee-core’).Storage; const _ = require(‘lodash’);
/**
- 数据存储
@class */ class StorageService extends Service {
constructor (ctx) { super(ctx);
// lowdb数据库
// ee-core所使用的库 this.systemDB = Storage.JsonDB.connection(‘system’);
// demo库 let lowdbOptions = { driver: ‘lowdb’ } this.demoDB = Storage.JsonDB.connection(‘demo’, lowdbOptions);
} }
module.exports = StorageService;
- 增加数据
/*
增 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 = [] }
更多语法
Storage对象文档