node-orm自动生成models

var fs = require(“fs”);
var mysql = require(“mysql”);

getdataBase(“loverus”);

function getdataBase(database){
let connection = mysql.createConnection({
host:‘localhost’,
user:‘root’,
password:‘518218’,
port: ‘3306’,
database:database
})
//遍历数据库中的表
connection.connect();
let sqlBase = “select table_name from information_schema.tables where table_schema=’”+database+"’";
connection.query(sqlBase,function(err,result){
for(var index in result){
writeSql(connection,result[index].TABLE_NAME);
}
})
}

//读取数据库中的字段
function writeSql(connection,table){
let sql = “select column_name,DATA_TYPE from information_schema.columns where table_name = '”+table+"’"
let list ="";
let first = true;
connection.query(sql,function(err,result){
console.log(err);
console.log(result)
result.forEach((item,index)=>{

if(item.COLUMN_NAME.indexOf(“id”) != -1&&first){
item.DATA_TYPE="{type: ‘serial’, key: true}";
first = false;
}

if(item.DATA_TYPE.indexOf("int") != -1||item.DATA_TYPE==="decimal"){
    item.DATA_TYPE="Number";
}else if(item.DATA_TYPE.indexOf("char") != -1||item.DATA_TYPE==="text"){
     item.DATA_TYPE = "String";
}else if(item.DATA_TYPE.indexOf("time") != -1||item.DATA_TYPE.indexOf("date") != -1){
    item.DATA_TYPE = "Date"
}else if(item.DATA_TYPE==="enum"){
    item.DATA_TYPE = "[0,1]"
}


let column = "\n\t"+item.COLUMN_NAME+" : "+item.DATA_TYPE+",";
 list+=column;

})
getColumn(table,list);
})
}

//将参数写出到文件
function getColumn(table,columns){

let content = “module.exports = function(db,callback){”+
“\n”+“db.define(’”+table.replace(/_/g,’’)+“Models’,{”+
columns+
“\n”+"},{"+
“\n”+“table : '”+table+"’"+
“\n”+"});"+
“\n”+“return callback();”+
“\n”+"}";

fs.mkdirSync(“models”,{ recursive: true });
fs.writeFile(“models/”+table.replace(/_/g,’’)+“Models.js”, content, function(err) {
if (err) {
return console.error(err);
}
console.log(‘写入成功’)
});
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值