Sequelize 根据条件查找
Sequelize find based on conditions
我有 x 个帐户,其中列出了 50 个成员,包括帐户的创建者。
我想以登录用户应出现在顶部的方式显示该帐户的成员列表,然后应用另一个排序。
注意:Api 会包含 skipCount(offset) 和 LimitCount(limit) 。只有当 skipCount 为 0 时,才会应用上述逻辑 (登录用户应出现在顶部).
假设您的登录用户 ID 为 loggedUserId
,那么您可以像这样进行排序:
const order = []
if (skipCount === 0) {
order.push([Sequelize.literal('($userId = accounts.id)'), 'DESC'])
}
order.push(<other sorting>)
const accounts = await Accounts.findAll({
bind: {
userId: loggedUserId
},
order,
limit: LimitCount,
offset: skipCount
})
我有 x 个帐户,其中列出了 50 个成员,包括帐户的创建者。 我想以登录用户应出现在顶部的方式显示该帐户的成员列表,然后应用另一个排序。 注意:Api 会包含 skipCount(offset) 和 LimitCount(limit) 。只有当 skipCount 为 0 时,才会应用上述逻辑 (登录用户应出现在顶部).
假设您的登录用户 ID 为 loggedUserId
,那么您可以像这样进行排序:
const order = []
if (skipCount === 0) {
order.push([Sequelize.literal('($userId = accounts.id)'), 'DESC'])
}
order.push(<other sorting>)
const accounts = await Accounts.findAll({
bind: {
userId: loggedUserId
},
order,
limit: LimitCount,
offset: skipCount
})