Files
2025-01-19 13:22:58 +08:00

81 lines
1.6 KiB
JavaScript

import {
join,
dirname
} from 'path'
import {
Low,
JSONFile
} from 'lowdb'
import {
fileURLToPath
} from 'url'
import ConfigHelper from "./ConfigHelper.mjs";
const __dirname = dirname(fileURLToPath(
import.meta.url));
const getDbPath = (dbName, defPath) => {
let file = "";
if (ConfigHelper.config && ConfigHelper.config.db && ConfigHelper.config.db[dbName]) {
file = ConfigHelper.config.path + ConfigHelper.config.db[dbName];
} else {
file = defPath;
}
return file;
}
/**
* 基础数据库
* @returns
*/
export const getDb = async () => {
let file = getDbPath('db', join(__dirname, 'db.json'));
const adapter = new JSONFile(file)
const db = new Low(adapter)
await db.read();
return db;
}
/**
* 文件资源库
* @returns
*/
export const getResourceDb = async () => {
let file = getDbPath('resourceDB', join(__dirname, 'resourceDB.json'));
const adapter = new JSONFile(file)
const db = new Low(adapter)
await db.read();
return db;
}
/**
* 备份记录存储库
* @returns
*/
export const getBackupDb = async () => {
let file = getDbPath('backupDB', join(__dirname, 'backupDB.json'));
const adapter = new JSONFile(file)
const db = new Low(adapter)
await db.read();
return db;
}
/**
* 日志存储库
* @returns
*/
export const getLogDb = async () => {
let file = getDbPath('logDB', join(__dirname, 'logDB.json'));
const adapter = new JSONFile(file)
const db = new Low(adapter)
await db.read();
return db;
}
export default getDb;