needle-innovision / nestjs-tenancy

Multi-tenancy approach for nestjs - currently supported only for mongodb with mongoose
MIT License
187 stars 58 forks source link

Provider or utility to get tenantId #23

Closed AdamJessop closed 2 years ago

AdamJessop commented 2 years ago

it would be great if there was a provider available to get the tenantId from the current request.

I have a use case in that my tenants are all configured in a central database but I want to load their configurations based on their ID.

either by having an injection token of TENANT_ID or a utility getTenantId() for example.

Is this somehow possible already?

sandeepsuvit commented 2 years ago

Hi @AdamJessop thanks for writing in. I didn't quite catch what is it that you are looking for. But this library actually extract the tenantId from the request object. And that parameter on which you are sending your tenantId can be customised. Please refer to the readme section for the configuration.

AdamJessop commented 2 years ago

Hi @sandeepsuvit

I actually found a token that does exactly what I am looking for: TENANT_CONTEXT

I basically wanted to create a provider for my config, which is from a core DB. EG:

{
            provide: 'TENANT_CONFIG',
            useFactory: async (tenantId: string, tenants: Model<DBTenant>) => {
                return tenants.findOne({ id: tenantId });
            },
            inject: [TENANT_CONTEXT, getModelToken(TENANT_SCHEMA)],
            scope: Scope.REQUEST,
        },