【Node】:使用 sequelize 在 node 环境下操作 Mysql
усил の博客 2021/3/20 NodeMysql
# 2-7、sequelize 创建连接数据库
(1)sequelize 介绍
- ORM - Object Relational Mapping (对象关系映射)
(2)使用 sequelize 创建数据库模型方法
- 数据库,用 JS 中的模型(class或对象) 代替
- 一条或多条记录,用 JS 中一个对象或数组代替
- sql 语句,用对象方法代替
(3)代码演示
- 环境配置
- 代码演示
/* seq.js */
const Sequelize = require('sequelize')
const conf = {
host:'localhost',
dialect:'mysql'
}
const seq = new Sequelize('koa2-weibo_db', 'root', '123456',conf)
//测试连接
seq.authenticate().then(() => {
console.log('ok')
}).catch(() => {
console.log('err')
})
// module.exports = seq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2-8、sequelize 创建数据模型及关联外键
# 2-9、sequelize 插入数据
const { User, Blog} = require('./model')
!(async function demo () {
//创建用户
const zhangsan = await User.create({
userName:'zhangsan',
password:'123',
nickName:'张三'
})
console.log('zhangsan',zhangsan.dataValues)
})()
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2-10、sequelize 查询数据
- 查询一条记录
const { User} = require('./model')
!(async function () {
const zhangsan = await User.findOne({
where: {
userName: 'zhangsan'
}
})
console.log(zhangsan.dataValues)
})()
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 查询特定的列
const { User} = require('./model')
const zhangsanName = await User.findOne({
attributes: ['userName', 'nickName'],
where: {
userName: 'zhangsan'
}
})
console.log(zhangsanName.dataValues)
})()
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 查询一个列表
const { Blog} = require('./model')
!(async function () {
const zhangsanBlogList = await Blog.findAll({
where:{
userId:1
},
order:[
["id","desc"]
]
})
console.log(zhangsanBlogList.map(blog => blog.dataValues))
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 数据分页
const { Blog} = require('./model')
!(async function () {
const blogPageList = await Blog.findAll({
limit:2,//一页多少个数据
offset:0,//跳过多少个数据
order:[
['id','desc']
]
})
console.log(blogPageList.map(blog => blog.dataValues))
})()
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- 查询总数和数据分页
const { Blog} = require('./model')
!(async function () {
const blogListAndCount = await Blog.findAndCountAll({
limit:2,
offset:0,
order:[
["id", "desc"]
]
})
console.log(blogListAndCount.count,blogListAndCount.rows.map(blog => blog.dataValues))
})()
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2-11、sequelize 连表查询
- 连表查询1 查询zhangsan博客(主)连带查询用户信息(次)
//连表查询1 查询zhangsan博客(主)连带查询用户信息(次)
const blogListWithUser = await Blog.findAndCountAll({
order:[
["id","desc"]
],
include:[
{
model:User,//次表
attributes:['userName','nickName'],
where: {
userName :'zhangsan'
}
},
],
})
console.log(blogListWithUser.count)
console.log(
blogListWithUser.rows.map(blog => {
//取出全部数据
const blogVal = blog.dataValues
//数据格式优化
blogVal.user = blogVal.user.dataValues
return blogVal;
})
)
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
代码讲解
- 连表查询2 查询zhangsan用户信息(主)连带查询博客(次)
const userListWithBlog = await User.findAndCountAll({
attributes:['username','nickName'],
include:[
{
model:Blog
}
]
})
console.log(userListWithBlog.count)
console.log(
userListWithBlog.rows.map(user => {
const userVal = user.dataValues
userVal.blogs =userVal.blogs.map(blog => blog.dataValues)
return userVal
})
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
代码讲解 另外:如果控制台想查看输出的博客信息可以进行以下修改
# 2-12、sequelize 更新与删除操作
- 更新操作
const {User} = require('./model')
!(async function () {
const updateRes = await User.update({
nickName:'张三'
},
{
where: {
userName:'zhangsan'
}
})
console.log('updateRes...',updateRes[0] > 0)
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 删除操作
const {Blog} = require('./model')
!(async function ()){
const delBlogRes = await Blog.destroy({
where:{
id:4
}
})
console.log('delBlogRes',delBlogRes>1)
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10