@suger-tdy/electron-sqlite3
Version:
Personal electron sqlite3 Utils For Electron
122 lines (118 loc) • 3.98 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
SqliteBuilder: () => db_default
});
module.exports = __toCommonJS(src_exports);
// src/db.ts
var import_path2 = __toESM(require("path"));
var import_fs = require("fs");
var import_better_sqlite3 = __toESM(require("better-sqlite3"));
var import_electron_log = __toESM(require("electron-log"));
var import_knex = require("knex");
// src/constant.ts
var import_path = __toESM(require("path"));
var import_electron = require("electron");
var USER_DATA_DIR = import_electron.app.getPath("userData");
var USER_DB_DIR = import_path.default.join(USER_DATA_DIR, "teddy_db");
var USER_STORE_DIR = import_path.default.join(USER_DATA_DIR, "teddy_storage");
var DB_LIST = {
IMAGE_LINK: "i_link"
};
// src/db.ts
var SqliteBuilder = class {
constructor(_options) {
this.sqliteDB = null;
this.knexInstance = null;
this._init(_options);
}
static getInstance(options = {
dbName: DB_LIST.IMAGE_LINK
}) {
if (!this.instance)
this.instance = new SqliteBuilder(options);
return this.instance;
}
_dbPath(name) {
if (!(0, import_fs.existsSync)(USER_DB_DIR))
(0, import_fs.mkdirSync)(USER_DB_DIR, { recursive: true });
return import_path2.default.join(USER_DB_DIR, `${name}.db`);
}
_init(opt) {
this.initDB(opt.dbName);
this.initSqlParser();
}
initDB(dbName) {
const absDbPath = this._dbPath(dbName);
this.sqliteDB = new import_better_sqlite3.default(absDbPath);
import_electron_log.default.info(`[DB] init database ${absDbPath} completed`);
return this.sqliteDB;
}
db(name = DB_LIST.IMAGE_LINK) {
if (!this.sqliteDB)
this.initDB(name);
return this.sqliteDB;
}
initSqlParser() {
const config = {
client: "better-sqlite3",
connection: {
filename: ":memory:"
},
useNullAsDefault: true
};
this.knexInstance = (0, import_knex.knex)(config);
}
sqlParser() {
if (!this.knexInstance) {
const config = {
client: "better-sqlite3",
connection: {
filename: ":memory:"
},
useNullAsDefault: true
};
this.knexInstance = (0, import_knex.knex)(config);
}
return this.knexInstance;
}
async dispose() {
var _a, _b;
(_a = this.sqliteDB) == null ? void 0 : _a.close();
await ((_b = this.knexInstance) == null ? void 0 : _b.destroy());
}
};
var db_default = SqliteBuilder;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SqliteBuilder
});
//# sourceMappingURL=index.js.map