1.0.1 • Published 2 years ago

nestjs-mysql2-async v1.0.1

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
2 years ago

nestjs-mysql2-async

解决问题

  1. @nestjs/typeorm使用时必须声明至少一个entity,并且entity必须有主键,否则会抛错。在业务上,我们使用了 StarRocks,数据组不建议给表加上主键,所以没法使用@nestjs/typeorm做数据查询
  2. 本来打算使用nest-mysql2这个包做数据库连接配置,不过它不支持异步,我希望获取到数据库变量配置后再做链接,达到类似@nestjs/typeorm的效果
TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (configService: ConfigService) => ({
    type: 'mysql',
    entities: [Empty],
    host: configService.get('STARROCKS_HOST'),
    port: configService.get<number>('STARROCKS_PORT'),
    username: configService.get('STARROCKS_USER'),
    password: configService.get('STARROCKS_PWD'),
    database: configService.get('STARROCKS_DATABASE'),
  }),
}),

解决方案

nest-mysql2这个包的基础上,添加forRootAsync方法

安装

npm i nestjs-mysql2-async mysql2