我想用续集按用户的订阅类型过滤用户。
用户实体:
@HasMany(() => Purchase, { onDelete: "cascade" })
purchases?: Purchase[];
购买实体:
@CreatedAt
date: Date;
@Column
item: PURCHASE_ITEMS;
@ForeignKey(() => User)
userId: number;
@BelongsTo(() => User)
user: User;
因此,如您所见,我的用户还将过去的订阅存储在一个数组中。
我想做的是回馈用户,
item eq 1month.sub
但是我只想要那些最新的子类是我要过滤的类型的子类。 我当前的代码返回了一系列购买,当我针对当前每月订阅1个月的用户进行过滤时,我得到了订阅了1个月的所有用户,即使他们现在已经订阅了12个月。而且,如果我尝试按6month或12month子项进行过滤,我的查询将不会返回任何内容。
if (query.purchase) {
// query.purchase == '1month.sub'
const purch = sqs.find(query.purchase);
const where1 = { ...purch };
const purchaseUserIds = await Purchase.findAll({
attributes: ['userId'],
where: where1,
raw: true,
group: ['userId', 'updatedAt'],
order: [['updatedAt', 'desc']]
});
where.id = purchaseUserIds.map(object => (object as any).userId);
}
,此sql查询稍后在User.FindAll(sql)中使用:
const sql: IFindOptions = {
where,
include: [
{
model: Image,
as: 'images'
},
{ model: Purchase, as: 'purchases' },
{ model: Relationship, as: 'relationships' }
],
order: query.sort ? sqs.sort(query.sort) : [['createdAt', 'desc']],
};
您对我接下来可以尝试的内容有什么想法/建议吗?