140 lines
3.0 KiB
JavaScript
140 lines
3.0 KiB
JavaScript
|
|
// 数据库连接和模型定义
|
|||
|
|
const { Sequelize } = require('sequelize');
|
|||
|
|
const config = require('../config/database.js');
|
|||
|
|
|
|||
|
|
// 根据环境变量选择配置
|
|||
|
|
const env = process.env.NODE_ENV || 'development';
|
|||
|
|
const dbConfig = config[env];
|
|||
|
|
|
|||
|
|
// 创建Sequelize实例
|
|||
|
|
const sequelize = new Sequelize(
|
|||
|
|
dbConfig.database,
|
|||
|
|
dbConfig.username,
|
|||
|
|
dbConfig.password,
|
|||
|
|
{
|
|||
|
|
host: dbConfig.host,
|
|||
|
|
port: dbConfig.port,
|
|||
|
|
dialect: dbConfig.dialect,
|
|||
|
|
dialectOptions: dbConfig.dialectOptions,
|
|||
|
|
timezone: dbConfig.timezone,
|
|||
|
|
logging: dbConfig.logging,
|
|||
|
|
pool: dbConfig.pool
|
|||
|
|
}
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 测试数据库连接
|
|||
|
|
const testConnection = async () => {
|
|||
|
|
try {
|
|||
|
|
await sequelize.authenticate();
|
|||
|
|
console.log('✅ 数据库连接成功');
|
|||
|
|
return true;
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 数据库连接失败:', error);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 定义模型
|
|||
|
|
const models = {
|
|||
|
|
sequelize,
|
|||
|
|
Sequelize,
|
|||
|
|
|
|||
|
|
// 用户模型(匹配实际数据库结构)
|
|||
|
|
User: sequelize.define('User', {
|
|||
|
|
id: {
|
|||
|
|
type: Sequelize.INTEGER,
|
|||
|
|
primaryKey: true,
|
|||
|
|
autoIncrement: true
|
|||
|
|
},
|
|||
|
|
openid: {
|
|||
|
|
type: Sequelize.STRING(64),
|
|||
|
|
allowNull: false,
|
|||
|
|
unique: true
|
|||
|
|
},
|
|||
|
|
nickname: {
|
|||
|
|
type: Sequelize.STRING(50),
|
|||
|
|
allowNull: false
|
|||
|
|
},
|
|||
|
|
avatar: {
|
|||
|
|
type: Sequelize.STRING(255)
|
|||
|
|
},
|
|||
|
|
gender: {
|
|||
|
|
type: Sequelize.ENUM('male', 'female', 'other')
|
|||
|
|
},
|
|||
|
|
birthday: {
|
|||
|
|
type: Sequelize.DATE
|
|||
|
|
},
|
|||
|
|
phone: {
|
|||
|
|
type: Sequelize.STRING(20),
|
|||
|
|
unique: true
|
|||
|
|
},
|
|||
|
|
email: {
|
|||
|
|
type: Sequelize.STRING(100),
|
|||
|
|
unique: true
|
|||
|
|
},
|
|||
|
|
uuid: {
|
|||
|
|
type: Sequelize.STRING(36),
|
|||
|
|
unique: true
|
|||
|
|
}
|
|||
|
|
}, {
|
|||
|
|
tableName: 'users',
|
|||
|
|
timestamps: true,
|
|||
|
|
createdAt: 'created_at',
|
|||
|
|
updatedAt: 'updated_at'
|
|||
|
|
}),
|
|||
|
|
|
|||
|
|
// 为了兼容现有API,创建一个简化版的用户模型
|
|||
|
|
ApiUser: sequelize.define('ApiUser', {
|
|||
|
|
id: {
|
|||
|
|
type: Sequelize.INTEGER,
|
|||
|
|
primaryKey: true,
|
|||
|
|
autoIncrement: true
|
|||
|
|
},
|
|||
|
|
username: {
|
|||
|
|
type: Sequelize.STRING(50),
|
|||
|
|
allowNull: false,
|
|||
|
|
unique: true
|
|||
|
|
},
|
|||
|
|
password_hash: {
|
|||
|
|
type: Sequelize.STRING(255),
|
|||
|
|
allowNull: false
|
|||
|
|
},
|
|||
|
|
phone: {
|
|||
|
|
type: Sequelize.STRING(20),
|
|||
|
|
allowNull: false,
|
|||
|
|
unique: true
|
|||
|
|
},
|
|||
|
|
email: {
|
|||
|
|
type: Sequelize.STRING(100)
|
|||
|
|
},
|
|||
|
|
user_type: {
|
|||
|
|
type: Sequelize.ENUM('client', 'supplier', 'driver', 'staff', 'admin'),
|
|||
|
|
allowNull: false
|
|||
|
|
},
|
|||
|
|
status: {
|
|||
|
|
type: Sequelize.ENUM('active', 'inactive', 'locked'),
|
|||
|
|
defaultValue: 'active'
|
|||
|
|
}
|
|||
|
|
}, {
|
|||
|
|
tableName: 'api_users',
|
|||
|
|
timestamps: true
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 同步数据库模型
|
|||
|
|
const syncModels = async () => {
|
|||
|
|
try {
|
|||
|
|
// 只同步API用户表(如果不存在则创建)
|
|||
|
|
await models.ApiUser.sync({ alter: true });
|
|||
|
|
console.log('✅ API用户表同步成功');
|
|||
|
|
console.log('✅ 数据库模型同步完成');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 数据库模型同步失败:', error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
module.exports = {
|
|||
|
|
...models,
|
|||
|
|
testConnection,
|
|||
|
|
syncModels
|
|||
|
|
};
|