mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-15 04:02:10 +08:00
52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
const { SqliteStorage } = require('ee-core/storage');
|
|
const { getDataDir } = require('ee-core/ps');
|
|
const path = require('path');
|
|
|
|
/**
|
|
* sqlite数据存储
|
|
* @class
|
|
*/
|
|
class BasedbService {
|
|
|
|
constructor(options) {
|
|
const { dbname } = options;
|
|
this.dbname = dbname;
|
|
this.db = undefined;
|
|
this._init();
|
|
}
|
|
|
|
/*
|
|
* 初始化
|
|
*/
|
|
_init() {
|
|
// 定义数据文件
|
|
const dbFile = path.join(getDataDir(), "db", this.dbname);
|
|
const sqliteOptions = {
|
|
timeout: 6000,
|
|
verbose: console.log
|
|
}
|
|
this.storage = new SqliteStorage(dbFile, sqliteOptions);
|
|
this.db = this.storage.db;
|
|
}
|
|
|
|
/*
|
|
* change data dir (sqlite)
|
|
*/
|
|
changeDataDir(dir) {
|
|
// the absolute path of the db file
|
|
const dbFile = path.join(dir, this.dbname);
|
|
const sqliteOptions = {
|
|
timeout: 6000,
|
|
verbose: console.log
|
|
}
|
|
this.storage = new SqliteStorage(dbFile, sqliteOptions);
|
|
this.db = this.storage.db;
|
|
}
|
|
}
|
|
|
|
BasedbService.toString = () => '[class BasedbService]';
|
|
module.exports = {
|
|
BasedbService,
|
|
}; |