UNPKG

mira-app-core

Version:

Core library for Mira TypeScript project - provides base functionality without auto-execution

89 lines 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseRoutes = void 0; const express_1 = require("express"); class DatabaseRoutes { constructor(backend) { this.backend = backend; this.router = (0, express_1.Router)(); this.setupRoutes(); } setupRoutes() { // 获取数据库表列表 this.router.get('/tables', async (req, res) => { try { // 这里需要根据实际的数据库实现来获取表信息 // 暂时返回一个基本的表列表,后续可以扩展 const libraryCount = Object.keys(this.backend.libraries.libraries).length; const tables = [ { name: 'users', schema: '', rowCount: 0 }, { name: 'libraries', schema: '', rowCount: libraryCount }, { name: 'plugins', schema: '', rowCount: 0 }, { name: 'files', schema: '', rowCount: 0 }, { name: 'tags', schema: '', rowCount: 0 } ]; res.json(tables); } catch (error) { console.error('Error getting database tables:', error); res.status(500).json({ error: 'Failed to get database tables' }); } }); // 获取表数据 this.router.get('/tables/:tableName/data', async (req, res) => { try { const { tableName } = req.params; let data = []; if (tableName === 'libraries') { for (const [id, libraryObj] of Object.entries(this.backend.libraries.libraries)) { if (libraryObj.libraryService) { const stats = await libraryObj.libraryService.getStats(); data.push({ id: id, name: libraryObj.libraryService.config.name, path: libraryObj.libraryService.config.path, type: libraryObj.libraryService.config.type || 'local', file_count: stats.totalFiles, size: stats.totalSize, created_at: libraryObj.libraryService.config.createdAt || new Date().toISOString() }); } } } res.json(data); } catch (error) { console.error('Error getting table data:', error); res.status(500).json({ error: 'Failed to get table data' }); } }); // 获取表结构 this.router.get('/tables/:tableName/schema', async (req, res) => { try { const { tableName } = req.params; let schema = []; if (tableName === 'libraries') { schema = [ { name: 'id', type: 'TEXT', notnull: 1, pk: 1, dflt_value: null }, { name: 'name', type: 'TEXT', notnull: 1, pk: 0, dflt_value: null }, { name: 'path', type: 'TEXT', notnull: 1, pk: 0, dflt_value: null }, { name: 'type', type: 'TEXT', notnull: 0, pk: 0, dflt_value: 'local' }, { name: 'file_count', type: 'INTEGER', notnull: 0, pk: 0, dflt_value: '0' }, { name: 'size', type: 'INTEGER', notnull: 0, pk: 0, dflt_value: '0' }, { name: 'created_at', type: 'DATETIME', notnull: 0, pk: 0, dflt_value: 'CURRENT_TIMESTAMP' } ]; } res.json(schema); } catch (error) { console.error('Error getting table schema:', error); res.status(500).json({ error: 'Failed to get table schema' }); } }); } getRouter() { return this.router; } } exports.DatabaseRoutes = DatabaseRoutes; //# sourceMappingURL=DatabaseRoutes.js.map