您的位置:首页 > 产品设计 > UI/UE

sequelize-auto简易使用--封装一个端口

2017-10-19 16:57 429 查看
1. 前提已下载 sequelize mysql,未下载可看http://blog.csdn.net/my_roads/article/details/78285768

2. 在项目中 : npm install sequelize-auto --save

3.创建auto.js



var SequelizeAuto = require('sequelize-auto')
var auto = new SequelizeAuto(
'hhdj', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
directory: './models', // prevents the program from writing to disk
port: '3306',
additional: {
timestamps: false
//...
}
}
)
auto.run(function (err) {
if (err) throw err;

console.log(auto.tables); // table list
console.log(auto.foreignKeys); // foreign key list
});

4.创建createModelsExport.js,自动在models生成index.js



let fs = require('fs')
let files = fs.readdirSync('./models')
let models = []
// 解析名称做成驼峰命名法
files.forEach(item => {
if (item != 'index.js') {
let names = item.split('.')[0].split('_')
let name = ''
for (let i = 1; i < names.length; i++) {
name += names[i].substring(0,1).toUpperCase() + names[i].substring(1)
}
models.push({name: name, path: './' + item})
}
})
// 文件内容模板
const template = `
var  Sequelize = require('sequelize');
// 创建数据库连接
var sequelize = new Sequelize('hhdj', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
})
// 数据库模型名称及lujing
const models =${JSON.stringify(models, null, 4)}
// 数据模型输出
models.forEach(item => {
module.exports[item.name] = require(item.path)(sequelize, Sequelize)
})
`
fs.writeFile("./models/index.js", template, function () {
console.log('创建成功')
})


4.生成的index.js



var  Sequelize = require('sequelize');
// 创建数据库连接
var sequelize = new Sequelize('hhdj', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
})
// 数据库模型名称及lujing
const models =[
{
"name": "ApplyInfo",
"path": "./tb_apply_info.js"
},
{
"name": "Branch",
"path": "./tb_branch.js"
},
{
"name": "Carousel",
"path": "./tb_carousel.js"
},
{
"name": "Comment",
"path": "./tb_comment.js"
},
{
"name": "CommentRelation",
"path": "./tb_comment_relation.js"
},
{
"name": "CommentUser",
"path": "./tb_comment_user.js"
},
{
"name": "Coordinate",
"path": "./tb_coordinate.js"
},
{
"name": "Forum",
"path": "./tb_forum.js"
},
{
"name": "ForumComment",
"path": "./tb_forum_comment.js"
},
{
"name": "Impress",
"path": "./tb_impress.js"
},
{
"name": "Integral",
"path": "./tb_integral.js"
},
{
"name": "IntegralRule",
"path": "./tb_integral_rule.js"
},
{
"name": "News",
"path": "./tb_news.js"
},
{
"name": "Notice",
"path": "./tb_notice.js"
},
{
"name": "PartyStyle",
"path": "./tb_party_style.js"
},
{
"name": "Payfee",
"path": "./tb_payfee.js"
},
{
"name": "Picture",
"path": "./tb_picture.js"
},
{
"name": "Report",
"path": "./tb_report.js"
},
{
"name": "StudyFile",
"path": "./tb_study_file.js"
},
{
"name": "User",
"path": "./tb_user.js"
},
{
"name": "Manager",
"path": "./user_manager.js"
}
]
// 数据模型输出
models.forEach(item => {
module.exports[item.name] = require(item.path)(sequelize, Sequelize)
})

5.在user.js中引出



var express = require('express');
var router = express.Router();
var User = require('../models/').User

/* GET users listing. */
router.get('/login', function(req, res) {
var phone = req.query.phone
User.find({where: {phone: phone}})
.then( su => {
res.send({code: 200, message: '创建成功', data: su})
}
)
.catch( ex => {
res.send({code: 500, message: '服务器异常'})
}
)
});

module.exports = router;


97c4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐