Menu

Post image 1
Post image 2
1 / 2
0

Multi-tenant Mongoose Module for NestJS

DEV Community·phen0menon·about 1 month ago
#kSs64OnZ
#nestjs#mongodb#mongoose#tenancy#tenant#model
Reading 0:00
15s threshold

Building a multi-tenant service? If you're using NestJS and MongoDB (Mongoose), you've probably hit a wall when trying to isolate customer data. The common approach - database-per-tenant - often leads to messy code where you have to manually pass tenant IDs around or dynamically create connections on every single request. It gets even worse when you add background jobs into the mix. I got tired of the boilerplate and edge cases, so I built @phen0menon/nestjs-mongoose-tenancy . The main idea The goal was to make it feel exactly like the official @nestjs/mongoose package, but tenant-aware. Instead of doing manual lookups in your business logic, the library handles it at the Dependency Injection layer. You just define a resolver in your app.module.ts (like grabbing an x-tenant-id header), and the DI container gives you the correct model. @ Injectable () export class ProductService { constructor ( // It just gives you the model connected to the right DB 👇 @ InjectTenantModel ( Product .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More