rentease-backend/sync-db.js

206 lines
4.0 KiB
JavaScript
Raw Normal View History

2026-03-02 12:36:41 +00:00
const sequelize = require('./config/db');
const { DataTypes } = require('sequelize');
// 定义 WaterBill 模型
const WaterBill = sequelize.define('WaterBill', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
roomId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'rooms',
key: 'id'
}
},
startDate: {
type: DataTypes.DATE,
allowNull: false
},
endDate: {
type: DataTypes.DATE,
allowNull: false
},
startReading: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
endReading: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
usage: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
unitPrice: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
amount: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
status: {
type: DataTypes.ENUM('unpaid', 'paid'),
allowNull: false,
defaultValue: 'unpaid'
},
createTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'water_bills',
timestamps: false
});
// 定义 ElectricityBill 模型
const ElectricityBill = sequelize.define('ElectricityBill', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
roomId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'rooms',
key: 'id'
}
},
startDate: {
type: DataTypes.DATE,
allowNull: false
},
endDate: {
type: DataTypes.DATE,
allowNull: false
},
startReading: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
endReading: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
usage: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
unitPrice: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
amount: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
status: {
type: DataTypes.ENUM('unpaid', 'paid'),
allowNull: false,
defaultValue: 'unpaid'
},
createTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'electricity_bills',
timestamps: false
});
// 定义 Rental 模型
const Rental = sequelize.define('Rental', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
roomId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'rooms',
key: 'id'
}
},
tenantId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'tenants',
key: 'id'
}
},
contractId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'contracts',
key: 'id'
}
},
startDate: {
type: DataTypes.DATE,
allowNull: false
},
endDate: {
type: DataTypes.DATE,
allowNull: false
},
rent: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
deposit: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
status: {
type: DataTypes.ENUM('active', 'expired'),
allowNull: false,
defaultValue: 'active'
},
remark: {
type: DataTypes.TEXT,
allowNull: true
},
createTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'rentals',
timestamps: false
});
// 同步数据库模型
async function syncDatabase() {
try {
console.log('开始同步数据库模型...');
// 同步 Rental 模型
await Rental.sync({ alter: true });
console.log('Rental 模型同步成功');
// 同步 WaterBill 模型
await WaterBill.sync({ alter: true });
console.log('WaterBill 模型同步成功');
// 同步 ElectricityBill 模型
await ElectricityBill.sync({ alter: true });
console.log('ElectricityBill 模型同步成功');
console.log('数据库模型同步成功');
process.exit(0);
} catch (error) {
console.error('数据库模型同步失败:', error);
process.exit(1);
}
}
// 执行同步
syncDatabase();