接下来我们实现文章相关的具体api, Sequelize 提供了多种方法来协助查询数据库中的数据.也就是模型查询。
首先引入对应的模型, 然后调用。
文章列表
列表
const { article } = require('../models')
// 文章列表
router.get('/list', async (req, res, next) => {
const lists = await article.findAll()
res.json({ status: 200, data: { lists }, msg: '成功' })
})
attributes
列表中只需要查询某些特定属性时,可以使用 attributes 参数
// 文章列表
router.get('/list', async (req, res, next) => {
const lists = await article.findAll({
attributes: ['id', 'title', 'createdAt'],
})
res.json({ status: 200, data: { lists }, msg: '成功' })
})
可以使用嵌套数组来重命名属性
const lists = await article.findAll({
attributes: ['id', ['title', 'name'], 'createdAt'],
})
模型查询(基础) | Sequelize中文文档 | Sequelize中文网
排序
对id进行降序排列
const lists = await article.findAll({
attributes: ['id', ['title', 'name'] ],
order: [
['id', 'DESC'],
]
})
模型查询(基础) | Sequelize中文文档 | Sequelize中文网
文章详情
where
// 读取文章详情
router.get('/list/:id', async (req, res, next) => {
const contents = await article.findAll({
where: {
id: req.params.id,
},
})
res.json({ status: 200, data: { contents }, msg: '成功' })
})
where 参数用于过滤查询,可以传递多个校验, 比如上面的示例,我们可以增加校验条件
多条件
router.get('/list/:id', async (req, res, next) => {
const contents = await article.findAll({
where: {
id: req.params.id,
"title": "Article 3",
},
})
res.json({ status: 200, data: { contents }, msg: '成功' })
})
运算符
同时where 也支持运算符(来自Op), 比如上面的多条件, 我们可以选择and/or, 记得引入op
const { Op } = require("sequelize");
// 读取文章详情
router.get('/list/:id', async (req, res, next) => {
const contents = await article.findAll({
where: {
[Op.or]: [
{ id: req.params.id },
{ title: 'Article 3' }
],
},
})
res.json({ status: 200, data: { contents }, msg: '成功' })
})
关于where更多用法, 可以查询模型查询(基础) | Sequelize中文文档 | Sequelize中文网
findByPk&findOne
findByPk 方法使用提供的主键从表中仅获得一个条目
router.get('/list/:id', async (req, res, next) => {
const contents = await article.findByPk(req.params.id)
res.json({ status: 200, data: { contents }, msg: '成功' })
})
结果一样, 还有一个findOne 方法获得它找到的第一个条目
router.get('/list/:id', async (req, res, next) => {
const contents = await article.findOne({
where: {
id: req.params.id,
}
})
res.json({ status: 200, data: { contents }, msg: '成功' })
})
添加文章
create
// 添加文章
router.post('/add', async (req, res, next) => {
const { title, content } = req.body
const result = await article.create({ title, content })
res.json({ status: 200, data: { result }, msg: '成功' })
})
删除文章
// 删除文章
router.post('/del', async (req, res, next) => {
const { id } = req.body
const result = await article.destroy({
where: {
id
}
})
res.json({ status: 200, data: { result }, msg: '成功' })
})
返回的结果
{
"status": 200,
"data": {
"result": 1 // 1成功; 0 失败
},
"msg": "成功"
}
编辑文章
// 更新文章
router.post('/update', async (req, res, next) => {
const { id, title, content } = req.body
const result = await article.update({ title, content }, {
where: {
id
}
})
res.json({ status: 200, data: { result }, msg: '成功' })
})
result与上面一致,0 代表成功
列表分页
上面的列表目前直接返回10000条数据, 数据量太大, 接下来我们来进行分页.
使用 limit 和 offset 参数可以进行 限制/分页
router.get('/list', async (req, res, next) => {
const { pagesize, page } = req.query
const offset = (page - 1) * pagesize
const limit = parseInt(pagesize)
const lists = await article.findAll({
attributes: ['id', ['title', 'name']],
order: [['id', 'DESC']],
offset,
limit,
})
res.json({ status: 200, data: { lists }, msg: '成功' })
})
首先需要从请求参数中获取当前页码和每页显示的数量
然后计算数据库查询的偏移量, 并将每页显示数量转换为整数