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

Url query resolver for @Controller methods #65

Closed
rjlopezdev opened this issue Feb 26, 2019 · 6 comments
Closed

Url query resolver for @Controller methods #65

rjlopezdev opened this issue Feb 26, 2019 · 6 comments
Labels

Comments

@rjlopezdev
Copy link

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:

@ivanvs
Copy link

ivanvs commented Feb 27, 2019

Maybe this should be a feature request in nestjs/typeorm reposiotory. TypeOrm integration is not part of this repository.

Interesting feature, by the way!

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Feb 28, 2019
@rjlopezdev
Copy link
Author

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);
  }
}

@xmlking
Copy link

xmlking commented Apr 7, 2019

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

@LeandroBarral
Copy link

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

This library are so helpfull! it could be integrated sometime on the core project to garantee the support

@rjlopezdev
Copy link
Author

Oooops! So sorry, I didn't pay attention to this issue.
Thanks for recommendation @xmlking (I've been using that since I created the issue).

I think that nestjsx/crud is well supported & its integration is very simple.
So I think this feature should not be part of the core.

Closing... 😺

@julien-sarazin
Copy link

julien-sarazin commented Sep 27, 2019

I have been on this issue few weeks ago and started working on small components: https://github.com/implicity-healthcare/nest-criteria.

Far from being as complete as nestjsx/crud this library has been designed focusing only to handle querying/pagination from the controller layer to the repository layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants