-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close the connection when app is being shut down #10
Comments
I was just about to post an issue about this. @B4nan, could you post an example of how you're closing it now? I want to figure out how to do it at the appmodule level so it works for e2e testing as well. |
I ended up doing this in case anyone needs an example: @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();
}
} |
Personally I just call |
Mm, yeah, but then I'd assume you'd have to call it somewhere in your app as well, because I suspect it could prevent graceful shutdowns from happening (for example when handling a SIGTERM). Have not tested if this behavior is indeed affected. |
So far there was no issue in this regard (never called close anywhere else, only in tests, and I am using the ORM from early days :]). Trying to find some clues on other adapters, but looks like nothing there, only the nestjs/typeorm one does what I suggested here in the OP (they actually use |
Yeah, I had tested |
When one calls
app.close()
, it should automatically callorm.close()
too. This means we need to hook intoonApplicationShutdown
and call theorm.close()
there.https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown
The text was updated successfully, but these errors were encountered: