Node.js ORM written in TypeScript for type lovers.

View the Project on GitHub twinlogix/typetta

Multi-Tenancy

The management of multi-tenancy is an extremely common problem in modern cloud systems and consists of serving multiple users (tenants) with a single software architecture, therefore, as far as Typetta is concerned, a single data access layer.

There are three approaches to managing multi-tenancy:

Partitioning

Using the middlewares mechanism, Typetta offers fully automatic management of the multi-tenancy scenario via partitioning. Here is an example of how to configure an EntityManager:

const entityManager = new EntityManager({
  metadata: {
    tenantId: 'user-tenant-id'
  },
  middlewares: [
    tenantSecurityPolicy({
      tenantKey: 'tenantId',
    }),
  ]
}

The tenantKey field, which in the specific example is set as tenantId, identifies the name of the discriminator that each entity of the data model will have and that represents its belonging to a tenant. The EntityManager must also be initialised with the metadata of the calling user and, in particular, must contain the discriminator defined with the same tenantId key. Note that, alternatively, you can set the metadata for each operation instead of creating the EntityManager.

The middleware adds the following behaviours to the data access layer:

{ tenantId: 'user-tentant-id' }

This means that, by defining the tenantId attribute in all the entities of the model, the developer no longer has to worry about setting its value correctly and checking it with each operation.