export database
This commit is contained in:
83
src/handlers/adminDumpHandler.js
Normal file
83
src/handlers/adminDumpHandler.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import config from '../config/config.js';
|
||||
import fs from "fs";
|
||||
import db from "../config/database.js";
|
||||
import archiver from "archiver";
|
||||
|
||||
export default class AdminDumpHandler {
|
||||
constructor(bot) {
|
||||
this.bot = bot;
|
||||
this.userStates = new Map();
|
||||
}
|
||||
|
||||
isAdmin(userId) {
|
||||
return config.ADMIN_IDS.includes(userId.toString());
|
||||
}
|
||||
|
||||
async handleDump(msg) {
|
||||
const chatId = msg.chat.id;
|
||||
|
||||
const keyboard = {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{text: '📥 Import dump', callback_data: 'import_database'},
|
||||
{text: '📤 Export dump', callback_data: 'export_database'},
|
||||
],
|
||||
[{text: '« Back', callback_data: 'admin_menu'}]
|
||||
]
|
||||
}
|
||||
|
||||
await this.bot.sendMessage(chatId, 'Choose an option', {reply_markup: keyboard});
|
||||
}
|
||||
|
||||
async exportDatabase(callbackQuery) {
|
||||
const chatId = callbackQuery.message.chat.id;
|
||||
|
||||
const tables = [
|
||||
"categories",
|
||||
"crypto_wallets",
|
||||
"locations",
|
||||
"products",
|
||||
"purchases",
|
||||
"transactions",
|
||||
"users"
|
||||
]
|
||||
|
||||
const dumpPath = "./dump"
|
||||
|
||||
try {
|
||||
fs.rmdirSync(dumpPath, {recursive: true, force: true});
|
||||
fs.rmSync(`${dumpPath}/dump.zip`);
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
|
||||
fs.mkdirSync(dumpPath);
|
||||
|
||||
for (const table of tables) {
|
||||
const result = await db.runAsync(`SELECT * FROM ${table}`);
|
||||
const tableData = JSON.stringify(result);
|
||||
fs.writeFileSync(`${dumpPath}/${table}.json`, tableData);
|
||||
}
|
||||
|
||||
const archive = archiver('zip', {zlib: { level: 9 } });
|
||||
archive.directory(dumpPath, false);
|
||||
|
||||
const output = fs.createWriteStream('./dump.zip');
|
||||
archive.pipe(output);
|
||||
|
||||
await archive.finalize();
|
||||
|
||||
output.on('close', () => {
|
||||
this.bot.sendDocument(chatId, './dump.zip', {caption: 'Database dump'});
|
||||
});
|
||||
}
|
||||
|
||||
async importDatabase(callbackQuery) {
|
||||
|
||||
}
|
||||
|
||||
async confirmImport(callbackQuery) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user