Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
TypeORM createQueryBuilder: Is possible to use table name as string instead of entity

In my current scenario, a third party will be creating tables dynamically on my DB, and storing the table name as a varchar and column names as jsonb in other table which is defined as an Entity in my NestJS backend.

This is so I can keep track (and query) these tables, since I have no control over its creation.

For this purpose I'd like to use TypeORM's createQueryBuilder instead of using raw queries as its easier for me to play with abstraction.

As far as I know TypeORMs createQueryBuilder needs a defined Entity in the from clause.

Something like this:

return await getManager()
  .createQueryBuilder()
  .select('*')
  .from(MyDefinedModel, 'modelAlias')
  .getRawMany();
}

So I'd like to do something like:

const tableName = await getDynamicallyGenetaredTableNames().getFirstOne()
// now tableName points to a string that is a table name, i.e 'table-data-192239'

 return await getManager()
  .createQueryBuilder()
  .select('*')
  .from(tableName, 'tableAlias')
  .getRawMany();

So passing the table name I point to the right table, but TypeORM (and TS) complains because that tableName is a string, and not an Entity (Class) type

I really don't want to type-cast and start doing nasty things if there is something cleaner to achieve this. I didn't find any solution in the official docs

Any brilliant ideas out there? Thanks, y'all

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

We can pass table name instead of Entity for getRepository.

let tableName = 'user';
let query = getRepository(tableName).createQueryBuilder(tableName);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda