How to tear down MikroOrm in NestJS - nestjs

I've recently converted my AppModule to a dynamic module so that I'm able to provide different configurations to MikroOrm depending on context (E2E tests, etc) and it currently looks like this:
#Module({
imports: [
MikroOrmModule.forFeature({
entities: [Todo],
}),
],
providers: [TodoService],
controllers: [AppController, TodosController],
})
export class AppModule {
static register(options?: {
mikroOrmOptions?: MikroOrmModuleOptions;
}): DynamicModule {
return {
module: AppModule,
imports: [
MikroOrmModule.forRoot({
entities: [Todo],
type: 'postgresql',
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
user: process.env.DB_USER,
password: process.env.DB_PASS,
dbName: process.env.DB_DB,
...options?.mikroOrmOptions,
}),
],
};
}
}
Now I'm trying to ensure graceful shutdown of the app by disconnecting from the database, but not sure where to place a life-cycle hook in this case. It doesn't seem to be possible to have a dynamic module with life-cycle hooks, so I'm thinking of developing a separate provider that injects the orm and write the hook on that.
What would be the correct approach? Thanks.
Edit:
I came up with the following solution. Would appreciate someone indicating if this is the best way:
import { MikroORM } from 'mikro-orm';
...
#Module({
imports: [
MikroOrmModule.forFeature({
entities: [Todo],
}),
],
providers: [TodoService],
controllers: [AppController, TodosController],
})
export class AppModule implements OnModuleDestroy {
static register(options?: {
mikroOrmOptions?: MikroOrmModuleOptions;
}): DynamicModule {
return {
module: AppModule,
imports: [
MikroOrmModule.forRoot({
entities: [Todo],
type: 'postgresql',
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
user: process.env.DB_USER,
password: process.env.DB_PASS,
dbName: process.env.DB_DB,
...options?.mikroOrmOptions,
}),
],
};
}
constructor(private orm: MikroORM) {}
async onModuleDestroy(): Promise<void> {
await this.orm.close();
}
}

As discussed in the issues, I would go with the way nestjs/typeorm is using, so using onApplicationShutdown hook.
Also linking the issue here for possible future readers:
https://github.com/dario1985/nestjs-mikro-orm/issues/10

Related

can't inject custom repository without #InjectRepository decorator

In NestJS Documentation, it says that when I make custom repository, I don't need to use #InjectRepository() decorator docs
But in my code, I cannot inject my custom repository like that
these are my codes
app.module.ts
#Module({
imports: [
CacheModule.register(),
ConfigModule.forRoot({
isGlobal: true
}),
TypeOrmModule.forRootAsync({
useFactory: () => ({
type: 'postgres',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [`${__dirname}/**/entities/*.entity{.ts,.js}`],
synchronize: true,
logging: true
})
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
cors: {
origin: 'http://localhost:3000',
credentials: true
},
autoSchemaFile: 'schema.gql'
}),
AuthModule,
UserModule,
]
})
export class AppModule {}
user.repository.ts
#EntityRepository(User)
export class UserRepository extends Repository<User> {}
user.module.ts
#Module({
imports: [TypeOrmModule.forFeature([UserRepository]), CacheModule.register()],
providers: [
UserResolver,
UserService,
],
exports: [UserService, TypeOrmModule]
})
export class UserModule {}
user.service.ts
#Injectable()
export class UserService {
constructor(
private readonly userRepository: UserRepository,
#Inject(CACHE_MANAGER) private cacheManager: Cache
) {}
}
error message
ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService (?, CACHE_MANAGER). Please make sure that the argument UserRepository at index [0] is available in the UserModule context.
Potential solutions:
- If UserRepository is a provider, is it part of the current UserModule?
- If UserRepository is exported from a separate #Module, is that module imported within
UserModule?
#Module({
imports: [ /* the Module containing UserRepository */ ]
})
I don't want to use #InjectRepository(UserRepositry) decorator.
How can I do that?

NestJs: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

i have a problem with connecting to database in nest.js with typeorm and postgres.
I created a .env file in the root project directory with the following content
POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DATABASE=db-name
In the app.module.ts I writed the code below:
import { Module } from '#nestjs/common';
import { ConfigModule } from '#nestjs/config';
import { TypeOrmModule } from '#nestjs/typeorm';
import { FeedModule } from './feed/feed.module';
#Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: parseInt(<string>process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
autoLoadEntities: true,
synchronize: true,
}),
FeedModule,
],
})
export class AppModule {}
But when im running the app by npm start it throws this error: new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')
What am I missing or doing wrong?
In NestJs you should use ConfigService to get environment variables inside your typeorm module, read the docs for more information.
You can use it like that:
import { ConfigModule, ConfigService } from '#nestjs/config';
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
#Module({
imports: [
ConfigModule.forRoot(
envFilePath: `.${process.env.NODE_ENV}.env`
),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
injects: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get("POSTGRES_HOST"),
port: configService.get("POSTGRES_PORT"),
username: configService.get("POSTGRES_USER"),
password: configService.get("POSTGRES_PASSWORD"),
database: configService.get("POSTGRES_DB"),
entities: [],
synchronize: true,
}),
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
As explained in the docs, you can define a factory function where you inject the config-service allowing you to resolve the corresponding values:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('POSTGRES_HOST'),
port: +configService.get<number>('POSTGRES_PORT'),
username: configService.get('POSTGRES_USER'),
password: configService.get('POSTGRES_PASSWORD'),
database: configService.get('POSTGRES_DATABASE'),
synchronize: true,
autoLoadEntities: true,
}),
inject: [ConfigService],
});
I was able to fix the problem by using the config module.
Just do npm i #nestjs/config. Then in the imports array just above the TypeOrmModule put ConfigModule.forRoot({ isGlobal: true }),. This allows your module to get the environment variables from the .env file
I got this error because I put the .env file inside the src by mistake. If you put it outside of the src it will fix it
I was facing the same issue and it was weird because I modified several times that configuration just to check if "something new happens" but have no success.
Long story short, I deleted the "dist" folder of the project and build the app again (npm run build) and it worked! It appeared that I had a "bad build" running over and over again so this workaround kind of "refreshed" the build and let things running well again.
Hope this help!

NestJs not reading environmental variables

I followed the the Nest documentation to create the config but it's not working
app.module.ts
#Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot(config),
AuthModule,
UsersModule,
MailModule,
CloudinaryModule,
],
controllers: [AppController],
providers: [AppService],
})
.env file is on the src folder
mail.module.ts
#Module({
imports: [
MailerModule.forRoot({
transport: {
service: 'Gmail',
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
},
}),
],
But when I run the app its undefined my key value pairs are also there.
The problem is ConfigModule's env variables are only available at run time but not on the nestjs initial state.
To allow getting the .env after nestjs initialised, you can use async config to in MailerModule.
mail.config.ts
export class MailerConfig implements MailerOptionsFactory {
createMailerOptions(): MailerOptions | Promise<MailerOptions> {
console.log(process.env.MAIL_USER); // should have value
return {
transport: {
service: 'Gmail',
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
},
};
}
}
mail.module.ts
console.log(process.env.MAIL_USER); // undefined
#Module({
imports: [
MailerModule.forRootAsync({
useClass: MailerConfig,
}),
],
})
export class MailModule {}
you can use useFactory as well without the need of class, here I want to console.log the .env for you to check with so i used config class.

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

Reading .env for nestjs app with typeorm having custom provider

I am new to nestJS and I want to setup .env for existing application & facing issue.
I have custom provider for appModule as below,
#Module({
providers: [
AbcService,
XyzService,
],
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'xxxxxxxx',
port: 3230,
username: 'xyz',
password: 'password',
database: 'xyz-db',
entities: [__dirname + '/entities/**/*.entity{.ts,.js}'],
synchronize: true,
migrationsRun: true,
logging: true,
}),
TypeOrmModule.forFeature([
Transaction,
Payment,
]),
KafkaModule.forRoot(serviceConfig),
],
exports: [],
controllers: [ServiceSubscriptionController],
})
export class TopicModule { }
I have imported it inside AppModule as below,
#Module({
imports: [TopicModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
I want to keep these stuff inside .env and I tried it as per documentation as below,
TypeOrmModule.forRootAsync({
imports: [ConfigModule.forRoot({
envFilePath: '.env',
})],
useFactory: async (configService: ConfigService) => {
return {
host: configService.get('HOST'),
type: 'mysql',
port: 3230,
username: 'xyz',
password: 'password',
database: 'xyz-db',
entities: [__dirname + '/entities/**/*.entity{.ts,.js}'],
synchronize: true,
migrationsRun: true,
logging: true,
}
},
inject: [ConfigService]
}),
I have .env at root path with HOST key-value pair as below but it read undefined from it.
In package.json,
"start": "nest start",
"start:dev": "nest start --watch",
It seems that Nest's ConfigModule will run fs.readFileSync(envFilePath) if you pass a file path to the forRoot() method. If you want it to read from the root directory, either remove the envFilePath option, or set the full file path, from your user's home directory.
I have loaded config in main.ts manually as below.
import { config } from 'dotenv';
async function bootstrap() {
//factory method for normal TS app
await config();
const app = await NestFactory.create(AppModule);
Now I can access it as,
configService.get('HOST') // as provided in question
or as process.env.HOST
Note: I have to use forRootAsync instead of forRoot to access process.env

Resources