Files
yunrui_asset/backend/models/Rack.js
T
zhang1106 7f1df28858 feat: 实现ID生成器并重构模型ID生成逻辑
refactor: 统一ID生成方式,使用新的idGenerator模块
refactor: 重构模型ID生成逻辑,允许空ID并在创建前自动生成
fix(roomSchema): 使机房ID字段变为可选并更新前端表单验证
style: 格式化代码并优化导入语句
test: 添加操作日志集成测试文件
docs: 添加错误处理模块文档
2026-04-02 10:34:13 +08:00

64 lines
1.3 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const { generateId } = require('../utils/idGenerator');
const Room = require('./Room');
const Rack = sequelize.define(
'Rack',
{
rackId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: true,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
height: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 45, // 标准机柜高度(U数)
},
maxPower: {
type: DataTypes.FLOAT,
allowNull: false,
},
currentPower: {
type: DataTypes.FLOAT,
defaultValue: 0,
},
status: {
type: DataTypes.STRING,
defaultValue: 'active',
},
roomId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: Room,
key: 'roomId',
},
},
},
{
tableName: 'racks',
timestamps: true,
indexes: [{ fields: ['roomId'] }, { fields: ['status'] }, { fields: ['roomId', 'status'] }],
hooks: {
beforeCreate: (rack) => {
if (!rack.rackId) {
rack.rackId = generateId({ prefix: 'RCK' });
}
},
},
}
);
// 关联关系
Rack.belongsTo(Room, { foreignKey: 'roomId' });
Room.hasMany(Rack, { foreignKey: 'roomId' });
module.exports = Rack;