25 lines
743 B
TypeScript
25 lines
743 B
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { INestApplicationContext } from '@nestjs/common';
|
|
import { AppModule } from './app.module';
|
|
|
|
let appContext: INestApplicationContext | null = null;
|
|
|
|
export async function initializeDIContainer(): Promise<void> {
|
|
if (appContext) {
|
|
return; // Already initialized
|
|
}
|
|
|
|
appContext = await NestFactory.createApplicationContext(AppModule);
|
|
}
|
|
|
|
export function getDIContainer(): INestApplicationContext {
|
|
if (!appContext) {
|
|
throw new Error('DI container not initialized. Call initializeDIContainer() first.');
|
|
}
|
|
return appContext;
|
|
}
|
|
|
|
export async function getService<T>(token: string | symbol): Promise<T> {
|
|
const container = getDIContainer();
|
|
return container.get<T>(token);
|
|
} |