3

I am trying to run a nestjs project locally without using any docker images. But I am not able to connect to the mysql database. I can't figure out what is happening. Here I am adding my config files:

app.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigureEnum } from './common/enums/configure.enum';
import { UserEntity } from './common/entities/user.entity';

const ENV = process.env['NODE_ENV'];
const envFilePath = [`env/${!ENV ? `.env` : `.env.${ENV}`}`];

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath,
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'relaxy',
      database: 'relaxy',
      entities: [__dirname + '/**/common/entities/*.entity{.ts,.js}'],
      // entities: [UserEntity],
      synchronize: true,
      logging: true,
      logger: 'file',
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  constructor() {
    console.log(__dirname);
  }
}

Now I am adding photos of databases in mysql:

Databases

Now I am adding photos of users in mysql:

users in mysql

The error I am getting:

errors

1
  • typeorm need a default connection like TypeOrmModule.forRoot({ name: 'default', ...otheroptions}) Commented May 22, 2022 at 22:17

2 Answers 2

2

Had similar problems.

Make sure that your entity path is pointing to the correct location. I removed the __dirname and for some or other reason I could never get things to work by passing an env file into the ConfigModule, could be that too... Heres what I know to work, with pg though.

type: 'postgres',
  host: this.config.get('DEV_DB_HOST'),
  port: this.config.get('DEV_DB_PORT'),
  database: this.config.get('DEV_DB_NAME'),
  username: this.config.get('DEV_DB_USER'),
  password: this.config.get('DEV_DB_PASSWORD'),
  entities: ['dist/**/*.entity.{ts,js}'],
  migrations: ['dist/migrations/*.{ts,js}'],
  migrationsTableName: 'typeorm_migrations',
  synchronize: true,

Good luck and I hope that you get it going! :)

Sign up to request clarification or add additional context in comments.

1 Comment

I changed my entities path from src/**/entities/*.entitiy.ts to dist/**/entities/*.entity.{ts,js} and deleted my migrations property (which was anyways empty), and it worked. Also helped my migration:generate
0

In my case I had to add the entity in forFeature to the module enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.