Best practice to use config service in NestJS Module - javascript

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)
};
});

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 ConfigModule.forRoot load is not working as I expected

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

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.

Ionic3 / Ionic2+ settings.json file for environment variable configuration [duplicate]

I'm working on an ionic2 project and I need to create a new custom JSON config file. I found some tutorials to create one and access it through http.get but I think it's weird to call it through a get request. I want it in the root folder (where all the config JSONs are) and I open/read the file directly.
I don't know if it's possible, or even recommended ? This is why I'm posting here to have some opinions and solutions :)
Thanks
Personally I don't like the read the config.json file by using the http.get way to handle configuration information, and even though there must be another way to just include and read the json file in your code, since we're using Angular2 and Typescript, why not using classes, interfaces and doing it in a more fancy way?
What I'll show you next may seem more complicated than it should at first (although after reading it you will find it very straightforward and easy to understand), but when I started learning Angular2 I saw an example of how they handled config files in the Dependency Injection guide and I followed that in the apps I've worked on to handle config information (like API endpoints, default values, and so on).
According the docs:
Non-class dependencies
[...]
Applications often define configuration objects with lots of small
facts (like the title of the application or the address of a web API
endpoint) but these configuration objects aren't always instances of a
class.
One solution to choosing a provider token for non-class dependencies
is to define and use an OpaqueToken
So you would need to define a config object with the urls and so on, and then an OpaqueToken to be able to use it when injecting the object with your configuration.
I included all my configuration in the app-config.ts file
// Although the ApplicationConfig interface plays no role in dependency injection,
// it supports typing of the configuration object within the class.
export interface ApplicationConfig {
appName: string;
apiEndpoint: string;
}
// Configuration values for our app
export const MY_CONFIG: ApplicationConfig = {
appName: 'My new App',
apiEndpoint: 'http://www...'
};
// Create a config token to avoid naming conflicts
export const MY_CONFIG_TOKEN = new OpaqueToken('config');
What OpaqueToken is may be confusing at first, but it just a string that will avoid naming conflicts when injecting this object. You can find an amazing post about this here.
Then, you just need to include it in the page you need it like this:
import { NavController } from 'ionic-angular/index';
import { Component, OpaqueToken, Injectable, Inject } from "#angular/core";
// Import the config-related things
import { MY_CONFIG_TOKEN, MY_CONFIG, ApplicationConfig } from 'app-config.ts';
#Component({
templateUrl:"home.html",
providers: [{ provide: MY_CONFIG_TOKEN, useValue: MY_CONFIG }]
})
export class HomePage {
private appName: string;
private endPoint: string;
constructor(#Inject(MY_CONFIG_TOKEN) private config: ApplicationConfig) {
this.appName = config.appName;
this.endPoint = config.apiEndpoint;
}
}
Please notice how to include it in the providers array
providers: [{ provide: MY_CONFIG_TOKEN, useValue: MY_CONFIG }]
And how to tell the injector how it should obtain the instance of the config object
#Inject(MY_CONFIG_TOKEN) config: ApplicationConfig
UPDATE
OpaqueToken has been deprecated since v4.0.0 because it does not support type information, use InjectionToken<?> instead.
So instead of these lines:
import { OpaqueToken } from '#angular/core';
// Create a config token to avoid naming conflicts
export const MY_CONFIG_TOKEN = new OpaqueToken('config');
Now we should use
import { InjectionToken } from '#angular/core';
// Create a config token to avoid naming conflicts
export const MY_CONFIG_TOKEN = new InjectionToken<ApplicationConfig>('config');
After reading and reading different solutions I ended up using this hacky implementation. Hopefully there will be a nice and native solution available soon:
import { NgModule } from '#angular/core';
import { environment as devVariables } from './environment.dev';
import { environment as testVariables } from './environment.test';
import { environment as prodVariables } from './environment.prod';
export function environmentFactory() {
const location = window.location.host;
switch (location) {
case 'www.example.org': {
return prodVariables;
}
case 'test.example.org': {
return testVariables;
}
default: {
return devVariables;
}
}
}
#NgModule({
providers: [
{
provide: 'configuration',
useFactory: environmentFactory
}
]
})
export class EnvironmentsModule {}
and then where ever needed, e.g.:
import { Injectable, Injector, Inject } from '#angular/core';
import { AuthenticationService } from '../authentication';
#Injectable()
export class APIService {
private http: Http;
private apiURL: string;
protected authentication: AuthenticationService;
constructor(
public injector: Injector,
#Inject('configuration') public configuration: any
) {
this.http = injector.get(Http);
this.authentication = injector.get(AuthenticationService);
this.apiURL = configuration.apiURL;
};
...

Is there a way to have a base class for Jasmine Unit Tests (Angular 4)?

I have several .spec.ts files that all need the following beforeEach:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule, HttpModule, CovalentHttpModule.forRoot({
interceptors: [{
interceptor: CustomInterceptorService, paths: ['**'],
}],
}),],
declarations: [LoginComponent],
providers: [AuthService, { provide: Router, useClass: class { navigate = jasmine.createSpy('navigate'); } }, LoggingService, CustomInterceptorService],
})
.compileComponents();
}));
Is there a way to outsource this TestBed configuration ? Right now I have to adapt every new test file with the same imports and providers.
I am looking for something like a Base Unit Test. Is it possible with Jasmine ?
This case isn't really specific to Jasmine. If a function should be reused, this can be done by means of JavaScript/TypeScript.
A helper function:
export const setupFooTestbed = async(() => {
TestBed.configureTestingModule({...})...
});
...
beforeEach(setupFooTestbed);
Or it can be a base class for TestModuleMetadata object that is accepted by TestBed.configureTestingModule:
export FooTestModuleMetadata implements TestModuleMetadata { ... }
...
beforeEach(async(() => {
TestBed.configureTestingModule(new FooTestModuleMetadata)...
}));
Or a common testing module can be supplied to initTestEnvironment method:
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.
This may only be called once, to set up the common providers for the current test suite on the current platform. If you absolutely need to change the providers, first use resetTestEnvironment.

Categories

Resources