/
Gabryelf
/
Pixel-Orb
Обзор
Документация
Войти
/
Gabryelf
/
Pixel-Orb
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/server/storage/database.js
79 строк
2 KB
Valeev Serj
restruct_v0.0.1
07 авг 2026, 22:50
07 авг 2026, 22:50
41f78a4
Код
Авторство
О чём код?
const fs = require('fs'); const path = require('path'); class Database { constructor() { this.data = {}; this.dbPath = path.join(__dirname, '../../../data'); this.dbFile = path.join(this.dbPath, 'database.json'); // Создаем папку если её нет if (!fs.existsSync(this.dbPath)) { fs.mkdirSync(this.dbPath, { recursive: true }); } this.load(); } load() { try { if (fs.existsSync(this.dbFile)) { const content = fs.readFileSync(this.dbFile, 'utf8'); this.data = JSON.parse(content); } } catch (error) { console.warn('Failed to load database:', error); this.data = {}; } } save() { try { fs.writeFileSync(this.dbFile, JSON.stringify(this.data, null, 2)); } catch (error) { console.error('Failed to save database:', error); } } get(collection, id) { if (!this.data[collection]) return null; return this.data[collection][id] || null; } getAll(collection) { if (!this.data[collection]) return []; return Object.values(this.data[collection]); } set(collection, id, value) { if (!this.data[collection]) { this.data[collection] = {}; } this.data[collection][id] = value; this.save(); return value; } delete(collection, id) { if (!this.data[collection]) return false; const result = delete this.data[collection][id]; this.save(); return result; } find(collection, predicate) { if (!this.data[collection]) return []; return Object.values(this.data[collection]).filter(predicate); } clear(collection) { if (collection) { this.data[collection] = {}; } else { this.data = {}; } this.save(); } } module.exports = new Database();