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

264
Views
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 answers
Answer question

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 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!