requirement:ee-core >= 1.2.8

supports sqlite databases.

Basic

features

  • large data volume: 0-1024GB (single database)
  • high performance
  • mysql-like syntax

installation

preparation: python Environment ( install Python on macos)

  1. # Install the build tool (the new version comes with it, and the old version EE < 2.1.6 needs to be installed)
  2. npm i electron-rebuild -D
  3. # (must) rebuild SQLite suitable for the current operating system
  4. npm run re-sqlite

note: For ee < 2.1.6, you need to add the script object of package.json.

  1. "scripts": {
  2. "re-sqlite": "electron-rebuild -f -w better-sqlite3"
  3. }

Failed to install? View: FAQ

Data file location

before packaging: the root directory of the project

  1. electron-egg/data/xxx.db

packaged: software cache Directory

  1. # windows (example)
  2. C:\Users\Administrator\AppData\Roaming\ee\data\xxx.db
  3. # macOS (example)
  4. Users/apple/Library/Application Support/ee/data/xxx.db
  5. # Linux (example)
  6. $XDG_CONFIG_HOME or ~/.config/ee/data/xxx.db

Simple operations: add, delete, modify, and query

  • connect to a database ``` ‘use strict’;

const Service = require(‘ee-core’).Service; // Storage const Storage = require(‘ee-core’).Storage; const _ = require(‘lodash’);

/**

  • StorageService
  • @class */ class StorageService extends Service {

    constructor (ctx) { super(ctx);

    // sqlite db let sqliteOptions = { driver: ‘sqlite’, default: {

    1. timeout: 6000,
    2. verbose: console.log // Print SQL syntax

    } } this.demoSqliteDB = Storage.JsonDB.connection(‘sqlite-demo.db’, sqliteOptions); }

}

module.exports = StorageService;

  1. - options

let sqliteOptions = { driver: ‘sqlite’, // driver:lowdb、sqlite default: { timeout: 6000, verbose: console.log } }

default object

default.readonly: false Open database connection in read-only mode (default: false).

default.fileMustExist: false If the database does not exist, an error will be raised instead of creating a new file Ignore this option for in memory, temporary, or read-only database connections (default: false).

default.timeout: 5000 When a query is executed on a locked database, SQLite is thrown_ Number of milliseconds to wait before busy error (default: 5000).

default.verbose: null Provide a function that is called using each SQL string executed by the database connection (default: null).

default.nativeBinding: If you are using a complex build system that moves, transforms, or connects JS files, better-sqlite3 may not be able to locate its native C++ addon (better_sqlite3.node). If you encounter such an error, you can provide better_sqlite3.node (Relative to the current working directory).

  1. - check whether the table exists. If it does not exist, create

/*

  • Check and create tables (sqlite) / async checkAndCreateTableSqlite(tableName = ‘’) { if (_.isEmpty(tableName)) { throw new Error(table name is required); } // Check whether the table exists const userTable = this.demoSqliteDB.db.prepare(‘SELECT FROM sqlite_master WHERE type=? AND name = ?’); const result = userTable.get(‘table’, tableName); //console.log(‘result:’, result); if (result) { return; }

    // Create table const create_table_user = `CREATE TABLE ${tableName} (

    1. id INTEGER PRIMARY KEY AUTOINCREMENT,
    2. name CHAR(50) NOT NULL,
    3. age INT

    );` this.demoSqliteDB.db.exec(create_table_user); } ```

  • add data ``` /*
  • add Test data (sqlite) */ async addTestDataSqlite(data) { //console.log(“add data:”, data);

    let table = ‘user’; await this.checkAndCreateTableSqlite(table);

    const insert = this.demoSqliteDB.db.prepare(INSERT INTO ${table} (name, age) VALUES (@name, @age)); insert.run(data);

    return true; } ```

  • delete data ``` /*
  • del Test data (sqlite) */ async delTestDataSqlite(name = ‘’) { //console.log(“delete name:”, name);

    let table = ‘user’; await this.checkAndCreateTableSqlite(table);

    const delUser = this.demoSqliteDB.db.prepare(DELETE FROM ${table} WHERE name = ?); delUser.run(name);

    return true; } ```

  • modify data ``` /*
  • modify Test data (sqlite) */ async updateTestDataSqlite(name= ‘’, age = 0) { //console.log(“update :”, {name, age});

    let table = ‘user’; await this.checkAndCreateTableSqlite(table);

    const updateUser = this.demoSqliteDB.db.prepare(UPDATE ${table} SET age = ? WHERE name = ?); updateUser.run(age, name);

    return true; } ```

  • query data ``` /*
  • query Test data (sqlite) */ async getTestDataSqlite(age = 0) { //console.log(“select :”, {age});

    let table = ‘user’; await this.checkAndCreateTableSqlite(table);

    const selectUser = this.demoSqliteDB.db.prepare(SELECT * FROM ${table} WHERE age = @age); const users = selectUser.all({age: age}); //console.log(“select users:”, users); return users; }

/*

  • all Test data (sqlite) */ async getAllTestDataSqlite() { //console.log(“select all user”);

    let table = ‘user’; await this.checkAndCreateTableSqlite(table);

    const selectAllUser = this.demoSqliteDB.db.prepare(SELECT * FROM ${table}); const allUser = selectAllUser.all(); //console.log(“select allUser:”, allUser); return allUser; } ```

    Graphical data (Navicat)

  • connect to the local sqlite-demo.db file

image.png

  • table data

image.png

Storage object API documentation

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

more syntax

better-sqlite3

document