TriPSs/nestjs-query

Alias And Field Names In Generated SQL For Aggregate GroupBy Date Are Not Properly Escaped With Quotes

Opened this issue · 1 comments

Describe the bug
When using groupBy date the alias and field names in the generated SQL are not being properly escaped, resulting in an invalid query and failure.

Have you read the Contributing Guidelines?

Yes.

To Reproduce
Steps to reproduce the behavior:

  1. With a backing Postgres data structure wherein the schema concerns a TypeORM ViewEntity class named UserRecord targeting the database schema tenant and view user.
  2. Issue an aggregate query, such as:
{
  userAggregate {
    groupBy {
      createdAt(by: MONTH )
    }
    count {
      userId
    }
  }
}

After which an error is thrown by the database and introspection of the generated SQL for the query shows that the alias and field name in the TO_CHAR() function for the group by aggregate is not properly escaped in quotes, whereas the count aggregate is.

SELECT
  DATE(TO_CHAR(UserRecord.createdAt, 'YYYY-mm-01')) AS "GROUP_BY_createdAt",
  COUNT("UserRecord"."userId") AS "COUNT_userId"
FROM
  "tenant"."user" "UserRecord"
GROUP BY
  "GROUP_BY_createdAt"
ORDER BY
  "GROUP_BY_createdAt" ASC

Expected behavior
The generated SQL does not error and results are subsequently returned.

Desktop (please complete the following information):

  • Node Version: v20.6.0
  • Nestjs-query Version: v4.3.3

Interesting, we do not do anything special or more for the COUNT. What driver are you using as the tests for MySQL and Postgres both pass.