refactor(backend): 将ApiUser模型重命名为Admin并更新相关引用

This commit is contained in:
ylweng
2025-09-19 00:42:14 +08:00
parent 2ada0cb9bc
commit 2d77e83fad
14 changed files with 855 additions and 93 deletions

92
backend/test_db.js Normal file
View File

@@ -0,0 +1,92 @@
const { Sequelize, DataTypes } = require('sequelize');
require('dotenv').config();
// 数据库配置
const sequelize = new Sequelize(
process.env.DB_NAME || 'niumall',
process.env.DB_USERNAME || 'root',
process.env.DB_PASSWORD || 'aiotAiot123!',
{
host: process.env.DB_HOST || '129.211.213.226',
port: process.env.DB_PORT || 9527,
dialect: 'mysql',
logging: false,
dialectOptions: {
connectTimeout: 60000
}
}
);
// 定义User模型
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
user_type: {
type: DataTypes.ENUM('admin', 'customer', 'driver', 'supplier', 'staff'),
defaultValue: 'customer'
}
}, {
tableName: 'users',
timestamps: false
});
async function testConnection() {
try {
console.log('Testing database connection...');
await sequelize.authenticate();
console.log('Database connection successful!');
// 查找admin用户
console.log('Searching for admin user...');
const adminUser = await User.findOne({
where: {
username: 'admin',
user_type: 'admin'
}
});
if (adminUser) {
console.log('Admin user found:', {
id: adminUser.id,
username: adminUser.username,
user_type: adminUser.user_type
});
} else {
console.log('Admin user not found');
// 查看前几个用户
console.log('Getting first 5 users...');
const users = await User.findAll({
limit: 5,
attributes: ['id', 'username', 'user_type']
});
console.log('Users:', users.map(u => ({
id: u.id,
username: u.username,
user_type: u.user_type
})));
}
} catch (error) {
console.error('Database connection error:', error.message);
if (error.original) {
console.error('Original error:', error.original.message);
}
} finally {
await sequelize.close();
}
}
testConnection();