refactor(backend): 优化API文档、认证路由和分页查询,统一响应格式并添加字段验证

This commit is contained in:
2025-09-23 18:38:37 +08:00
parent 00cf840e6f
commit 6d76281c6b
35 changed files with 2027 additions and 535 deletions

View File

@@ -0,0 +1,99 @@
const mysql = require('mysql2');
async function listAllTables() {
try {
// 数据库连接配置
const connection = await mysql.createConnection({
host: process.env.DB_HOST || 'nj-cdb-3pwh2kz1.sql.tencentcdb.com',
port: process.env.DB_PORT || 20784,
user: process.env.DB_USER || 'jiebanke',
password: process.env.DB_PASSWORD || 'aiot741$12346',
database: process.env.DB_NAME || 'niumall'
});
console.log('正在连接到远程数据库...');
// 获取所有表名
const [tables] = await connection.execute(
`SELECT TABLE_NAME, TABLE_COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = ?
ORDER BY TABLE_NAME`,
[process.env.DB_NAME || 'niumall']
);
console.log('\n📊 远程数据库中的所有表:');
console.log('┌─────────┬─────────────────────────────┬─────────────────────────────┐');
console.log('│ 序号 │ 表名 │ 表注释 │');
console.log('├─────────┼─────────────────────────────┼─────────────────────────────┤');
tables.forEach((table, index) => {
console.log(`${(index + 1).toString().padEnd(3)}${table.TABLE_NAME.padEnd(27)}${(table.TABLE_COMMENT || '无注释').padEnd(27)}`);
});
console.log('└─────────┴─────────────────────────────┴─────────────────────────────┘');
console.log(`\n总共 ${tables.length} 张表`);
// 获取每张表的字段信息
console.log('\n🔍 开始检查每张表的字段结构...');
console.log('=' .repeat(80));
for (const table of tables) {
console.log(`\n📋 表名: ${table.TABLE_NAME}`);
if (table.TABLE_COMMENT) {
console.log(`📝 注释: ${table.TABLE_COMMENT}`);
}
// 获取表字段信息
const [columns] = await connection.execute(
`SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT, EXTRA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
ORDER BY ORDINAL_POSITION`,
[process.env.DB_NAME || 'niumall', table.TABLE_NAME]
);
console.log('┌──────┬─────────────────┬──────────────┬──────────┬─────────────────┬─────────────────┐');
console.log('│ 序号 │ 字段名 │ 数据类型 │ 允许空值 │ 默认值 │ 注释 │');
console.log('├──────┼─────────────────┼──────────────┼──────────┼─────────────────┼─────────────────┤');
columns.forEach((col, idx) => {
const extra = col.EXTRA ? `(${col.EXTRA})` : '';
console.log(`${(idx + 1).toString().padEnd(4)}${col.COLUMN_NAME.padEnd(15)}${(col.COLUMN_TYPE + extra).padEnd(12)}${col.IS_NULLABLE.padEnd(8)}${(col.COLUMN_DEFAULT || 'NULL').padEnd(15)}${(col.COLUMN_COMMENT || '无').padEnd(15)}`);
});
console.log('└──────┴─────────────────┴──────────────┴──────────┴─────────────────┴─────────────────┘');
console.log(`字段数量: ${columns.length}`);
console.log('─'.repeat(80));
}
// 获取表数据量统计
console.log('\n📈 表数据量统计:');
console.log('┌─────────────────────────────┬──────────────┐');
console.log('│ 表名 │ 数据行数 │');
console.log('├─────────────────────────────┼──────────────┤');
for (const table of tables) {
try {
const [rows] = await connection.execute(
`SELECT COUNT(*) as count FROM ${table.TABLE_NAME}`
);
console.log(`${table.TABLE_NAME.padEnd(27)}${rows[0].count.toString().padEnd(12)}`);
} catch (error) {
console.log(`${table.TABLE_NAME.padEnd(27)} │ 无法统计 │`);
}
}
console.log('└─────────────────────────────┴──────────────┘');
await connection.end();
console.log('\n✅ 数据库表结构检查完成!');
} catch (error) {
console.error('❌ 数据库连接或查询错误:', error.message);
process.exit(1);
}
}
// 执行函数
listAllTables().catch(console.error);