bhavesh
bhavesh

Reputation: 755

how to use tenant and microservice in node js

I am planning to user tenanting in MongoDB with microservice in node js my preferrences are

my questions are

  1. how can I use multiple DB connections?
  2. how to use common middlewares like auth?
  3. is MongoDB is good for multi-tenancy?

Upvotes: 0

Views: 257

Answers (1)

Dan Moldovan
Dan Moldovan

Reputation: 3591

No entirely sure what answer you're looking for here.

A common way is for microservices to be isolated and deployed independently to the cloud. Communication is event-based using tools like Apache Kafka, RabbitMQ or cloud-specific tools like AWS SQS.

In terms of using mongo or passport, there's nothing really stopping each individual microservices from implementing any of these.

As far as multi-tenancy, I would recommend using a strategy pattern or polymorphism, where your server maintains connections to all your tenant dbs and once a request hits, one of those connections gets chosen to do the actual work

Edit to answer some of the questions: any service is good for multi-tenancy. Implementing a multi-tenant system is solely based on your implementation and not the services involved. Look at polymorphism to achieve a true multi-tenant system in nodejs

As an example, say you have two tenants in a system called dev and prod. You can have a base class for your db connection like

class DbRoot {
  private connection;

  constructor(tenant) {
    this.setConnection(tenant);
  }

  private setConnection(tenant) {
    switch(tenant) {
      case Tenants.DEV:
        this.connection = 'https://dev.db.com';
        break;
      case Tenants.PROD:
        this.connection = 'https://prod.db.com';
        break;
      default:
        throw new Error('tenant not recognized');
     }
  }

Afterwards, you can use this base class to the implement a class that leverages your actual db implementation

  class DbClass extends DbRoot {
    constructor(tenant) {
      super(tenant);
    }

    public getUsers() {}
    public createUser() {}
    ...
  }

Upvotes: 1

Related Questions