question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Url query resolver for @Controller methods

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

With the current implementation, there is no easy way to filter data through url query string.

Expected behavior

Empowering Controllers the ability to resolve url querys & building an apropiate SelectQuery using TypeORM QueryBuilder.

@Controller('foo')
export class FooController {

  @Get()
  @Filterable()
  filter(@ORMQuery() query //A SelectQuery built) {
    // Call Provider here. In order to simplify the example I'll call Repository directly
    return this.fooRepository.find(query);
  }
}

Given a query string like:

foo/?name=foo&surname__contains=fake&fk__related_prop=2&page=2

It will return the following SelectQuery:

const filterQuery = await connection
    .getRepository(Foo)
    .createQueryBuilder('foo')
    .where('foo.name = foo')
    .andWhere('surname like fake')
    .innerJoinAndSelect(Foo, 'foo', 'foo.related_prop = 2')
    .take(25)
    .skip(25 * 2 - 1)

Update 02/27/2019

I found this library whose owner is @bashleigh that resolves pagination & limit problem (and filterParameters, right). Could it be interesting to implement proposed feature in this library? Parsing query string and transform it like TypeORM findOptions

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

A very common use case is to be able to filter content for an endpoint using the url query format.

Environment


Nest version: @next

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

3reactions
rjlopezdevcommented, Mar 30, 2019

Update:

I publish this package that allows transform Express req.query into TypeORM queries. For now, doesn’t allow join/nested props queries, but it will coming soon 😃

If you consider interesting adding it as a nestjs/typeorm feature, I could integrate on it maybe like:

  • Writing a ORMQuery decorator, used like:
import { ORMQuery } from 'nestjs/typeorm';

@Controller('foo')
export class FooController {

  @Get()
  filter(@ORMQuery() query) {
    return this.fooService.find(query);
  }
}

1reaction
xmlkingcommented, Apr 7, 2019

@rjlopezdev please also check some similar work here https://github.com/nestjsx/crud

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >
Routing to controller actions in ASP.NET Core - Microsoft Learn
Learn how ASP.NET Core MVC uses Routing Middleware to match URLs of incoming requests and map them to actions.
Read more >
How to configure query parameters in Spring Controllers
In this post, we will discuss what query parameters are and how to configure them in a Spring controller using the @RequestParam annotation....
Read more >
Custom Web Controller Arguments with Spring MVC and ...
Spring MVC provides a very convenient programming model for creating web controllers. We declare a method signature and the method arguments ...
Read more >
MVC: How to correctly resolve Url property in Controller?
But in some cases I need to resolve the URL in a Controller, so I use UrlResolver.Current.GetUrl. Howerver, the resulting friendly url still...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found