node.js Sequelize操作mysql基本的增删改查demo

Posted 码农Doggy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node.js Sequelize操作mysql基本的增删改查demo相关的知识,希望对你有一定的参考价值。

//引入数据表配置
var Records = require('../../mysqlOrmModel/Records');//记账主表记录模型
var Details = require('../../mysqlOrmModel/Details');//记账详情表模型
var Sequelize = require('sequelize');
const Op = Sequelize.Op;
/**
 * 查找账本记录数据(ORM 操作mysql) 单表查询
 */

exports.index = function (req, res, next{
    if (req.query.id == undefined) {
        res.json({'status'-1'message''缺失请求参数''data'null});
    } else {
        Records.findOne({where: {id: req.query.id}}).then(records => {
        /* 结果 req.query.id=62
         {
         "id": 62,
         "time": "2018-5-4",
         "title": "3131",
         "amount": "3131.00"
         }
         */

            res.json(records);
        });

        // search for known ids
        Records.findByPk(req.query.id).then(records => {
        // project will be an instance of Project and stores the content of the table entry
        // with id 123. if such an entry is not defined you will get null
        /* 结果 req.query.id=62
         {
         "id": 62,
         "time": "2018-5-4",
         "title": "3131",
         "amount": "3131.00"
         }
         */

           res.json(records);
        })


        //查找单条记录只返回特定字段
        Records.findOne({
            where: {id: req.query.id},
           attributes: ['id', ['amount','amount'],['title','title']]
        }).then(records => {
        // project will be the first entry of the Projects table with the title 'aProject' || null
        // project.title will contain the name of the project
        /* req.query.id=66
         {
         "id": 66,
         "amount": "300.00",
         "title": "test"
         }
         */

            res.json(records);
        })


        // SELECT * FROM Records WHERE title = 'test' OR amount = 600;
        Records.findAll({
            where: {
                [Op.or]: [{title'test'}, {amount600}]
            }
        }).then(records=>{
            res.json(records);
        });

        // SELECT * FROM Records WHERE amount= 550 OR amount = 3131;
        Records.findAll({
            where: {
                amount: {
                    [Op.or]: [5503131]
                }
            }
        }).then(records=> {
            res.json(records);
            /**
             [
             {
                 "id": 62,
                 "time": "2018-5-4",
                 "title": "3131",
                 "amount": "3131.00"
             },
             {
                 "id": 61,
                 "time": "2018-5-4",
                 "title": "入行工资",
                 "amount": "3131.00"
             },
             {
                 "id": 83,
                 "time": "2018-5-4",
                 "title": "测试",
                 "amount": "550.00"
             }
             ]
             */

        });


    }
}


/**
 * 获取账本详情 一对一查询
 */

exports.details = function (req, res, next{
    if (req.query.id == undefined) {
        res.json({'status'-1'message''缺失请求参数''data'null});
    } else {
        Records.findOne({
            include: [{
                model: Details,
                where: {pid: req.query.id},//req.query.id=63
            }]
        }).then(records => {
            /*
             返回结果
             {
             "id": 62,
             "time": "2018-5-4",
             "title": "3131",
             "amount": "3131.00",
             "t_detail": {
             "id": 6,
             "pid": 62,
             "details": "312313131"
             }
             }
             */

            res.json(records);
        });
    }
}

/**
 * 统计账本记录总数
 */

exports.total = function (req, res, next{
    Records.findAndCountAll()
        .then(result => {
            res.json(result.count);//总记录数量 15
            //res.json(result.rows);//所有记录
        });
}

/**
 * 账本列表(findAndCountAll适合数据分页)
 */

exports.list = function (req, res, next{
    if (req.query.start == undefined || req.query.number == undefined) {
        res.json({'status'-1'message''缺失请求参数''data'null});
    } else {
        let s = (Number(req.query.start) > 1) ? (Number(req.query.start - 1) * Number(req.query.number)) :
        Number(req.query.start) - 1;
        let e = Number(req.query.number);
        Records.findAndCountAll({
                offset: s,
                limit: e
            })
            .then(result => {
                res.json(result.rows);
            });
    }
}

/**
 * 删除账本记录
 */

exports.del = function (req, res, next{
    if (req.query.id == undefined) {
        res.json({'status'-1'message''缺失请求参数''data'null});
    } else {
        Records.destroy({
            where: {
                id: req.query.id
            }
        }).then(records=> {
            res.json(records);
        });
        // DELETE FROM post WHERE id = req.query.id;
    }
}


/**
 * 更新账本记录
 * @param req
 * @param res
 * @param next
 */

exports.up = function (req, res, next{
    Records.update({
        time: req.query.time,
        title: req.query.title,
        amount: req.query.amount
    }, {
        where: {
            id: req.query.id
        }
    }).then(records=> {
        res.json(records);
    });
// UPDATE Records SET time = req.query.time,title=req.query.title,amount=req.query.amount WHERE id=req.query.id;
};

/**
 * 添加账本记录
 * @param req
 * @param res
 * @param next
 */

exports.add=function(req,res,next){
    if(req.query.time==undefined&&req.query.title==undefined&&req.query.amount==undefined){
        res.json({'status'-1'message''缺失请求参数''data'null});
    }else{
        Records.create({
            time: req.query.time?req.query.time:'',
            title: req.query.title?req.query.title:'',
            amount: req.query.amount?req.query.amount:''
        }).then(records=>{
            res.json(records);//返回添加的数据实例
        })

    }

}


以上是关于node.js Sequelize操作mysql基本的增删改查demo的主要内容,如果未能解决你的问题,请参考以下文章

[转]在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查

多张图片 + Node.js + Sequelize + Mysql

sequelize 用于PostgreSQL,MySQL,SQLite和MSSQL的Node.js / io.js ORM

Node.js ORM框架Sequelize搭建服务

使用 ORM Sequelize / node.js 操作数据

Node.js的ORM框架sequelize