When using TypeORM's QueryBuilder, you can use aliases for selected columns. These aliases can be useful for computed columns or for renaming columns in the result set. However, the aliases themselves are not included in the final result.
Here's an example of using aliases with getMany()
:
const orders = await this.connection
.createQueryBuilder(Orders, "order")
.where("customer_id = :userId", { userId })
.select(["order.id"])
.addSelect(`order.paid`, 'payment')
.addSelect(`order.hashed_id`, 'hash')
.getMany();
In this example, payment and hash are aliases for the order.paid
and order.hashed_id
columns, respectively. These aliases are used in the query but are not included in the result set.
If you need to use aliases and include them in the result set, you can use getRawMany()
:
const orders = await this.connection
.createQueryBuilder(Orders, "order")
.where("customer_id = :userId", { userId })
.select([
"order.id",
"order.hashed_id AS hash",
"order.paid AS payment"
])
.getRawMany();
In this example, the aliases hash and payment are used to rename the order.hashed_id
and order.paid
columns, respectively. These aliases will be included in the result set.
Remember, when using aliases with getRawMany()
, the result set will be an array of objects with the aliased column names as keys.