一、node JS 中 sequelize 结合 mysql 实现增加、查询、修改和删除
sequelize
结合mysql
实现增加,create.js
代码如下所示:
const {
Blog, User } = require('./model')
!(async function () {
// 创建用户
const zhangsan = await User.create({
userName: 'zhangsan',
password: '123',
nickName: '张三'
})
// insert into users (...) values (...)
console.log('zhangsan: ', zhangsan.dataValues)
const zhangsanId = zhangsan.dataValues.id
const lisi = await User.create({
userName: 'lisi',
password: '123',
nickName: '李四'
})
const lisiId = lisi.dataValues.id
// 创建博客
const blog1 = await Blog.create({
title: '标题1',
content: '内容1',
userId: zhangsanId
})
console.log('blog1', blog1.dataValues)
const blog2 = await Blog.create({
title: '标题2',
content: '内容2',
userId: zhangsanId
})
const blog3 = await Blog.create({
title: '标题3',
content: '内容3',
userId: lisiId
})
const blog4 = await Blog.create({
title: '标题4',
content: '内容4',
userId: lisiId
})
})()
sequelize
结合mysql
实现查询,select.js
代码如下所示:
const {
User, Blog } = require('./model')
!(async function () {
// 查询一条记录
// const zhangsan = await User.findOne({
// where: {
// userName: 'zhangsan'
// }
// })
// console.log('zhangsan', zhangsan.dataValues )
// 查询特定的列
// const zhangsanName = await User.findOne({
// attributes: ['userName', 'nickName'],
// where: {
// userName: 'zhangsan'
// }
// })
// console.log('zhangsanName', zhangsanName.dataValues)
// 查询一个列表
// const zhangsanBlogList = await Blog.findAll({
// where: {
// userId: 1
// },
// order: [
// ['id', 'desc']
// ]
// })
// console.log('zhangsanBlogList', zhangsanBlogList.map(blog => blog.dataValues))
// 分页
// const blogPageList = await Blog.findAll({
// limit: 2, // 限制本次查询 2 条,每页显示的条数
// offset: 2, // 跳过多少条,从第几页开始
// order: [
// ['id', 'desc']
// ]
// })
// console.log('blogPageList', blogPageList.map(blog => blog.dataValues))
// 查询总数
// const blogListAndCount = await Blog.findAndCountAll({
// limit: 2,
// offset: 2,
// order: [
// ['id', 'desc']
// ]
// })
// console.log('blogListAndCount',
// blogListAndCount.count, // 所有的总数,不考虑分页
// blogListAndCount.rows.map(blog => blog.dataValues)
// )
// 连表查询 1
// 先有 blog,后有 user
// const blogListWithUser = await Blog.findAndCountAll({
// order: [
// ['id', 'desc']
// ],
// include: [
// {
// model: User,
// attributes: ['userName', 'nickName'],
// where: {
// userName: 'zhangsan'
// }
// }
// ]
// })
// console.log('blogListWithUser',
// blogListWithUser.count,
// blogListWithUser.rows.map(blog => {
// const blogVal = blog.dataValues
// blogVal.user = blogVal.user.dataValues
// return blogVal
// })
// )
// 连表查询 2
const userListWithBlog = await User.findAndCountAll({
attributes: ['userName', 'nickName'],
include: [
{
model: Blog
}
]
})
console.log('userListWithBlog',
userListWithBlog.count,
userListWithBlog.rows.map(user => {
const userVal = user.dataValues
userVal.blogs = userVal.blogs.map(blog => blog.dataValues)
return userVal
})
)
})()
sequelize
结合mysql
实现修改,update.js
代码如下所示:
const {
User } = require('./model')
!(async function () {
const updateRes = await User.update({
nickName: '张三1'
}, {
where: {
userName: 'zhangsan'
}
})
console.log('updateRes', updateRes[0] > 0)
})()
sequelize
结合mysql
实现删除,delete.js
代码如下所示:
const {
User, Blog } = require('./model')
!(async function () {
// 删除一条博客
const deleteBlogRes = await Blog.destroy({
where: {
id: 4
}
})
console.log('deleteBlogRes', deleteBlogRes > 0)
// 删除一个用户
const deleteUserRes = await User.destroy({
where: {
id: 1
}
})
console.log('deleteUserRes', deleteUserRes)
})()