NestJS + GraphQL + Sequelize-typescript 연동 - 단일 DB인 경우

in hive-101145 •  3 years ago 

이번에는 BackEnd 내용입니다..

현재 프로젝트에 NestJS + Sequelize 을 연동하면서 Multi DB 커넥트가 되도록 구성중입니다.

강력한 Bacnend 하나를 만들어서, 왠만한 기능들을 다 돌아가도록 만들고 싶어서 Multi로 DB를 활용해야 하는 경우가 생기네요.

오늘은 단일 DB인 경우(쉬운 방법..)를 한번 보고, Multi DB 커넥트가 필요한 경우 하는 방법에 대해서는 다음 포스팅에서 보겠습니다.


Sequelize 설치

npm install --save @nestjs/sequelize sequelize sequelize-typescript mysql2
npm install --save-dev @types/sequelize

app.module.ts

import { SequelizeModule } from '@nestjs/sequelize';

@Module({
  imports: [
    SequelizeModule.forRoot({
      dialect: 'mysql',
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT),
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME,
      retryAttempts: 5,
      retryDelay: 3000,
      autoLoadModels: false,
      synchronize: false, // true 시 테이블 자동 생성 될듯
      models: [config],
    })
  ]
})

실제로 사용될 module 파일(여기서는 SteemModule로 구성)

import { SequelizeModule } from '@nestjs/sequelize';

@Module({})
export class SteemModule {
  static forRoot(options: SteemNetworkOptions): DynamicModule {
    return {
      module: SteemModule,
      imports: [SequelizeModule.forFeature([사용될 Table list]), DatabaseModule],
      providers: [
        { provide: CHAIN_OPTION, useValue: options },
        SteemResolver,
        SteemService,
      ],
      exports: [],
    };
  }
}

steem.service.ts

  • contructor에 구성을 하고 아래에 this.model.. 형태로 사용을 하면 됩니다.
@Injectable()
export class SteemService extends CommonService {
  constructor(
    private sequelize: Sequelize,
    @InjectModel(table)
    private configModel: typeof table,
  ) {
    super();
  }

async findTest(): Promise<table> {
    return await this.configModel.findOne({
      where: {
        config_group: 'mint',
        config_key: 'current_stage',
      },
    });
  }
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

1- Are you looking for bigger muscles? We have all the ways that will show you how to get bigger, stronger and faster to get the physique of your dreams
visit my website :-
Enlarge your muscle

2- Health Care

Is The Brand Men & Women Live By For Fitness, Nutrition, Health, Sex, Style, Grooming, Tech, Weight Loss, And More visit my websites

sexual health care

sexual health care xnxx

live strong healthy

healthy care plans

sexual health care

https://www.miaxxx.us/ , https://www.healthylifegoals.us/ , https://www.livestronghealthy.com/
, https://www.healthycareplans.com/ , https://www.healthylifegoals.us/xnxx

image.png

@wonsama님이 당신을 멘션하였습니다.

https://www.steemit.com/@wonsama/2krkvn