findAndCountAll 方法
findAndCountAll
会在查出列表记录的同时, 还会返回符合你查询列表数据的条数。
分页
1 2 3 4 5 6 7 8 9 10 11
| async queryProjectList(params: { currentPage: number, pageSize: number }) { const { currentPage, pageSize } = params; const result = await UserModel.findAndCountAll({ limit: params.pageSize, offset: (currentPage - 1) * pageSize, }); return { data: result.rows, total: result.count }; }
|
条件查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| async queryProjectList(params: { pid: string, type: ProjectType }) { const { pid, type } = params; const condition: any = { deletedAt: null, } if (pid) { condition.pid = { [Op.like]: `%${pid}%` } } if(type) { condition.type = { [Op.eq]: type } } }
|
distinct
如果使用了 include
参数,需要额外传入 distinct: true
配置来去除 include
的数量。
1 2 3 4 5 6 7 8 9
| const result = await Model.findAndCountAll({ limit: params.pageSize, offset: (currentPage - 1) * pageSize, include: [ { model: HobbyModel}, { model: FriendModel}, ], distinct: true, });
|
参考