feat: 多数据库类型支持 — Redis/MongoDB/PostgreSQL 数据库+表浏览
后端: - InstanceService.list_databases: Redis 通过 CONFIG GET databases 返回 db0-db15 - InstanceService.list_databases: MongoDB 通过 pymongo list_database_names 返回库列表 - InstanceService.list_tables: MongoDB 通过 list_collection_names 返回集合列表 - PostgreSQL 数据库/表列表已有支持 前端: - TicketCreatePage: 所有数据库类型统一走后端 listDatabases API - Redis 数据库改为下拉选择(db0-db15),替代手动输入 - 浏览面板: MySQL/PG 显示表结构+索引, MongoDB 显示集合, Redis 显示 Keys - 新增 renderCollectionsTab 用于 MongoDB 集合浏览 - isMySQL 重命名为 isSQL, 新增 isMongo 类型判断
This commit is contained in:
1 parent
fb167233bc
commit
ed13243bb0
2 files changed
+94
-20
No files matched your search
@@ -23,7 +23,7 @@ class InstanceService:
|
||||
|
||||
@staticmethod
|
||||
async def list_databases(instance: Instance) -> List[str]:
|
||||
"""List databases for a MySQL instance."""
|
||||
"""List databases for the given instance type."""
|
||||
if instance.db_type == "mysql":
|
||||
conn = await aiomysql.connect(
|
||||
host=instance.host, port=instance.port,
|
||||
@@ -45,6 +45,31 @@ class InstanceService:
|
||||
return [r["datname"] for r in rows]
|
||||
finally:
|
||||
await conn.close()
|
||||
elif instance.db_type == "redis":
|
||||
from app.services.redis_connector import RedisConnector
|
||||
conn = await RedisConnector.get_connection(instance)
|
||||
try:
|
||||
db_count = await conn.config_get("databases")
|
||||
num = int(db_count.get("databases", 16))
|
||||
return [f"db{i}" for i in range(num)]
|
||||
finally:
|
||||
await conn.close()
|
||||
elif instance.db_type == "mongo":
|
||||
from pymongo import MongoClient
|
||||
pwd = instance.password
|
||||
if pwd and not pwd.startswith("$"):
|
||||
from app.core.crypto import decrypt_password
|
||||
try:
|
||||
pwd = decrypt_password(pwd)
|
||||
except Exception:
|
||||
pass
|
||||
uri = f"mongodb://{instance.username}:{pwd}@{instance.host}:{instance.port}/admin"
|
||||
client = MongoClient(uri, serverSelectionTimeoutMS=5000)
|
||||
try:
|
||||
dbs = client.list_database_names()
|
||||
return [d for d in dbs if d not in ("admin", "config", "local")]
|
||||
finally:
|
||||
client.close()
|
||||
elif instance.db_type == "sqlite":
|
||||
return [instance.database or "main"]
|
||||
else:
|
||||
@@ -80,6 +105,23 @@ class InstanceService:
|
||||
return [dict(r) for r in rows]
|
||||
finally:
|
||||
await conn.close()
|
||||
elif instance.db_type == "mongo":
|
||||
from pymongo import MongoClient
|
||||
pwd = instance.password
|
||||
if pwd and not pwd.startswith("$"):
|
||||
from app.core.crypto import decrypt_password
|
||||
try:
|
||||
pwd = decrypt_password(pwd)
|
||||
except Exception:
|
||||
pass
|
||||
uri = f"mongodb://{instance.username}:{pwd}@{instance.host}:{instance.port}/{database}"
|
||||
client = MongoClient(uri, serverSelectionTimeoutMS=5000)
|
||||
try:
|
||||
db_obj = client[database]
|
||||
colls = db_obj.list_collection_names()
|
||||
return [{"name": c} for c in colls]
|
||||
finally:
|
||||
client.close()
|
||||
elif instance.db_type == "sqlite":
|
||||
import sqlite3
|
||||
db_path = instance.host
|
||||
|
||||
Reference in new issue
Block a user