typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Url query resolver for @Controller methods

Open rjlopezdev opened this issue 7 years ago • 6 comments

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:

rjlopezdev avatar Feb 26 '19 18:02 rjlopezdev

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

Interesting feature, by the way!

ivanvs avatar Feb 27 '19 13:02 ivanvs

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

rjlopezdev avatar Mar 30 '19 01:03 rjlopezdev

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

xmlking avatar Apr 07 '19 17:04 xmlking

@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

LeandroBarral avatar Apr 09 '19 15:04 LeandroBarral

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... 😺

rjlopezdev avatar Sep 24 '19 21:09 rjlopezdev

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.

julien-sarazin avatar Sep 27 '19 13:09 julien-sarazin