sequelize-auto是一个帮助我们快速生成表结构的工具
https://www.npmjs.com/package/sequelize-auto-import
node.js项目中利用sequelize-auto对照数据库自动生成相应的models,使用sequelize-auto对照数据库自动生成相应的models减少了对数据库进行增删改查时的sql语句的编写。
npm install -g mysql
npm install -g mysql2
npm install -g sequelize-auto
npm install -g tedious
运行以下命令
sequelize-auto -h "数据库地址" -d "数据库名" -u "用户名" -x "密码" -p "端口号" --dialect mysql
即可在models文件夹里同步数据库里的表结构
其中单个表内容如下:
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'
});
};
db层引入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);
};
我们常用的增删改查语句可以这样使用
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;
};
接口示例:
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);