Skip to content

Cannot query using "find" with "order" when custom non-entity object column type is used #9895

@dylanseago

Description

@dylanseago

Issue description

Cannot query using "find" with "order" when custom non-entity object column type is used

Expected Behavior

When using a custom column type that is an object, I expect typeorm to allow ordering on that property because the underlying database column type may still be sortable despite the TypeScript type being an object.

Actual Behavior

The typeorm type definition FindOptionsOrder appears to be assuming that the column having an object type is another entity and requires "order" nested structure to match the fields of that object. However that's not correct when custom column transformers are in use. See the example in "Steps to reproduce" where a custom numeric "BigNumber" column is created by transforming between the string and BigNumber object. The underlying column type is numeric and thus should be sortable using FindOptionsOrder even though the TypeScript type is a BigNumber object.

Steps to reproduce

import {
  BaseEntity,
  Column,
  Entity,
} from 'typeorm'
import { BigNumber } from 'bignumber.js'

@Entity()
export class ExampleModel extends BaseEntity {
  @Column({
    type: 'numeric',
    transformer: {
      from: (value: any): BigNumber | null | undefined => {
        if (typeof value === 'undefined' || value === null) {
          return value
        }
        return new BigNumber(value)
      },
      to: (value: any): string | null | undefined => {
        if (typeof value === 'undefined' || value === null) {
          return value
        }
        return (value as BigNumber).toFixed()
      },
    },
  })
  total: BigNumber | null
}

// Type 'string' is not assignable to type 'FindOptionsOrder<BigNumber>'.ts(2322)
ExampleModel.find({ order: { total: 'DESC' }})

My Environment

Dependency Version
Operating System macOS 13.2.1
Node.js version 14.21.3
Typescript version 4.8.4
TypeORM version 0.3.12

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • 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.

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