Browse Source

refactor: move app init to app module

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
46710ac6ff
  1. 27
      packages/nocodb-nest/src/app.module.ts
  2. 4
      packages/nocodb-nest/src/connection/connection.ts
  3. 20
      packages/nocodb-nest/src/meta/meta.service.ts

27
packages/nocodb-nest/src/app.module.ts

@ -1,4 +1,9 @@
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common'; import {
MiddlewareConsumer,
Module,
RequestMethod,
OnApplicationBootstrap,
} from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core'; import { APP_FILTER } from '@nestjs/core';
import { Connection } from './connection/connection'; import { Connection } from './connection/connection';
import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter'; import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter';
@ -9,6 +14,7 @@ import { UsersModule } from './modules/users/users.module';
import { MetaService } from './meta/meta.service'; import { MetaService } from './meta/meta.service';
import { UtilsModule } from './modules/utils/utils.module'; import { UtilsModule } from './modules/utils/utils.module';
import { ProjectsModule } from './modules/projects/projects.module'; import { ProjectsModule } from './modules/projects/projects.module';
import Noco from './Noco';
import { JwtStrategy } from './strategies/jwt.strategy'; import { JwtStrategy } from './strategies/jwt.strategy';
import { TablesModule } from './modules/tables/tables.module'; import { TablesModule } from './modules/tables/tables.module';
import { ViewsModule } from './modules/views/views.module'; import { ViewsModule } from './modules/views/views.module';
@ -103,10 +109,27 @@ import { PluginsModule } from './modules/plugins/plugins.module';
], ],
exports: [Connection, MetaService], exports: [Connection, MetaService],
}) })
export class AppModule { export class AppModule implements OnApplicationBootstrap {
constructor(
private readonly connection: Connection,
private readonly metaService: MetaService,
) {}
// Global Middleware
configure(consumer: MiddlewareConsumer) { configure(consumer: MiddlewareConsumer) {
consumer consumer
.apply(GlobalMiddleware) .apply(GlobalMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL }); .forRoutes({ path: '*', method: RequestMethod.ALL });
} }
// app init
async onApplicationBootstrap(): Promise<void> {
await this.connection.init();
await this.metaService.init();
// todo: remove
// temporary hack
Noco._ncMeta = this.metaService;
Noco.config = this.connection.config;
}
} }

4
packages/nocodb-nest/src/connection/connection.ts

@ -6,7 +6,7 @@ import NcConfigFactory from '../utils/NcConfigFactory';
@Global() @Global()
@Injectable() @Injectable()
export class Connection implements OnModuleInit { export class Connection {
private knex: knex.Knex; private knex: knex.Knex;
private _config: any; private _config: any;
@ -19,7 +19,7 @@ export class Connection implements OnModuleInit {
} }
// init metadb connection // init metadb connection
async onModuleInit(): Promise<void> { async init(): Promise<void> {
this._config = await NcConfigFactory.make(); this._config = await NcConfigFactory.make();
this.knex = XKnex({ this.knex = XKnex({
...this._config.meta.db, ...this._config.meta.db,

20
packages/nocodb-nest/src/meta/meta.service.ts

@ -180,7 +180,7 @@ const nanoidv2 = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 14);
@Global() @Global()
@Injectable() @Injectable()
export class MetaService implements OnApplicationBootstrap { export class MetaService {
constructor(private metaConnection: Connection) {} constructor(private metaConnection: Connection) {}
public get connection() { public get connection() {
@ -346,21 +346,6 @@ export class MetaService implements OnApplicationBootstrap {
return `${prefix}${nanoidv2()}`; return `${prefix}${nanoidv2()}`;
} }
async onApplicationBootstrap(): Promise<void> {
await this.metaInit();
// todo: tobe fixed - temporary workaround
Noco._ncMeta = this;
Noco.config = this.metaConnection.config;
}
// //
@ -1051,8 +1036,7 @@ export class MetaService implements OnApplicationBootstrap {
return this.metaInsert(project_id, dbAlias, target, data); return this.metaInsert(project_id, dbAlias, target, data);
} }
public async metaInit(): Promise<boolean> { public async init(): Promise<boolean> {
NocoCache.init(); NocoCache.init();
await this.connection.migrate.latest({ await this.connection.migrate.latest({
migrationSource: new XcMigrationSource(), migrationSource: new XcMigrationSource(),

Loading…
Cancel
Save