needle-innovision / nestjs-tenancy

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

Is need provider with manual get tenant service? #45

Open xiaokhkh opened 1 year ago

xiaokhkh commented 1 year ago

Hi @sandeepsuvit , Thank you very much for developing this interesting module for multi tenancy and its helping me a lot.

All service need request context, because scope of "TENANT-CONTEXT" Request context be destroy when i called method of tenant service because i use Subscription Publishing Mode. [error] req is undefine

so i write this

export type ManualGetModelFn<T> = (tenanId: string) => Promise<Model<T>>;
export function ManualTennecyModel(entity: string): ParameterDecorator {
    return InjectManualGetFn(entity);
}
//manual service
export class xxxManualService {
    constructor(
        @ManualTennecyModel(xxxEntity.name)
        private readonly manualGetfn: ManualGetModelFn<xxxEntity>
    ) {}
    async select(tenantId: string) {
        await this.bookUseDataRepos.setModel(tenantId);
        return this;
    }
}
//manual service usage
export class usageService {
    constructor(private readonly xxxManualService: xxxManualService) {}
    getTenantService(tenantId: string): Promise<xxxManualService> {
        return this.xxxManualService.select(tenantId);
    }
    call() {
        this.getTenantService(organId)
            .then((service) => {
                //...use service do something
            })
            .catch(console.error);
    }
}

Manual service will not spread [request] scope.

If need i will submit the pr.

A question Can we improve the performance of tenant mode by saving service instances or manually obtaining models?

Thanks for all of your efforts. Looking forward to your reply :)

sandeepsuvit commented 1 year ago

@xiaokhkh thanks for writing in. Yes you are correct the tenant can be resolved only by a request context until its available (since the information of the tenant is embedded inside the request context). But i did't get you last point on Can we improve the performance of tenant mode by saving service instances or manually obtaining models? - Are you saying you have a solution for this and you want to submit a pr?

xiaokhkh commented 1 year ago

@sandeepsuvit pr is about manual get model above the above requirements.

The last question is the idea when doing the above requirements. but it has not been fully implemented. I might achieve it(improve the performance) in the direction or other idea?

sandeepsuvit commented 1 year ago

@xiaokhkh your solution looks promising. I can evaluate it if you can provide a working example.

mbraz commented 1 year ago

Hi guys, an example of use when I add a service to a Bull queue:

myapi | 2023-03-22T04:09:56.482Z ioredis:redis write command[221.20.0.5:6379]: 0 -> hmset('xxx:mail-queue:9,attemptsMade,1,stacktrace,["TypeError: req.get is not a function\n at Function.getTenantFromRequest (/home/node/app/node_modules/@needle-innovision/nestjs-tenancy/dist/tenancy-core ...

The request context is not available... I believe the solution would be to pass the tenantId manually from the Job parameters.