NestJS ConfigModule.forRoot load is not working as I expected - javascript

I am trying to get the load env variable functionality to work in ConfigModule in NestJS.
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '#nestjs/config';
#Module({
imports: [
ConfigModule.forRoot({
load: [
() => ({
port: 3000,
database: {
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT, 10) || 5432,
},
}),
],
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
When I inspect process.env.port it is undefined. I have a feeling I am not understanding what this is actually doing. I thought it would be adding the env variable port and database. I think it may have something to do with expandable env variables which I don't understand how to access. Can someone explain how to get this to work and how to access these variables. The code above was taken from the NestJS documentation located at https://docs.nestjs.com/techniques/configuration
Thanks

Okay, as usual I answered my own question 15 minutes after I posted it. My assumption that this modified process.env was incorrect. It looks like it stores these values internally to the ConfigModule and they are accessible through the ConfigService.
this.configService.get<number>('port') // returns 3000
process.env.port // returns undefined
I will leave this up here if it helps someone else. You know what they say about assuming.
Thanks

Related

Local MongoDB instance is not getting connected with NestJS app.module

I'm trying to use NestJS, where I've a .env file at project root level and I'm using import { ConfigModule, ConfigService } from '#nestjs/config';. In order to get the data first I'm loading the .env into app module and then adding mongodb and using the ConfigModule to get DB URI from .env file.
So, my app.module looks something like this.
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { ConfigModule, ConfigService } from '#nestjs/config';
import { UsersModule } from './users/users.module';
import configuration from '../configs/configuration';
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env.dev',
isGlobal: true,
load: [configuration],
}),
MongooseModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
uri: config.get<string>('MONGODB_URI'),
})
}),
UsersModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
But, when I'm trying to hit the port It's throwing an error saying unable to connect to the database.
Can anyone please suggest me where am I making the mistake.
Thanks In advance.
The issue is in .env file use DB connection as mongodb://127.0.0.1:27017/euphoria-dev instead of mongodb://localhost:27017/euphoria-dev
Check the MONGODB_URI once whit console.log(MONGODB_URI) to ensure that this string is read from the file .env File.
error in connecting to the Mongo database is due to the wrong address, which will probably return null.
You can also test from the package #dotenv and use: process.env.MONGODB_URI
good luck.

NestJS: Enforce implementation of a service at *compile-time*

To keep it brief:
I have a service - let's call it CatsService...
import { Injectable } from '#nestjs/common';
#Injectable()
export class CatsService {
GetCat(id: number){
return id + 12345;
}
}
Later on, a module wishes to use this CatsService in order to inject it into a constructor. It also dynamically resolves this CatsService
import { Module } from '#nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import * as ProductionCatsService from "./production"
import * as DevelopmentCatsService from "./development"
const catsServiceProvider = {
provide: CatsService,
useValue: process.env.NODE_ENV === "development" ?
DevelopmentCatsService :
ProductionCatsService
}
#Module({
controllers: [CatsController],
providers: [catsServiceProvider],
exports:[CatsService]
})
export class CatsModule {}
Both of these service implementations are strictly empty. This means that the /cats/:id route returns:
{"statusCode":500,"message":"Internal server error"}
I have had the idea since beginning writing this to make everything implement a ICatsService interface.
After a lot of fiddling about, I have build a sort-of solution:
I have a "service_interface" file, which has an abstract class called ICatsInterface with one abstract method (GetCat)
I have made every one of these implementations implement this service_interface.
I have made the controller take ICatsService instead of CatsService
My problem is that now, the thing compiles, but I get the below error:
TypeError: this.catsService.GetCat is not a function
at CatsController.get (/home/a/learning-nest/src/cats/cats.controller.ts:12:33)
at /home/a/learning-nest/node_modules/#nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at /home/a/learning-nest/node_modules/#nestjs/core/router/router-execution-context.js:46:28
at /home/a/learning-nest/node_modules/#nestjs/core/router/router-proxy.js:9:17
I simply don't know what to do anymore. Even though I have made it compile, I cannot make it actually be forced to implement the interface/abstract class that I want. I have made sure that both DevelopmentCatsService and ProductionCatsService have the implementation of GetCat. They are both marked with #Injectable. I don't truthfully know where to go from here.
Should I abandon my dream of having an compile-time error message for when someone does not implement the methods that I want implemented in my service?
I have solved my own issue. It was very simple.
import { Module } from '#nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import * as ProductionCatsService from "./production"
import * as DevelopmentCatsService from "./development"
const catsServiceProvider = {
provide: CatsService,
useValue: process.env.NODE_ENV === "development" ?
DevelopmentCatsService :
ProductionCatsService
}
#Module({
controllers: [CatsController],
providers: [catsServiceProvider],
exports:[CatsService]
})
export class CatsModule {}
In the above snippet, I am importing "* as ProductionCatsService". This caused my ProductionCatsService to actually be an object wrapping around the real service implementation that I wanted!
Sorry for the bother, all.

Tests fail when using #InjectRepository: Nest can't resolve Repository

I'm using NestJs with Typeorm, normal setup. UsersService gets the Typeorm Repository injected:
constructor(
#InjectRepository(User)
private usersRepository: Repository<User>,
) {}
In the UsersModule:
#Module({
imports:[ TypeOrmModule.forFeature([User])],
controllers: [UsersController ],
providers: [UsersService]
})
Nothing special as you can see. But the auto-generated test for UsersService fails, no matter what I do:
describe('UsersService', () => {
let service: UsersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
I get the following error:
Nest can't resolve dependencies of the UsersService (?). Please
make sure that the argument UserRepository at index [0] is available
in the RootTestModule context.
The solutions on Stackoverflow that I found seem to be obsolete, or over-complicated. I understand that the problem stems from the usage of #InjectRepository.
What is the solution? I tried downloading other people's fairly-similar projects, and get the same error! Both with nest 8 and 7.
Nest can't resolve the dependency because you don't provide the repository in the testing module. If you're doing unit tests you probably want to mock the repository using a custom provider:
import { getRepositoryToken } from '#nestjs/typeorm';
describe('UsersService', () => {
let service: UsersService;
let repository: Repository<User>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{
provide: getRepositoryToken(User),
useValue: {},
}
],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
})
You can provide an object, a class or a factory function, more details in the doc: https://docs.nestjs.com/fundamentals/custom-providers
Then in your tests you can mock the methods of the repository this way:
jest.spyOn(repository, 'find').mockResolvedValueOnce([])
It's not the only way to mock, but that's a simple and standard one.
The docs are pretty clear on how to write tests when using #nestjs/typeorm: https://docs.nestjs.com/techniques/database#testing
There are a bunch of samples here as well: https://github.com/jmcdo29/testing-nestjs
So I managed to "solve" this myself. I should've mentioned perhaps that i didn't intend to do any mocking, but wanted the test to work "as is"(I prefer using a test-dedicated DB, rather than mocking units. Seems more realistic to me).
So it appears i kind of misunderstood, that every call to createTestingModule() needs to make sure all relevant dependencies are created, including stuff like ORM initialization, which is usually done in the AppModule(here i'm testing a service in UserModule..). So what I did in users.service.specs.ts:
const module: TestingModule = await Test.createTestingModule({
imports: [TypeOrmModule.forFeature([User]), TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: '',
database: 'postgres',
schema: 'test-db',
entities: [User],
synchronize: true,
}), TypeOrmModule.forFeature([User])],
providers: [UsersService],
}).compile();
Notice that I had to both create the TypeOrm connection, and register the entity.
Now I understand that each test suite is totally isolated, and therefore needs all relevant dependencies to be passed to it, even if in the "original" application this code is already imported in the root module.

Best practice to use config service in NestJS Module

I want to use environment variables to configure the HttpModule per module, from the docs I can use the configuration like this:
#Module({
imports: [HttpModule.register({
timeout: 5000,
maxRedirects: 5,
})],
})
But I don't know what is the best practice to inclue a baseURL from environment vairable (or a config service), for example like this:
#Module({
imports: [HttpModule.register({
baseURL: this.config.get('API_BASE_URL'),
timeout: 5000,
maxRedirects: 5,
})],
The this.config is undefined here cause it's out of class.
What is the best practice to set baseURL from environment variables (or config service)?
Update Jan 19
HttpModule.registerAsync() was added in version 5.5.0 with this pull request.
HttpModule.registerAsync({
imports:[ConfigModule],
useFactory: async (configService: ConfigService) => ({
baseURL: configService.get('API_BASE_URL'),
timeout: 5000,
maxRedirects: 5,
}),
inject: [ConfigService]
}),
Original Post
This problem was discussed in this issue. For the nestjs modules like the TypeOrmModule or the MongooseModule the following pattern was implemented.
The useFactory method returns the configuration object.
TypeOrmModule.forRootAsync({
imports:[ConfigModule],
useFactory: async (configService: ConfigService) => ({
type: configService.getDatabase()
}),
inject: [ConfigService]
}),
Although Kamil wrote
Above convention is now applied in all nest modules and will be
treated as a best practice (+recommendation for 3rd party modules).
More in the docs
it does not seem to be implemented for the HttpModule yet, but maybe you can open an issue about it. There are also some other suggestions in the issue I mentioned above.
Also have a look at the official docs with best practices on how to implement a ConfigService.
Although the top rated answer to this question is technically correct for most implementations, users of the #nestjs/typeorm package, and the TypeOrmModule should use an implementation that looks more like the below.
// NestJS expects database types to match a type listed in TypeOrmModuleOptions
import { TypeOrmModuleOptions } from '#nestjs/typeorm/dist/interfaces/typeorm-options.interface';
#Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [mySettingsFactory],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
type: configService.get<TypeOrmModuleOptions>('database.type', {
infer: true, // We also need to infer the type of the database.type variable to make userFactory happy
}),
database: configService.get<string>('database.host'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
logging: true,
}),
inject: [ConfigService],
}),
],
controllers: [],
})
export class AppRoot {
constructor(private connection: Connection) {}
}
The major thing this code is doing is retrieving the correct typings from TypeORM (see the import) and using them to hint the return value configService.get() method. If you don't use the correct TypeORM typings, Typescript would get mad.
I also encountered several issues with implementing a ConfigService as described in the NestJS documentation (no type-safety, no modularity of configuration values, ...), I wrote down our company's final NestJS configuration management strategy in great detail here: NestJS Configuration Management
The basic idea is to have a central config module that loads all configuration values from the processes' environment. However, instead of providing a single service to all modules, each module can inject a dedicated subset of the configuration values! So each module contains a class that specifies all configuration values that this module needs to be provided at runtime. This simultaneously gives the developer type-safe access to configuration values (instead of using string literals throughout the codebase)
Hope this pattern also works for your use-case :)
Great answer by #Kim Kern, which clearly goes over injection of the ConfigService into a module configuration, that might be dependent on environment variables; however, from my personal experience, your app-root module or some other module with a couple of imports might get crowded and/or hard to read as well as understand the imports, module configuration and what the module you are defining relies on. So, thanks to Jay McDoniel, who curated me on this question, you can move configuration logic into a separate file.
First Solution
Example of app.module.ts:
import { Module } from '#nestjs/common';
import { ConfigModule } from '#nestjs/config';
import { MikroOrmModule } from '#mikro-orm/nestjs';
import { AppService } from './users.service';
import { AppController } from './users.controller';
import { get_db_config } from './config/database.config';
#Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
expandVariables: true,
}),
MikroOrmModule.forRootAsync( get_db_config() ),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Example of config/database.config.ts:
import { MikroOrmModuleAsyncOptions } from "#mikro-orm/nestjs";
import { ConfigService } from "#nestjs/config";
export function get_db_config(): MikroOrmModuleAsyncOptions
{
return {
useFactory: (configService: ConfigService) =>
({
dbName: 'driver',
type: 'postgresql',
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
user: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
autoLoadEntities: true
}),
inject: [ConfigService]
}
}
However, NestJS Docs - Configuration Namespaces as well as NestJS Authentication and Authorization Course provide an alternative method of solving this issue.
Second Solution
Example of auth.module.ts:
import { Module } from '#nestjs/common';
import { ConfigModule } from '#nestjs/config';
import { JwtModule } from '#nestjs/jwt';
import jwtConfig from './jwt.config';
#Module({
imports: [
ConfigModule.forFeature( jwtConfig ),
JwtModule.registerAsync( jwtConfig.asProvider() ),
]
})
export class AuthModule {}
Example of jwt.config.ts:
import { registerAs } from "#nestjs/config"
export default registerAs('jwt', () => {
return {
secret: process.env.JWT_SECRET,
issuer: process.env.JWT_TOKEN_ISSUER,
accessTokenTtl: parseInt(process.env.JWT_TOKEN_TTL)
};
});

Switch from mock backend to real backend on application start

I have an Angular 5 application that I want to be able to start from either a mock backend or a real backend depending on an environment variable.
This is my environment file:
export const environment = {
production: false,
mockBackend: true
};
I want to be able to have this sort of structure:
AppModule
SharedAppModules
AppModule, imports SharedAppModules and adds what is necessary.
MockAppModule, imports SharedAppModules and adds what is necessary:
MockBackendService,
MockBackend,
BaseRequestOptions,
{ provide: LocationStrategy, useClass: HashLocationStrategy },
{
provide: Http,
deps: [MockBackend, BaseRequestOptions],
useFactory: mockHttpFactory
}
My goal would be to do in main.ts:
if (environment.production) {
enableProdMode();
}
if(environment.mockBackend) {
platformBrowserDynamic().bootstrapModule(MockAppModule);
} else {
platformBrowserDynamic().bootstrapModule(AppModule);
}
But I have an error from Angular saying it did not find anything to bootstrap. Has anybody already tried to do the same thing or have any idea to achieve the same goal differently?

Categories

Resources