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

  1. electron-egg/data/xxx.json

packaged: software cache Directory

  1. # windows (example)
  2. C:\Users\Administrator\AppData\Roaming\ee\data\xxx.json
  3. # macOS (example)
  4. Users/apple/Library/Application Support/ee/data/xxx.json
  5. # Linux (example)
  6. $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;

  1. - 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) {

    1. let isHas = true;
    2. isHas = age === o.age ? true : false;
    3. 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 = [] }

    return data; } ```

    More syntax

    Storage object documents

    https://www.yuque.com/u34495/mivcfg/vv5cmv

lodash documentation

https://www.lodashjs.com/