Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
TypeORM select alias of column name
this.sampleRepo.find(
  {
    order: {
      id: "DESC"
    },
    select: ['id','group']
  }
);

this returns the id and group as expected, but how to return id as user_id ? and also how to select distinct values from group?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Just add an alias in you select string, e.g.:

select: ['id AS user_id','group AS user_group']

If the previous option didn't work, it should work in queryBuilder:

this.sampleRepo
      .createQueryBuilder('user')
      .orderBy('user.id', 'DESC')
      .select(['id AS user_id','group AS user_group'])
      .getRawMany() // or .getMany()


I've made smth as you need with one of my examples (last one here) but wrote this (upd. fixed with getRawMany and distinct):

getMany(): Promise<UserEntity[]> {
    return this.userRepo.createQueryBuilder('user')
      .where({ username: 'breckhouse0' })
      .select(['DISTINCT (user.username) AS user_name', 'user.id AS user_id'])
      .getRawMany();
  }

and this works as you expect - results

over 4 years ago · Santiago Trujillo Report

0

I made a gambiarra:

var qb = this.siteSurveyRepository
        .createQueryBuilder('sites')
        .leftJoinAndSelect('sites.responsavel', 'responsavel')
        .leftJoinAndSelect('sites.endereco', 'endereco')
        .leftJoinAndSelect('sites.localizacao', 'localizacao')
        .leftJoinAndSelect('sites.fotos', 'fotos')
        .leftJoin('sites.contato', 'contato')
        .leftJoin('contato.empresa', 'empresa')
        .select([
          'sites.id',
          'sites.nome',
          'sites.esfera',
          'sites.poder',
          'sites.dataCriacao',
          'sites.dataCriacaoLocal',
          'responsavel',
          'endereco',
          'localizacao',
          'fotos.id',
          'fotos.idSurvey',
          'fotos.label',
          'fotos.src',
          'contato.id',
          'contato.nome',
          'contato.email',
          'contato.telefone',
          'empresa'
        ])
const all = qb.orderBy('sites.dataCriacao', 'ASC').getMany();
const result = JSON.stringify(all).replace('dataCriacao', 'data_criacao').replace('dataCriacaoLocal', 'data_criacao_local');
const data = JSON.parse(result);
return response.status(201).json(data);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!