Understanding Multi-Tenancy in NJS

Aug 2, 2024

Multi-Tenancy in NJS

Introduction to Multi-Tenancy

  • Single-Tenant Application:

    • Each customer has a dedicated instance of the application.
    • High cost and maintenance (e.g., 100 customers = 100 instances).
  • Multi-Tenant Application:

    • One instance serves multiple tenants (organizations or companies).
    • Shared resources lead to cost savings in maintenance.

Implementation of Multi-Tenant Applications

  • SaaS (Software as a Service):

    • Multi-tenancy structure is primarily used for SaaS applications.
  • Database Structure:

    • Not necessary to have a dedicated database for every tenant.
    • Example approaches:
      • Dedicated Database per Tenant:
        • Each tenant has its own database (e.g., tenant_ABC, tenant_XYZ).
      • Single Database for All Tenants:
        • One database with records having a tenant ID to filter data per tenant.

Challenges in Multi-Tenant Applications

  • Connecting to the correct database based on the tenant making the request.
  • Implementing multi-tenancy in NJS with MongoDB:
    • Use a Master Database as a control center holding tenant metadata.
    • Example collections in master database:
      • Tenants collection (tenant ID, company name, etc.).
      • Subscriptions and shared settings.

Coding Example: Setting Up Multi-Tenant Structure

  • Boilerplate Code Explanation:

    • Configuring the application with necessary modules.
    • Using the Mongoose module for database connections.
  • Folder Structure:

    • Organized into config, middlewares, products, tenants, and providers.

Products and Tenants Modules

  • Product Module:

    • Contains product schema (fields: name, description, price).
  • Tenants Module:

    • Contains tenant schema (fields: company name, tenant ID).
    • Each tenant database has its own product collection.

Middleware and Tenant ID Handling

  • Middleware checks for tenant ID in request headers:
    • If tenant ID is missing, throw a bad request error.
    • If tenant ID is valid, attach it to the request for later access.

Database Connection Logic

  • Use a method to retrieve the correct database connection based on the tenant ID:
    • Example: getTenantConnection(tenantID) switches the connection to the specific tenant database.

Custom Providers and Dependency Injection

  • Creating a custom provider to manage tenant connections efficiently:
    • Registering providers to return appropriate models based on tenant IDs.
    • Use request-scoped providers for tenant connections.

Summary

  • Single vs Multi-Tenant:
    • Single-tenancy requires multiple instances; multi-tenancy is cost-effective with shared resources.
  • Connection Handling:
    • Use request headers for tenant ID; implement middleware for validation.
    • Create custom providers for managing connections and models based on tenant ID.

Conclusion

  • The video covered multi-tenancy implementation in NJS with MongoDB, focusing on efficient database connections and model retrieval based on tenant requests.