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 { BaseViewStrategy } from './strategies/base-view.strategy/base-view.strategy';
import { MetasModule } from './modules/metas/metas.module'; import { MetasModule } from './modules/metas/metas.module';
import { JobsModule } from './modules/jobs/jobs.module'; import { JobsModule } from './modules/jobs/jobs.module';
import { AppInitService } from './services/app-init.service';
import type { MiddlewareConsumer } from '@nestjs/common'; import type { MiddlewareConsumer } from '@nestjs/common';
@Module({ @Module({
@ -41,7 +40,6 @@ import type { MiddlewareConsumer } from '@nestjs/common';
AuthTokenStrategy, AuthTokenStrategy,
BaseViewStrategy, BaseViewStrategy,
HookHandlerService, HookHandlerService,
AppInitService,
], ],
}) })
export class AppModule { 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 { MetaService } from './meta/meta.service';
import { NcConfig } from './utils/nc-config';
import Noco from './Noco'; import Noco from './Noco';
// run upgrader // run upgrader
import NcUpgrader from './version-upgrader/NcUpgrader'; import NcUpgrader from './version-upgrader/NcUpgrader';
export default async () => { export default async () => {
// TODO fix const config = await NcConfig.createByEnv();
// await Connection.init(); Noco._ncMeta = new MetaService(config);
// Noco._ncMeta = new MetaService(new Connection()); await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
// await NcUpgrader.upgrade({ ncMeta: Noco._ncMeta });
}; };

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

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

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

@ -1,25 +1,26 @@
import { Global, Module } from '@nestjs/common'; import { Global, Module } from '@nestjs/common';
import { ExtractJwt } from 'passport-jwt'; import { ExtractJwt } from 'passport-jwt';
import { import { T } from 'nc-help';
AppInitService,
appInitServiceProvider,
} from '../../services/app-init.service';
import { SocketGateway } from '../../gateways/socket.gateway'; import { SocketGateway } from '../../gateways/socket.gateway';
import { Connection } from '../../connection/connection';
import { GlobalGuard } from '../../guards/global/global.guard'; import { GlobalGuard } from '../../guards/global/global.guard';
import { MetaService } from '../../meta/meta.service'; import { MetaService } from '../../meta/meta.service';
import Noco from '../../Noco';
import { JwtStrategy } from '../../strategies/jwt.strategy'; import { JwtStrategy } from '../../strategies/jwt.strategy';
import { UsersService } from '../../services/users/users.service'; 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 { Provider } from '@nestjs/common';
import type { IEventEmitter } from '../event-emitter/event-emitter.interface';
export const JwtStrategyProvider: Provider = { export const JwtStrategyProvider: Provider = {
provide: JwtStrategy, provide: JwtStrategy,
useFactory: async ( useFactory: async (usersService: UsersService, metaService: MetaService) => {
usersService: UsersService, const config = metaService.config;
appInitService: AppInitService,
) => {
const config = appInitService.appConfig;
await Noco.initJwt(); await Noco.initJwt();
@ -34,24 +35,71 @@ export const JwtStrategyProvider: Provider = {
return new JwtStrategy(options, usersService); return new JwtStrategy(options, usersService);
}, },
inject: [UsersService, AppInitService], inject: [UsersService, MetaService],
}; };
@Global() @Global()
@Module({ @Module({
imports: [], imports: [],
providers: [ providers: [
appInitServiceProvider, {
Connection, // initialize app,
MetaService, // 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, UsersService,
JwtStrategyProvider, JwtStrategyProvider,
GlobalGuard, GlobalGuard,
...(!process.env['NC_WORKER_CONTAINER'] ? [SocketGateway] : []), ...(!process.env['NC_WORKER_CONTAINER'] ? [SocketGateway] : []),
], ],
exports: [ exports: [
AppInitService,
Connection,
MetaService, MetaService,
JwtStrategyProvider, JwtStrategyProvider,
UsersService, 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 = { ncConfig.auth = {
jwt: { jwt: {
secret: secret ?? 'temporary-key', secret: secret,
}, },
}; };

Loading…
Cancel
Save