jipson saad
jipson saad

Reputation: 83

Why does the useFactory option make me an error in the nestjs configuration?

Type '(configService: ConfigService) => Promise<{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }>' is not assignable to type '(...args: any[]) => ({ retryAttempts?: number; retryDelay?: number; keepConnectionAlive?: boolean; } & Partial) | ({ retryAttempts?: number; retryDelay?: number; keepConnectionAlive?: boolean; } & Partial<...>) | ... 11 more ... | Promise<...>'. Type 'Promise<{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }>' is not assignable to type '({ retryAttempts?: number; retryDelay?: number; keepConnectionAlive?: boolean; } & Partial) | ({ retryAttempts?: number; retryDelay?: number; keepConnectionAlive?: boolean; } & Partial<...>) | ... 11 more ... | Promise<...>'. Type 'Promise<{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }>' is not assignable to type 'Promise'. Type '{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }' is not assignable to type 'TypeOrmModuleOptions'. Type '{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }' is not assignable to type '{ retryAttempts?: number; retryDelay?: number; keepConnectionAlive?: boolean; } & Partial'. Type '{ type: string; port: string; username: string; password: string; database: string; host: string; entities: string[]; synchronize: boolean; }' is not assignable to type 'Partial'. Types of property 'type' are incompatible. Type 'string' is not assignable to type '"aurora-data-api"'.

that's the message that nestjs gives me, I follow the instructions from the documentation but is not work for me.

this is my app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CategoryModule } from './category/category.module';
import { ProductModule } from './product/product.module';
import { UnitModule } from './unit/unit.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { RolModule } from './rol/rol.module';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';

@Module({
  imports: [TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
      type: 'mysql',
      port: configService.port,
      username: configService.username,
      password: configService.password,
      database: configService.database,
      host: configService.host,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
    }),
  }), CategoryModule, UnitModule, ProductModule, RolModule, UserModule, AuthModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

this is my config/config.service.ts

import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as Joi from '@hapi/joi';

export interface EnvConfig {
    [key: string]: string;
}

export class ConfigService {
    private readonly envConfig: EnvConfig;

    constructor(filePath: string) {
        const config = dotenv.parse(fs.readFileSync(filePath));
        this.envConfig = this.validateInput(config);
    }

    private validateInput(envConfig: EnvConfig): EnvConfig {
        const envVarsSchema: Joi.ObjectSchema = Joi.object({
            NODE_ENV: Joi.string()
            .valid('development', 'production', 'test', 'provision')
            .default('development'),
            PORT: Joi.number().default(3000),
            HOST: Joi.strict(),
            USERNAME: Joi.string(),
            PASSWORD: Joi.string(),
            DATABASE: Joi.string(),
        });

        const { error, value: validatedEnvConfig } = envVarsSchema.validate(
            envConfig,
        );
        if (error) {
            throw new Error(`Config validation error: ${error.message}`);
        }
        return validatedEnvConfig;
    }

    get port(): string {
        return String(this.envConfig.PORT);
    }
    get host(): string {
        return String(this.envConfig.HOST);
    }
    get username(): string {
        return String(this.envConfig.USERNAME);
    }
    get password(): string {
        return String(this.envConfig.PASSWORD);
    }
    get database(): string {
        return String(this.envConfig.DATABASE);
    }
}

and this is my config/config.module.ts

import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';

@Module({
  providers: [{
    provide: ConfigService,
    useValue: new ConfigService(`${process.env.NODE_ENV || 'development'}.env`),
  }],
  exports: [ConfigService],
})
export class ConfigModule {}

the useFacetory option is the one that produces the error but i don't understand why I appreciate anyone help

Upvotes: 4

Views: 6158

Answers (2)

Luis Mendoza
Luis Mendoza

Reputation: 21

I had the same error, in my case it was the port and I solved it like this:

app.moodule.ts:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: [`.env.${process.env.NODE_ENV}`],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port: +configService.get<number>('DB_PORT'),
        username: configService.get('DB_USER'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        autoLoadEntities: true,
        synchronize: true,
      }),
    }),
    AuthModule,
  ],
})
export class AppModule {}

.env.development:

DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=db-test

Upvotes: 2

jipson saad
jipson saad

Reputation: 83

So the problem is when I try to get the PORT from the .env file is necessary convert the type to Number. Example:

useFactory: async (configService: ConfigService) => ({
  type: 'mysql',
  port: Number(configService.port),
  username: configService.username,
  password: configService.password,
  database: configService.database,
  host: configService.host,
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true,
}),

that solves the problem

Upvotes: 3

Related Questions