Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is need provider with manual get tenant service? #45

Open
xiaokhkh opened this issue Jan 5, 2023 · 4 comments
Open

Is need provider with manual get tenant service? #45

xiaokhkh opened this issue Jan 5, 2023 · 4 comments

Comments

@xiaokhkh
Copy link

xiaokhkh commented Jan 5, 2023

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
Copy link
Contributor

@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
Copy link
Author

xiaokhkh commented Jan 5, 2023

@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
Copy link
Contributor

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

@mbraz
Copy link

mbraz commented Mar 23, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants