How to let Kafka in the NestJs read the env file? - node.js

I am working on a nestjs project using Kafka microservices (import from #nestjs/microservices).
for listening to a message, I am using the following code in main.ts:
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
transport: Transport.KAFKA,
options: {
client: {
brokers: configService.get('brokers'),
},
},
}),
inject: [ConfigService],
},
);
await app.listen();
I am trying to let the nestjs read brokers from .env.
It can not work.
and I got error :
Argument of type '{ imports: (typeof ConfigModule)[]; useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
Object literal may only specify known properties, and 'imports' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.
if delete 'imports: [ConfigModule]', I have the following error:
Argument of type '{ useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
Object literal may only specify known properties, and 'useFactory' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.
Please help :)

as I see that you included config module at root level, and you did not initialize it. I think you should add Config Module like below.
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
someModule,
fooModule,
],
useFactory: async (configService: ConfigService) => ({
transport: Transport.KAFKA,
options: {
client: {
brokers: configService.get('brokers'),
},
},
}),
inject: [ConfigService],
})

Related

nestjs providing DataSource dependency not working whit async initialization

I need to use the ConfigService to get the environment variables, but when I use "forRootAsync" of the TypeOrmModule nestjs is unable to resolve the DataService dependency
This is my module
#Module({
imports: [
ConfigModule.forRoot({ validate: validateConfig(AssetEnvironmentVariables), isGlobal: true }),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService): TypeOrmModuleOptions => ({
type: "postgres",
host: configService.get("PG_HOST"),
port: configService.get("PG_PORT"),
database: configService.get("PG_DB_NAME"),
username: configService.get("PG_USER"),
password: configService.get("PG_PASSWORD"),
entities: [AssetEntity],
migrations: ["./migrations/*"],
}),
});
],
providers: [
{ provide: AssetRepository, useClass: AssetPostgresRepository },
],
})
export class AssetModule {}
This is the implementation to AssetRepository
#Injectable()
export class AssetPostgresRepository extends AssetRepository {
private typeOrmRepository: Repository<AssetEntity>;
constructor(dataSource: DataSource) {
super();
this.typeOrmRepository = dataSource.getRepository(AssetEntity);
}
async save(asset: Asset): Promise<void> {
try {
await this.typeOrmRepository.save(asset.toPrimitives());
} catch (e) {
throw new SavingRepositoryException(e);
}
}
}
This is the error that it throw me
ERROR [ExceptionHandler] Nest can't resolve dependencies of the AssetPostgresRepository (?). Please make sure that the argument DataSource at index [0] is available in the AssetModule context.
Potential solutions:
- Is AssetModule a valid NestJS module?
- If DataSource is a provider, is it part of the current AssetModule?
- If DataSource is exported from a separate #Module, is that module imported within AssetModule?
#Module({
imports: [ /* the Module containing DataSource */ ]
})

NestJS - gRPC Client E2E Test

I am trying to write an e2e test for my NestJS microservice that has a gRPC client. No matter what I do to try and mock the ClientsModule in the e2e test it still seems to not pick up the config and is unable to locate the *.proto file.
Please find a sample of my code below:
The clients module in the app.module.ts
// src/app.module.ts
#Module({
imports: [
HttpModule,
...
...
ClientsModule.register([
{
name: 'USERS_PACKAGE',
transport: Transport.GRPC,
options: {
package: 'users',
credentials: credentials.createInsecure(),
protoPath: join(__dirname, '../proto/users.proto'),
url: 'users-grpc-server.users:50051',
},
},
]),
],
controllers: [UsersController],
providers: [UsersService, UsersClient],
})
export class AppModule {}
The users client to make the gRPC call to the gRPC Server NestJS microservice:
// src/clients/users.client.ts
#Injectable()
export class UsersClient implements OnModuleInit {
private readonly logger: Logger = new Logger(UsersClient.name);
private readonly API_KEY: string = this.configService.get<string>('services.v1.api-key');
private usersController: usersController;
constructor(#Inject('USERS_PACKAGE') private client: ClientGrpc, private configService: ConfigService) {}
onModuleInit() {
this.usersController = this.client.getService<UsersController>('UsersController');
}
async getUsers(headers: IncomingHttpHeaders, userId: string): Promise<Users> {
let users: Users = null;
const metadata = new Metadata();
metadata.add('Authorization', headers.authorization);
metadata.add('x-api-key', this.API_KEY);
try {
users = await this.usersController.getUsers({}, metadata);
} catch (e) {
const { message, response: { status = 500 } = {}, stack } = e;
this.logger.error(stack);
throw new HttpException(message, status);
}
return users;
}
}
The update nest-cli.json
{
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [{"include": "../config/*.yaml", "outDir": "./dist/config"}, {"include": "**/*.proto", "outDir": "./dist"}]
}
}
The e2e test configuration
// test/app.e2e-spec.json
let app: NestFastifyApplication;
let httpService: HttpService;
let cacheManager: Cache;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
HttpModule,
...
...
ClientsModule,
],
})
.overrideProvider(ClientsModule)
.useValue({
name: 'USERS_PACKAGE',
transport: Transport.GRPC,
options: {
package: 'users',
credentials: ServerCredentials.createInsecure(),
protoPath: join(__dirname, '../src/proto/users.proto'),
url: 'localhost:50051',
},
})
.compile();
app = module.createNestApplication(new FastifyAdapter());
await app.init();
await app.getHttpAdapter().getInstance().ready();
httpService = module.get<HttpService>(HttpService);
cacheManager = module.get<Cache>(CACHE_MANAGER);
});
The error:
● Test suite failed to run
The invalid .proto definition (file at "/users-service-grpc-client/app/proto/users.proto" not found)
> 26 | ClientsModule.register([
| ^
27 | {
28 | name: 'USERS_PACKAGE',
29 | transport: Transport.GRPC,
at ClientGrpcProxy.loadProto (node_modules/#nestjs/microservices/client/client-grpc.js:220:39)
at ClientGrpcProxy.createClients (node_modules/#nestjs/microservices/client/client-grpc.js:193:34)
at new ClientGrpcProxy (node_modules/#nestjs/microservices/client/client-grpc.js:29:33)
at Function.create (node_modules/#nestjs/microservices/client/client-proxy-factory.js:27:24)
at node_modules/#nestjs/microservices/module/clients.module.js:12:80
at Array.map (<anonymous>)
at Function.register (node_modules/#nestjs/microservices/module/clients.module.js:10:41)
at Object.<anonymous> (src/app.module.ts:26:19)
It seems like the the e2e test isnt using the ClientsModule configuration from the test and is still using the ClientsModule configuration from the app.module.ts.
Does anyone know of a way to configure this correctly ?
I am not sure how this is working but I changed the ClientsModule in the src/app.module.ts to be registerAsync because I wanted to make the url dynamic and it now seems to be working correctly
// src/app.module.ts
#Module({
imports: [
HttpModule,
...
...
ClientsModule.registerAsync([
{
name: 'USERS_PACKAGE',
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
transport: Transport.GRPC,
options: {
package: 'users',
credentials: credentials.createInsecure(),
protoPath: join(__dirname, '../proto/users.proto'),
url: configService.get<string>('services.users-grpc-server-service.url'),
},
}),
inject: [ConfigService],
},
]),
],
controllers: [UsersController],
providers: [UsersService, UsersClient],
})
export class AppModule {}
Note: In the E2E test, I also had to mock the grpc call using overrideProvider
// test/app.e2e-spec.ts
const module: TestingModule = await Test.createTestingModule({
imports: [
HttpModule,
...
...
ClientsModule,
],
})
.overrideProvider('USERS_PACKAGE')
.useValue({
getService: () => ({
getUsers: jest.fn().mockReturnValue({
"name": "test user",
"age": "55",
"height": "1.89"
}),
}),
})
.compile();

#InjectConnection not working for aTypeORM-SQL-Database, because its forRootAsync()

I am Registering my SQL-server in app.module.ts as follows:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mssql',
host: configService.get('HOST'),
port: 1434,
username: configService.get('USERNAME'),
database: 'testdatabase',
password: configService.get('PASSWORD'),
name: 'myDatabase',
entities: [],
}),
inject: [ConfigService],
}),
In some other service I am Injecting the Database connection as follows:
constructor(
#InjectConnection("myDatabase") private readonly connection: Connection,
) { }
ThisService is inside a Module which is Imported in app.module.ts
If I register the TypeOrm Module without the async (just TypeOrmModule.forRoot()), and don't use config.service I can access the connection, but since I wanna use ConficService it registers the SQL-Server asynchronously. The connection doesn't exist yet when I inject it throwing the error:
Nest can't resolve dependencies
of the UsersService (?). Please make sure that the argument myDatabaseConnection at index [0] is available in the UsersModule context.
I use the injected connection to run SQL-Queries on it.
How can I make this work?
If you use name in the #InjectConnection(), then name also needs to be at the same level as imports and useFactory as well as inside the options. There's two things keeping track of the name here, Nest, which needs it for the injection tokens, and TypeORM, which needs it for the metadata storage.
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mssql',
host: configService.get('HOST'),
port: 1434,
username: configService.get('USERNAME'),
database: 'testdatabase',
password: configService.get('PASSWORD'),
name: 'myDatabase',
entities: [],
}),
inject: [ConfigService],
name: 'myDatabase',
}),

How to initialize on nestjs module multiple times with different configurations?

How can I create two or multiple instances of a nestjs module? E.g. we would like to have two different instances of the TwilioModule and use different configurations for them.
import { TwilioModule } from 'nestjs-twilio';
#Module({
imports: [
TwilioModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (cfg: ConfigService) => ({
accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
authToken: cfg.get('TWILIO_AUTH_TOKEN'),
}),
inject: [ConfigService],
}),
TwilioModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (cfg: ConfigService) => ({
accountSid: cfg.get('TWILIO_ACCOUNT_SID_2'),
authToken: cfg.get('TWILIO_AUTH_TOKEN_2'),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
Hey for this the package needs to have this feature.
I recommend this.
import { Twilio } from 'twilio';
providers: [
{
provide: 'twilio1',
useFactory: () => {
return new Twilio('ACasd', 'wasdsa');
},
},
{
provide: 'twilio2',
useFactory: () => {
return new Twilio('ACasd', 'wasdsa');
},
},
]
Use the following in the controller or in service
#Inject("twilio1") t1 : Twilio
Example:-
constructor(#Inject('twilo1') t1: Twilio) {}
read more # https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory

How to get values from custom configuration file in Nestjs with interface?

I'm trying to set up all of my configurations in one file in "config.ts", load it to ConfigService and then with config interfaces get values from it.
So here is my config.ts that have ENV vars from my .env file and static variables.
UPD: Made repo with this example
import { Config } from './config.interface';
const config: Config = {
typeorm: {
type: 'postgres',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: process.env.NODE_ENV !== 'prod',
logging: true,
entities: [User, RefreshToken],
},
};
export default () => config;
And here is my interfaces:
export interface Config {
typeorm: TypeOrmConfig;
}
export interface TypeOrmConfig {
type: string;
host: string;
port: number;
username: string;
password: string;
database: string;
synchronize: boolean;
logging: boolean;
entities: any[];
}
The config is loaded to ConfigModule in app.module.ts
#Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.dev.env',
load: [config],
}),
}),
For example I want to set up my TypeOrmModule with this setting.
Based on NestJs documentation
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const config = configService.get<TypeOrmConfig>('typeorm');
console.log(config);
return {
type: config.type,
host: config.host,
port: +config.port,
username: config.username,
password: config.password,
database: config.database,
synchronize: config.synchronize,
logging: config.logging,
entities: config.entities,
};
},
inject: [ConfigService],
}),
Here is the problem. Static values from config is okay, but all my ENV vars is undefined. Here is console.log output:
{
type: 'postgres',
host: undefined,
port: NaN,
username: undefined,
password: undefined,
database: undefined,
synchronize: true,
logging: true,
entities: [
[class User extends CoreEntity],
[class RefreshToken extends CoreEntity]
]
}
I don't understand what's the problem with undefined ENV vars
I would be grateful for any explanations and help
In short, NestJS requires you to define a namespace for your custom configuration "typeorm" which you are trying to access here (see Configuration Namespace):
const config = configService.get<TypeOrmConfig>('typeorm');
This means that you must use registerAs function to create your namespace:
import { registerAs } from '#nestjs/config';
import { TypeOrmConfig } from './config.interface';
export default registerAs(
'typeorm',
(): TypeOrmConfig => ({
type: 'postgres',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: process.env.NODE_ENV !== 'prod',
logging: true,
entities: [],
}),
);
This will do the trick. However, we can improve upon this.
Instead of defining your own interface for the TypeORM configuration, there are existing interfaces you can use and / or extend from to reduce boilerplate code (below I mention ConnectionOptions though TypeOrmModuleOptions might be more appropriate depending on your configuration needs). I would also suggest providing fallback values to environmental variables:
import { registerAs } from '#nestjs/config';
import { ConnectionOptions } from 'typeorm';
const CONNECTION_TYPE = 'postgres';
export default registerAs(
'typeorm',
(): ConnectionOptions => ({
type: CONNECTION_TYPE,
host: process.env.DB_HOST || 'default_value',
port: +process.env.DB_PORT || 3000,
username: process.env.DB_USERNAME || 'default_value',
password: process.env.DB_PASSWORD || 'default_value',
database: process.env.DB_NAME || 'default_value',
synchronize: process.env.NODE_ENV !== 'prod',
logging: true,
entities: [],
}),
);
Also, there is no need to explicitly assign configuration values to TypeOrmModule again as you can simply:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) =>
await configService.get('typeorm'),
inject: [ConfigService],
}),
As a personal flavour I like to create configuration enum to contain configuration names to prevent misspelling. Consider this:
export enum ConfigEnum {
TYPEORM = 'typeorm',
}
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) =>
await configService.get<Promise<TypeOrmModuleOptions>>(
ConfigEnum.TYPEORM,
),
inject: [ConfigService],
})
Async / Await pattern is also redundant here, as we do not actually perform anything async, so this will work as well:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) =>
configService.get<TypeOrmModuleOptions>(ConfigEnum.TYPEORM),
inject: [ConfigService],
})
Welcome to StackOverflow!
Ps. I have made a pull request, see https://github.com/JustDenP/nest-config/pull/1

Resources