Implementing schema-based multi-tenancy with NestJS-Query modules #303
Unanswered
mariusorani
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I can only help give a partial answer as I have worked with multi-tenancy but all the tenants had the same schema: First, you can create a class that implements @Injectable()
export class TypeOrmConfigFactory implements TypeOrmOptionsFactory {
private readonly tenantID: string;
// Here you can inject the request to extract the tenant id (from the JWT for example)
constructor(@Inject(REQUEST) req: Request) {
tenantID = ...
}
createTypeOrmOptions() {
// Build your ORM config here with the tenant ID.
//! Do not give a name here, or you will have to do so in all the calls to TypeOrm.forFeature.
return {
database: tenantID,
// ...
}
}
} Then in your AppModule (or wherever you're configuring TypeOrm), you can do this: @Module({
imports: [
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService,
}),
// ...
],
})
export class AppModule implements NestModule { /* ... */ } Now, nestjs-query will automatically use the right connection. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
I'm developing a multi-tenant application using NestJS with NestJS-Query modules. Specifically, I need to implement schema-based multi-tenancy, where each tenant has its own database schema. I'm having difficulty integrating this approach with NestJS-Query.
What I've Researched
I've looked into:
However, I'm unsure how to properly implement these ideas with NestJS-Query, which abstracts much of the database interaction.
My Questions
Additional Context
Any guidance, code snippets, or links to relevant resources would be immensely helpful. I've searched extensively but haven't found clear examples of NestJS-Query being used in a schema-based multi-tenant application.
Beta Was this translation helpful? Give feedback.
All reactions