feat: allow type FindOptionsOrderValue for order by object property (#9895) (#9896)

When using an Entity column transformer for an object type, the underlying database column may be
sortable, despite the static TypeScript type being an object. The `FindOptionsOrder` typing
should allow sorting on that object property and not require further nesting.

Fixes issue #9895.
This commit is contained in:
Dylan Seago 2023-04-06 01:51:17 -04:00 committed by GitHub
parent 07221a3646
commit 0814970a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 1 deletions

View File

@ -24,7 +24,7 @@ export type FindOptionsOrderProperty<Property> = Property extends Promise<
: Property extends ObjectID
? FindOptionsOrderValue
: Property extends object
? FindOptionsOrder<Property>
? FindOptionsOrder<Property> | FindOptionsOrderValue
: FindOptionsOrderValue
/**

View File

@ -0,0 +1,31 @@
import { Entity } from "../../../../src/decorator/entity/Entity"
import { Column } from "../../../../src/decorator/columns/Column"
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"
class ExampleBigNumber {
constructor(private value: string) {}
toFixed() {
return this.value
}
}
@Entity()
export class ExampleEntity {
@PrimaryGeneratedColumn()
id: number
@Column({
type: "numeric",
nullable: false,
transformer: {
from: (value: any): ExampleBigNumber => {
return new ExampleBigNumber(value)
},
to: (value: any): string => {
return value.toFixed()
},
},
})
total: ExampleBigNumber
}

View File

@ -0,0 +1,33 @@
import { DataSource } from "../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { ExampleEntity } from "./entity/ExampleEntity"
describe("github issues > #9895", () => {
let dataSources: DataSource[]
before(async () => {
dataSources = await createTestingConnections({
entities: [ExampleEntity],
enabledDrivers: ["postgres"],
})
})
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))
it("should allow find order on object property", async () => {
await Promise.all(
dataSources.map(async (dataSource) => {
await dataSource.manager.find(ExampleEntity, {
order: {
total: "DESC",
},
})
}),
)
})
})