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

该博客演示了如何使用Sequelize ORM在Node.js中进行MySQL的基本操作,包括查找单条记录、按条件查询、一对多关联查询、统计总数、分页、删除和更新记录等。
摘要由CSDN通过智能技术生成

//引入数据表配置

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'}, {amount: 600}]

}

}).then(records=>{

res.json(records);

});

// SELECT * FROM Records WHERE amount= 550 OR amount = 3131;

Records.findAll({

where: {

amount: {

[Op.or]: [550, 3131]

}

}

}).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);//返回添加的数据实例

})

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值