Advanced
Multitenancy

Multitenancy

Ocoda Event Sourcing provides built-in support for multitenancy. This allows you to separate the event- and snapshot-streams of different tenants. This can be achieved by using pools. A pool is a collection of streams that belong to the same tenant. By default the event- and snapshot-store create a shared collection of streams for all tenants. To enable multitenancy you need to:

Disable the creation of a default pool in the store configuration

  @Module({
      imports: [
          EventSourcingModule.forRoot({
              events: Events,
              eventStore: {
                  driver: InMemoryEventStore,
                  useDefaultPool: false,
              },
              snapshotStore: {
                  driver: InMemorySnapshotStore,
                  useDefaultPool: false,
              },
          }),
      ]
  })
  export class AppModule {}

Create a pool for each tenant

  await eventStore.ensureCollection('tenant-1');
  await snapshotStore.ensureCollection('tenant-1');

Provide a pool when interacting with the event-store or snapshot-repository

  await this.eventStore.appendEvents(stream, account.version, events, 'tenant-1');
  await this.accountSnapshotRepository.save(accountId, account, 'tenant-1');
  await this.accountSnapshotRepository.load(accountId, 'tenant-1');