TypeORM: How to Use Column Alias when Querying Data

Updated: September 7, 2022 By: Snowball Post a comment

This succinct tutorial shows you how to use column aliases to assign temporary names to columns when selecting data with TypeORM. The point here is the AS keyword. Let’s study a couple of examples for more clarity.

User entity:

// KindaCode.com
// User entity
import { Entity, PrimaryGeneratedColumn, Column} from 'typeorm';

export class User {
  id: number;

  name: string;

  @Column({nullable: true})
  age: number;

Using column aliases within a query builder to retrieve all users:

const userRepository = myDataSource.getRepository(User);
const users = await userRepository
        'user.id AS user_id',
        'user.name AS user_name',
        'user.age as user_age',


Your output will look similar to this:

  { user_id: 1, user_name: 'User 1', user_age: 20 },
  { user_id: 2, user_name: 'User 2', user_age: 30 },
  { user_id: 3, user_name: 'User 3', user_age: 40 }

TypeORM is evolving quickly and there is much to be learned. Make your skills keep up with the times by taking a look at the following articles:

You can also check out our database topic page for the latest tutorials and examples.

Notify of
Inline Feedbacks
View all comments

Related Articles