Crud: Error comparison operator since 4.3.0

Created on 24 Dec 2019  路  19Comments  路  Source: nestjsx/crud

Hello,

When i'm using the new release version to get with ?filter=name||$eq||test
I got this error :
Error: Invalid comparison operator. eq,ne,gt,lt,gte,lte,starts,ends,cont,excl,in,notin,isnull,notnull,between expected
at validateComparisonOperator (project\node_modules\@nestjsx\crud\node_modules\@nestjsx\crud-request\src\request-query.validator.ts:63:11)
When I'm using the previous operator 'eq' the filter do nothing
Thank you

All 19 comments

@njournaud have you used 4.3.3?

Oh No I didn't see the new release I was in 4.3.0 thx!

After testing with 4.3.3 I don't have the error for $eq but the filter still not working.

@njournaud what error do you have now after 4.3.3?

None just the filter not filtering.
I have the alwaysPaginate true for information

@njournaud could you please give some examples?

I put a project in Database with
{ name: project1 }
When I call '/projects?filter=name||$eq||project2' it should not return my previous entity
I got :
{ "data": [ { "name": "project1" } ], "count": 1, "total": 1, "page": 1, "pageCount": 1 }

Selection_071
It's very strange. Could you please debug more and provide generated SQL maybe?

SQL : query: SELECT "Project"."code" AS "Project_code", "Project"."name" AS "Project_name" FROM "public"."project" "Project" LIMIT 25 query: SELECT COUNT(DISTINCT("Project"."code")) as "cnt" FROM "public"."project" "Project"

So no filter
console.log(req.parsed.filter): [ { field: 'name', operator: '$eq', value: 'project2' } ]

Could you please send me what does req.parsed.search show?

Hi,
console.log(JSON.stringify(req.parsed.search)) : {"$and":[null,{},{"code":{"$eq":"project2"}}]}

I have this in my package.json :

"@nestjsx/crud": "4.3.3",
"@nestjsx/crud-typeorm": "4.3.3",
"@nestjsx/crud-request": "4.3.3",

My controller and getManyBase :

@Crud({
  model: {
    type: Project,
  },
  dto: {
    create: ProjectDTO,
  },
  params: {
    id: {
      field: 'code',
      type: 'string',
      primary: true,
    },
  },
  routes: {
    exclude: [ 'createManyBase', 'updateOneBase', 'replaceOneBase' ],
    only: [ 'getOneBase', 'getManyBase', 'createOneBase', 'deleteOneBase' ],
    createOneBase: {
      returnShallow: true,
    },
  },
  query: {
    alwaysPaginate: true,
  },
})
@ApiUseTags('Projects')
@ApiBearerAuth()
@Controller()
@UseGuards(AuthGuard)
export class ProjectController implements CrudController<Project> {
  constructor(public service: ProjectService) { }

  get base(): CrudController<Project> {
    return this;
  }
@Override('getManyBase')
  @ApiResponseModelProperty({ type: GetManyResponseForProjectDTO })
  @ApiOkResponse({ type: GetManyResponseForProjectDTO })
  async getMany(@ParsedRequest() req: CrudRequest) {
    console.log(JSON.stringify(req.parsed.search));
    const projects = await this.base.getManyBase(req);

    if (!Array.isArray(projects)) {
      return {
        data: ProjectFactory.entitiesToDtos(projects.data),
        count: projects.count,
        total: projects.total,
        page: projects.page,
        pageCount: projects.pageCount,
      };
    }

    return ProjectFactory.entitiesToDtos(projects);
  }
}

and what does console.log(JSON.stringify(req.parsed.search)); show on getManyBase?
and what's your URL for this method looks like?

console.log(JSON.stringify(req.parsed.search));
{"$and":[null,{},{"name":{"$eq":"test"}}]}

the URL looks like this : http://localhost:3002/projects?filter=name||$eq||test

LGTM so far.
But there is no WHERE statement in the generated SQL, right?

Right
Complete Stack trace with post and getMany with filter:
```
express:router dispatching POST /projects +30m
express:router query : /projects +18ms
express:router expressInit : /projects +6ms
express:router jsonParser : /projects +1ms
body-parser:json content-type "application/json" +0ms
body-parser:json content-encoding "identity" +112ms
body-parser:json read body +12ms
body-parser:json parse body +136ms
body-parser:json parse json +3ms
express:router urlencodedParser : /projects +267ms
body-parser:urlencoded body already parsed +2ms
[Nest] 11852 - 2019-12-26 11:03:41 [{"targetName":"ProjectDTO","property":"code","object":{"name":"project2","code":"project2"},"value":"project2","constraints":[null,"code"]}] validateTEST +577ms
query: SELECT COUNT(DISTINCT("Project"."code")) as "cnt" FROM "public"."project" "Project" WHERE "Project"."code" = $1 -- PARAMETERS: ["project2"]
[Nest] 11852 - 2019-12-26 11:03:42 [{"targetName":"ProjectDTO","property":"code","object":{"name":"project2","code":"project2"},"value":"project2","constraints":[null,"code"]}] validateTEST +627ms
query: SELECT COUNT(DISTINCT("Project"."code")) as "cnt" FROM "public"."project" "Project" WHERE "Project"."code" = $1 -- PARAMETERS: ["project2"]
query: SELECT "Project"."code" AS "Project_code", "Project"."name" AS "Project_name" FROM "public"."project" "Project" WHERE "Project"."code" IN
($1) -- PARAMETERS: ["project2"]
query: START TRANSACTION
query: INSERT INTO "public"."project"("code", "name") VALUES ($1, $2) -- PARAMETERS: ["project2","project2"]
query: COMMIT
express:router dispatching GET /projects?filter=code%7C%7C$eq%7C%7Ctest +9s
express:router query : /projects?filter=code%7C%7C$eq%7C%7Ctest +6ms
express:router expressInit : /projects?filter=code%7C%7C$eq%7C%7Ctest +3ms
express:router trim prefix (/) from url /projects?filter=code%7C%7C$eq%7C%7Ctest +6ms
express:router / : /projects?filter=code%7C%7C$eq%7C%7Ctest +0ms
express:router trim prefix (/) from url /projects?filter=code%7C%7C$eq%7C%7Ctest +6ms
express:router serveStatic / : /projects?filter=code%7C%7C$eq%7C%7Ctest +20ms
express:router jsonParser : /projects?filter=code%7C%7C$eq%7C%7Ctest +29ms
body-parser:json skip empty body +9s
express:router urlencodedParser : /projects?filter=code%7C%7C$eq%7C%7Ctest +1ms
body-parser:urlencoded skip empty body +0ms
JSON.stringify(req.parsed.search : {"$and":[null,{},{"code":{"$eq":"test"}}]}
JSON.stringify(req.parsed.filter : [{"field":"code","operator":"$eq","value":"test"}]
req.parsed.paramsFilter : []
JSON.stringify(req) : {"parsed":{"fields":[],"paramsFilter":[],"search":{"$and":[null,{},{"code":{"$eq":"test"}}]},"filter":[{"field":"code","operator":"$eq","value":"test"}],"or":[],"join":[],"sort":[]},"options":{"query":{"alwaysPaginate":true,"limit":25,"cache":2000},"routes":{"getManyBase":{"interceptors":[],"decorators":[]},"getOneBase":{"interceptors":[],"decorators":[]},"createOneBase":{"interceptors":[],"decorators":[],"returnShallow":true},"createManyBase":{"interceptors":[],"decorators":[]},"updateOneBase":{"interceptors":[],"decorators":[],"allowParamsOverride":false,"returnShallow":false},"replaceOneBase":{"interceptors":[],"decorators":[],"allowParamsOverride":false,"returnShallow":false},"deleteOneBase":{"interceptors":[],"decorators":[],"returnDeleted":false},"exclude":["createManyBase","updateOneBase","replaceOneBase"],"only":["getOneBase","getManyBase","createOneBase","deleteOneBase"]},"params":{"id":{"field":"code","type":"string","primary":true}}}}
query: SELECT "Project"."code" AS "Project_code", "Project"."name" AS "Project_name" FROM "public"."project" "Project" LIMIT 25
query: SELECT COUNT(DISTINCT("Project"."code")) as "cnt" FROM "public"."project" "Project"

HTTP Responses : 
first POST  http://localhost:3002/projects : 201

{
"code": "project2",
"name": "project2"
}

second : 
GET http://localhost:3002/projects?filter=code||$eq||test : 200 

{
"data": [
{
"code": "project2",
"name": "project2"
}
],
"count": 1,
"total": 1,
"page": 1,
"pageCount": 1
}
```

I've tested again my integration project, tried to simulate your situation, but no luck.
So the last option here - could you please create a sample repo with the same controller so that I could test it?

So I create a project from scratch slimpiest and it's working so It's specific to my architecture I'm looking for. Thank you for your time ;)

Was this page helpful?
0 / 5 - 0 ratings