Home > Blockchain >  Nestjs Typeorm query still using 'postgres' database
Nestjs Typeorm query still using 'postgres' database

Time:05-10

When I try to query using repository for login api, I receive this error

QueryFailedError: relation "user" does not exist

After debuging, I realize the nestjs still using database with name "postgres". eventhough I set my database name in my .env as "nestjs". (I can confirm because when I migrate and seed the database "postgres" the login function works)

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

All my migration and seed process already using database "nestjs". Below is my files to configure my database connection

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 {}

And here is my databases:

enter image description here

If you guys need more file for information please let me know. I'm new to nestjs and typeorm, So please tell me what's wrong with my code. Thank you!

CodePudding user response:

TypeormModule has the database property which provides the database name. You are using name in provider.module.ts:

  • Wrong name : databaseName
  • Right database : databaseName
TypeOrmModule.forRootAsync({
  useFactory: () => ({
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: 'root',
    database: 'test',
    entities: [__dirname   '/**/*.entity{.ts,.js}'],
    synchronize: true,
  }),
});

  • Related