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

Allow to change FROM clause using QueryBuilder #3374

Closed
derevnjuk opened this issue Aug 8, 2022 · 2 comments · Fixed by #3378
Closed

Allow to change FROM clause using QueryBuilder #3374

derevnjuk opened this issue Aug 8, 2022 · 2 comments · Fixed by #3378
Labels
enhancement New feature or request

Comments

@derevnjuk
Copy link
Contributor

Description
As a user, I would like to have the ability to change/add FROM clause in which the entity's table select/update/delete will be executed.

Motivation

For example, implementing the seek pagination with the ability to navigate to the previous page, you have to reverse the ordering of the result set (ASC vs DESC) using the same predicate so that the results you want are to the right of the cursor:

SELECT * 
FROM (
  SELECT * 
  FROM users 
  WHERE createdAt < ? 
  ORDER BY createdAt DESC 
  LIMIT 10
) AS u 
ORDER BY createdAt ASC;

Currently, this is possible to do only using knex.fromRaw(), exposing the implementation details of QueryBuilder. What could be a issue from design perspective. As soon as the implementation of either QueryBuilder or knex is changed (e.g. mikro-orm starts using a different query builder from knex), the user will have to rework a significant part of his code.

Just by declaring a from() method, we are able to give the user a powerful mechanism to handle this without necessity to rely on knex:

const qb = orm.em.qb(User, 'user')
  .where({ createdAt: { $lt: new Date() } })
  .orderBy({ createdAt: 'DESC' })
  .limit(10);

const users1 = qb.from(qb.getKnexQuery())
  .orderBy({ createdAt: 'ASC' });

const users2 = qb.from(qb.clone())
  .orderBy({ createdAt: 'ASC' });

Possible solution
The new method(s) can be introduced to be used to change/add FROM clause, removing all previously set from-s:

export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
    from<TFrom extends AnyEntity<TFrom> = T>(entityTarget: TFrom | Knex.QueryBuilder | QueryBuilder<TFrom>): QueryBuilder<TFrom>;
    addFrom<TFrom extends AnyEntity<TFrom> = T>(entityTarget: TFrom | Knex.QueryBuilder | QueryBuilder<TFrom>): QueryBuilder<TFrom>;
}
@derevnjuk derevnjuk added the enhancement New feature or request label Aug 8, 2022
@B4nan
Copy link
Member

B4nan commented Aug 8, 2022

I hope you also plan to send some PRs, and not just a huge round of issues :]

@derevnjuk
Copy link
Contributor Author

derevnjuk commented Aug 8, 2022

@B4nan I hope so, but I can't do this without your help 😄

derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Aug 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Aug 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Aug 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Aug 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
derevnjuk added a commit to derevnjuk/mikro-orm that referenced this issue Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants