Skip to content

Feature request: @Filter on a relation #704

@mikeconley12

Description

@mikeconley12

Is your feature request related to a problem? Please describe.
I think it would be useful to be able to add filters to the join conditions.

Describe the solution you'd like

@Entity()
export class Post {
  @OneToMany(() => Comment, comment => comment.post)
  @Filter({ name: "enabledComments", cond: { enabledComments: true } })
  @FilterJoinTable({ name: "notDeletedComments", cond: { isDeleted: false } })
  comments = new Collection<Comment>(this);
}
const posts = await orm.em.find(Post, { title: "Example" }, { 
  populate: ["comments"]
});

It should generate a query like this:

select * from "Post" as p
  join "Comment" as c on (p.id = c."postId")
    and (p."enabledComments" = true)
    and (c."isDeleted" = false);

See Hibernate docs (Example 19.2. Using @FilterJoinTable for filterting on the association table)

Describe alternatives you've considered
I've read about filters and LoadStrategy.SELECT_IN, but I think LoadStrategy.SELECT_IN is bad for performance

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions