Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Filter is ignored when using Query Builder #3009

Closed
daniel-entitle opened this issue Apr 13, 2022 · 8 comments
Closed

Global Filter is ignored when using Query Builder #3009

daniel-entitle opened this issue Apr 13, 2022 · 8 comments
Labels
wontfix This will not be worked on

Comments

@daniel-entitle
Copy link

Describe the bug
It seems the "applyFilters" logic that happens when using EntityManager.find does not appear to exist when using the query builder.
Stack trace

No stack trace needed

To Reproduce
Steps to reproduce the behavior:

  1. Make global Filter orm.em.addFilter("tenant",(args: any) => ({}))
  2. Query the model with query builder (await orm.em.createQueryBuilder(ModelToFilterWithGlobalFilter).getResultList()

Expected behavior
Same behavior as EntityManager.find

Additional context
const temp = await orm.em.createQueryBuilder(TModel).getResultList(); // does not fire global filter
const temp2 = await orm.em.find(TModel, {}); //does fire global filter
Versions

Dependency Version
node irrelevant
typescript irrelevant
mikro-orm 5.1
your-driver postgres
@B4nan B4nan added the wontfix This will not be worked on label Apr 13, 2022
@B4nan
Copy link
Member

B4nan commented Apr 13, 2022

Yes, because filters are only handled by EM, with QB things need to be explicit.

@B4nan B4nan closed this as completed Apr 13, 2022
@daniel-entitle
Copy link
Author

@B4nan Hey! Is there any workaround for auto filters when using filters? (Because not all queries are available with EM.find)

@B4nan
Copy link
Member

B4nan commented Apr 14, 2022

Not tested, but something like this might work:

const qb = ...;
const filters = em.applyFilters('EntityName', {}, {}, 'read');
qb.andWhere(filters);

@bozorgmehr96
Copy link

@B4nan, Is there any way to get global filters from EM and apply them to QB? Or maybe an option on createQueryBuilder() to apply global filters.
IMO, the fact that they are global makes them different from other filters, and it's better to apply them or at least give the option to apply them on QB, too.

@B4nan
Copy link
Member

B4nan commented Jan 13, 2024

Its a sync method, to apply filters you need async context as they can be async. You can use em.applyFilters() to do this.

@bozorgmehr96
Copy link

Yeah, I ended up doing that.
Also, I think I found a bug on filters; relation filters do not apply on the count query of the findAndCount() method. (In the JOIN ON condition of the query)
I will test it further and create a new issue for it.

@B4nan
Copy link
Member

B4nan commented Jan 13, 2024

I see, looks like the regular filters work, but not the filters on relations.

@B4nan
Copy link
Member

B4nan commented Jan 14, 2024

I will wait for your repro, but after spending a few hours looking into the count and filters problem, I feel like it actually works correctly. Filters on relations are not considered for the where condition (unless you use populateWhere: infer), so it has no effect on the root entity count. I just got it implemented and realized that it only complicates the query without any outcome, as you get left joins that cannot affect the root entity count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants