mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 21:27:47 +00:00
refactor: 重构lib代码(无实质性功能更新,可不更新)
This commit is contained in:
parent
a7f06d404b
commit
aa3b7928ec
26 changed files with 547 additions and 483 deletions
45
lib/db.js
45
lib/db.js
|
|
@ -1,47 +1,4 @@
|
|||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import path from 'path';
|
||||
import { checkFolderExistAndCreate } from '../utils/file.js';
|
||||
import { dataPath } from './path.js';
|
||||
const dbPath = {
|
||||
gacha: 'gacha',
|
||||
panel: 'panel',
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} dbName
|
||||
* @param {string} dbFile
|
||||
* @returns {object | Array<object> | null}
|
||||
*/
|
||||
export function getDB(dbName, dbFile) {
|
||||
const db = dbPath[dbName];
|
||||
const dbFolder = path.join(dataPath, db);
|
||||
try {
|
||||
const dbPath = path.join(dbFolder, `${dbFile}.json`);
|
||||
return JSON.parse(readFileSync(dbPath, 'utf-8'));
|
||||
} catch (error) {
|
||||
logger.debug(`读取数据库失败: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} dbName
|
||||
* @param {string} dbFile
|
||||
* @param {object} data
|
||||
*/
|
||||
export function setDB(dbName, dbFile, data) {
|
||||
const db = dbPath[dbName];
|
||||
const dbFolder = path.join(dataPath, db);
|
||||
try {
|
||||
checkFolderExistAndCreate(dbFolder);
|
||||
const dbPath = path.join(dbFolder, `${dbFile}.json`);
|
||||
writeFileSync(dbPath, JSON.stringify(data, null, 2));
|
||||
} catch (error) {
|
||||
logger.debug(`读取数据库失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
import { getDB, setDB } from './db/core.js';
|
||||
|
||||
/**
|
||||
* @param {string} uid
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue