2026-03-20 17:15:14 +08:00
|
|
|
const { DataTypes } = require('sequelize');
|
|
|
|
|
const { sequelize } = require('../db');
|
|
|
|
|
const Device = require('./Device');
|
|
|
|
|
const Business = require('./Business');
|
|
|
|
|
|
2026-03-27 19:12:16 +08:00
|
|
|
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,
|
|
|
|
|
},
|
2026-03-20 17:15:14 +08:00
|
|
|
},
|
2026-03-27 19:12:16 +08:00
|
|
|
{
|
|
|
|
|
tableName: 'device_business',
|
|
|
|
|
timestamps: true,
|
|
|
|
|
indexes: [
|
|
|
|
|
{ fields: ['deviceId'] },
|
|
|
|
|
{ fields: ['businessId'] },
|
|
|
|
|
{ unique: true, fields: ['deviceId', 'businessId'] },
|
|
|
|
|
],
|
2026-03-20 17:15:14 +08:00
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
);
|
2026-03-20 17:15:14 +08:00
|
|
|
|
|
|
|
|
Device.belongsToMany(Business, {
|
|
|
|
|
through: DeviceBusiness,
|
|
|
|
|
foreignKey: 'deviceId',
|
2026-03-27 19:12:16 +08:00
|
|
|
otherKey: 'businessId',
|
2026-03-20 17:15:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Business.belongsToMany(Device, {
|
|
|
|
|
through: DeviceBusiness,
|
|
|
|
|
foreignKey: 'businessId',
|
2026-03-27 19:12:16 +08:00
|
|
|
otherKey: 'deviceId',
|
2026-03-20 17:15:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
DeviceBusiness.belongsTo(Business, { foreignKey: 'businessId' });
|
|
|
|
|
DeviceBusiness.belongsTo(Device, { foreignKey: 'deviceId' });
|
|
|
|
|
|
|
|
|
|
module.exports = DeviceBusiness;
|