92 lines
2.1 KiB
JavaScript
92 lines
2.1 KiB
JavaScript
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(); |