Nestjs global cache: CacheInterceptor problem - nestjs

After configured cache globally like the docs, the CacheInterceptor throws an error if i use it outside the app.module.
app.module.ts
const cacheConfig = {
store: redisStore,
host: 'localhost',
port: 6379
}
#Module({
imports: [
CacheModule.register(cacheConfig),
CustomerModule,
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor
}
]
})
export class AppModule {}
customer.module.ts
#Module({
imports: [TypeOrmModule.forFeature([CustomerRepository]), TypeOrmModule.forFeature([User])],
controllers: [CustomerController]
})
export class CustomerModule {}
customer.controller.ts
#Controller('customer')
export class CustomerController {
constructor(
#InjectRepository(CustomerRepository) private customerRepository: CustomerRepository,
#InjectRepository(User) private userRepository: Repository<User>
) {}
#Get()
#UseInterceptors(CacheInterceptor)
async get(): Promise<any> {
const user = await this.userRepository.findOne({ where: { id: 1 }, relations: ['customer'] })
console.log(user.customer.name)
const customer = await this.customerRepository.findOne({ where: { id: 1 }, select: ['id', 'name'] })
return { customer: customer.name, email: user.email }
}
}
I would like using the CacheInterceptor along any modules without import the CacheModule each one.
Nest can't resolve dependencies of the APP_INTERCEPTOR (UUID: 6aa42c77-1bac-4098-b217-1b01eb268240) (?, Reflector). Please make sure that the argument at index [0] is available in the CustomerModule context.

If you have { provide: APP_INTERCEPTOR, useClass: CacheInterceptor } you don't need to add in the #UseInterceptors() decorator in your controller. You should have the CahceInterceptor working by default with the rest of the set up

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 */ ]
})

How to do unit testing to #Inject with ClientKafka in NestJS

I need to do some unit test to a kafka implementation in my project with NestJS but I don't know how to do it.
I have a Service thats inject a Client Kafka
export class Service {
private static readonly logger = new Logger(ProducerService.name);
constructor(
#Inject('kafka-registrar') private client: ClientKafka,
private someOtherService: SomeOtherService,
) {}
Module
#Module({
imports: [
ClientsModule.register([
{
name: 'kafka-registrar',
transport: Transport.KAFKA,
options: {
client: {
clientId: 'hero',
brokers: ['localhost:9092'],
},
consumer: {
groupId: '1',
},
},
},
]),
SomeOtherService,
],
providers: [Service],
})
export class Module {}
Unit test
describe('Test Controller', () => {
let clientKafka: ClientKafka;
let someOtherService: SomeOtherService;
let producerService: ProducerService;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
ProducerService,
{
provide: SchemaRegistryService,
useValue: {
encodeWithId: jest.fn(),
},
},
{
provide: ClientKafka,
useValue: {
emit: jest.fn(),
},
},
],
}).compile()
clientKafka = moduleRef.get(ClientKafka);
schemaRegistryService = moduleRef.get(SchemaRegistryService);
producerService = moduleRef.get(ProducerService);
});
The project give me this error:
Error: Nest can't resolve dependencies of the ProducerService (?, SchemaRegistryService). Please make sure that the argument kafka-registrar at index [0] is available in the RootTestModule context.
Potential solutions:
- If kafka-registrar is a provider, is it part of the current RootTestModule?
- If kafka-registrar is exported from a separate #Module, is that module imported within RootTestModule?
#Module({
imports: [ /* the Module containing kafka-registrar */ ]
})
I don't know how to resolve this in NestJS. For example in Java,I belive that this can be with #Mock ClientKafka clientKafka bit I dont have any other experience with NestJS... Please helpme! :)
In your test file, you can change provide: ClientKafka to this provide: 'kafka-registrar'.
const moduleRef = await Test.createTestingModule({
providers: [
ProducerService,
{
provide: SchemaRegistryService,
useValue: {
encodeWithId: jest.fn(),
},
},
{
provide: 'kafka-registrar',
useValue: {
emit: jest.fn(),
},
},
],
}).compile()

How to use #admin-bro/nestjs with #admin-bro/typeorm and postgres in a right way?

The admin-bro-nestjs repository contains a comprehensive example with example with mongoose. But I need use it with typeorm and postgres.
I tried to adapt this example for typeorm:
// main.ts
import AdminBro from 'admin-bro';
import { Database, Resource } from '#admin-bro/typeorm';
import { NestFactory } from '#nestjs/core';
import { AppModule } from './app.module';
AdminBro.registerAdapter({ Database, Resource });
const bootstrap = async () => {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
and
// app.module.ts
import { Module } from '#nestjs/common';
import { AdminModule } from '#admin-bro/nestjs';
import { TypeOrmModule, getRepositoryToken } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserEntity } from './user/user.entity';
#Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'database_test',
entities: [UserEntity],
synchronize: true,
logging: false,
}),
AdminModule.createAdminAsync({
imports: [
TypeOrmModule.forFeature([UserEntity]),
],
inject: [
getRepositoryToken(UserEntity),
],
useFactory: (userRepository: Repository<UserEntity>) => ({
adminBroOptions: {
rootPath: '/admin',
resources: [
{ resource: userRepository },
],
},
auth: {
authenticate: async (email, password) => Promise.resolve({ email: 'test' }),
cookieName: 'test',
cookiePassword: 'testPass',
},
}),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
But on application start I get the following error:
NoResourceAdapterError: There are no adapters supporting one of the resource you provided
Does anyone have any experience putting all these libraries together?
I also got NoResourceAdapterError: There are no adapters supporting one of the resource you provided when I tried to integrate AdminBro with NestJS/Express/TypeORM (though I'm using MySQL instead Postgres). The solution posted in the admin-bro-nestjs Github issue which links to this question didn't help me.
The cause of NoResourceAdapterError for me simply was that UserEntity didn't extend BaseEntity. NestJS/TypeORM seems to work fine without extending BaseEntity but apparently it is required for AdminBro. You can see it used in the examples of the AdminBro TypeORM adapter documentation.
Here is an example which works for me.
// user.entity
import { Entity, BaseEntity, Column, PrimaryGeneratedColumn } from 'typeorm';
#Entity()
export class UserEntity extends BaseEntity{
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
}
// app.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { AdminModule } from '#admin-bro/nestjs';
import { Database, Resource } from '#admin-bro/typeorm';
import AdminBro from 'admin-bro'
AdminBro.registerAdapter({ Database, Resource });
#Module({
imports: [
TypeOrmModule.forRoot({
// ...
}),
AdminModule.createAdmin({
adminBroOptions: {
resources: [UserEntity],
rootPath: '/admin',
},
})
],
// ...
})

Test InjectModel using Jest and nestjs-typegoose

My controller has a argument is a InjectModel, code:
constructor(
#InjectModel(Poll) private readonly model: ReturnModelType<typeof Poll>,
){
}
and the Poll code is:
import { prop, modelOptions, getModelForClass } from '#typegoose/typegoose';
import { ApiProperty, ApiPropertyOptions } from '#nestjs/swagger';
#modelOptions({
schemaOptions: {
timestamps: true
}
})
export class Poll {
#ApiProperty({
})
#prop({required: true})
title: string
#prop({required: true})
description: string
#prop()
poll: number
#prop({select: false})
userId: string
#prop()
userName: string
}
Jest code:
import { Poll } from '#libs/db/models/poll.model';
const module: TestingModule = await Test.createTestingModule({
imports: [PollsModule],
controllers: [PollsController],
providers: [
{
provide: getModelForClass(Poll),
useValue: getModelForClass(Poll)
},
]
}).compile();
and I get this error:
Nest can't resolve dependencies of the PollsController (?). Please make sure that the argument PollModel at index [0] is available in the PollsModule context.
Potential solutions:
- If PollModel is a provider, is it part of the current PollsModule?
- If PollModel is exported from a separate #Module, is that module imported within PollsModule?
#Module({
imports: [ /* the Module containing PollModel */ ]
})
poll is in a global model as 'DbModel', and DbModel is in the appModel. so the testModel need imports the DbModel, and then fix !

NestJS - Use multiple MongoDB connections per module

Is there a way to connect multiple MongoDB connections per module?
app.module.ts
#Module({
imports: [
MongooseModule.forRoot('mongodb://localhost/masterDB'),
UserModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
Similarly, can we define another connection in another module which is a child of app.module?
child.module.ts
#Module({
imports: [
MongooseModule.forRoot('mongodb://localhost/childDB'),
MongooseModule.forFeature([{ name: 'child', schema: ChildSchema }]),
],
controllers: [ChildController],
providers: [ChildService],
})
export class ChildModule { }
Or any other way to access different databases at once.
Thanks in advance!
[SOLVED March 2021]
Here you'll find the solution:
https://www.learmoreseekmore.com/2020/04/nestjs-multiple-mongodb-databases.html
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { studentSchema } from './schemas/myworld/student.schema';
import { animalSchema } from './schemas/wildlife/animal.schema';
#Module({
imports: [
MongooseModule.forFeature([
{
name: 'Student',
schema: studentSchema,
collection: 'Student',
},
],'myWorldDb'),
MongooseModule.forFeature([
{
name: 'Animals',
schema: animalSchema,
collection: 'Animals'
}
],'wildLifeDb'),
MongooseModule.forRoot(
'mongodb+srv://<userName>:<password>#cluster0-igk.mongodb.net/MyWorld?retryWrites=true&w=majority',
{
connectionName: 'myWorldDb'
}
),
MongooseModule.forRoot(
'mongodb+srv://<username>:<password>#cluster0-igk.mongodb.net/WildLife?retryWrites=true&w=majority',
{
connectionName: 'wildLifeDb'
}
)
],
controllers: [],
providers: [],
})
export class AppModule {}
You have to do it manually you have to use a providers file:
mongoose.providers.ts
import * as mongoose from 'mongoose';
export const mongooseProviders = [
{
provide: 'MASTER_CONNECTION',
useFactory: (): Promise<typeof mongoose> =>
// This mongoose.connect never working for multples DB connection
// mongoose.connect('mongodb://localhost/masterDB'),
// Following is working fine and tested by me
mongoose.createConnection('mongodb://localhost/masterDB'),
},
{
provide: 'CHILD_CONNECTION',
useFactory: (): Promise<typeof mongoose> =>
// This mongoose.connect never working for multples DB connection
// mongoose.connect('mongodb://localhost/masterDB'),
// Following is working fine and tested by me
mongoose.createConnection('mongodb://localhost/ChildDB'),
},
];
mongoose.module.ts
import { Module } from '#nestjs/common';
import { mongooseProviders } from './mongoose.providers';
#Module({
providers: [...mongooseProviders],
exports: [...mongooseProviders],
})
export class MongooseModule {}
model.providers.ts
import { Connection } from 'mongoose';
import { ChildSchema } from './schemas/child/child.schema';
import { MasterSchema } from './schemas/master/master.schema';
export const modelProviders = [
{
provide: 'CHILD_MODEL',
useFactory: (connection: Connection) => connection.model('Child', ChildSchema),
inject: ['CHILD_CONNECTION'],
},
{
provide: 'MASTER_MODEL',
useFactory: (connection: Connection) => connection.model('Master', MasterSchema),
inject: ['MASTER_CONNECTION'],
},
];
And on the constructor instead of using #InjectModel you use #Inject:
#Injectable
export Class ModelService {
constructor(#Inject('MASTER_MODEL') private masterModel: Model<Master>) {}
...
Note: in the module you provide the service you should import the MongooseModule and put as provider modelProviders.
Post above from RalphJS really helped, but it only was using one connection for everything. Had to change 1 thing in model.providers.ts:
instead of mongoose.connect
you have to use mongoose.createConnection
model.providers.ts
import * as mongoose from 'mongoose';
export const mongooseProviders = [
{
provide: 'MASTER_CONNECTION',
useFactory: async (): Promise<unknown> =>
await mongoose.createConnection('mongodb://localhost/masterDB'),
},
{
provide: 'CHILD_CONNECTION',
useFactory: async (): Promise<unknown> =>
await mongoose.createConnection('mongodb://localhost/childDB'),
},
];
https://mongoosejs.com/docs/connections.html#multiple_connections

Resources