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

Sql Snippets in @Filter decorator #2214

Closed
JoelVenable opened this issue Sep 18, 2021 · 2 comments
Closed

Sql Snippets in @Filter decorator #2214

JoelVenable opened this issue Sep 18, 2021 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@JoelVenable
Copy link

JoelVenable commented Sep 18, 2021

First of all, really loving this project.

Current state

So I have a find query that works fine in a repository, though it needs access to a QueryBuilder to create a raw SQL snippet. I think the readability in custom repositories could be enhanced if the Filter decorator has access to a configured query builder.

** Note: I am aware this particular query could have been a nested condition if the relations were defined, however I am deliberately not defining all possible relationships to better enforce domain boundaries.

productRepo.find({
  $or: [
    {
      orgId: qb.raw('(select za.org_id from "zone" za where za.id = ?)', [zoneId]),
      concurrentZoneAvailable: true,
    },
    { zones: zoneId },
  ]
})

Proposal

Add the query builder to the @Filter decorator condition callback.

@Filter<Product>({
  name: 'zoneId',
  cond: (args, type, qb) => ({
    $or: [
      {
        orgId: qb.raw('(select za.org_id from "zone" za where za.id = ?)', [
          args.zoneId,
        ]),
        concurrentZoneAvailable: true,
      },
      {
        zones: args.zoneId,
      },
    ],
  }),
})
@JoelVenable JoelVenable added the enhancement New feature or request label Sep 18, 2021
@B4nan
Copy link
Member

B4nan commented Sep 18, 2021

Maybe we could have a static helper to mark a raw query instead. We could expose the QB but it lives in the knex package, the core one technically does not know about its existence.

@B4nan B4nan added this to the 5.0 milestone Sep 19, 2021
@B4nan
Copy link
Member

B4nan commented Sep 19, 2021

Let's do it another way - we can expose the EM, there you can just type it yourself to the SqlEntityManager and access createQueryBuilder method, and we can as well add em.raw() shortcut.

edit: the em will need to be defined as any unfortunately, otherwise TS will complain if you try to type it in the definition as SqlEntityManager

@B4nan B4nan closed this as completed in 6858986 Sep 19, 2021
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

No branches or pull requests

2 participants