Browse Source

feat: move initialization inside bootstrap to allow workers to process right after bootstrap

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5711/head
mertmit 2 years ago
parent
commit
5da006bab7
  1. 2
      packages/nocodb/src/app.module.ts
  2. 19
      packages/nocodb/src/connection/connection.spec.ts
  3. 29
      packages/nocodb/src/connection/connection.ts
  4. 9
      packages/nocodb/src/init.ts
  5. 45
      packages/nocodb/src/meta/meta.service.ts
  6. 82
      packages/nocodb/src/modules/global/global.module.ts
  7. 19
      packages/nocodb/src/services/app-init.service.spec.ts
  8. 74
      packages/nocodb/src/services/app-init.service.ts
  9. 2
      packages/nocodb/src/utils/nc-config/NcConfig.ts

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

@ -16,7 +16,6 @@ import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.str
import { BaseViewStrategy } from './strategies/base-view.strategy/base-view.strategy';
import { MetasModule } from './modules/metas/metas.module';
import { JobsModule } from './modules/jobs/jobs.module';
import { AppInitService } from './services/app-init.service';
import type { MiddlewareConsumer } from '@nestjs/common';
@Module({
@ -41,7 +40,6 @@ import type { MiddlewareConsumer } from '@nestjs/common';
AuthTokenStrategy,
BaseViewStrategy,
HookHandlerService,
AppInitService,
],
})
export class AppModule {

19
packages/nocodb/src/connection/connection.spec.ts

@ -1,19 +0,0 @@
import { Test } from '@nestjs/testing';
import { Connection } from './knex';
import type { TestingModule } from '@nestjs/testing';
describe('Knex', () => {
let provider: Connection;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [Connection],
}).compile();
provider = module.get<Connection>(Connection);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});

29
packages/nocodb/src/connection/connection.ts

@ -1,29 +0,0 @@
import { Injectable, Scope } from '@nestjs/common';
import { XKnex } from '../db/CustomKnex';
import { NcConfig } from '../utils/nc-config';
import type * as knex from 'knex';
@Injectable({
scope: Scope.DEFAULT,
})
export class Connection {
private knex: knex.Knex;
private _config: any;
constructor(config: NcConfig) {
this._config = config;
this.knex = XKnex({
...this._config.meta.db,
useNullAsDefault: true,
});
}
get knexInstance(): knex.Knex {
return this.knex;
}
get config(): NcConfig {
return this._config;
}
}

9
packages/nocodb/src/init.ts

@ -1,13 +1,12 @@
import { Connection } from './connection/connection';
import { MetaService } from './meta/meta.service';
import { NcConfig } from './utils/nc-config';
import Noco from './Noco';
// run upgrader
import NcUpgrader from './version-upgrader/NcUpgrader';
export default async () => {
// TODO fix
// await Connection.init();
// Noco._ncMeta = new MetaService(new Connection());
// await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
const config = await NcConfig.createByEnv();
Noco._ncMeta = new MetaService(config);
await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
};

45
packages/nocodb/src/meta/meta.service.ts

@ -1,22 +1,15 @@
import {
Global,
Inject,
Injectable,
OnApplicationBootstrap,
OnModuleInit,
Optional,
} from '@nestjs/common';
import { Injectable, Optional } from '@nestjs/common';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { customAlphabet } from 'nanoid';
import CryptoJS from 'crypto-js';
import { Connection } from '../connection/connection';
import Noco from '../Noco';
import NocoCache from '../cache/NocoCache';
import { XKnex } from '../db/CustomKnex';
import { NcConfig } from '../utils/nc-config';
import XcMigrationSourcev2 from './migrations/XcMigrationSourcev2';
import XcMigrationSource from './migrations/XcMigrationSource';
import type { Knex } from 'knex';
import type * as knex from 'knex';
dayjs.extend(utc);
dayjs.extend(timezone);
@ -192,18 +185,38 @@ const nanoidv2 = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 14);
@Injectable()
export class MetaService {
constructor(private metaConnection: Connection, @Optional() trx = null) {
private _knex: knex.Knex;
private _config: any;
constructor(config: NcConfig, @Optional() trx = null) {
this._config = config;
this._knex = XKnex({
...this._config.meta.db,
useNullAsDefault: true,
});
this.trx = trx;
}
get knexInstance(): knex.Knex {
return this._knex;
}
get config(): NcConfig {
return this._config;
}
public get connection() {
return this.trx ?? this.metaConnection.knexInstance;
return this.trx ?? this.knexInstance;
}
get knexConnection() {
return this.connection;
}
public get knex(): any {
return this.knexConnection;
}
public async metaGet(
project_id: string,
dbAlias: string,
@ -758,7 +771,7 @@ export class MetaService {
});
// todo: tobe done
return new MetaService(this.metaConnection, trx);
return new MetaService(this.config, trx);
}
async metaReset(
@ -1027,10 +1040,6 @@ export class MetaService {
.delete();
}
public get knex(): any {
return this.knexConnection;
}
private getNanoId() {
return nanoid();
}

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

@ -1,25 +1,26 @@
import { Global, Module } from '@nestjs/common';
import { ExtractJwt } from 'passport-jwt';
import {
AppInitService,
appInitServiceProvider,
} from '../../services/app-init.service';
import { T } from 'nc-help';
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 { UsersService } from '../../services/users/users.service';
import Noco from '../../Noco';
import NcPluginMgrv2 from '../../helpers/NcPluginMgrv2';
import NcUpgrader from '../../version-upgrader/NcUpgrader';
import NocoCache from '../../cache/NocoCache';
import getInstance from '../../utils/getInstance';
import initAdminFromEnv from '../../helpers/initAdminFromEnv';
import { User } from '../../models';
import { NcConfig, prepareEnv } from '../../utils/nc-config';
import type { Provider } from '@nestjs/common';
import type { IEventEmitter } from '../event-emitter/event-emitter.interface';
export const JwtStrategyProvider: Provider = {
provide: JwtStrategy,
useFactory: async (
usersService: UsersService,
appInitService: AppInitService,
) => {
const config = appInitService.appConfig;
useFactory: async (usersService: UsersService, metaService: MetaService) => {
const config = metaService.config;
await Noco.initJwt();
@ -34,24 +35,71 @@ export const JwtStrategyProvider: Provider = {
return new JwtStrategy(options, usersService);
},
inject: [UsersService, AppInitService],
inject: [UsersService, MetaService],
};
@Global()
@Module({
imports: [],
providers: [
appInitServiceProvider,
Connection,
MetaService,
{
// initialize app,
// 1. init cache
// 2. init db connection and create if not exist
// 3. init meta and set to Noco
// 4. init jwt
// 5. init plugin manager
// 6. run upgrader
useFactory: async (eventEmitter: IEventEmitter) => {
// NC_DATABASE_URL_FILE, DATABASE_URL_FILE, DATABASE_URL, NC_DATABASE_URL to NC_DB
await prepareEnv();
const config = await NcConfig.createByEnv();
// set version
process.env.NC_VERSION = '0107004';
// init cache
await NocoCache.init();
// init meta service
const metaService = new MetaService(config);
await metaService.init();
// provide meta and config to Noco
Noco._ncMeta = metaService;
Noco.config = config;
Noco.eventEmitter = eventEmitter;
// init jwt secret
await Noco.initJwt();
// load super admin user from env if env is set
await initAdminFromEnv(metaService);
// init plugin manager
await NcPluginMgrv2.init(Noco.ncMeta);
await Noco.loadEEState();
// run upgrader
await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
T.init({
instance: getInstance,
});
T.emit('evt_app_started', await User.count());
return metaService;
},
provide: MetaService,
inject: ['IEventEmitter'],
},
UsersService,
JwtStrategyProvider,
GlobalGuard,
...(!process.env['NC_WORKER_CONTAINER'] ? [SocketGateway] : []),
],
exports: [
AppInitService,
Connection,
MetaService,
JwtStrategyProvider,
UsersService,

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

@ -1,19 +0,0 @@
import { Test } from '@nestjs/testing';
import { AppInitService } from './app-init.service';
import type { TestingModule } from '@nestjs/testing';
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();
});
});

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

@ -1,74 +0,0 @@
import { T } from 'nc-help';
import NocoCache from '../cache/NocoCache';
import { Connection } from '../connection/connection';
import initAdminFromEnv from '../helpers/initAdminFromEnv';
import NcPluginMgrv2 from '../helpers/NcPluginMgrv2';
import { MetaService } from '../meta/meta.service';
import { User } from '../models';
import Noco from '../Noco';
import getInstance from '../utils/getInstance';
import NcUpgrader from '../version-upgrader/NcUpgrader';
import type { IEventEmitter } from '../modules/event-emitter/event-emitter.interface';
import type { Provider } from '@nestjs/common';
export class AppInitService {
private readonly config: any;
constructor(config) {
this.config = config;
}
get appConfig(): any {
return this.config;
}
}
export const appInitServiceProvider: Provider = {
provide: AppInitService,
// initialize app,
// 1. init cache
// 2. init db connection and create if not exist
// 3. init meta and set to Noco
// 4. init jwt
// 5. init plugin manager
// 6. run upgrader
useFactory: async (
connection: Connection,
metaService: MetaService,
eventEmitter: IEventEmitter,
) => {
process.env.NC_VERSION = '0107004';
await NocoCache.init();
await metaService.init();
// todo: remove
// temporary hack
Noco._ncMeta = metaService;
Noco.config = connection.config;
Noco.eventEmitter = eventEmitter;
// init jwt secret
await Noco.initJwt();
// load super admin user from env if env is set
await initAdminFromEnv(metaService);
// init plugin manager
await NcPluginMgrv2.init(Noco.ncMeta);
await Noco.loadEEState();
// run upgrader
await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
T.init({
instance: getInstance,
});
T.emit('evt_app_started', await User.count());
// todo: move app config to app-init service
return new AppInitService(connection.config);
},
inject: [Connection, MetaService, 'IEventEmitter'],
};

2
packages/nocodb/src/utils/nc-config/NcConfig.ts

@ -70,7 +70,7 @@ export class NcConfig {
ncConfig.auth = {
jwt: {
secret: secret ?? 'temporary-key',
secret: secret,
},
};

Loading…
Cancel
Save