sequelize用法-前言

数据库 waitig 515℃ 百度已收录 0评论

sequence是nodejs操作数据库的orm

前言主要是搭建express的框架是尝试sequelize的功能
我搭建的express的目录结构

zengwe@zengwe-PC:nodeser$ tree -I 'node_modules'
.
├── app.js
├── config
│   └── config.js
├── controllers
│   └── login.js
├── models
│   ├── baseModel
│   │   ├── group.js
│   │   ├── group_permission.js
│   │   ├── info.js
│   │   ├── init.js
│   │   ├── permission.js
│   │   ├── user_group.js
│   │   └── user.js
│   └── user.js
├── package.json
└── router.js

app.js 为入口文件

let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let app = express();
let router = require('./router');
let serverConf = require('./config/config').server;

app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', router);
app.use(function (req, res, next) {
    let err = new Error('Not Found');
    err.status = 404;
    next(err);
});
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: (app.get('env') === 'development') ? err : {}
    });
});

app.listen(serverConf.port, serverConf.host, function () {
    console.log('-------------------------------');
    console.log('server is run;http://' + serverConf.host + ':' + serverConf.port);
    console.log('-------------------------------');
});

路由文件routerjs

let express = require('express');
let router = express.Router();
let controller = require('./controllers/login');
router.get('/login', function (req, res, next) {
    console.log('login middle');
    next();
}, (req, res) => controller.login(req, res));
router.get('/insert', (req, res) => controller.insert(req, res));
module.exports = router;

controllser

let userModel = require('../models/user');
class loginController{
    constructor(){
        console.log("loginController constructor");
    }
    login(req,res){
        let data= userModel.getModel().findAll().spread(function(item,created){
              //console.log(item);
              //return item.dataValues;
              return item;
        });
        data.then((mydata)=>{
            console.log(mydata);
            //console.log(JSON.stringify(mydata));
            //JSON()
            res.send(JSON.stringify(mydata));
        });
        //res.send('loginController');
    }
    insert(req,res){
        userModel.getModel().create({changeName:'dfads',password:'123456',age:19}).then(function(success){
            console.log('success');
            console.dir(arguments);
            res.send(success);
        },function(err){
            console.log('error');
            console.log(err);
            //res.send(err);
            res.send(err.errors[0].message);
            console.dir(JSON.stringify(arguments));
        });
    }
}
module.exports =new loginController();

链接数据库

let Sequelize = require('sequelize');
let mysqlCfg = require('../../config/config').mysql;
let sequelize = new Sequelize(mysqlCfg.database, mysqlCfg.username, mysqlCfg.password, {
    host: mysqlCfg.host,
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    }
});
module.exports = sequelize;

基础的环境大概就是这样了


本文由【waitig】发表在等英博客
本文固定链接:sequelize用法-前言
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)