如何根据与其他表的多对多关系筛选出元素?

时间:2019-05-04 20:05:40

标签: javascript node.js sequelize.js

我有Adventures类,Hashtag类和关系类HashtagAdventure。我想做的是从用户提供的集合中获取所有至少包含一个#标签的冒险。

@Table
export class HashtagAdventure extends Model<HashtagAdventure> {
    @AllowNull(false)
    @ForeignKey(() => Hashtag)
    @Column
    hashtagId: string;

    @AllowNull(false)
    @ForeignKey(() => Adventure)
    @Column
    adventureId: string;
}


@Table
export class Hashtag extends Model<Hashtag> {
    @PrimaryKey
    @Column(DataType.STRING)
    id: string;

    @Column(DataType.TEXT)
    text: string;

    @BelongsToMany(() => Adventure, () => HashtagAdventure)
    adventures: Adventure[];
}

@Table
export class Adventure extends Model<Adventure> {
    @PrimaryKey
    @Column(DataType.STRING)
    id: string;

    @BelongsToMany(() => Hashtag, () => HashtagAdventure)
    hashtags: Hashtag[];
}

考虑一个例子:

MyAdventure [#awesome,#great]

OtherAdventure [#cool]

BoringAdvenure [#boring]


用户查询:[#awesome,#cool]


预期输出:

MyAdventure [#awesome,#great]

OtherAdventure [#cool]


到目前为止,我通过玩查询(例如通过include)获得了最好的成绩:

MyAdventure [#awesome]

OtherAdventure [#cool]

BoringAdvenure []

0 个答案:

没有答案