Files
zhang1106 63f0cb570e refactor: 统一代码风格并迁移至 ESLint 新配置
style(backend): 格式化模型文件代码
style(frontend): 调整组件代码格式
chore: 删除旧 ESLint 配置并添加新配置
refactor(backend): 重构模型定义语法
style: 统一箭头函数和对象属性简写
2026-03-27 19:12:16 +08:00

62 lines
1.3 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Device = require('./Device');
const Business = require('./Business');
const DeviceBusiness = sequelize.define(
'DeviceBusiness',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
deviceId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: Device,
key: 'deviceId',
},
},
businessId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: Business,
key: 'businessId',
},
},
isPrimary: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
},
{
tableName: 'device_business',
timestamps: true,
indexes: [
{ fields: ['deviceId'] },
{ fields: ['businessId'] },
{ unique: true, fields: ['deviceId', 'businessId'] },
],
}
);
Device.belongsToMany(Business, {
through: DeviceBusiness,
foreignKey: 'deviceId',
otherKey: 'businessId',
});
Business.belongsToMany(Device, {
through: DeviceBusiness,
foreignKey: 'businessId',
otherKey: 'deviceId',
});
DeviceBusiness.belongsTo(Business, { foreignKey: 'businessId' });
DeviceBusiness.belongsTo(Device, { foreignKey: 'deviceId' });
module.exports = DeviceBusiness;