Nestjs Typeorm 查询仍在使用 'postgres' 数据库

Nestjs Typeorm query still using 'postgres' database

当我尝试使用 repository 查询登录 api 时,我收到此错误

QueryFailedError: relation "user" does not exist

调试后,我发现 nestjs 仍在使用名为“postgres”的数据库。尽管我在 .env 中将我的数据库名称设置为“nestjs”。 (我可以确认,因为当我迁移和播种数据库“postgres”时,登录功能有效)

DB_TYPE=postgres
DB_HOST=db
DB_USER=postgres
DB_PASS=postgres
DB_NAME=nestjs
DB_NAME_TEST=nestjs_test
DB_PORT=5432

我所有的迁移和种子过程都已经在使用数据库“nestjs”。下面是我配置数据库连接的文件

configuration.ts

import { registerAs } from '@nestjs/config';

export default registerAs('database', () => ({
  type: process.env.DB_TYPE,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  name: process.env.DB_NAME,
  nameTest: process.env.DB_NAME_TEST,
  port: process.env.DB_PORT,
}));

database/config.service.ts

...
get name(): string {
    return this.configService.get<string>(
      this.configService.get<string>('app.env') === 'test'
        ? 'database.nameTest'
        : 'database.name',
    );
  }
...

database/config.migration.ts

import { SnakeNamingStrategy } from 'typeorm-naming-strategies';

export = {
  type: process.env.DB_TYPE,
  host: process.env.DB_HOST,
  port: +process.env.DB_PORT,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database:
    process.env.NODE_ENV === 'test'
      ? process.env.DB_NAME_TEST
      : process.env.DB_NAME,
  entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
  migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
  cli: {
    migrationsDir: __dirname + '/../../database/migrations',
  },
  extra: {
    charset: 'utf8mb4_unicode_ci',
  },
  synchronize: false,
  logging: true,
  keepConnectionAlive: true,
  namingStrategy: new SnakeNamingStrategy(),
};

database/provider.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { DatabaseConfigModule } from '../../config/database/config.module';
import { DatabaseConfigService } from '../../config/database/config.service';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [DatabaseConfigModule],
      inject: [DatabaseConfigService],
      useFactory: async (
        service: DatabaseConfigService,
      ): Promise<TypeOrmModuleOptions> => ({
        type: service.type,
        host: service.host,
        port: service.port,
        username: service.user,
        password: service.password,
        name: service.name,
        entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
        migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
        cli: {
          migrationsDir: __dirname + '/../../database/migrations',
        },
        extra: {
          charset: 'utf8mb4_unicode_ci',
        },
        synchronize: false,
        logging: true,
        keepConnectionAlive: true,
        namingStrategy: new SnakeNamingStrategy(),
      }),
    }),
  ],
})
export class DatabaseProviderModule {}

这是我的数据库:

如果你们需要更多文件以获取信息,请告诉我。我是 nestjs 和 typeorm 的新手,所以请告诉我我的代码有什么问题。谢谢!

TypeormModule 具有提供数据库名称的数据库 属性。您在 provider.module.ts:

中使用姓名
  • 错误 名称:数据库名称
  • 数据库:databaseName
TypeOrmModule.forRootAsync({
  useFactory: () => ({
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: 'root',
    database: 'test',
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
    synchronize: true,
  }),
});