built-in json database, using lowdb package
basic
features
- small data volume: 0 to 100MB (single database)
- json Database
- compatible with lodash syntax
data file location
before packaging: the root directory of the project
electron-egg/data/xxx.json
packaged: software cache Directory
# windows (example)
C:\Users\Administrator\AppData\Roaming\ee\data\xxx.json
# macOS (example)
Users/apple/Library/Application Support/ee/data/xxx.json
# Linux (example)
$XDG_CONFIG_HOME or ~/.config/ee/data/xxx.json
example
- connection data ``` ‘use strict’;
const Service = require(‘ee-core’).Service; // Database objects provided by the framework const Storage = require(‘ee-core’).Storage; const _ = require(‘lodash’);
/**
- data storage
@class */ class StorageService extends Service {
constructor (ctx) { super(ctx);
// lowdb this.systemDB = Storage.JsonDB.connection(‘system’);
// demo db let lowdbOptions = { driver: ‘lowdb’ } this.demoDB = Storage.JsonDB.connection(‘demo’, lowdbOptions);
} }
module.exports = StorageService;
- add data
/*
add 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; } ```
- delete data ``` /*
del Test data */ async delTestData(name = ‘’) { const key = this.demoDBKey.test_data; const data = this.demoDB.db .get(key) .remove({name: name}) .write();
return data; } ```
- modify data ``` /*
modify Test data */ async updateTestData(name= ‘’, age = 0) { const key = this.demoDBKey.test_data; const data = this.demoDB.db .get(key) .find({name: name}) .assign({age: age}) .write();
return data; } ```
- search for data ``` /*
search 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’]) sort //.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 = [] }
More syntax
Storage object documents