Sequelize
Last updated
Last updated
useQuery Sequelize to Simplify Query Management data
Using commonjs
const { useSequelize } = require('expresso-query')
Using ES6
import { useSequelize } from 'expresso-query'
import { useSequelize } from 'expresso-query'
import { User } from 'database/entities/User'
import { Role } from 'database/entities/Role'
import route from 'routes/v1'
route.get(
'/user',
async function findAll(req: Request, res: Response) {
const reqQuery = req.query
const relations = [{ model: Role }]
const query = useSequelize.queryBuilder({
entity: User,
reqQuery,
includeRule: useSequelize.makeIncludeQueryable(reqQuery.filtered, relations),
}, { type: 'postgres' })
const data = await User.findAll({
...query,
order: query.order ? query.order : [['createdAt', 'desc']],
})
const total = await User.count({
include: query.includeCount,
where: query.where,
})
return res.status(200).json({ data, total })
})