Skip to content

QueryBuilder.orderBy() support for FIELD() #7280

@martinsotirov

Description

@martinsotirov

Feature Description

The Problem

My use case is that I want to get results from Typeorm based on an array of IDs after I've filtered and ordered my items in an Elastic Search query. My main WHERE clause is queryBuilder.andWhere('vendor.id IN (:ids)', { ids }).

I tried ordering by the same array of IDs like so:

queryBuilder.orderBy(`FIELD(\`vendor\`.\`id\`, ${ids.join(',')})`)

...but the query builder insists on putting ASC or DESC at the end of the ORDER BY so the query fails. If I get the raw query with getSql(), remove the ASC at the end, and try to run it manually it works correctly.

The Solution

I guess some kind of mechanism to specify an order other than ASC or DESC or some kind of way to omit this part completely so that we can use custom MySQL functions like ORDER BY FIELD(id, 53, 644, 7491, 666).

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions