Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda