web-service/apps/assets/src/assets.module.ts

43 lines
1.4 KiB
TypeScript

import { Module, OnModuleInit } from '@nestjs/common';
import { AssetsController } from './assets.controller';
import { AssetsService } from './services/assets.service';
import { makeKnex, makeTypeOrm, natsClient } from '@freeblox/shared';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule } from '@nestjs/microservices';
import { TypeOrmModule } from '@nestjs/typeorm';
import knex from 'knex';
import { AssetEntity } from './database/entities/asset.entity';
import { s3Config } from './config/s3.config';
import { S3Service } from './services/s3.service';
const entities = [AssetEntity];
@Module({
imports: [
ConfigModule.forRoot({
ignoreEnvFile: process.env.NODE_ENV === 'development',
load: [makeKnex('assets', __dirname), makeTypeOrm('assets'), s3Config],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
...config.get('typeorm'),
entities,
}),
}),
TypeOrmModule.forFeature(entities),
ClientsModule.register([natsClient('assets'), natsClient('auth')]),
],
controllers: [AssetsController],
providers: [S3Service, AssetsService],
})
export class AssetsModule implements OnModuleInit {
constructor(private readonly config: ConfigService) {}
async onModuleInit() {
const knexInstance = knex(this.config.get('knex'));
await knexInstance.migrate.latest();
}
}