如何在sequelize中设置包含聚合函数别名的顺序?
how to set order on include aggregate function alias in sequelize?
下面是我的代码
export async function getMany(page: number, recordsPerPage: number, condition: any = {}, order: any, attributes: string[] = [], other: object = {}) {
try {
let { count, rows }: any = await User.findAndCountAll({
attributes: {
include: [[sequelize.literal('(SELECT SUM(reputation) FROM scores where scores.user_id = User.id)'), 'reputation']],
exclude: attributes,
},
where: condition,
distinct: true,
include: [
{
model: Skill,
as: 'skills',
attributes: ['skill'],
through: { attributes: [] },
},
],
order: order,
offset: page,
limit: recordsPerPage,
...other,
logging: console.log,
});
return { count, rows };
} catch (e) {
return false;
}
}
我想按信誉字段设置顺序,它是求和函数列的别名。我希望我的数据的声誉从最高到最低。
您可以简单地将订单 属性 替换为给定的代码段。为我工作。
order: [[sequelize.literal('table alias name goes here'), 'DESC']]
下面是我的代码
export async function getMany(page: number, recordsPerPage: number, condition: any = {}, order: any, attributes: string[] = [], other: object = {}) {
try {
let { count, rows }: any = await User.findAndCountAll({
attributes: {
include: [[sequelize.literal('(SELECT SUM(reputation) FROM scores where scores.user_id = User.id)'), 'reputation']],
exclude: attributes,
},
where: condition,
distinct: true,
include: [
{
model: Skill,
as: 'skills',
attributes: ['skill'],
through: { attributes: [] },
},
],
order: order,
offset: page,
limit: recordsPerPage,
...other,
logging: console.log,
});
return { count, rows };
} catch (e) {
return false;
}
}
我想按信誉字段设置顺序,它是求和函数列的别名。我希望我的数据的声誉从最高到最低。
您可以简单地将订单 属性 替换为给定的代码段。为我工作。
order: [[sequelize.literal('table alias name goes here'), 'DESC']]