1
votes

I have an NestJS application wich consist of many modules: databaseModule, userModule, devicesModule, etc. They all packaged to one module ApplicationModule, which handle server actions.

But now I need to add some lambda function to my project and I need to exec some methods from databaseModule, but I don't know how to do it.

Pseudo code that I imagine:

export const handler: Handler = (event: any, context: Context, callback) => {
  const dbModule = DataBaseModule.build();
  dbModule.get(UserService).createProject('my_project');
  callback(null, event);
};

I think that nestjs should have similar functionality but I can't find it on official page.

P.S. I can't use just UserService because it depends on other services and providers in DatabaseModule. That is why I want this module to be fully configured and I can use its services

2

2 Answers

0
votes

I found an answer https://docs.nestjs.com/application-context

We can use our submodules in the next way:

const app = await NestFactory.create(ApplicationModule);
const tasksService = app.get(TasksService);
0
votes

You can use the Lifecycle events from NestJS (https://docs.nestjs.com/fundamentals/lifecycle-events).

That way, you can implement OnModuleInit() on the Service and set the desired function to run when the Module is loaded (it can be sync or async).

There are some other events that can also be useful, like onApplicationBootstrap()