Initial commit: 宁夏智慧养殖监管平台

This commit is contained in:
shenquanyi
2025-08-25 15:00:46 +08:00
commit ec72c6a8b5
177 changed files with 37263 additions and 0 deletions

View File

@@ -0,0 +1,218 @@
/**
* 迁移: initial_schema
* 创建时间: 2023-01-01T00:00:00.000Z
* 描述: 初始化数据库架构
*/
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
const { DataTypes } = require('sequelize');
const { literal } = require('sequelize');
// 创建用户表
await queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING(50),
allowNull: false,
unique: true
},
email: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING(255),
allowNull: false
},
created_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
allowNull: false
}
});
// 创建角色表
await queryInterface.createTable('roles', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(50),
allowNull: false,
unique: true
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
created_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP'),
allowNull: false
}
});
// 创建用户角色关联表
await queryInterface.createTable('user_roles', {
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
},
onDelete: 'CASCADE'
},
role_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'roles',
key: 'id'
},
onDelete: 'CASCADE'
},
assigned_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP'),
allowNull: false
}
});
// 用户角色关联表使用复合主键
// 注意在MySQL中复合主键应该在创建表时定义
// 创建产品表
await queryInterface.createTable('products', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(100),
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
stock: {
type: DataTypes.INTEGER,
defaultValue: 0
},
status: {
type: DataTypes.ENUM('active', 'inactive'),
defaultValue: 'active'
},
created_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
allowNull: false
}
});
// 创建订单表
await queryInterface.createTable('orders', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
},
onDelete: 'CASCADE'
},
total_amount: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
status: {
type: DataTypes.ENUM('pending', 'paid', 'shipped', 'delivered', 'cancelled'),
defaultValue: 'pending'
},
created_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
allowNull: false
}
});
// 创建订单项表
await queryInterface.createTable('order_items', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
order_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'orders',
key: 'id'
},
onDelete: 'CASCADE'
},
product_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'products',
key: 'id'
},
onDelete: 'CASCADE'
},
quantity: {
type: DataTypes.INTEGER,
allowNull: false
},
price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
}
});
},
down: async (queryInterface, Sequelize) => {
// 按照依赖关系的相反顺序删除表
await queryInterface.dropTable('order_items');
await queryInterface.dropTable('orders');
await queryInterface.dropTable('products');
await queryInterface.dropTable('user_roles');
await queryInterface.dropTable('roles');
await queryInterface.dropTable('users');
}
};