Commit c8e6fd99 authored by nanahira's avatar nanahira

add disconnect hooks for closing everyting

parent 84096c51
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
Caller, Caller,
Provide, Provide,
Inject, Inject,
LifecycleEvents,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Connection, ConnectionOptions, createConnection } from 'typeorm'; import { Connection, ConnectionOptions, createConnection } from 'typeorm';
import { Context, Database } from 'koishi'; import { Context, Database } from 'koishi';
...@@ -31,7 +32,10 @@ declare module 'koishi' { ...@@ -31,7 +32,10 @@ declare module 'koishi' {
@Provide('typeorm', { immediate: true }) @Provide('typeorm', { immediate: true })
@DefinePlugin({ name: 'typeorm', schema: TypeORMPluginConfig }) @DefinePlugin({ name: 'typeorm', schema: TypeORMPluginConfig })
export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> { export default class TypeORMPlugin
extends BasePlugin<TypeORMPluginConfigLike>
implements LifecycleEvents
{
private registryMap = new Map<string, ConnectionEntry>(); private registryMap = new Map<string, ConnectionEntry>();
private entityMap = new Map<TypeORMEntity, string>(); private entityMap = new Map<TypeORMEntity, string>();
...@@ -96,4 +100,11 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> { ...@@ -96,4 +100,11 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> {
} }
return connection; return connection;
} }
async onDisconnect() {
const tokens = Array.from(this.registryMap.keys());
await Promise.all(tokens.map((token) => this.close(token)));
this.registryMap.clear();
this.entityMap.clear();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment