Typeorm-通过ManyToMany关系查找条目

我正在将nestjs与Typeorm和Mysql一起使用,但我想不出一种按条目之间的多对多关系过滤条目的好方法。

我有以下两个实体:

组实体:

@Entity({ name: 'groups' })
export class Group {
    @ManyToMany(() => Tag,{ eager: true })
    @JoinTable()
    tags: Tag[];
}

标签实体

@Entity({ name: 'tags' })
export class Tag {
    @Column()
    @Index({ unique: true })
    tag?: string;
}

并希望搜索所有带有带有特定文本标签的组。

即。所有拥有tag.tag“运动”

的组

尝试过此代码:

const args = {
    where: [
        {
            'tags': In([Like(`%sport%`)]),}
    ],relations: ['tags'],// TAGS
    take: filter.take,skip: filter.skip,order: filter.order
};

return super.findAll(args);

但它似乎不起作用。

任何帮助都会很棒!

hewei600 回答:Typeorm-通过ManyToMany关系查找条目

return find({
  where: {
    tags: {
      tag: Like(`%sport%`),},relations: ['tags'],});

几乎,typeorm从如下关系中接受ObjectLiteral或keyof typeof标签:

FindConditions<T>: {
  where: {
    [s: keyof typeof T]: any,}

那不是全部,但这是一般要点。而且,如果keyof T是一个关系,则any几乎会被keyof relation取代。

这是findConditions https://github.com/typeorm/typeorm/blob/master/src/find-options/FindConditions.ts

的完整类型
本文链接:https://www.f2er.com/3137505.html

大家都在问