title: TypeORM: Dynamically set database schema for EntityManager (or repositories) at runtime?
body: <p><strong>Situation:</strong></p>
  
  <p>For our SaaS API we use <a href="https://vladmihalcea.com/database-multitenancy/" rel="noreferrer">schema-based multitenancy</a>, which means every customer (~tenant) has its own separate <a href="https://www.tutorialspoint.com/postgresql/postgresql_schema.htm" rel="noreferrer">schema</a> within the same (postgres) database, without interfering with other customers. Each schema consists of the same underlying entity-model. </p>
  
  <p>Everytime a new customer is registered to the system, a new isolated schema is automatically created within the db. This means, the schema is created at runtime and not known in advance. The customer's schema is named according to the customer's domain.</p>
  
  <p>For every request that arrives at our API, we extract the user's tenancy-affiliation from the JWT and determine which db-schema to use to perform the requested db-operations for this tenant.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>After having established a connection to a (postgres) database via <a href="https://github.com/typeorm/typeorm" rel="noreferrer">TypeORM</a> (e.g. using <a href="https://github.com/typeorm/typeorm/blob/master/docs/connection-api.md#main-api" rel="noreferrer">createConnection</a>), our only chance to set the schema for a db-operation is to resort to the <code>createQueryBuilder</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>const orders = await this.entityManager
    .createQueryBuilder()
    .select()
    .from(`${tenantId}.orders`, 'order') // &lt;--- setting schema-prefix here
    .where("order.priority = 4")
    .getMany();
  </code></pre>
  
  <p>This means, we are forced to use the <code>QueryBuilder</code> as it does not seem to be possible to set the schema when working with the <a href="https://github.com/typeorm/typeorm/blob/master/docs/entity-manager-api.md" rel="noreferrer">EntityManager API</a> (or the <a href="https://github.com/typeorm/typeorm/blob/master/docs/repository-api.md" rel="noreferrer">Repository API</a>).</p>
  
  <p>However, we want/need to use these APIs, because they are much simpler to write, require less code and are also less error-prone, since they do not rely on writing queries "manually" employing a string-based syntax.</p>
  
  <p><strong>Question</strong></p>
  
  <p>In case of TypeORM, is it possible to somehow set the db-schema when working with the <code>EntityManager</code> or repositories?</p>
  
  <p>Something like this?</p>
  
  <pre class="lang-js prettyprint-override"><code>// set schema when instantiating manager
  const manager = connection.createEntityManager({ schema: tenantDomain });
  
  // should find all matching "order" entities within schema
  const orders = manager.find(Order, { priority: 4 })
  
  // should find a matching "item" entity within schema using same manager
  const item = manager.findOne(Item, { id: 321 })
  </code></pre>
  
  <p><strong>Notes:</strong></p>
  
  <ul>
  <li>The db-schema needs to be set in a request-scoped way to avoid setting the schema for other requests, which may belong to other customers. Setting the schema for the whole connection is not an option.</li>
  <li>We are aware that one could create a whole new connection and set the schema for this connection, but we want to reuse the existing connection. So simply creating a new connection to set the schema is not an option.</li>
  </ul>