sequelize-auto is a tool that helps us quickly generate table structures
https://www.npmjs.com/package/sequelize-auto-import
In the node.js project, use sequelize-auto to automatically generate corresponding models against the database, and use sequelize-auto to automatically generate corresponding models against the database to reduce the writing of SQL statements when adding, deleting, modifying and checking the database.
npm install -g mysql
npm install -g mysql2
npm install -g sequelize-auto
npm install -g tedious
run the following command
sequelize-auto -h "数据库地址" -d "数据库名" -u "用户名" -x "密码" -p "端口号" --dialect mysql
You can synchronize the table structure in the database in the models folder
For example, the content of a single table is as follows:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('t_group_public', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
group: {
type: DataTypes.CHAR(14),
allowNull: false,
defaultValue: ''
},
activation: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '1'
},
app: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
type: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
}
}, {
tableName: 't_group_public'
});
};
The db layer introduces the specific table files of models
db.prototype.model=function(name){
if(!this.sequelize.isDefined(name)){
this.sequelize.import(path.join(this.config.path,name));
}
return this.sequelize.model(name);
};
Our commonly used addition, deletion, modification and query statements can be used like this
db.prototype.add=async function(table,data,ignore,option){
var model=this.model(table);
var opt={fields:Object.keys(data)};
if(ignore){
opt.ignoreDuplicates=true;
}
var ret = await model.create(data,Object.assign(opt,option || {}));
var data = ret.get({plain:true});
if(model.autoIncrementAttribute && !model.rawAttributes[model.autoIncrementAttribute]._autoGenerated){
return data[model.autoIncrementAttribute];
}
return data;
};
db.prototype.update=async function(table,data,where,limit,option){
var model=this.model(table);
var opt={};
opt.where=where;
opt.limit=limit;
var [affectedRows]=await model.update(data,Object.assign(opt,option || {}));
return affectedRows;
};
db.prototype.del=async function(table,where,limit,option){
var model=this.model(table);
var opt={};
opt.where=where;
opt.limit=limit;
var affectedRows = await model.destroy(Object.assign(opt,option || {}))
return affectedRows;
};
db.prototype.find=async function(table,field,where,order,option){
var opt={where:where,raw:true};
if(field!=='*'){
opt.attributes=field.split(',');
}
if(order){
opt.order=[order];
}
var ret= this.model(table).findOne(Object.assign(opt,option || {}));
return ret;
};
db.prototype.select= async function(table,field,where,order,limit,option){
var opt={where:where,raw:true};
if(field!=='*'){
opt.attributes=field.split(',');
}
if(order){
opt.order=[order];
}
if(limit){
opt.limit=limit[0];
opt.offset=limit[1];
}
var ret= await this.model(table).findAll(Object.assign(opt,option || {}));
return ret;
};
Interface example:
async function getGroupPublic(req,res){
var {group,app} = req.query;
var where = {};
if (group) {
where.group = group;
}
if (app) {
where.app = app;
}
var order = ['id', 'asc'];
try {
var list = await global.DB.select('t_group_public','*',where,order);
return res.ok(list);
} catch (error) {
return res.err(error);
}
}
userRouter.get('/getGroupPublic', CHK.getGroupPublic, getGroupPublic);