// import {Sequelize,DataTypes } from 'sequelize'
const IdGenerator = require('../pgInsert/id-generator')
const generator = new IdGenerator();
const Mock = require("mockjs");
const {v4: uuidv4} = require("uuid");
const {Sequelize, DataTypes} = require("sequelize");
/**
* 使用orm连接数据库
* @type {Sequelize}
*/
const sequelize = new Sequelize('postgres', 'postgres', '123456', {
host: 'localhost',
port: '5432',
dialect: 'postgresql',
pool: { //连接池设置
max: 5,//最大连接数
min: 0,
idle: 10000
}
})
/**
* 批量插入500w条数据
*/
ormInsert()
function ormInsert() {
//插入500w条数据
for (let i = 0; i < 1000; i++) {
const dataList = getSqlParamData(2500)
User.bulkCreate(dataList)
}
}
/**
* 定义orm模型
* @type {ModelCtor<Model>}
*/
const User = sequelize.define(
'yhs_user5', {
id: {
type: DataTypes.BIGINT,
field: 'id',
primaryKey: true,
allowNull: false,
defaultValue: Sequelize.literal('nextval(\'yhs_user5_id_seq\')') // 设置默认值为序列生成的值
},
enabled: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: '是否启用,TRUE=启用,FALSE=未启用'
},
acct_active: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: '是否已经激活,TRUE=激活'
},
pwd_valid: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: '是否密码有效,TRUE=有效'
},
acct_locked: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: '是否用户被锁定,TRUE=锁定'
},
username: {
type: DataTypes.STRING(50),
allowNull: false,
comment: '用户名'
},
password: {
type: DataTypes.CHAR(64),
allowNull: false,
comment: '密码(密文)'
},
nickname: {
type: DataTypes.STRING(50),
comment: '昵称'
},
avatar: {
type: DataTypes.STRING(255),
comment: '头像URL'
},
phone: {
type: DataTypes.STRING(50),
comment: '手机号码'
},
email: {
type: DataTypes.STRING(50),
comment: '电子邮箱'
},
name: {
type: DataTypes.STRING(100),
comment: '姓名',
unique: true
},
age: {
type: DataTypes.SMALLINT,
comment: '年龄'
},
gender: {
type: DataTypes.STRING(10),
comment: '性别',
validate: {
checkGender: function (value) {
if (!['男', '女', '其他'].includes(value)) {
throw new Error('性别只能是男、女或其他');
}
}
}
},
description: {
type: DataTypes.STRING(255),
comment: '简介'
},
modified_by: {
type: DataTypes.BIGINT,
comment: '最后修改用户ID'
},
gmt_create: {
type: DataTypes.DATE,
comment: '创建时间'
},
gmt_modified: {
type: DataTypes.DATE,
comment: '最后修改时间'
}
}, {
timestamps: false, // 模型不使用自动管理的时间戳
tableName: 'yhs_user5', // 指定表名
freezeTableName: true // 防止Sequelize复数化表名
}
)
/**
* 指定需要的参数量
* @returns {*[]}
*/
function getSqlParamData(len) {
let sqlParamTemp = []
// 生成指定长度的随机数据
for (let j = 0; j < len; j++) {
sqlParamTemp.push(getRequestData())
}
return sqlParamTemp
}
/**
* 生成一个请求 对象
*/
function getRequestData() {
//生成一个临时模拟数据
return Mock.mock({
enabled: '@boolean',
acct_active: '@boolean',
pwd_valid: '@boolean',
acct_locked: '@boolean',
username: 'user_' + uuidv4().replace(/-/g, ''),
password: uuidv4(),
nickname: '@last',
//用户头像
avatar() {
return Mock.Random.image('100×100', Mock.Random.color(), '#757575', 'png', this.nickname)
},
phone: /^1[34578]\d{9}$/g,
email: '@email()',
name: '@cname()',
'age|18-40': 0,
gender() {
const genders = ['男', '女'];
return Mock.Random.pick(genders);
},
description: Mock.Random.cparagraph(),
modified_by: generator.nextId(),
gmt_create: '@date(yyyy-MM-dd)',
gmt_modified: '@date(yyyy-MM-dd)'
})
}