Nodejs + sequelize 实现增删改查操作

2022-01-14,,,

这篇文章主要介绍了Nodejs + sequelize 实现增删改查操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1. 下载资源库

npm install sequelize --save

npm install mysql2 --save // npm install mysql 提示不完整

2. 创建数据库配置文件 db.js,配置数据库

 var Sequelize = require('sequelize'); module.exports = new Sequelize('blog', 'root', '123456', { host: 'localhost', // 数据库地址 dialect: 'mysql', // 指定连接的数据库类型 operatorsAliases: false, pool: { max: 5, // 连接池中最大连接数量 min: 0, // 连接池中最小连接数量 idle: 10000 // 如果一个线程 10 秒钟内没有被使用过的话,那么就释放线程 } });

3. 创建一个model 文件 user.js

 var Sequelize = require('sequelize'); var sequelize = require('./db'); // 创建 model var User = sequelize.define('user', { id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true, unique : true}, userName: { type: Sequelize.STRING, // 指定值的类型 field: 'user_name' // 指定存储在表中的键名称 }, // 没有指定 field,表中键名称则与对象键名相同,为 email email: { type: Sequelize.STRING } }, { // 如果为 true 则表的名称和 model 相同,即 user // 为 false MySQL创建的表名称会是复数 users // 如果指定的表名称本就是复数形式则不变 freezeTableName: true }); /*User.sync({force:false}).then(function(){ console.log("success to start"); }).catch(function(err){ console.log("failed to start ") })*/ // 创建表 // User.sync() 会创建表并且返回一个Promise对象 // 如果 force = true 则会把存在的表(如果users表已存在)先销毁再创建表 // 默认情况下 forse = false //var user = User.sync({ force: false }); // 添加新用户 exports.addUser = function(userName, email) { // 向 user 表中插入数据 return User.create({ userName: userName, email: email }).then(function(result){ console.log("插入操作成功"+result); }).catch(function(err){ console.log("添加数据发生错误:"+err) }); }; exports.findByName = function(userName) { return User.findOne({where: {user_name:userName }}).then(function(result){ console.log("成功:" + result.id); }).catch(function(err){ console.log("发生错误:" + err); }); }; // 通过用户名查找用户 exports.update = function(id){ return User.findOne({where: {id:id }}).then(function(user){ return user.update({ email:'jack3@qq.com' }).then(function(result){ console.log("update success: "+result); }).catch(function(err){ console.log("更新操作出错:"+err); }); }); }; exports.destroy = function(id){ return User.destroy({where:{id:id}}).then(function(result){ console.log("delete success"); }).catch(function(err){ console.log("delete data err: "+err); }) } 

4. 测试文件

 var user = require('./user'); //查询操作 //user.findByName("jack"); // 添加用户 //user.addUser('jack2', 'jack@163.com'); // 更新 //user.update(1001); //删除 //user.destroy(1001);

补充知识:nodejs Sequelize 简单查询语句和 mysql常用的几个查询命令

我是前端,但总有需求让做后端的活,所以顺带着熟悉了下简单的查询语句

贴出来,如果有需要可以参考下,备注很详细,就不多解释了

废话不多说贴代码:

 #去除unionid 重复的搜索结果 #query_resultsign 表名 select *, count(unionid) from query_resultsign where issign='false' group by unionid ; #去除unionid 重复的搜索结果 #query_resultsign 表名 select *, count(unionid) from query_resultsign where issign='true' group by unionid ; #求未签约用户的平均访问频率(即为求搜索结果列的平均值issign='false' 未签约) #cuid 是unid的别名 #query_resultsign 表名 select AVG(bs.cuid) as unUserAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='false' group by unionid ) as bs; #求平均值 #(即为求搜索结果issign='true' count的平均值) #bs为子查询的别名,不带别名会报错 #query_resultsign 表名 select AVG(bs.cuid) userAvg FROM (select *, count(unionid) cuid from query_resultsign where issign='true' group by unionid ) as bs; #增加id 列 int #query_resultsign ALTER TABLE query_resultsign add id int; #使表 query_resultsign (上一步)增加的列变为自增列 alter table query_resultsign change id id int NOT NULL AUTO_INCREMENT primary key; #获取两列数据中有相同数据的列 #query_resultsign 表名 select  p1.*  from  query_resultsign  p1,query_resultsign  p2  where  p1.idp2.id and  p1.x  =  p2.x and  p1.y  =  p2.y ; #查找表query_resultsign unionid 相同的用户 select  p1.*  from  query_resultsign  p1,query_resultsign  p2  where  p1.idp2.id and  p1.unionid  =  p2.unionid ;

sequelize 的调用sql语句的方法顺带提一下,网上大多教程都是用model 查询的,每次都要建立model。有点麻烦 。配置的教程请参看配置教程。

sequelize调用sql主要用query(sql,{})方法:

 var Sequelize = require('sequelize');//引入sequelize var sequelize = require('./../../database/dataconfig'); //引入连接配置文件 //查找签约用户 exports.selectHeatData = function (req, res) { return sequelize.query("select * from `query_resultSign` where issign ='true'", { type: sequelize.QueryTypes.SELECT }).then(data => { // console.log('******', data); res.send(data); }).catch(err => { console.log('错误', err) }) } //其他方法就是换了下sql语句

主要知识点就是query方法内传入查询出的结果的类型 { type: sequelize.QueryTypes.SELECT } 这样就不用手动转换成json对象了。

附带配置文件代码

dataconfig.js

 var Sequelize = require('sequelize'); module.exports = new Sequelize('pingan_scame', 'root', '123456', { host: 'localhost', // 数据库地址 dialect: 'mysql', // 指定连接的数据库类型 operatorsAliases: false, pool: { max: 5, // 连接池中最大连接数量 min: 0, // 连接池中最小连接数量 idle: 10000 // 如果一个线程 10 秒钟内没有被使用过的话,那么就释放线程 } });

以上就是Nodejs + sequelize 实现增删改查操作的详细内容,更多请关注本站其它相关文章!

《Nodejs + sequelize 实现增删改查操作.doc》

下载本文的Word格式文档,以方便收藏与打印。