Browse Source

fix: move app init to a service

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5680/head
Pranav C 2 years ago
parent
commit
bb81e54441
  1. 54
      packages/nocodb/src/app.module.ts
  2. 15
      packages/nocodb/src/modules/global/global.module.ts
  3. 18
      packages/nocodb/src/services/app-init.service.spec.ts
  4. 52
      packages/nocodb/src/services/app-init.service.ts

54
packages/nocodb/src/app.module.ts

@ -1,35 +1,25 @@
import { Inject, Module, RequestMethod } from '@nestjs/common';
import { Module, RequestMethod } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { BullModule } from '@nestjs/bull';
import { EventEmitterModule as NestJsEventEmitter } from '@nestjs/event-emitter';
import { Connection } from './connection/connection';
import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter';
import NcPluginMgrv2 from './helpers/NcPluginMgrv2';
import { GlobalMiddleware } from './middlewares/global/global.middleware';
import { GuiMiddleware } from './middlewares/gui/gui.middleware';
import { PublicMiddleware } from './middlewares/public/public.middleware';
import { DatasModule } from './modules/datas/datas.module';
import { IEventEmitter } from './modules/event-emitter/event-emitter.interface';
import { EventEmitterModule } from './modules/event-emitter/event-emitter.module';
import { AuthService } from './services/auth.service';
import { UsersModule } from './modules/users/users.module';
import { MetaService } from './meta/meta.service';
import Noco from './Noco';
import { TestModule } from './modules/test/test.module';
import { GlobalModule } from './modules/global/global.module';
import { HookHandlerService } from './services/hook-handler.service';
import { LocalStrategy } from './strategies/local.strategy';
import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.strategy';
import { BaseViewStrategy } from './strategies/base-view.strategy/base-view.strategy';
import NcConfigFactory from './utils/NcConfigFactory';
import NcUpgrader from './version-upgrader/NcUpgrader';
import { MetasModule } from './modules/metas/metas.module';
import NocoCache from './cache/NocoCache';
import { JobsModule } from './modules/jobs/jobs.module';
import type {
MiddlewareConsumer,
OnApplicationBootstrap,
} from '@nestjs/common';
import { AppInitService } from './services/app-init.service';
import type { MiddlewareConsumer } from '@nestjs/common';
@Module({
imports: [
@ -60,15 +50,10 @@ import type {
AuthTokenStrategy,
BaseViewStrategy,
HookHandlerService,
AppInitService,
],
})
export class AppModule implements OnApplicationBootstrap {
constructor(
private readonly connection: Connection,
private readonly metaService: MetaService,
@Inject('IEventEmitter') private readonly eventEmitter: IEventEmitter,
) {}
export class AppModule {
// Global Middleware
configure(consumer: MiddlewareConsumer) {
consumer
@ -79,33 +64,4 @@ export class AppModule implements OnApplicationBootstrap {
.apply(GlobalMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
// app init
async onApplicationBootstrap(): Promise<void> {
process.env.NC_VERSION = '0105004';
await NocoCache.init();
await this.connection.init();
await NcConfigFactory.metaDbCreateIfNotExist(this.connection.config);
await this.metaService.init();
// todo: remove
// temporary hack
Noco._ncMeta = this.metaService;
Noco.config = this.connection.config;
Noco.eventEmitter = this.eventEmitter;
// init jwt secret
await Noco.initJwt();
// init plugin manager
await NcPluginMgrv2.init(Noco.ncMeta);
await Noco.loadEEState();
// run upgrader
await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
}
}

15
packages/nocodb/src/modules/global/global.module.ts

@ -1,19 +1,24 @@
import { Global, Module } from '@nestjs/common';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { ExtractJwt } from 'passport-jwt';
import {
AppInitService,
appInitServiceProvider,
} from '../../services/app-init.service';
import { SocketGateway } from '../../gateways/socket.gateway';
import { Connection } from '../../connection/connection';
import { GlobalGuard } from '../../guards/global/global.guard';
import { MetaService } from '../../meta/meta.service';
import Noco from '../../Noco';
import { JwtStrategy } from '../../strategies/jwt.strategy';
import NcConfigFactory from '../../utils/NcConfigFactory';
import { UsersService } from '../../services/users/users.service';
import type { Provider } from '@nestjs/common';
export const JwtStrategyProvider: Provider = {
provide: JwtStrategy,
useFactory: async (usersService: UsersService) => {
const config = await NcConfigFactory.make();
const config = Noco.config;
await Noco.initJwt();
const options = {
// ignoreExpiration: false,
@ -26,13 +31,14 @@ export const JwtStrategyProvider: Provider = {
return new JwtStrategy(options, usersService);
},
inject: [UsersService],
inject: [UsersService, AppInitService],
};
@Global()
@Module({
imports: [],
providers: [
appInitServiceProvider,
Connection,
MetaService,
UsersService,
@ -41,6 +47,7 @@ export const JwtStrategyProvider: Provider = {
SocketGateway,
],
exports: [
AppInitService,
Connection,
MetaService,
JwtStrategyProvider,

18
packages/nocodb/src/services/app-init.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppInitService } from './app-init.service';
describe('AppInitService', () => {
let service: AppInitService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AppInitService],
}).compile();
service = module.get<AppInitService>(AppInitService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

52
packages/nocodb/src/services/app-init.service.ts

@ -0,0 +1,52 @@
import { Inject, Injectable } from '@nestjs/common';
import NocoCache from '../cache/NocoCache';
import { Connection } from '../connection/connection';
import NcPluginMgrv2 from '../helpers/NcPluginMgrv2';
import { MetaService } from '../meta/meta.service';
import Noco from '../Noco';
import NcConfigFactory from '../utils/NcConfigFactory';
import NcUpgrader from '../version-upgrader/NcUpgrader';
import type { IEventEmitter } from '../modules/event-emitter/event-emitter.interface';
import type { Provider } from '@nestjs/common';
@Injectable()
export class AppInitService {}
export const appInitServiceProvider: Provider = {
provide: AppInitService,
useFactory: async (
connection: Connection,
metaService: MetaService,
eventEmitter: IEventEmitter,
) => {
process.env.NC_VERSION = '0105004';
await NocoCache.init();
await connection.init();
await NcConfigFactory.metaDbCreateIfNotExist(connection.config);
await metaService.init();
// todo: remove
// temporary hack
Noco._ncMeta = metaService;
Noco.config = connection.config;
Noco.eventEmitter = eventEmitter;
// init jwt secret
await Noco.initJwt();
// init plugin manager
await NcPluginMgrv2.init(Noco.ncMeta);
await Noco.loadEEState();
// run upgrader
await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
// todo: move app config to app-init service
return new AppInitService();
},
inject: [Connection, MetaService, 'IEventEmitter'],
};
Loading…
Cancel
Save